Download presentation
Presentation is loading. Please wait.
Published byΦερενίκη Δαγκλής Modified over 5 years ago
1
BFS: 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
2
Solution 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!
3
Need To Store More Data struct WaveElem { Node *node;
int edgeID; // ID of edge used to reach this node double travelTime; // Total travel time to reach node WaveElem (Node *n, int id, float time) { node = n; edgeID = id; travelTime = time; } }; class Node { ... // Outgoing edges etc. int reachingEdge; // ID of the edge used to reach this node double bestTime; // Shortest time found to this node so far }
4
BFS with Re-expansion bool bfsPath (Node* sourceNode, int destID) {
... while (wavefront not empty) { WaveElem wave = wavefront.front (); // Get next node wavefront.pop_front(); // Remove from wavefront Node *currNode = wave.node; if (wave.travelTime < node->bestTime) { // Was this a better path to this node? Update if so. currNode->reachingEdge = wave.edgeID; currNode->bestTime = wave.travelTime; if (currNode->id == destID) // Found destination? return true;
5
BFS with Re-expansion for (each outEdge of currNode) {
Node *toNode = outEdge.toNode; wavefront.push_back ( WaveElem(toNode, outEdge.id, currNode->bestTime + travelTime (outEdge)); } } // End if best path to this node } // End while wavefront not empty (more to search) return false; // No path exists!
6
Breadth-First Search: Complexity
while (wavefront not empty) { If we use a visited flag: Executes maximum N times Work in loop is O(1) O(N) ~100,000 10-7 < 1 s BFS with re-expansion: Difficult to bound Can be significantly slower Demo How could we limit re-expansion?
7
Limiting Re-expansion
Only re-visit a node in BFS when We find a new path with a lower cost to that node
8
Re-Expansion Wastes CPU
9
BFS Wavefront bool bfsPath (Node* sourceNode, int destID) {
list<WaveElem> wavefront; while (wavefront not empty) { WaveElem wave = wavefront.front (); // Get next element wavefront.pop_front(); // Remove from wavefront Node *currNode = wave.node; if (wave.travelTime < currNode->bestTime) { ... currNode->bestTime = wave.travelTime; wavefront.push_back ( WaveElem(toNode, outEdge.id, currNode->bestTime + travelTime (outEdge)); 78, 20s 2, 5s 39, 12s Wavefront: FIFO Queue
10
Take the Smallest Entry in Wavefront?
source wavefront = {0/0s} {1/20s,1/10s} {1/20s,4/19s} {1/20s} {1/20s,3/16s} {1/20s,2/5s} b t = 5 s t = 20 s a 2 b, 5 s c t = 5 s c, 10 s 1 d t = 6 s d, 16 s 3 t = 3 s dest e Found shortest path with no / minimal re-expansion Djikstra’s algorithm e, 19 s 4
11
How Much Work To Get Smallest?
list<WaveElem> wavefront Linked list Insertion: O(1) Remove smallest: scan entire list O(M), where M is active wavefront entries M can be up to O(N), but usually more like O(N0.5) Binary tree? Insertion: O(log M) Remove smallest O(log M)
12
Overall Complexity O(N) items added/removed in wavefront
Work to remove next item: Linked list: O(M) O(N0.5) to O(N) Binary tree: O(log M) O (log N) Overall complexity Linked list: O(N1.5) to O(N2) N 100,000 and ~10-7 s per function 3.2 s to 1000 s (wide range!) Binary tree: O(N logN) <1 s
13
Anything Faster than Binary Tree?
Heap Insertion: O(log N) Removal of smallest element: O(log N) But absolute time smaller than binary tree
14
(Min) Heap Like a binary tree Key of parent < key of children
But not fully sorted Key of parent < key of children For all nodes in the tree Very good for priority queues Smallest item always at root of heap 3 20 36 40 35 37 60 No ordering between sibling nodes in the heap 50
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.