简体   繁体   中英

Printing edge list with node degrees using igraph in R

I am new to using igraph and R and want to do the following :

1) Calculate the path length distribution. I could find the average path length using the straight forward function from the library but how to do it for every pair of vertices so i get the path length distribution ?

2) I want to find out all nodes who have higher degree than their neighbors. I tried combining the E() function with degree() but its not working.

Thanks

1) can be solved using path.length.hist :

g <- graph.data.frame(data.frame(a=c(1:3),b=c(2:4)),directed=FALSE)
plot(g)

## (1)--(2)--(3)--(4)

out <- path.length.hist(g,directed=FALSE)
out
#$res
#[1] 3 2 1
#
#$unconnected
#[1] 0

So out$res gives 3 paths with length==1, 2 with length==2 and 1 with length==3 which is essentially a histogram/barplot data. If necessary you could expand to get the vector representing the distribution like:

rep(seq_along(out$res),out$res)
#[1] 1 1 1 2 2 3

Which checks out:

mean(rep(seq_along(out$res),out$res))
#[1] 1.666667
average.path.length(g)
#[1] 1.666667

2) - something like this maybe:

g <- graph.data.frame(data.frame(a=c(1:3,2),b=c(2:4,5)),directed=FALSE)
# node 2 has a higher degree than its neighbours
sapply(
  neighborhood(g,order=1),
  function(x) {degs <- degree(g,x); all(degs[1] > degs[-1]);} 
)
#[1] FALSE  TRUE FALSE FALSE FALSE

For 2) it is actually simpler, faster and in my opinion more readable to do something like this:

g <- graph.formula(1-2-3-4, 2-5)
degs <- degree(g)
sapply(V(g), function(x) all(degs[neighbors(g, x)] < degs[x]))
# [1] FALSE  TRUE FALSE FALSE FALSE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM