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 non-square matrix. Can somebody help me with the correct code in R?
An adjacency matrix is a way of representing a graph as a matrix of booleans (0's and 1's). A finite graph can be represented in the form of a square matrix on a computer, where the boolean value of the matrix indicates if there is a direct path between two vertices.
The adjacency matrix of a simple labeled graph is the matrix A with A[[i,j]] or 0 according to whether the vertex vj, is adjacent to the vertex vj or not. For simple graphs without self-loops, the adjacency matrix has 0 s on the diagonal. For undirected graphs, the adjacency matrix is symmetric.
The adjacency matrix, also called the connection matrix, is a matrix containing rows and columns which is used to represent a simple labelled graph, with 0 or 1 in the position of (Vi , Vj) according to the condition whether Vi and Vj are adjacent or not.
The advantage of the adjacency matrix representation is that it takes constant time (just one memory access) to determine whether or not there is an edge between any two given vertices.
Maybe something like:
dat <- read.table(text="A B
1 2
1 3
1 4
2 5
3 7", header=TRUE)
x <- table(dat)
x %*% t(x)
But maybe you actually want: igraph::graph.data.frame
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With