Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to link a graph to any particular vertex of another graph (igraph)?

Tags:

r

igraph

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

like image 386
Aman Avatar asked Dec 31 '22 11:12

Aman


2 Answers

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
like image 50
ThomasIsCoding Avatar answered Apr 25 '23 12:04

ThomasIsCoding


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 and H
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 and H with edge 0-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)

like image 31
lovalery Avatar answered Apr 25 '23 14:04

lovalery