//Here there one additional array "distTo" which tells you how many edges there is to "v" vertex from starting "s" vertex. //I use Adjacency List Graph so if you want to test code provided below or solve some problems you can use this Graph : public BreadthFirstSearch(GraphAdjList G, int s) { edgeTo = new int[G.VertexCount]; distTo = new int[G.VertexCount]; for (int i = 0; i < G.VertexCount; i++) { distTo[i] = -1; edgeTo[i] = -1; } this.s = s; BFSearch(G, s); }
static void Main(string[] args) { GraphAdjList g = new GraphAdjList(4); g.AddEdge(0, 1); g.AddEdge(0, 2); g.AddEdge(1, 2); g.AddEdge(2, 0); g.AddEdge(2, 3); g.AddEdge(3, 3); BreadthFirstSearch bfs = new BreadthFirstSearch(g, 2); }
public void BFSearch(GraphAdjList G, int S) { var queue = new Queue <int>(); queue.Enqueue(S); distTo[S] = 0; while (queue.Count != 0) { int v = queue.Dequeue(); Console.Write(v + " "); foreach (var w in G.GetAdj(v)) { if (distTo[w] == -1) { queue.Enqueue(w); distTo[w] = distTo[v] + 1; edgeTo[w] = v; } } } }