Breadth-First Search: Complexity

Slides:



Advertisements
Similar presentations
Breadth-First Search Graph Algorithm Type #3. Depth-First Search.
Advertisements

CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
A few m3 tips. Speed Tuning 1.Algorithm 2.Data structures 3.Low level code string streetName1, streetName2; if (streetName1 != streetName2) {... int streetId1,
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Administration CI meetings resume next week, as usual Some TAs in labs, plus Dr. Betz –Go to your lab as usual –If your TA is not there, ask for help from.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue. Nov 4,
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Dijkstra animation. Dijksta’s Algorithm (Shortest Path Between 2 Nodes) 2 Phases:initialization;iteration Initialization: 1. Included:(Boolean) 2. Distance:(Weight)
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
A few m3 tips. Street Segment Length / Travel Time Need to compute in double precision –Otherwise too much round off See piazza.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
Homework 6 Shortest Paths!. The Basic Idea You have seen BFS and DFS tree searches. These are nice, but if we add the concept of “distance”, we can get.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Fibonacci Heaps. Fibonacci Binary insert O(1) O(log(n)) find O(1) N/A union O(1) N/A minimum O(1) O(1) decrease key O(1) O(log(n)) delete O(log(n) O(log(n))
Breadth-First Search Graph Algorithm Type #3.
Sorting With Priority Queue In-place Extra O(N) space
Programming Abstractions
Chapter 11 Heap.
Recitation 3 (Heap Sort and Binary Search Tree)
Depth First Seach: Output Fix
B-Trees B-Trees.
Priority Queues An abstract data type (ADT) Similar to a queue
Programming Abstractions
Priority Queues and Heaps
October 30th – Priority QUeues
Hashing Exercises.
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Discrete Optimization Lecture 1
ADT Heap data structure
Interval Heaps Complete binary tree.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Chapter 8 – Binary Search Tree
Programming Abstractions
Priority Queue & Heap CSCI 3110 Nan Chen.
i206: Lecture 14: Heaps, Graphs intro.
CSE 373: Data Structures and Algorithms
Priority Queue and Binary Heap Neil Tang 02/12/2008
ITEC 2620M Introduction to Data Structures
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
Ch. 8 Priority Queues And Heaps
Hassan Khosravi / Geoffrey Tien
Heap Sort CSE 2011 Winter January 2019.
Algorithms and Data Structures Lecture VIII
Binary Heaps What if we’re mostly concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap.
Priority Queues An abstract data type (ADT) Similar to a queue
Breadth-First Search Graph Algorithm #2.
Min Heap Update E.g. remove smallest item 1. Pop off top (smallest) 3
Algorithms: Design and Analysis
BFS: Min. Path Issues What happened? Node 1 was re-expanded
Informed Search Idea: be smart about what paths to try.
CSC 380: Design and Analysis of Algorithms
CSE 373 Data Structures and Algorithms
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
B-Trees.
OPIM 915 Fall 2010 Data Structures 23-38,
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
Informed Search Idea: be smart about what paths to try.
Interesting Algorithms for Real World Problems
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

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?

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

Re-Expansion Wastes CPU

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

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

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)

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

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

(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

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

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

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

BFS and Dijkstra Demo

Better than Dijkstra’s Algorithm?

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

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

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”

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)

A* Demo

Milestone 3 Performance Tuning Tips

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

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

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

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?