library(igraph)
# Preferential attachment graph
g = sample_pa(100, m=2, directed=FALSE)
coords = layout_with_fr(g)
plot(g, layout=coords, vertex.size = 3, vertex.label=NA)
# average distance (degree of separation)
mean_distance(g)
## [1] 3.083636
# maximum distance (diameter)
diameter(g)
## [1] 6
# highlight the diameter
d = get_diameter(g)
V(g)$color = "white"
E(g)$color = "grey"
E(g)$width = 1
E(g, path=d)$color = "red"
E(g, path=d)$width = 2
V(g)[d]$color = "red"
plot(g, layout=coords, vertex.label = NA, vertex.size=5)
# single-pair distances and paths
s = d[1]
t = d[length(d)]
distances(g, v=s, to=t)
## [,1]
## [1,] 6
shortest_paths(g, from=s, to=t)$vpath
## [[1]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 39 20 1 37 46 76
all_shortest_paths(g, from=s, to=t)$res
## [[1]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 39 20 1 45 36 76
##
## [[2]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 39 23 1 45 36 76
##
## [[3]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 55 5 1 45 36 76
##
## [[4]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 55 42 11 10 36 76
##
## [[5]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 39 20 59 10 36 76
##
## [[6]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 39 20 27 10 36 76
##
## [[7]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 39 20 22 10 36 76
##
## [[8]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 39 20 8 10 36 76
##
## [[9]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 55 5 2 16 36 76
##
## [[10]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 39 89 13 16 36 76
##
## [[11]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 55 5 2 65 36 76
##
## [[12]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 55 5 2 37 46 76
##
## [[13]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 39 20 1 37 46 76
##
## [[14]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 39 23 1 37 46 76
##
## [[15]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 55 5 1 37 46 76
##
## [[16]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 55 42 11 10 46 76
##
## [[17]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 39 20 59 10 46 76
##
## [[18]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 39 20 27 10 46 76
##
## [[19]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 39 20 22 10 46 76
##
## [[20]]
## + 7/100 vertices, from 3bd18dd:
## [1] 60 39 20 8 10 46 76
# histogram of distances
distance_table(g)
## $res
## [1] 197 1066 2051 1404 226 6
##
## $unconnected
## [1] 0
paths = distance_table(g)$res
names(paths) = 1:length(paths)
barplot(paths / sum(paths), xlab="Distance", ylab="Frequency")