Questions tagged [adjacency-matrix]

A means of representing which vertices (or nodes) of a graph are adjacent to which other vertices.

The adjacency matrix of a finite graph G on n vertices is the n×n matrix where the non-diagonal entry aij is the number of edges from vertex i to vertex j, and the diagonal entry aii, depending on the convention, is either once or twice the number of edges (loops) from vertex i to itself.

Undirected graphs often use the latter convention of counting loops twice, whereas directed graphs typically use the former convention.
There exists a unique adjacency matrix for each isomorphism class of graphs (up to permuting rows and columns), and it is not the adjacency matrix of any other isomorphism class of graphs.
In the special case of a finite simple graph, the adjacency matrix is a (0,1)-matrix with zeros on its diagonal.

If the graph is undirected, the adjacency matrix is symmetric.

The relationship between a graph and the eigenvalues and eigenvectors of its adjacency matrix is studied in spectral graph theory.

http://en.wikipedia.org/wiki/Adjacency_matrix

900 questions
162
votes
11 answers

What is better, adjacency lists or adjacency matrices for graph problems in C++?

What is better, adjacency lists or adjacency matrix, for graph problems in C++? What are the advantages and disadvantages of each?
magiix
  • 1,711
  • 2
  • 13
  • 11
40
votes
7 answers

Generating Symmetric Matrices in Numpy

I am trying to generate symmetric matrices in numpy. Specifically, these matrices are to have random places entries, and in each entry the contents can be random. Along the main diagonal we are not concerned with what entries are in there, so I have…
Ryan
  • 403
  • 1
  • 4
  • 5
31
votes
5 answers

How can I convert a two column array to a matrix with counts of occurences?

I have the following numpy array: import numpy as np pair_array = np.array([(205, 254), (205, 382), (254, 382), (18, 69), (205, 382), (31, 183), (31, 267), (31, 382), (183, 267), (183, 382)]) print(pair_array) #[[205…
16
votes
5 answers

Adjacency matrix in Python

I cannot find any clear explanation as to how to create an adjacency matrix in Python, with weights taken into consideration. I assume it should be relatively simple to create. I have the following matrix... 1 2 3 4 5 6 1 0 15 0 7…
buydadip
  • 8,890
  • 22
  • 79
  • 154
14
votes
4 answers

How to create weighted adjacency list/matrix from edge list?

My problem is very simple: I need to create an adjacency list/matrix from a list of edges. I have an edge list stored in a csv document with column1 = node1 and column2 = node2 and I would like to convert this to a weighted adjacency list or a…
Milo
  • 185
  • 1
  • 1
  • 9
13
votes
3 answers

Adjacency List and Adjacency Matrix in Python

Hello I understand the concepts of adjacency list and matrix but I am confused as to how to implement them in Python: An algorithm to achieve the following two examples achieve but without knowing the input from the start as they hard code it in…
Baraa
  • 687
  • 1
  • 9
  • 26
12
votes
2 answers

Python, Scipy: Building triplets using large adjacency matrix

I am using an adjacency matrix to represent a network of friends which can be visually interpreted as Mary 0 1 1 1 Joe 1 0 1 1 Bob 1 1 0 1 Susan 1 1 1 0 …
will
  • 225
  • 1
  • 7
12
votes
1 answer

How to 'zero' out rows & columns in an array

I have a 2D array to represent a many-many mapping : 0 3 1 3 3 0 0 0 1 0 0 0 3 0 0 0 What is the quickest way to 'zero' out rows and column entries corresponding to a particular index in this array?
IUnknown
  • 9,301
  • 15
  • 50
  • 76
12
votes
5 answers

How can you make an adjacency matrix which would emulate a 2d grid

Basically just want to know what a good way to do this is in python, I have done this before with a kind of bruteforce way also in python but it just doesnt to be the intuitive way. So if anyone could help out it would be good.
11
votes
1 answer

graphs representation : adjacency list vs matrix

I'm preparing for a coding interview, and was refreshing my mind on graphs. I was wondering the following : in all places I've seen, it is assumed that adjacency lists are more memory efficient than adjacency matrices for large sparse graphs, and…
nbonneel
  • 3,286
  • 4
  • 29
  • 39
11
votes
1 answer

Generate an Adjacency Matrix for a Weighted Graph

I am trying to implement Floyd-Warshall Algorithm. To do this it requires me to set up an adjacency matrix of a weighted graph. How would I go about doing this? I know the values and have attached a picture of the weighted graph. I have tried to…
JLott
  • 1,818
  • 3
  • 35
  • 56
10
votes
3 answers

Properly plotting large adjacency matrix in R

I've got a rather large (but quite sparse) adjacency matrix (500x500) that I am trying to visually represent. It seems to me that something akin to a force directed graph is my best bet and while trying to figure out the best way to implement this,…
Drofdarb
  • 159
  • 1
  • 9
10
votes
6 answers

find neighbouring elements of a matrix in R

Edit: huge thanks to the users below for great contributions and to Gregor for benchmarking. Say I have a matrix filled with integer values like this... mat <- matrix(1:100, 10, 10) I can create a list of x, y coordinates of each element like…
roman
  • 1,340
  • 9
  • 33
10
votes
2 answers

Adjacency matrix in R

I want to find the adjacency matrix from a csv file that includes the information as follows: A B 1 2 1 3 1 4 2 5 3 7 and so on. There are 100 nodes but everytime I try to create a matrix and subsequently plot the graph, the error is that it is a…
user2203793
  • 135
  • 1
  • 2
  • 8
9
votes
3 answers

2-opt algorithm to solve the Travelling Salesman Problem in Python

I couldn't find any complete implementation of the 2-opt algorithm in Python so I am trying to add the missing parts to the code found here, which I present below. def two_opt(route): best = route improved = True while improved: …
rrz0
  • 2,182
  • 5
  • 30
  • 65
1
2 3
59 60