J.1 Basic graphs

# Create a small graph and plot it
g <- graph.ring(20)
plot(g)

# Get the degree of each node
degree(g)
 [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
# Get the average path length
average.path.length(g)
[1] 5.263158
# Create a "Small world" graph
g <- watts.strogatz.game(1, 20, 5, 0.05)
# Get the degree of each node
degree(g)
 [1]  9 11  9 10 10 10 12 11 11 11 10 10 10  9 10 10 10  9  9  9
# Get the average path length
average.path.length(g)
[1] 1.473684
# Get the transitivity
transitivity(g)
[1] 0.5887541
plot(g)

# Directed "scale free" graph
set.seed(456)
g <- barabasi.game(20)
plot(g)

# Get the degree of each node
degree(g)
 [1] 7 4 2 4 1 2 1 3 1 2 1 1 2 1 1 1 1 1 1 1
# Get the average path length
average.path.length(g)
[1] 1.571429
# Get the transitivity
transitivity(g)
[1] 0

To check for a power-law, remember what the scaling relation is supposed to represent: A relation between number of nodes of a particular size, with the size of those nodes.

The following produces an unsorted graph:

plot(log(1:20),log(degree(g)))

So, we need to sort:

plot(log(1:20),sort(log(degree(g)), decreasing = TRUE))

Or even better, sort and bin it:

d <- degree(g)
y <- hist(d,breaks=0.5:(max(d)+0.5),plot=TRUE, xlab = "Node degree")$counts

op <-par("xlog","ylog")
plot(1:length(y),rev(y), xlim = c(length(y),1), xlab = "Node degree (log)", ylab = "Frequency of Node degree (log)", log= c("xy"))

par(op)

(alpha=coef(lm(rev(log1p(y)) ~ log1p(1:length(y)))))
       (Intercept) log1p(1:length(y)) 
         -0.992961           1.283407