创建无向图并在 QuickGraph 中使用 BFS 遍历它

发布于 2024-08-23 13:36:01 字数 158 浏览 3 评论 0 原文

我试图弄清楚如何使用 QuickGraph for C# 创建无向加权图的新实例。

我的目标是创建一个无向加权图,其中填充了随机数量的节点以及随机生成的起始和结束节点,其最短路径可以使用广度优先搜索算法找到。

文档内容不多,因此如果有人可以提供任何帮助,我们将不胜感激。

I'm trying to figure out how to create a new instance of a undirected weighted graph using QuickGraph for C#.

My goal is to create a undirected weighted graph populated with a random number of nodes and randomly generated start and finish nodes whose shortest path can found using Breadth-First Search algorithm.

There's not much to the documentation, so if anyone can provide any assistance that would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以往的大感动 2024-08-30 13:36:01

Richard,QuickGraph 不会为您执行任何操作,它只会提供您可以订阅的事件。通过订阅这些事件,您可以做出相应的回应。来自公认缺乏深度优先搜索的 QuickGraph 文档(是的,我知道你正在执行 BFS 而不是 DFS,但如何订阅事件的概念是相同的):

  1. InitializeVertex,在开始计算之前在每个顶点上调用,
  2. DiscoverVertex ,在第一次遇到顶点时调用,
  3. ExamineEdge,在发现顶点后在每个顶点的每个出边上调用,
  4. TreeEdge,在每条边成为形成搜索树的边的成员时调用。
  5. FinishVertex,在顶点的所有出边已添加到搜索树并且已发现所有相邻顶点(但在检查其出边之前)后在顶点上调用。

顺便说一下,打开 Reflector 并查看 QuickGraph.Algorithms.Observers。使用与 BFS 不同的方法,您的最短路径要求会更容易。

Richard, QuickGraph does not do any of this for you, it only makes events available which you can subscribe to. By subscribing to those events you can respond accordingly. From the admittedly lacking QuickGraph documentation on Depth First Search (yes, I realize you're doing BFS and not DFS, but the concept of how to subscribe to events is the same):

  1. InitializeVertex, invoked on each vertex before starting the computation,
  2. DiscoverVertex, invoked when a vertex is encountered for the first time,
  3. ExamineEdge, invoked on every out-edge of each vertex after it is discovered,
  4. TreeEdge, invoked on each edge as it becomes a member of the edges that form the search tree.
  5. FinishVertex, invoked on a vertex after all of its out edges have been added to the search tree and all of the adjacent vertices have been discovered (but before their out edges have been examined).

By the way, open up Reflector and take a look at QuickGraph.Algorithms.Observers. And your shortest path requirement would be easier with a different method than BFS.

赢得她心 2024-08-30 13:36:01

目前还没有该算法的文档;但还有下一个最好的事情(或者甚至更好的事情):单元测试!

如果您下载 QuickGraph 源代码,并找到 BreadthFirstAlgorithmSearchTest.BreadthFirstSearchAll(),您将看到该算法的示例用法,该算法在测试项目中的所有有向图上运行 BFS。

There's no documentation for this algorithm yet; but there's the next best thing (or perhaps even a better thing): a Unit Test!

If you download the QuickGraph sources, and find the BreadthFirstAlgorithmSearchTest.BreadthFirstSearchAll(), you will see an example usage of the algorithm which runs BFS on all the directed graphs in the test project.

苍风燃霜 2024-08-30 13:36:01

Github 上有一个简短的帖子,其中有一个有用的基本示例,说明如何设置 BFS 并从中获取一些结果。

特定于您的应用程序的其他详细信息(创建随机图等)显然不属于此示例的一部分。

来源:
https://github.com/YaccConstructor/QuickGraph/issues/189#issuecomment- 487493207

这是一个完整的示例:

UndirectedGraph<字符串,边<字符串>>> g = new UndirectedGraph>();

g.AddVerticesAndEdge(new Edge("0", "1"));
g.AddVerticesAndEdge(new Edge("0", "2"));
g.AddVerticesAndEdge(new Edge("2", "3"));

var algo = new UndirectedBreadthFirstSearchAlgorithm<字符串,Edge<字符串>>(g);

var Observer = new UndirectedVertexPredecessorRecorderObserver>();

var rootVertex =“0”;
使用 (observer.Attach(algo))
{
    算法.Compute(rootVertex);
}

var 目标顶点 = "3";
bool findPath = Observer.TryGetPath(targetVertex, out IEnumerable> path);

路径将包含两条边:

[0]: "0"->"2"
[1]:“2”->“3”

There was a brief thread on Github which had a useful basic example of how to setup BFS and get some results from it.

The other details specific to your application (creating a random graph, etc.) are obviously not part of this example.

Source:
https://github.com/YaccConstructor/QuickGraph/issues/189#issuecomment-487493207

Here's a complete example:

UndirectedGraph<string, Edge<string>> g = new UndirectedGraph<string, Edge<string>>();

g.AddVerticesAndEdge(new Edge<string>("0", "1"));
g.AddVerticesAndEdge(new Edge<string>("0", "2"));
g.AddVerticesAndEdge(new Edge<string>("2", "3"));

var algo = new UndirectedBreadthFirstSearchAlgorithm<string, Edge<string>>(g);

var observer = new UndirectedVertexPredecessorRecorderObserver<string, Edge<string>>();

var rootVertex = "0";
using (observer.Attach(algo))
{
    algo.Compute(rootVertex);
}

var targetVertex = "3";
bool foundPath = observer.TryGetPath(targetVertex, out IEnumerable<Edge<string>> path);

path will then contain the two edges:

[0]: "0"->"2"
[1]: "2"->"3"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文