Search code examples
graphjuliaedges

sorting edges and vertice of a graph


let

G=DiMultigraph(4)
 add_edge!(G,2,4)
 add_edge!(G,4,1)
 add_edge!(G,4,1)
 add_edge!(G,1,3)
 add_edge!(G,1,3)
 add_edge!(G,2,3)

When I run

e=edges(G)
e1=collect(e)

I got

 Multiple edge 1 => 3 with multiplicity 2
 Multiple edge 2 => 3 with multiplicity 1
 Multiple edge 2 => 4 with multiplicity 1
 Multiple edge 4 => 1 with multiplicity 2

What I want to have is the non sorted list

 Multiple edge 2 => 4 with multiplicity 1
 Multiple edge 4 => 1 with multiplicity 2
 Multiple edge 1 => 3 with multiplicity 1
 Multiple edge 2 => 3 with multiplicity 1

it seems like there is a code for that here but I don't really understand it.


Solution

  • I think you want the edges in the order you inserted them. To do that, you need to have a key to sort by. Here is one way:

    edgevector = [[2,4], [4,1], [4,1],[1,3],[1,3],[2,3]]
    G = DiMultigraph(4)
    for edg in edgevector
        add_edge!(G, edg[1], edg[2])
    end
    
    e = edges(G)
    e1 = sort!(collect(e), by = edg -> findfirst(==([edg.src, edg.dst]), edgevector))