Instructor: Lilian de Greef Quarter: Summer 2017

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Tyler Robison Summer
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
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.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Dan Grossman Spring 2012.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CSE373: Data Structures & Algorithms Lecture 8: Priority Queues Aaron Bauer Winter 2014.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps Linda Shapiro Spring 2016.
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2012.
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.
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
CSE332: Data Abstractions Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structure & Algorithms
CSE373: Data Structures & Algorithms
CS 201 Data Structures and Algorithms
Week 7 - Friday CS221.
Priority Queues and Heaps
CSE373: Data Structures & Algorithms
CSIT 402 Data Structures II
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
October 30th – Priority QUeues
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Week 11 - Friday CS221.
Source: Muangsin / Weiss
March 31 – Priority Queues and the heap
Cse 373 April 26th – Exam Review.
November 1st – Exam Review
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Monday, April 16, 2018 Announcements… For Today…
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CMSC 341: Data Structures Priority Queues – Binary Heaps
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued
Instructor: Lilian de Greef Quarter: Summer 2017
Instructor: Lilian de Greef Quarter: Summer 2017
CMSC 341 Lecture 14 Priority Queues & Heaps
B-Tree Insertions, Intro to Heaps
CSE332: Data Abstractions Lecture 4: Priority Queues
ITEC 2620M Introduction to Data Structures
Instructor: Lilian de Greef Quarter: Summer 2017
Heaps and the Heapsort Heaps and priority queues
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
CE 221 Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
CSE 326: Data Structures Priority Queues & Binary Heaps
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
CSE 332: Data Abstractions AVL Trees
CSE 373: Data Structures and Algorithms
Data Structures and Analysis (COMP 410)
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
Richard Anderson Spring 2016
Lecture 10: BST and AVL Trees
Data Structures and Algorithm Analysis Priority Queues (Heaps)
CSE 373 Data Structures Lecture 8
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Priority Queues Binary Heaps
Presentation transcript:

Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373: Data Structures and Algorithms Lecture 11: Finish AVL Trees; Priority Queues; Binary Heaps Instructor: Lilian de Greef Quarter: Summer 2017

Today Announcements Finish AVL Trees Priority Queues Binary Heaps

Announcements Changes to Office Hours: from now on… AVL Tree Height No more Wednesday morning & shorter Wednesday afternoon office hours New Thursday office hours! Kyle’s Wednesday hour is now 1:30-2:30pm Ben’s office hours is now Thursday 1:00-3:00pm AVL Tree Height In section, computed that minimum # nodes in AVL tree of a certain height is S(h) = 1 + S(h-1) + S(h-2) where h = height of tree Posted a proof next to these lecture slides online for your perusal

Announcements Midterm Course Feedback Next Friday! (at usual class time & location) Everything we cover in class until exam date is fair game (minus clearly-marked “bonus material”). That includes next week’s material! Today’s hw3 due date designed to give you time to study. Course Feedback Heads up: official UW-mediated course feedback session for part of Wednesday Also want to better understand an anonymous concern on course pacing → Poll

The AVL Tree Data Structure An AVL tree is a self-balancing binary search tree. Structural properties Binary tree property (same as BST) Order property (same as for BST) Balance condition: balance of every node is between -1 and 1 where balance(node) = height(node.left) – height(node.right) Result: Worst-case depth is O(log n)

(Figures by Melissa O’Neill, reprinted with her permission to Lilian) Single Rotations (Figures by Melissa O’Neill, reprinted with her permission to Lilian)

Case #3: Right-Left Case (Figures by Melissa O’Neill, reprinted with her permission to Lilian)

Case #3: Right-Left Case (after one rotation) right rotate (Figures by Melissa O’Neill, reprinted with her permission to Lilian)

Case #3: Right-Left Case (after two rotations) left rotate right rotate A way to remember it: Move d to grandparent’s position. Put everything else in their only legal positions for a BST. (Figures by Melissa O’Neill, reprinted with her permission to Lilian)

Practice time! Example of Case #4 50 10 80 30 60 Starting with this AVL tree: Which of the following is the updated AVL tree after inserting 42? D) A) B) C) 10 30 42 80 50 60 42 10 30 80 60 50 10 30 50 80 42 60 10 30 50 80 42 60

(extra space for your scratch work and notes)

Practice time! Example of Case #4 50 10 80 30 60 Starting with this AVL tree: Which of the following is the updated AVL tree after inserting 42? What’s the name of this case? What rotations did we do?

Insert, summarized Insert as in our generic BST Check back up path for imbalance, which will be 1 of 4 cases: Node’s left-left grandchild is too tall Node’s left-right grandchild is too tall Node’s right-left grandchild is too tall Node’s right-right grandchild is too tall Only occurs because After the appropriate single or double rotation, the smallest-unbalanced subtree has the same height as before the insertion So all ancestors are now balanced

AVL Tree Efficiency Worst-case complexity of find: Worst-case complexity of insert: Worst-case complexity of buildTree: Takes some more rotation action to handle delete…

Pros and Cons of AVL Trees Arguments for AVL trees: All operations logarithmic worst-case because trees are always balanced Height balancing adds no more than a constant factor to the speed of insert and delete Arguments against AVL trees: Difficult to program & debug [but done once in a library!] More space for height field Asymptotically faster but rebalancing takes a little time If amortized logarithmic time is enough, use splay trees (also in the text, not covered in this class)

Lots of cool Self-Balancing BSTs out there! Popular self-balancing BSTs include: AVL tree Splay tree 2-3 tree AA tree Red-black tree Scapegoat tree Treap (Not covered in this class, but several are in the textbook and all of them are online!) (From https://en.wikipedia.org/wiki/Self-balancing_binary_search_tree#Implementations)

Wrapping Up: Hash Table vs BST Dictionary Hash Table advantages: BST advantages: Can get keys in sorted order without much extra effort Same for ordering statistics, finding closest elements, range queries Easier to implement if don’t have hash function (which are hard!). Can guarantee O(log n) worst-case time, whereas hash tables are O(1) average time.

Priority Queue ADT & Binary Heap data structure Like a Queue, but with priorities for each element.

An Introductory Example… Gill Bates, the CEO of the software company Millisoft, built a robot secretary to manage her hundreds of emails. During the day, Bates only wants to look at a few emails every now and then so she can stay focused. The robot secretary sends her the most important emails each time. To do so, he assigns each email a priority when he gets them and only sends Bates the highest priority emails upon request. All of your computer servers are on fire! Priority: Here’s a reminder for our meeting in 2 months. Priority:

highest priority email Priority Queue ADT A priority queue holds compare-able data Like dictionaries, we need to compare items Given x and y, is x less than, equal to, or greater than y Meaning of the ordering can depend on your data The data typically has two fields: We’ll now use integers for examples, but can use other types /objects for priorities too! In our introductory example: new email highest priority email 7 18 12 23 3 45 15 6 2

Priority Queue ADT Meaning: A priority queue holds compare-able data Key property: Operations: 7 18 12 23 3 45 15 6 2

Priority Queue: Example insert x1 with priority 5 insert x2 with priority 3 a = deleteMin insert x3 with priority 2 insert x4 with priority 6 c = deleteMin d = deleteMin Analogy: insert is like enqueue, deleteMin is like dequeue But the whole point is to use priorities instead of FIFO insert deleteMin

Applications Like all good ADTs, the priority queue arises often Run multiple programs in the operating system “critical” before “interactive” before “compute-intensive” Treat hospital patients in order of severity (or triage) Forward network packets in order of urgency Select most frequent symbols for data compression Sort (first insert all, then repeatedly deleteMin) Much like Homework 1 uses a stack to implement reverse

Finding a good data structure Will show an efficient, non-obvious data structure for this ADT But first let’s analyze some “obvious” ideas for n data items data insert algorithm / time deleteMin algorithm / time unsorted array unsorted linked list sorted circular array sorted linked list binary search tree AVL tree add at end search add at front search search / shift move front put in right place remove at front put in right place leftmost

Our data structure A binary min-heap (or just binary heap or just heap) has: Structure property: Heap property: The priority of every (non-root) node is less important than the priority of its parent So: Where is the highest-priority item? Where is the lowest priority? What is the height of a heap with n items? 99 60 40 80 20 10 50 700 85 7 3 18 5 10 Where is the highest-priority item? Root Where is lowest priority item? Leaf node What is the height of a heap with n items? height is logn COMPLETE TREE!

deleteMin: Step #1 3 4 9 8 5 7 10 6 11 1

deleteMin: Step #2 (Keep Structure Property) Want to keep structure property 3 4 9 8 5 7 10 6 11

deleteMin: Step #3 Want to restore heap property 3 4 9 8 5 7 6 11

deleteMin: Step #3 (Restore Heap Property) Percolate down: Compare priority of item with its children If item has lower priority, swap with the most important child Repeat until both children have lower priority or we’ve reached a leaf node What is the run time? 3 4 9 8 5 7 10 6 11 ? 11 4 9 8 5 7 10 6 3 ? 8 4 9 10 5 7 6 11 3 Run time? Runtime is O(height of heap) = O(logn) Height of a complete binary tree of n nodes =  log2(n) 

deleteMin: Run Time Analysis Run time is A heap is a So its height with n nodes is So run time of deleteMin is

insert: Step #1 2 3 4 9 8 5 7 6 11 1

insert: Step #2 2 3 4 9 8 5 7 6 11 1

insert: Step #2 (Restore Heap Property) Percolate up: Put new data in new location If higher priority than parent, swap with parent Repeat until parent is more important or reached root What is the running time? 2 8 4 9 10 5 7 6 11 1 ? 2 5 8 4 9 10 7 6 11 1 ? 2 5 8 9 10 4 7 6 11 1 ?

Summary: basic idea for operations findMin: return root.data deleteMin: answer = root.data Move right-most node in last row to root to restore structure property “Percolate down” to restore heap property insert: Put new node in next position on bottom row to restore structure property “Percolate up” to restore heap property Overall strategy: Preserve structure property Restore heap property

Binary Heap Operations O(log n) insert O(log n) deleteMin worst-case Very good constant factors If items arrive in random order, then insert is O(1) on average O(log n) insert O(log n) deleteMin worst-case Possible because we don’t support unneeded operations; no need to maintain a full sort

Summary: Priority Queue ADT insert deleteMin 7 18 12 23 3 45 15 6 2 Priority Queue ADT: insert comparable object, deleteMin Binary heap data structure: Complete binary tree Each node has less important priority value than its parent insert and deleteMin operations = O(height-of-tree)=O(log n) insert: put at new last position in tree and percolate-up deleteMin: remove root, put last element at root and percolate-down 99 60 40 80 20 10 700 50 85

Binary Trees Implemented with an Array From node i: left child: i*2 right child: i*2+1 parent: i/2 (wasting index 0 is convenient for the index arithmetic) A B C 2 * i D E F G (2 * i)+1 └ i / 2┘ H I J K L Left child of node I = 2 * I Right child I = (2*i) +1 Parent of node I is at i/2 (floor) if tree is complete, array won’t have empty slots (excpet maybe at end) 1 2 3 4 5 6 7 8 9 10 11 12 13

Judging the array implementation Pros: Non-data space: just index 0 and unused space on right In conventional tree representation, one edge per node (except for root), so n-1 wasted space (like linked lists) Array would waste more space if tree were not complete Multiplying and dividing by 2 is very fast (shift operations in hardware) Last used position is just index Cons: Same might-be-empty or might-get-full problems we saw with array-based stacks and queues (resize by doubling as necessary) Pros outweigh cons: min-heaps almost always use array implementation

insert (in this order): 16, 32, 4, 67, 105, 43, 2 deleteMin once Practice time! Starting with an empty array-based binary heap, which is the result after insert (in this order): 16, 32, 4, 67, 105, 43, 2 deleteMin once 1 2 3 4 5 6 7 A) C) 4 15 32 43 67 105 1 2 3 5 6 7 4 32 16 43 105 67 1 2 3 5 6 7 B) D) 16 32 4 67 105 43 1 2 3 5 6 7 4 32 16 67 105 43 1 2 3 5 6 7

(extra space for your scratch work and notes)

Semi-Pseudocode: insert into binary heap void insert(int val) { if(size==arr.length-1) resize(); size++; i=percolateUp(size,val); arr[i] = val; } int percolateUp(int hole, int val) { while(hole > 1 && val < arr[hole/2]) arr[hole] = arr[hole/2]; hole = hole / 2; } return hole; 99 60 40 80 20 10 70 0 50 85 This pseudocode uses ints. In real use, you will have data nodes with priorities. 10 20 80 40 60 85 99 700 50 1 2 3 4 5 6 7 8 9 11 12 13

Semi-Pseudocode: deleteMin from binary heap int deleteMin() { if(isEmpty()) throw… ans = arr[1]; hole = percolateDown (1,arr[size]); arr[hole] = arr[size]; size--; return ans; } int percolateDown(int hole, int val) { while(2*hole <= size) { left = 2*hole; right = left + 1; if(right > size || arr[left] < arr[right]) target = left; else target = right; if(arr[target] < val) { arr[hole] = arr[target]; hole = target; } else break; } return hole; 99 60 40 80 20 10 700 50 85 10 20 80 40 60 85 99 700 50 1 2 3 4 5 6 7 8 9 11 12 13

Example insert (in this order): 16, 32, 4, 67, 105, 43, 2 deleteMin once 1 2 3 4 5 6 7

Example insert (in this order): 16, 32, 4, 67, 105, 43, 2 deleteMin once 1 2 3 4 5 6 7

Other operations decreaseKey: given pointer to object in priority queue (e.g., its array index), lower its priority value by p Change priority and percolate up increaseKey: given pointer to object in priority queue (e.g., its array index), raise its priority value by p Change priority and percolate down remove: given pointer to object in priority queue (e.g., its array index), remove it from the queue decreaseKey with p = , then deleteMin Running time for all these operations? O(logn)