BFS: Min. Path Issues What happened? Node 1 was re-expanded

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

Breadth-First Search Graph Algorithm Type #3. Depth-First Search.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Milestone 3: Finding Routes ECE 297. Directions: How?
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
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.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Heaps & Priority Queues
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)
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.
Leftist Trees Linked binary tree.
Chapter 11 Heap.
Recitation 3 (Heap Sort and Binary Search Tree)
Depth First Seach: Output Fix
Breadth-First Search: Complexity
Priority Queues and Heaps
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
October 30th – Priority QUeues
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Hashing Exercises.
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Tutorial 8 An optional eighth tutorial will be held the week of March 6. This tutorial will give you practice with and feedback on oral presentation and.
O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the.
Data Structures and Database Applications Binary Trees in C#
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Interval Heaps Complete binary tree.
CSCI2100 Data Structures Tutorial 7
Chapter 8 – Binary Search Tree
Part-D1 Priority Queues
Priority Queues.
i206: Lecture 14: Heaps, Graphs intro.
Priority Queue and Binary Heap Neil Tang 02/12/2008
Priority Queues.
ITEC 2620M Introduction to Data Structures
Tree Representation Heap.
Heaps A heap is a binary tree.
CSCE 3110 Data Structures and Algorithm Analysis
Ch. 8 Priority Queues And Heaps
Priority Queues (Chapter 6.6):
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
CSCE 3110 Data Structures and Algorithm Analysis
Milestone 3: Finding Routes
Breadth-First Search Graph Algorithm #2.
CSCE 3110 Data Structures and Algorithm Analysis
Min Heap Update E.g. remove smallest item 1. Pop off top (smallest) 3
Algorithms: Design and Analysis
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Priority Queues & Heaps
Priority Queues (Chapter 6):
Heaps By JJ Shepherd.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
1 Lecture 13 CS2013.
Priority Queue and Heap
Priority Queues Binary Heaps
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Heaps Section 6.4, Pg. 309 (Section 9.1).
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

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

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!

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 }

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;

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!

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?

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

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