2
$\begingroup$

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 *)
go1=RandomInteger[{1,30}];
go2 =RandomInteger[{1,30}];
G1=RandomGraph[BarabasiAlbertGraphDistribution[go1,2],VertexLabels->Automatic];
G2 =RandomGraph[BarabasiAlbertGraphDistribution[go2,2],VertexLabels->Automatic];
(* Perform disjoint union to make one graphy with succesive vertices *)
G=GraphDisjointUnion[G1,G2,VertexLabels->Automatic];
(* one random vertex from each graph G1, and G2 *)
v1=RandomChoice[Range[go1]];
v2=RandomChoice[Range[go1+1,go1+go2,1]];
G =EdgeAdd[G,v1\[UndirectedEdge]v2, PlotLabel->"Random Edge Connection between G1 and G2"]
$\endgroup$

1 Answer 1

4
$\begingroup$

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

$\endgroup$
2
  • $\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.