I know this is probably one of the most fundamental algorithms you will have to learn when you are a CS major but I cannot seem to understand this part. I need to understand the BFS algorithm and this is what I have so far:
My question is how can you process the for loop... a.k.a how do you know which ones are unvisited and which ones are not. Assume that we are given a queue data strucutre and the start vertex and goal vertex.Code:#include <string> #include <queue> // aren't typedef's cool? typedef string Vertex; typedef queue<Vertex> queue; void BFS(V1, V2) { queue Q; // find the first vertex and push it in Q.push(V1); while(!Q.empty()){ Vertex t = Q.front(); // for(every unvisited neighbor of n) --- HOW DO YOU KNOW? Q.push(n); if(n == V2) Success(Q); // } Q.pop(); Failure(Q); } }



LinkBack URL
About LinkBacks


