Download presentation
Presentation is loading. Please wait.
1
Breadth-First Search Graph Algorithm Type #3
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 exits! 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
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 } Why did I use an int instead of an Edge?
8
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 exits! 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
9
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 = prevEdge.fromNode; 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
10
Min. Path Issues What happened? Node 1 was re-expanded
source wavefront = {0} {2,3} {1,4} {4,3} {3,1} {1,2} b t = 5 s t = 5 s a 2 b c t = 5 s reachingEdge = c reachingEdge = a 1 What happened? Node 1 was re-expanded Overwrote the reachingEdge Messed up the path traceback d t = 6 s d 3 t = 3 s dest e e 4
11
Solution #1 Store shortest travel time from source at each node
wavefront = {0} {1,2} {1,4} {4,3} {3,1} {2,3} b t = 5 s t = 5 s a 2 b, 5 s c t = 5 s a, 5 s 1 Store shortest travel time from source at each node Only process node if shortest travel time to it has dropped d t = 6 s d, 11 s 3 t = 3 s dest e e, 14 s 4 Fixed!
12
Solution #1 Another Test Case This path is wrong! source dest
wavefront = {1,2} {0} {3,1} {2,3} b t = 5 s t = 20 s a 2 b, 5 s c t = 5 s a, 20 s 1 Another Test Case d t = 6 s d, 26 s 3 dest This path is wrong!
13
Solution #2 Store shortest travel time from source at each node
wavefront = {2/5s,3/26s} {1/20s,2/5s} {1/10s,4/29s} {3/16s} {} {4/19s} {0/0s} {4/29s,3/16s} {3/26s,1/10s} b t = 5 s t = 20 s a 2 b, 5 s c t = 5 s Store shortest travel time from source at each node Only update reachingEdge if shortest travel time drops Don’t stop BFS on first reach of destination Continue BFS until all entries in wavefront have higher travel time than best path found so far c, 10 s a, 20 s 1 d t = 6 s d, 16 s d, 26 s 3 t = 3 s dest e e, 19 s e, 29 s 4
14
BFS with Re-expansion bool bfsPath (Node* sourceNode, int destID) {
... bool found = false; while (wavefront not empty && has entry < bestPathLen) { WaveElem wave = wavefront.front (); wavefront.pop_front(); // Remove from wavefront Node *node = wave.node; if (wave.pathLen < node->pathLen) { node->reachingEdge = wave.edgeID; node->pathLen = wave.pathLen; if (node->id == destID) { found = true; bestPathLen = node->pathLen; }
15
BFS with Re-expansion for each (outEdge of node) {
Node *toNode = outEdge.toNode; wavefront.push_back ( WaveElem(toNode, outEdge.id, node.pathLen + travelTime (outEdge)); } } // End if best path to this node } // End while wavefront might yield a better solution return (found);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.