Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can someone give a simplest example of BFS? #189

Open
petrasvestartas opened this issue Feb 11, 2019 · 1 comment
Open

Can someone give a simplest example of BFS? #189

petrasvestartas opened this issue Feb 11, 2019 · 1 comment

Comments

@petrasvestartas
Copy link

petrasvestartas commented Feb 11, 2019

I have undirected-graph below, and try to compute bfs, but I do not how how to get the visited edges.
So far I have:
The test file is too difficult for me to understand in the GitHub repository.
HELP

//Create undirected graph
UndirectedGraph<string, Edge<string>> g = new UndirectedGraph<string, Edge<string>>();

g.AddVertexRange(N);
for (int i = 0; i < U.Count; i++){
  g.AddEdge(new QuickGraph.Edge<string>(U[i], V[i]));
 }

//BFS
 var algo = new QuickGraph.Algorithms.Search.UndirectedBreadthFirstSearchAlgorithm<string, QuickGraph.Edge<string>>(g);
@jnyrup
Copy link
Contributor

jnyrup commented Apr 29, 2019

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants