"Join" two graphs in R

Asked

Viewed 237 times

3

I’m using the library igraph to create two graphs in R:

g.v1 <- graph.full(6, directed= TRUE)
E(g.v1)$weight <- 1

g.merchant <- graph.empty(1, directed=TRUE)
E(g.merchant)$weight <- 1

These two graphs, g.v1 and g.merchant, are disjoint. I would like to connect the graph g.merchant, which has only 1 vertex, with Qalquer one of the 6 vertices of the graph g.v1. The new graph will have 7 vertices. How do I do this ?

1 answer

2


The code below solves your problem. Even, it randomly selects one of the vertices of the g.v1 to connect to the graph g.merchant.

library(igraph)

g.v1 <- graph.full(6, directed= TRUE)
E(g.v1)$weight <- 1
plot(g.v1)

g.v1

g.merchant <- graph.empty(1, directed=TRUE)
E(g.merchant)$weight <- 1
plot(g.merchant)

g.merchant

g.novo <- g.v1 + g.merchant
v.aleatorio <- sample(unique(V(g.v1)), 1)
g.novo <- g.novo + path(v.aleatorio, 7, v.aleatorio)
plot(g.novo)

g.novo

Note that in the first Plot of the g.merchant, its single vertex has been identified as 1. By joining the two graphs, the number of this vertex has been updated to 7.

  • Perfect guy. Thank you so much!

  • 1

    If the answer helped you, please vote for it and accept it. So other users of the site can benefit if they have the same question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.