Search code examples
rgraphdata-visualizationnodes

R: "connecting" graphs


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?

enter image description here

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


Solution

  • 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))