R: “connecting” graphs

夙愿已清 提交于 2020-12-21 03:04:36

问题


Using R, I created and plotted a graph:

library(igraph)
library(igraphdata)
data(karate)

#cluster
cfg <- cluster_fast_greedy(karate)

#plot entire graph
plot(cfg, karate)

#plot first subgraph

a = induced_subgraph(karate, cfg[[1]])
plot(a)

#plot second subgraph

b = induced_subgraph(karate, cfg[[2]])
plot(b)

#plot third subgraph

c = induced_subgraph(karate, cfg[[3]])
plot(c)

Is it possible to write some code that shows which graph is connected to which graph?

Example: the green graph is connected to the orange and purple. the purple graph is only connected to the green graph. and the orange graph is connected to the green graph

("a" is the orange graph, "b" is the green graph, "c" is the purple graph)

b : c and a

c: b

a: b


回答1:


For the binary case, where the question is whether a connection between communities exists or not, you could do the following. First, contract the communities based on their membership (belonging to "a", "b", or "c").

V(karate)$mem <- membership(cfg)
g <- contract.vertices(karate, V(karate)$mem, vertex.attr.comb = "ignore")

Secondly, remove loops and multiple edges:

g <- simplify(g)

Finally, extract adjacent nodes (which now equal communities after contracting them above):

sapply(1:vcount(g), function(x) adjacent_vertices(g, x)) 


来源:https://stackoverflow.com/questions/64997562/r-connecting-graphs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!