October 30th – Priority QUeues

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
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.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
"Teachers open the door, but you must enter by yourself. "
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues and Heaps
Week 6 - Wednesday CS221.
Week 11 - Friday CS221.
Hashing Exercises.
Source: Muangsin / Weiss
March 31 – Priority Queues and the heap
Bohyung Han CSE, POSTECH
Cse 373 April 26th – Exam Review.
November 1st – Exam Review
Priority Queues (Heaps)
ADT Heap data structure
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.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CMSC 341: Data Structures Priority Queues – Binary Heaps
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued
Instructor: Lilian de Greef Quarter: Summer 2017
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
CSE 373: Data Structures and Algorithms
Priority Queue and Binary Heap Neil Tang 02/12/2008
B-Tree Insertions, Intro to Heaps
CSE332: Data Abstractions Lecture 4: Priority Queues
Instructor: Lilian de Greef Quarter: Summer 2017
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
Hassan Khosravi / Geoffrey Tien
"Teachers open the door, but you must enter by yourself. "
CE 221 Data Structures and Algorithms
CS Data Structure: Heaps.
Priority Queues (Chapter 6.6):
CSE 326: Data Structures Priority Queues & Binary Heaps
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Priority Queues & Heaps
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
CSE 373 Data Structures and Algorithms
CSE 373 Priority queue implementation; Intro to heaps
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
Priority Queues (Chapter 6):
CSE 373: Data Structures and Algorithms
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Lecture 16: Midterm recap + Heaps ii
CSE 373: Data Structures and Algorithms
Presentation transcript:

October 30th – Priority QUeues Cse 373 October 30th – Priority QUeues

Administrivia Practice exam out by tomorrow P1 EC graded by tonight P2 graded on Wednesday HW regrades tonight Please fill out the form if you have regrade questions.

Midterm Exam Friday, November 3rd, 2:30-3:20 No note sheets or calculators Exam review in class on Wednesday Covers everything through the end of today’s lecture

Priority Queue New ADT Objects in the priority queue have: Data Conditions Lower priority items should dequeue first Should be able to change priority of an item FIFO for equal priority?

Priority Queue insert(K key, int priority) Insert the key into the PQ with given priority findMin() Return the key that currently has lowest priority in the PQ (min-heap) deleteMin() Return and remove the key with lowest priority changePriority(K key, int newPri) Assign a new priority to the object key

Priority Queue How to implement? Keep data sorted (somehow) Array? Inserting into the middle is costly (must move other items) Linked list? Must iterate through entire list to find place Cannot move backward if priority changes

Priority Queue These data structures will all give us the behavior we want as far as the ADT, but they may be poor design decisions Any other data structures to try?

Priority QUeue Want the speed of trees (but not BST) Priority Queue has unique demands Other types of trees? Review BST first

Properties (BST) Tree (Binary) Root (Two) Children No cycles Search Comparable data Left child data < parent data Smallest child is at the left most node

Properties (BST) Binary tree may be useful Search property doesn’t help Always deleting min Put min on top!

Heap-order property Still a binary tree Instead of search (left < parent), parent should be less than children

Heap-order property Still a binary tree Instead of search (left < parent), parent should be less than children How to implement? Insert and delete are different than BST

Heap-order property Still a binary tree Instead of search (left < parent), parent should be less than children How to implement? Insert and delete are different than BST

Heaps The Priority Queue is the ADT The Heap is the Data Structure

Completeness Filling left to right and top to bottom is another property - completeness

Heap example

Heaps Heap property (parents < children) Complete tree property (left to right, bottom to top)

Heaps Heap property (parents < children) Complete tree property (left to right, bottom to top) How does this help? Array implementation

Heaps Insert into array from left to right For any parent at index i, children at 2*i+1 and 2*i+2 1 2 3 4

Review Array property A B C D E F G H I J K L A B C D E F G H I J K L 1 2 3 4 5 6 9 8 10 11 12 A B C 7 D E F G H I J K L A B C D E F G H I J K L 1 2 3 4 5 6 7 8 9 10 11 12 13

Heaps How to maintain heap property then? Parent must be higher priority than children Two functions – percolate up and percolate down

Heap Functions Percolate up When a new item is inserted: Place the item at the next position to preserve completeness Swap the item up the tree until it is larger than its parent

Heap Functions Percolate down When an item is deleted: Remove the root of the tree (to be returned) Move the last object in the tree to the root Swap the moved piece down while it is larger than it’s smallest child Only swap with the smallest child

Heaps as Arrays Because heaps are complete, they can be represented as arrays without any gaps in them. Naïve implementation: Left child: 2*i+1 Right child: 2*i + 2 Parent: (i-1)/2

Heaps as Arrays Alternate (common) implementation: Put the root of the array at index 1 Leave index 0 blank Calculating children/parent becomes: Left child: 2*i Right child: 2*i + 1 Parent: i/2

Heaps as Arrays Why do an array at all? + Memory efficiency + Fast accesses to data + Forces log n depth - Needs to resize - Can waste space Almost always done through an array

Analysis Let’s find an interesting algorithm to analyze

Analysis Let’s find an interesting algorithm to analyze Given an array of length n, how do we make that array into a heap?

Analysis Let’s find an interesting algorithm to analyze Given an array of length n, how do we make that array into a heap? Naïve approach? Make a new heap and add each element of the array into the heap

Analysis Let’s find an interesting algorithm to analyze Given an array of length n, how do we make that array into a heap? Naïve approach? Make a new heap and add each element of the array into the heap How long to finish?

Fun facts! Is it really O(n log n)?

Fun facts! Is it really O(n log n)? Early insertions are into empty trees

Fun facts! Is it really O(n log n)? Early insertions are into empty trees O(1)!

Fun facts! Is it really O(n log n)? Early insertions are into empty trees O(1)! Consider a simpler example, creating a sorted linked list. Adding at the end of a linked list with k items takes O(k) operations.

Fun facts! Is it really O(n log n)? Early insertions are into empty trees O(1)! Consider a simpler example, creating a sorted linked list. Adding at the end of a linked list with k items takes O(k) operations.

Fun facts! Is it really O(n log n)? Early insertions are into empty trees O(1)! Consider a simpler example, creating a sorted linked list. Adding at the end of a linked list with k items takes O(k) operations. 1+2+3+4+5…

Fun facts! Is it really O(n log n)? Early insertions are into empty trees O(1)! Consider a simpler example, creating a sorted linked list. Adding at the end of a linked list with k items takes O(k) operations. 1+2+3+4+5…

Fun facts! Is it really O(n log n)? Early insertions are into empty trees O(1)! Consider a simpler example, creating a sorted linked list. Adding at the end of a linked list with k items takes O(k) operations. 1+2+3+4+5… What is this summation?

Fun facts!

Fun facts! What does this mean?

Fun facts! What does this mean? Summing k from 1 to n is still O(n2)

Fun facts! What does this mean? Summing k from 1 to n is still O(n2) Similarly, summing log(k) from 1 to n is O(n log n)

Analysis Naïve approach: Must add n items

Analysis Naïve approach: Must add n items Each add takes how long?

Analysis Naïve approach: Must add n items Each add takes how long? log(n)

Analysis Naïve approach: Must add n items Each add takes how long? log(n) Whole operation is O(n log(n))

Analysis Naïve approach: Must add n items Each add takes how long? log(n) Whole operation is O(n log(n)) Can we do better? What is better? O(n)

Heaps Facts of binary trees

Heaps Facts of binary trees Increasing the height by one doubles the number of possible nodes

Heaps Facts of binary trees Increasing the height by one doubles the number of possible nodes Therefore, a complete binary tree has half of its nodes in the leaves

Heaps Facts of binary trees Increasing the height by one doubles the number of possible nodes Therefore, a complete binary tree has half of its nodes in the leaves A new piece of data is much more likely to have to percolate down to the bottom than be the smallest item in the heap

Buildheap So a naïve buildheap takes O(n log n)

Buildheap So a naïve buildheap takes O(n log n) Why implement at all?

Buildheap So a naïve buildheap takes O(n log n) Why implement at all? If we can get it O(n)!

Floyd’S Method Traverse the tree from bottom to top Reverse order in the array

Floyd’S Method Traverse the tree from bottom to top Reverse order in the array Start with the last node that has children. How to find?

Floyd’S Method Traverse the tree from bottom to top Reverse order in the array Start with the last node that has children. How to find? Size / 2

Floyd’S Method Traverse the tree from bottom to top Reverse order in the array Start with the last node that has children. How to find? Size / 2 Percolate down each node as necessary

Floyd’S Method Traverse the tree from bottom to top Reverse order in the array Start with the last node that has children. How to find? Size / 2 Percolate down each node as necessary Wait! Percolate down is O(log n)! This is an O(n log n) approach!

Floyd’S Method It is O(n log n), because big O is an upper bound, but there is a tighter analysis possible!

Floyd’S Method It is O(n log n), because big O is an upper bound, but there is a tighter analysis possible! How far does each node travel (at worst)

Floyd’S Method It is O(n log n), because big O is an upper bound, but there is a tighter analysis possible! How far does each node travel (at worst) Leaves don’t move at all: Height = 0

Floyd’S Method It is O(n log n), because big O is an upper bound, but there is a tighter analysis possible! How far does each node travel (at worst) Leaves don’t move at all: Height = 0 This is half the nodes in the tree

Floyd’S Method It is O(n log n), because big O is an upper bound, but there is a tighter analysis possible! How far does each node travel (at worst) 1/2 of the nodes don’t move: These are leaves – Height = 0 1/4 can move at most one 1/8 can move at most two

Floyd’S Method It is O(n log n), because big O is an upper bound, but there is a tighter analysis possible! How far does each node travel (at worst) 1/2 of the nodes don’t move: These are leaves – Height = 0 1/4 can move at most one 1/8 can move at most two …

Floyd’S Method

Floyd’S Method Thanks Wolfram Alpha!

Floyd’S Method Thanks Wolfram Alpha! Does this look like an easier summation?

Floyd’S Method

Floyd’S Method This is a must know summation!

Floyd’S Method This is a must know summation! 1/2 + 1/4 + 1/8 + … = 1

Floyd’S Method This is a must know summation! 1/2 + 1/4 + 1/8 + … = 1 How do we use this to prove our complicated summation?

Floyd’S Method 1/2 + 1/4 + 1/8 … … + 1/2n < 1

Floyd’S Method 1/2 + 1/4 + 1/8 … … + 1/2n < 1 1/4 + 1/8 … … + 1/2n < 1/2 1/8 … … + 1/2n < 1/4

Floyd’S Method 1/2 + 1/4 + 1/8 … … + 1/2n < 1 Vertical columns sum to: i/2^i, which is what we want What is the right summation? Our original summation plus 1

Floyd’S Method

Floyd’S Method This means that the number of swaps we perform in Floyd’s method is 2 times the size… So Floyd’s method is O(n)