1
$\begingroup$

I have two directed graphs given below:

one={1 -> 2, 1 -> 3, 3 -> 4, 4 -> 1};
two={1 -> 2, 1 -> 3, 1 -> 4};

I simply want to map the two graphs as a single graph keeping all the edges repeated as multiple edges like 1->2, 1->3. I tried

GraphUnion[one, two]

but it is not giving me what I am after. I want to keep all the edges in these two graphs and differentiate the two graphs with different colors.

Any idea?

Example 2

one = {1 \[DirectedEdge] 10, 3 \[DirectedEdge] 5, 3 \[DirectedEdge] 6,
6 \[DirectedEdge] 1, 6 \[DirectedEdge] 2, 6 \[DirectedEdge] 3, 
6 \[DirectedEdge] 4, 6 \[DirectedEdge] 7, 6 \[DirectedEdge] 8, 
6 \[DirectedEdge] 11, 6 \[DirectedEdge] 12, 6 \[DirectedEdge] 13, 
6 \[DirectedEdge] 14, 6 \[DirectedEdge] 15, 6 \[DirectedEdge] 16, 
6 \[DirectedEdge] 17, 11 \[DirectedEdge] 9};    

two = {1 \[DirectedEdge] 6, 2 \[DirectedEdge] 6, 3 \[DirectedEdge] 6, 
4 \[DirectedEdge] 6, 5 \[DirectedEdge] 6, 6 \[DirectedEdge] 8, 
7 \[DirectedEdge] 6, 8 \[DirectedEdge] 6, 9 \[DirectedEdge] 8, 
10 \[DirectedEdge] 6, 11 \[DirectedEdge] 9, 12 \[DirectedEdge] 6, 
13 \[DirectedEdge] 9, 14 \[DirectedEdge] 6, 15 \[DirectedEdge] 6, 
16 \[DirectedEdge] 6, 17 \[DirectedEdge] 6};

HighlightGraph[EdgeAdd[Graph[one, VertexLabels -> "Name"], two], two, 
VertexLabels -> "Name"]
GraphUnion[one, two, VertexLabels -> "Name"]

In Highlight there is something wrong because there are two edges like 11->9, and only one of them should be RED, the other be BLUE. It seems that identical edges are assumed to belong to graph two only. What I am after is to separate them with different colors as the colors have a meaning in the context. GraphUnion is not generating the same graph at all because it takes mathematical union of the two graphs, eliminating the repeated edges.

I simply want to map two separate graphs with different colors on top of each other without loosing any directed edge.

$\endgroup$
  • 2
    $\begingroup$ Graph@Join[one, two]? $\endgroup$ – Kuba Aug 21 at 11:53
1
$\begingroup$

You can use EdgeAdd:

EdgeAdd[one, two, VertexLabels -> "Name"]

enter image description here

Alternatively, you can use GraphComputation`GraphSum:

GraphComputation`GraphSum[Graph @ one, Graph @ two, VertexLabels->"Name"]

same picture

GraphUnion works as expected (that is, it will not the give desired output): it takes the Union of vertices and the Union of edges from the two input graphs:

GraphUnion -> Details and Options:

  • The graph union Graph[$v_1, e_1$] ⋃ Graph[$v_2, e_2$] is given by Graph[$v_1$$v_2$, $e_1$$e_2$].
$\endgroup$
  • $\begingroup$ It works but somehow I cannot highlight the second graph correctly. I will edit the question with a different example. By the way, GraphUnion is not giving the desired output. You might want to look at my edition to the question. $\endgroup$ – Tugrul Temel Aug 21 at 12:51
  • $\begingroup$ GraphComputationGraphSum[Graph @ one, Graph @ two, VertexLabels->"Name"] works fine but I need to differentiate the two graphs with different colors. $\endgroup$ – Tugrul Temel Aug 21 at 13:12
  • 1
    $\begingroup$ @Tugrul, Unfortunately, individual styling and labeling of multi-edges is a non-trivial task in mathematica. You can use this answer. See also this, and this, $\endgroup$ – kglr Aug 21 at 13:36
  • $\begingroup$ Thank you very much for your guidance and help. I will look into the links you sent me. $\endgroup$ – Tugrul Temel Aug 21 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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