简体   繁体   中英

Creating a undirected graph and traversing it using BFS in QuickGraph

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.

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.

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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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