简体   繁体   中英

Number of vertices in Igraph in R

I'm fairly new to IGraph in R.

I'm doing community detection using IGraph and have already built my communities /clusters using the walktrap technique.

Next, within each cluster, I want to count the number of vertices between each two certain vertices. The reason I want to do this is, for each vertex XX, I want to list vertices that are connected to XX via say max 3 vertices, meaning no further than 3 vertices away from XX.

Can anyone help how this can be done in R please?

making a random graph (for demonstration):

g <- erdos.renyi.game(100, 1/25)
plot(g,vertex.size=3)

get walktrap communities and save as vertex attribute:

V(g)$community<-walktrap.community(g, modularity = TRUE, membership = TRUE)$membership
V(g)$community

now make a subgraph containing only edges and vertices of one community, eg community 2:

sub<-induced.subgraph(g,v=V(g)$community==2)
plot(sub)

make a matrix containing all shortest paths:

shortestPs<-shortest.paths(sub)

now count the number of shortest paths smaller or equal to 3.

I also exclude shortest paths from each node to itself (shortestPaths!=0).

also divide by two because every node pair appears twice in the matrix for undirected graphs.

Number_of_shortest_paths_smaller_3 <- length(which(shortestPs<=3 & shortestPs!=0))/2
Number_of_shortest_paths_smaller_3

Hope that's close to what you need, good luck!

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