CSE 373: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Advanced Data Structures Chapter 16. Priority Queues Collection of elements each of which has a priority. Does not maintain a first-in, first-out discipline.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
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.
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.
Heapsort Based off slides by: David Matuszek
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Data Structure & Algorithm Lecture 5 Heap Sort & Binary Tree JJCAO.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Priority Queues and Binary Heaps Algorithms.
Heaps & Priority Queues
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Sorting With Priority Queue In-place Extra O(N) space
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
Partially Ordered Data ,Heap,Binary Heap
Heaps (8.3) CSE 2011 Winter May 2018.
Heaps, Heap Sort and Priority Queues
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
Source: Muangsin / Weiss
Part-D1 Priority Queues
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heaps.
ADT Heap data structure
CSCI2100 Data Structures Tutorial 7
Data Structures and Algorithms
Priority Queues.
CS200: Algorithm Analysis
Data Structures and Algorithms
Priority Queue and Binary Heap Neil Tang 02/12/2008
B-Tree Insertions, Intro to Heaps
Priority Queues.
Tree Representation Heap.
CSCE 3110 Data Structures and Algorithm Analysis
© 2013 Goodrich, Tamassia, Goldwasser
Ch. 8 Priority Queues And Heaps
Hassan Khosravi / Geoffrey Tien
"Teachers open the door, but you must enter by yourself. "
CS Data Structure: Heaps.
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Sorting and recurrence analysis techniques
CSE 373: Data Structures and Algorithms
Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
CSCE 3110 Data Structures and Algorithm Analysis
Data Structures Lecture 29 Sohail Aslam.
CSE 12 – Basic Data Structures
CSCE 3110 Data Structures and Algorithm Analysis
CSE 373 Priority queue implementation; Intro to heaps
Priority Queues CSE 373 Data Structures.
Heaps By JJ Shepherd.
Lecture 9: Intro to Trees
Lecture 16: Midterm recap + Heaps ii
Heaps.
Heaps Section 6.4, Pg. 309 (Section 9.1).
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

CSE 373: Data Structures and Algorithms Binary Heaps Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora Fung, Stuart Reges, Justin Hsia, Ruth Anderson, and many others for sample slides and materials ...

Problems 1. Merging multiple sorted arrays OutArray[k] = min(Array1[i1], Array2[i2]) // for 2 sorted arrays OutArray[k] = min(Array1[i1], Array2[i2], …, Arrayk[ik]) // for k sorted arrays 2. Given n 2D points, find k points which are closest to point P(x, y) S = Set of k distances For each point Q in the remaining points: if dist(P, Q) is less than max(S) removeMax from S Insert dist(P, Q) in S 3. Priority queues: Job schedulers Among all the job in a queue, get the job with the highest priority: removeMax(Priority Queue)

Problems 1. Merging multiple sorted arrays OutArray[k] = min(Array1[i1], Array2[i2]) // for 2 sorted arrays OutArray[k] = min(Array1[i1], Array2[i2], …, Arrayk[ik]) // for k sorted arrays 2. Given n 2D points, find k points which are closest to point P(x, y) S = Set of k distances For each point Q in the remaining points: if dist(P, Q) is less than max(S) removeMax from S Insert dist(P, Q) in S 3. Priority queues: Job schedulers Among all the job in a queue, get the job with the highest priority: removeMax(Priority Queue)

Desired behavior: Get extreme values (min or max) 1. Merging multiple sorted arrays OutArray[k] = min(Array1[i1], Array2[i2]) // for 2 sorted arrays OutArray[k] = min(Array1[i1], Array2[i2], …, Arrayk[ik]) // for k sorted arrays 2. Given n 2D points, find k points which are closest to point P(x, y) S = Set of k distances For each point Q in the remaining points: if dist(P, Q) is less than max(S) removeMax from S Insert dist(P, Q) in S 3. Priority queues: Job schedulers Among all the job in a queue, get the job with the highest priority: removeMax(Priority Queue)

Min Priority Queue ADT - Collection where elements ordered based on priority. - Behavior: - removeMin(): return element with smallest priority, removes element from collection - peekMin(): find, but do not remove, the element with smallest priority - insert(element): add element to the collection - Max Priority Queue ADT: - Same as Min Priority Queue ADT, just returns the largest instead of the smallest

Binary heap data structure - Invented in 1964 for sorting - Priority Queues is one of the main applications for binary heaps - Lots of other applications: greedy algorithms, shortest path - Basically, min-heap (or max-heap) is ideal when you want to maintain a collection of elements where you need to add arbitrary values but need an efficient removeMin (or removeMax).

Binary heap Binary heap is a 1. binary tree 2. that satisfies the heap property, and 3. where every heap is a “complete” tree

Binary heap: Heap property max-heap: Every node is larger than (or equal to) its children min-heap: Every node is smaller than (or equal to) its children

Binary heap: Complete binary tree A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. There are no “gaps” in a complete tree.

Complete binary tree 1 3 2 7 4 5 9 Complete binary tree 1 depth 0 2 3 depth 1 1 3 2 7 4 5 6 9 4 7 6 5 depth 2 9 depth 3 There as not complete binary trees

Question: Valid min-heap? Complete binary tree? Heap property satisfied? 1 2 3 4 7 6 5 9

Question: Valid min-heap? Complete binary tree? Heap property satisfied? Yes! 1 2 3 4 7 6 5 9

Question: Valid min-heap? Complete binary tree? Heap property satisfied? 1 2 8 4 7 6 5 9

Question: Valid min-heap? Complete binary tree? Heap property satisfied? No! 1 2 8 4 7 6 5 9

Binary heap insert 2 insert(12) 5 3 4 17 6 13 9

Binary heap insert 2 insert(12) 5 3 4 17 6 13 9 12

Binary heap insert 2 insert(12) 5 insert(7) 3 4 17 6 13 9 12

Binary heap insert 2 insert(12) 5 insert(7) 3 4 17 6 13 9 12 7

Binary heap insert insert(12) insert(7) Heap broken 2 5 3 4 17 6 13 9

Binary heap insert insert(12) insert(7) Heap broken 2 5 3 4 7 6 13 9 17 Heap broken

Binary heap insert 2 insert(12) 5 insert(7) 3 4 7 6 13 9 12 17

Binary heap insert insert(12) insert(7) insert(1) 2 5 3 4 7 6 13 9 12 17

Binary heap insert insert(12) insert(7) insert(1) 2 5 3 4 7 6 13 9 12 17 1

Binary heap insert insert(12) insert(7) insert(1) Heap broken 2 5 3 4 6 13 9 12 17 1 Heap broken

Binary heap insert insert(12) insert(7) insert(1) Heap broken 2 5 3 4 6 13 9 12 17 7 Heap broken

Binary heap insert insert(12) insert(7) insert(1) Heap broken 2 5 1 4 3 6 13 9 12 17 7 Heap broken

Binary heap insert insert(12) insert(7) insert(1) Heap broken 1 5 2 4 3 6 13 9 12 17 7 Heap broken

Binary heap insert insert(12) insert(7) insert(1) 1 5 2 4 3 6 13 9 12 17 7

Binary heap insert percolateUp insert(12) insert(7) insert(1) 1 5 2 4 3 6 13 9 12 17 7

Binary heap removeMin 1 removeMin() 5 2 4 3 6 13 9 12 17 7

Binary heap removeMin removeMin() 5 2 4 3 6 13 9 12 17 7

Binary heap removeMin 7 removeMin() 5 2 4 3 6 13 9 12 17

Binary heap removeMin 7 removeMin() 5 2 4 3 6 13 9 12 17 Heap broken

Binary heap removeMin percolateDown removeMin() Heap broken 7 5 2 4 3 6 13 9 12 17 Heap broken

Binary heap removeMin percolateDown removeMin() Heap broken 2 5 7 4 3 6 13 9 12 17 Heap broken

Binary heap removeMin percolateDown removeMin() Heap broken 2 5 3 4 7 6 13 9 12 17 Heap broken

Binary heap removeMin percolateDown 2 removeMin() 5 3 4 7 6 13 9 12 17

Binary heap: removeMin() runTime findLastNodeTime + removeRootTime + numOfSwaps * swapTime n + 1 + log(n) * 1 = O (n) How can we do better? What do we make efficient in the above expression?

Array implementation of a complete binary tree 1 2 3

Array implementation of a complete binary tree 1 2 3

Binary heap: Array implementation 2 11 3 4 7 6 13 9 12 17 Indices 1 2 3 4 5 6 7 8 9 10 11 12

Binary heap: Array implementation 5 3 4 7 6 13 9 12 17 2 Indices 1 2 3 4 5 6 7 8 9 10 11 12

Binary heap: Array implementation 4 7 6 13 9 12 17 2 3 5 Indices 1 2 3 4 5 6 7 8 9 10 11 12

Binary heap: Array implementation 9 12 17 2 3 5 4 7 6 13 Indices 1 2 3 4 5 6 7 8 9 10 11 12

Binary heap: Array implementation 2 3 5 4 7 6 13 9 12 17 Indices 1 2 3 4 5 6 7 8 9 10 11 12

Binary heap: Array implementation leftChild(i) = 2i rightChild(i) = 2i + 1 parent(i) = i / 2 2 3 5 4 7 6 13 9 12 17 Indices 1 2 3 4 5 6 7 8 9 10 11 12