Search code examples
javagraphfinite-automata

Merge/Join two separate graphs algorithm?


i Need an algorithm for merging or joining to graphs, by this I mean that the last node of graph 1 should be predecesor of first node of graph 2. Graph 1:

1->2->3
   \ /
    4->5

Graph 2:

6->7
   | \
   8->9

This is what I need:

1->2->3
   \ /
    4->5->6->7
             | \
             8->9

Solution

  • createNewGraph iterate graph 1 and add new graph iterate graph 1 for each node iteratesuccessors for each edge between these nodes add to the new graph

    iterate graph 2 and add new graph iterate graph 2 for each node iteratesuccessors for each edge between these nodes add to the new graph

    add edge from last node of graph 1 to firstnode of graph 2