Presentation is loading. Please wait.

Presentation is loading. Please wait.

Breadth-First Search Graph Algorithm #2.

Similar presentations


Presentation on theme: "Breadth-First Search Graph Algorithm #2."— Presentation transcript:

1 Breadth-First Search Graph Algorithm #2

2 Depth-First Search

3 More Orderly Technique?

4 Breadth First Search (BFS)
start – 0 hops source dest 1 hop away 2 hops away 3 hops away int main () { Node *sourceNode = getNodebyID (sourceID); bool found = bfsPath (sourceNode, destID); } bool bfsPath (Node* sourceNode, int destID) { ...

5 Breadth First Search source dest
bool bfsPath (Node* sourceNode, int destID) { list<Node *> wavefront; // Nodes to explore next wavefront.push_back (sourceNode); while (wavefront not empty) { Node *currNode = wavefront.front (); wavefront.pop_front(); // Remove node from wavefront if (currNode->id == destID) return (true); for (each outEdge of currNode) { Node *toNode = outEdge.toNode; wavefront.push_back (toNode); } return (false); // No path exits! wavefront = {2} {} {3} {5} {3,5} {5} {5,5} {4,3,5} {4,3} {2, 3} {0} {1, 2} {3,4,3} source dest 1 2 3 4 5

6 Breadth First Search source dest
bool bfsPath (Node* sourceNode, int destID) { list<Node *> wavefront; // Nodes to explore next wavefront.push_back (sourceNode); while (wavefront not empty) { Node *currNode = wavefront.front (); wavefront.pop_front(); // Remove node from wavefront if (currNode->id == destID) return (true); for (each outEdge of currNode) { Node *toNode = outEdge.toNode; wavefront.push_back (toNode); } return (false); // No path exists! wavefront = {2} {} {3} {5} {3,5} {5} {5,5} {4,3,5} {4,3} {2, 3} {3,4,3} {0} {1, 2} source dest 1 2 3 4 What do I know here? 5

7 Backtracing – General Idea
At each node (intersection) store the edge (streetSegment) that got you there When you reach the destination, follow these stored edges (streetSegments) back to the source Creates the path

8 BFS: How Do I Print Out the Path?
Need more information! struct WaveElem { Node *node; int edgeID; // ID of edge used to reach this node WaveElem (Node *n, int id) {node = n; edgeID = id;} }; #define NO_EDGE -1 // Illegal edge ID  no edge class Node { ... // Outgoing edges etc. int reachingEdge; // ID of the edge used to reach this node }

9 BFS: How Do I Print Out the Path?
bool bfsPath (Node* sourceNode, int destID) { list<WaveElem> wavefront; wavefront.push_back (waveElem (sourceNode, NO_EDGE)); while (wavefront not empty) { WaveElem curr = wavefront.front (); wavefront.pop_front(); // Remove node from wavefront curr.node->reachingEdge = curr.edgeID; if (curr.node->id == destID) return (true); for (each outEdge of curr.node) { Node *toNode = outEdge.toNode; wavefront.push_back ( WaveElem(toNode, outEdge.id)); } return (false); // No path exists! wavefront = {0/NO_EDGE} wavefront = {1/a, 2/b} wavefront = {} wavefront = {2/b} wavefront = {2/b, 3/e} source NO_EDGE b a 1 a 2 b e d c 3 d e 4 c f 5 dest f

10 BFS: How Do I Print Out the Path?
int main () { Node *sourceNode = getNodebyID (sourceID); bool found = bfsPath (sourceNode, destID); if (found) list<Edge> path = bfsTraceBack (destID); } list<Edge> bfsTraceBack (int destID) { list<Edge> path; Node *currNode = getNodebyID (destID); prevEdge = currNode->reachingEdge; while (prevEdge != NO_EDGE) { path.push_front (prevEdge); currNode = node at other end of prevEdge; return (path); path = {} path = {d,f} path = {f} path = {b,d,f} source NO_EDGE b a a b e d c d c f dest f


Download ppt "Breadth-First Search Graph Algorithm #2."

Similar presentations


Ads by Google