Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 non-square matrix. Can somebody help me with the correct code in R?

like image 378
user2203793 Avatar asked Jan 29 '14 00:01

user2203793


People also ask

What is adjacency matrix?

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.

How do you know if a matrix is adjacency?

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.

What is the condition for an adjacency matrix?

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.

What is the role and advantages of adjacency matrix?

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.


1 Answers

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

like image 163
Tyler Rinker Avatar answered Sep 28 '22 23:09

Tyler Rinker