November 19, 2015

The dialects of networkland

Which one do you speak?

  • Edge list
  • Adjacency list
  • Adjacency matrix

R speaks in tongues

library("igraph") # We will use it a lot
read.graph(file,
           format = c("edgelist", "pajek",
                      "ncol", "lgl", "graphml",
                      "dimacs","graphdb", "gml", "dl"), ...)

Edge list

Read the web as:

  • a is adjacent to b.
  • a is adjacent to c.
  • b is adjacent to a.
  • b is adjacent to b.
  • c is adjacent to b.

And write it as:

Edge.list <- matrix(c("a","b","a","c",
                      "b","a","b","b",
                      "c","b"),ncol=2,
                      byrow=TRUE)

Edge list

Which translates to:

Edge.list <- matrix(c("a","b","a","c",
                      "b","a","b","b",
                      "c","b"),ncol=2,
                      byrow=TRUE)
Edge.list
##      [,1] [,2]
## [1,] "a"  "b" 
## [2,] "a"  "c" 
## [3,] "b"  "a" 
## [4,] "b"  "b" 
## [5,] "c"  "b"

Adjacency matrix

Read the web as:

  • a is adjacent to a? no; to b? yes; to c? yes.
  • b is adjacent to a? yes; to b? yes; to c? no;
  • c is adjacent to a? no; to b? yes; to c? no.

Adjacency matrix

And write it as:

Adjacency.matrix <- matrix(c(0,1,1,
                             1,1,0,
                             0,1,0),
                           nrow=3,ncol=3,
                           byrow=TRUE)
row.names(Adjacency.matrix) <- colnames(Adjacency.matrix) <- c("a","b","c")

Which translates to:

Adjacency.matrix
##   a b c
## a 0 1 1
## b 1 1 0
## c 0 1 0

Into igraph

Edge list

igraph can read those webs easily:

graph.data.frame(Edge.list,directed=TRUE) -> Edge.web

The order matters: FROM -> TO.

graph.adjacency(Adjacency.matrix,mode="directed") -> Adjacency.web

Notice we have to use two different functions.

Into igraph

Edge list

plot(Edge.web)

Into igraph

Adjacency matrix

plot(Adjacency.web)