I want to join two independent graph by joining the node of Graph one to another
For example, I have two graph G & H
G = 1-3,1-5,5-9,5-21,21-22 H = 0-31,31-32,31-34,31-35,35-88
I want to link Graph H with Graph G, by joining the node 0 of Graph H to node 5 of Graph G
1-3,1-5,5-9,5-21,21-22,5-0, 0-31,31-32,31-34,31-35,35-88
CodePudding user response:
Please find below one possible solution from your data using the edge()
function of the library igraph
.
Reprex
- STEP 1: Building and visualizing the graphs
G
andH
library(igraph)
# Building the graph 'G'
G <- graph(edges = c("1","3", "1","5", "5","9", "5","21", "21","22"), directed = FALSE)
#Building the graph 'H'
H <- graph(edges = c("0","31", "31","32", "31","34", "31","35", "35","88"), directed = FALSE)
# Visualizing the graph 'G'
plot(G)
# Visualizing the graph 'H'
plot(H)
- STEP 2: Combining the two graphs
G
andH
with edge0-5
# Building the graph 'Results'
Results <- G H edge("0", "5")
# Visualizing the graph 'Results'
plot(Results)
Created on 2022-01-01 by the reprex package (v2.0.1)
CodePudding user response:
You can try disjoint_union
add_edges
> add_edges(disjoint_union(G, H), c("0", "5"))
IGRAPH 6cbfa1f UN-- 12 11 --
attr: name (v/c)
edges from 6cbfa1f (vertex names):
[1] 1 --3 1 --5 5 --9 5 --21 21--22 0 --31 31--32 31--34 31--35 35--88
[11] 5 --0