I have two random graphs G1 and G2 that I want to join with an edge from a particular vertex vx in G1 to a particular vertex vy in G2. I can't find a way to do it.

Here is my latest code:

(* Generate Two Random Barabasi Albert with order up to 30 *)
go2 =RandomInteger[{1,30}];
G2 =RandomGraph[BarabasiAlbertGraphDistribution[go2,2],VertexLabels->Automatic];
(* Perform disjoint union to make one graphy with succesive vertices *)
(* one random vertex from each graph G1, and G2 *)
G =EdgeAdd[G,v1\[UndirectedEdge]v2, PlotLabel->"Random Edge Connection between G1 and G2"]

1 Answer 1


You need to join the two graph into a single one before you can add edges between them. Look up GraphDisjointUnion.

  • $\begingroup$ Ah, yes that is what I was kind of doing by joining the incidence matrices, but this function is much more elegant since the vertices are renumbered automagically. $\endgroup$ Commented Jun 17, 2022 at 12:17
  • 2
    $\begingroup$ The IGraph/M package has IGDisjointUnion as well which provides some extra features, such as maintaining the mapping between the vertices of the original graphs and the combined graph. $\endgroup$
    – Szabolcs
    Commented Jun 17, 2022 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.