1

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.

Community
  • 1
  • 1
Notbad
  • 5,936
  • 12
  • 54
  • 100
  • 1
    Can you just reverse the results after the first? That way it is readable – Kale_Surfer_Dude May 02 '20 at 01:16
  • Nah, not working. It works for that example. There are other outputs that break this. 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. How can this be so difficult? – Notbad May 02 '20 at 09:00
  • Darn. I'm not sure, are you 100% sure that there's not something swapped in your algo? – Kale_Surfer_Dude May 03 '20 at 04:15
  • Is there any actual issue with the paths? To me, they look good, there's just always the higher vertex first reported in an edge and the edges are directed for some reason although you have an undirected graph. What is the run-time type of `e` that you print? Is it `Edge`, `UndirectedEdge` or something different? – Zdeněk Jelínek May 04 '20 at 11:12

1 Answers1

0

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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81