Last active
October 2, 2015 18:09
-
-
Save briandconnelly/6ce1351c5fe8b3bb08be to your computer and use it in GitHub Desktop.
Exploring Graph Structure with igraph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You'll need igraph. If you don't have it, run install.packages("igraph") | |
library(igraph) | |
# First load the function moore_lattice_2d() | |
bulky <- moore_lattice_2d(rows=100, columns=100) | |
slender <- moore_lattice_2d(rows=4, columns=2500) | |
# Other generators: sample_k_regular, make_ring, sample_smallworld, etc... | |
# Calculate the longest shortest paths between any two vertices | |
ebulky <- eccentricity(graph=bulky) | |
eslender <- eccentricity(graph=slender) | |
# What are the shortest and longest longest shortest paths (wow!) in the graph (radius and diameter, respectively) | |
range(ebulky) | |
range(eslender) | |
# What is the average shortest path length (characteristic path length)? | |
average.path.length(graph=bulky) | |
average.path.length(graph=slender) | |
# What is the probability that two of my neighbors are also neighbors? (aka clustering coefficient, transitivity) | |
transitivity(graph=bulky, type="global", vids=NA) | |
transitivity(graph=slender, type="global", vids=NA) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
moore_lattice_2d <- function(rows, columns) { | |
g <- make_lattice(dimvector=c(columns,rows), circular=TRUE, directed=FALSE) | |
b <- function(x, X) ((x - 2 + X) %% X) + 1 | |
a <- function(x, X) (x %% X) + 1 | |
vid <- function(r, c) c + (columns * (r-1)) | |
for (r in seq_len(rows)) | |
{ | |
next_r <- a(r, rows) | |
for (c in seq_len(columns)) | |
{ | |
prev_c <- b(c, columns) | |
next_c <- a(c, columns) | |
v <- vid(r,c) | |
g <- g + edge(v, vid(next_r, prev_c), v, vid(next_r, next_c)) | |
} | |
} | |
g | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment