Understanding QuickGraph ShortestPathsDijkstra results

741 Views Asked by At

I have a graph and want to do some shortest path searches with Dijkstra algorithm (I don't really care about the algorithm, but dijkstra is the one I am familiar with).

This is the relevant part of graph I have:

enter image description here

Now I do a dijkstra search following Quickgraph documentation:

//Build QuickGraph UndirectedGraph from our data
UndirectedGraph<int, Edge<int>> ug = g.CreateUndirectedQuickGraph();

Func<Edge<int>,double> weightFunc = (Edge<int> edge) =>
{
    return 1; //without weights at this moment
};

var tryGetPath = ug.ShortestPathsDijkstra(weightFunc, 20);

IEnumerable<Edge<int>> path;
if (tryGetPath(23, out path))
    foreach (var e in path)
        Trace.WriteLine(e);

As you see, I'm trying to get the shortest path between node 20 and 23. And the output I get is

20 -> 4
22 -> 4
23 -> 22

It seems to be kind of right, but I don't really understand how to extract the node path from it. I expected something like:

20 -> 4
4 -> 22
22 -> 23

How can I build the final path from this output?


An example to get from 20 to 34:

 20->3 
 5->3 
 8->5 
 9->8
 36->9
 36->11
 34->11

Notice how 36->11 appears just before last edge.

1

There are 1 best solutions below

0
StayOnTarget On

I have not used that particular algorithm in QuickGraph but this is what it seems like...

Since you are using an undirected graph, the alg probably is working as intended.

For an undirected edge A->B (really A-B) I think you can equally describe the edge as B->A and that should be considered equivalent.

ie, you could interpret the output as an ordering of edges rather than an ordering of vertexes.


If you could add the additional nodes for your second longer example maybe we can check if this idea fully works.