简体   繁体   English

了解 QuickGraph ShortestPathsDijkstra 结果

[英]Understanding QuickGraph ShortestPathsDijkstra results

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).我有一张图,想用 Dijkstra 算法进行一些最短路径搜索(我并不真正关心算法,但 dijkstra 是我熟悉的算法)。

This is the relevant part of graph I have:这是我拥有的图表的相关部分:

在此处输入图像描述

Now I do a dijkstra search following Quickgraph documentation:现在我按照 Quickgraph 文档进行 dijkstra 搜索:

//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 和 23 之间的最短路径。我得到的 output 是

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?如何从此 output 构建最终路径?


An example to get from 20 to 34:从 20 到 34 的示例:

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

Notice how 36->11 appears just before last edge.注意36->11是如何出现在最后一条边之前的。

I have not used that particular algorithm in QuickGraph but this is what it seems like...我没有在 QuickGraph 中使用过那个特定的算法,但这就是它的样子......

Since you are using an undirected graph, the alg probably is working as intended.由于您使用的是无向图,因此 alg 可能按预期工作。

For an undirected edge A->B (really AB ) I think you can equally describe the edge as B->A and that should be considered equivalent.对于无向边缘A->B (实际上是AB ),我认为您可以同样将边缘描述为B->A ,这应该被认为是等效的。

ie, you could interpret the output as an ordering of edges rather than an ordering of vertexes.即,您可以将 output 解释为边的排序而不是顶点的排序。


If you could add the additional nodes for your second longer example maybe we can check if this idea fully works.如果您可以为第二个更长的示例添加额外的节点,也许我们可以检查这个想法是否完全有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM