Presentation is loading. Please wait.

Presentation is loading. Please wait.

Breadth-First Search: Complexity

Similar presentations


Presentation on theme: "Breadth-First Search: Complexity"— Presentation transcript:

1 Breadth-First Search: Complexity
while (wavefront not empty && has entry < bestPathLen) { 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 But in practice much less than O(2N) of DFS with re-expansion How could we limit re-expansion?

2 Limiting Re-expansion
Only re-visit a node in BFS when We find a new path with a lower cost to that node

3 Re-Expansion Wastes CPU

4 BFS Wavefront bool bfsPath (Node* sourceNode, int destID) {
list<WaveElem> wavefront; 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) { ... wavefront.push_back ( WaveElem(toNode, outEdge.id, node.pathLen + travelTime (outEdge)); 78, 20s 2, 5s 39, 12s Wavefront: FIFO Queue

5 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

6 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)

7 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

8 Anything Faster than Binary Tree?
Heap Insertion: O(log N) Removal of smallest element: O(log N) But absolute time smaller than binary tree

9 (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

10 Min Heap Update E.g. remove smallest item 1. Pop off top (smallest) 3
20 36 40 35 37 60 50 2. Move bottom (large) item to top

11 Min Heap Update E.g. remove smallest item
3. Compare to children; swap if necessary 50 20 36 40 35 37 60

12 Min Heap Update E.g. remove smallest item O(log N) levels
20 4. Repeat for each level 50 36 40 35 37 60 O(log N) levels  O(log N) work

13 BFS and Dijkstra Demo

14 Better than Dijkstra’s Algorithm?

15 Dijkstra: Not So Smart! Why are we examining nodes in the wrong direction? Wasted CPU time!

16 Ideas? Only look at nodes that take us closer to the destination?
distToDest(node) < distToDest (predecessor) Too severe – can result in no path, or not shortest path

17 A* Algorithms Enhancements to Dijkstra’s algorithm
Use domain-specific heuristics E.g. we are working in a map Can estimate which graph nodes are closer vs. further from dest Incorporate into the wavefront sorting/ordering Look at the most promising partial solutions first “Best-first search”

18 Can go around blockages (e.g. 401)
A* Search Mostly searches in the right direction, but will explore some in other directions Can go around blockages (e.g. 401)

19 A* Demo

20 Milestone 3 Performance Tuning Tips

21 Speed Tuning Algorithm Data structures Low level code
string streetName1, streetName2; if (streetName1 != streetName2) { ... int streetId1, streetId2; if (streetId1 != streetId2) { Any speed difference?

22 Debugging Algorithms Start with a very simple test case
Adjacent intersections! Step / breakpoint code in debugger Make sure it does what you expect!

23 Debugging Algorithms Try a slightly harder case
Two intersections apart Step / breakpoint code in debugger again! Make sure code behaving exactly as you expect

24 Bigger Cases: Graphics Helps
Hard to see large amounts of data in debugger Use graphics to animate / visualize #define VISUALIZE while (wavefront.size() != 0) { WaveElem = wavefront.front(); #ifdef VISUALIZE draw_street_seg (WaveElem.edgeID); flushinput(); // Draw right away #endif ... Can’t leave this in when I submit (will fail unit tests). How do I turn this off when speed testing & before submitting code?


Download ppt "Breadth-First Search: Complexity"

Similar presentations


Ads by Google