I have a piece of code where I might want to convert Graph
s to DiGraph
s. However, I need to keep track of where the edges are (so that for the edge connecting vertices 1 and 2 in the original graph, I know where 1 => 2
and 2 => 1
are for the created digraph. However, it seems that whenever you iterate over a graph’s list o edges, they always do so in order of the index of the source followed by the destination, which makes it an utter mess to keep track of this order.
E.g, if I have:
using Graphs
g0 = DiGraph(4)
add_edge!(g0, 1, 2)
add_edge!(g0, 2, 3)
add_edge!(g0, 3, 4)
add_edge!(g0, 4, 1)
foreach(e -> println(e), edges(g0))
I get:
Edge 1 => 2
Edge 2 => 3
Edge 3 => 4
Edge 4 => 1
And then if I add the reverse edges and print:
add_edge!(g0, 2, 1)
add_edge!(g0, 3, 2)
add_edge!(g0, 4, 3)
add_edge!(g0, 1, 4)
foreach(e -> println(e), edges(g0))
I get:
Edge 1 => 2
Edge 1 => 4
Edge 2 => 1
Edge 2 => 3
Edge 3 => 2
Edge 3 => 4
Edge 4 => 1
Edge 4 => 3
While I ideally would have liked:
Edge 1 => 2
Edge 2 => 3
Edge 3 => 4
Edge 4 => 1
Edge 2 => 1
Edge 3 => 2
Edge 4 => 3
Edge 1 => 4
(where edge i maps to i and i+4)
or maybe
Edge 1 => 2
Edge 2 => 1
Edge 2 => 3
Edge 3 => 2
Edge 3 => 4
Edge 4 => 3
Edge 4 => 1
Edge 1 => 4
(where edge i maps to 2*(i-1)+1 and 2*(i-1)+2)