Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Similar documents
Binary Heaps. CSE 373 Data Structures Lecture 11

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Recall: Properties of B-Trees

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

CSCI2100B Data Structures Heaps

Algorithms and Data Structures

CS350: Data Structures Heaps and Priority Queues

Administration CSE 326: Data Structures

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

CMSC 341 Lecture 14: Priority Queues, Heaps

Priority Queues. Binary Heaps

The priority is indicated by a number, the lower the number - the higher the priority.

Data Structures and Algorithms

9. Heap : Priority Queue

Priority Queues Heaps Heapsort

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Priority Queues (Heaps)

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Priority Queues and Heaps

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Sorting and Searching

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CE 221 Data Structures and Algorithms

Priority Queues Heaps Heapsort

Sorting and Searching

Data Structures Lesson 9

Heap Model. specialized queue required heap (priority queue) provides at least

CSE332: Data Abstractions Lecture 4: Priority Queues; Heaps. James Fogarty Winter 2012

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

UNIT III BALANCED SEARCH TREES AND INDEXING

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

CSE373: Data Structures & Algorithms Priority Queues

Priority Queues (Heaps)

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

CSE 373: Data Structures and Algorithms

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Figure 1: A complete binary tree.

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Heaps Goodrich, Tamassia. Heaps 1

Priority queues. Priority queues. Priority queue operations

Binary Heaps in Dynamic Arrays

Priority Queues. 04/10/03 Lecture 22 1

COMP : Trees. COMP20012 Trees 219

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

Heaps, Heap Sort, and Priority Queues.

Figure 1: A complete binary tree.

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

CSE 373: Data Structures and Algorithms

Design and Analysis of Algorithms

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Nicki Dell Spring 2014

Binary Trees, Binary Search Trees

Programming II (CS300)

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Priority Queues and Binary Heaps

Heaps. 2/13/2006 Heaps 1

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

Dictionaries. Priority Queues

Course Review. Cpt S 223 Fall 2009

Data Structures in Java

Algorithms and Data Structures

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

CMSC 341 Lecture 15 Leftist Heaps

CSE 373: Data Structures and Algorithms

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

CompSci 201 Tree Traversals & Heaps

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

CMSC 341 Lecture 15 Leftist Heaps

8. Binary Search Tree

CS102 Binary Search Trees

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CMSC 341. Binary Heaps. Priority Queues

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

Analysis of Algorithms

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

CH 8. HEAPS AND PRIORITY QUEUES

Trees. (Trees) Data Structures and Programming Spring / 28

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

CH. 8 PRIORITY QUEUES AND HEAPS

Section 4 SOLUTION: AVL Trees & B-Trees

Section 1: True / False (1 point each, 15 pts total)

CSE 502 Class 16. Jeremy Buhler Steve Cole. March A while back, we introduced the idea of collections to put hash tables in context.

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

CS 315 Data Structures mid-term 2

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Balanced Binary Search Trees. Victor Gao

Today s Outline. The One Page Cheat Sheet. Simplifying Recurrences. Set Notation. Set Notation. Priority Queues. O( 2 n ) O( n 3 )

Lecture 7. Transform-and-Conquer

Transcription:

Binary Heaps COL Shweta Agrawal and Amit Kumar

Revisiting FindMin Application: Find the smallest ( or highest priority) item quickly Operating system needs to schedule jobs according to priority instead of FIFO Event simulation (bank customers arriving and departing, ordered according to when the event happened) Find student with highest grade, employee with highest salary etc.

Priority Queue ADT Priority Queue can efficiently do: FindMin (and DeleteMin) Insert What if we use Lists: If sorted, what is the run time for Insert and FindMin? Unsorted? Binary Search Trees: What is the run time for Insert and FindMin? Hash Tables: What is the run time for Insert and FindMin?

Less flexibility More speed Lists If sorted: FindMin is O(1) but Insert is O(N) If not sorted: Insert is O(1) but FindMin is O(N) Balanced Binary Search Trees (BSTs) Insert is O(log N) and FindMin is O(log N) Hash Tables Insert O(1) but no hope for FindMin BSTs look good but BSTs are efficient for all Finds, not just FindMin We only need FindMin

Better than a speeding BST We can do better than Balanced Binary Search Trees? Very limited requirements: Insert, FindMin, DeleteMin. The goals are: FindMin is O(1) Insert is O(log N) DeleteMin is O(log N)

Binary Heaps A binary heap is a binary tree (NOT a BST) that is: Complete: the tree is completely filled except possibly the bottom level, which is filled from left to right Satisfies the heap order property every node is less than or equal to its children or every node is greater than or equal to its children The root node is always the smallest node or the largest, depending on the heap order

Heap order property A heap provides limited ordering information Each path is sorted, but the subtrees are not sorted relative to each other A binary heap is NOT a binary search tree -1 0 1 0 These are all valid binary heaps (minimum) 1

Binary Heap vs Binary Search Tree Binary Heap min value Binary Search Tree min value Parent is less than both left and right children Parent is greater than left child, less than right child

Structure property A binary heap is a complete tree All nodes are in use except for possibly the right end of the bottom row

Examples complete tree, heap order is "max" not complete complete tree, heap order is "min" complete tree, but min heap order is broken

Array Implementation of Heaps (Implicit Pointers) Root node = A[1] Children of A[i] = A[i], A[i + 1] Parent of A[j] = A[j/] Keep track of current size N (number of nodes) value - 1 index 0 1 N =

FindMin and DeleteMin FindMin: Easy! Return root value A[1] Run time =? DeleteMin: 1 Delete (and return) value at root node 1

DeleteMin Delete (and return) value at root node 1 1

Maintain the Structure Property We now have a Hole at the root Need to fill the hole with another value 1 When we get done, the tree will have one less node and must still be complete 1 1

Maintain the Heap Property The last value has lost its node we need to find a new place for it 1 1

DeleteMin: Percolate Down? 1? 1 1 Keep comparing with children A[i] and A[i + 1] Copy smaller child up and go down one level Done if both children are item or reached a leaf node What is the run time? 1

1 1 1 Percolate Down no children one child at the end children PercDown(i:integer, x: integer): { // N is the number elements, i is the hole, x is the value to insert Case{ i > N : A[i] := x; //at bottom// i = N : if A[i] < x then A[i] := A[i]; A[i] := x; else A[i] := x; i < N : if A[i] < A[i+1] then j := i; else j := i+1; if A[j] < x then A[i] := A[j]; PercDown(j,x); else A[i] := x; }} 1//0 Binary Heaps - Lecture 1

DeleteMin: Run Time Analysis Run time is O(depth of heap) A heap is a complete binary tree Depth of a complete binary tree of N nodes? depth = log (N) Run time of DeleteMin is O(log N) 1

Insert Add a value to the tree Structure and heap order properties must still be correct when we are done 1 1

Maintain the Structure Property The only valid place for a new node in a complete tree is at the end of the array We need to decide on the correct value for the new node, and adjust the heap accordingly 1 0

Maintain the Heap Property The new value goes where? 1 1

Insert: Percolate Up? 1? 1? 1 Start at last node and keep comparing with parent A[i/] If parent larger, copy parent down and go up one level Done if parent item or reached top node A[1]

Insert: Done 1 Run time?

Binary Heap Analysis Space needed for heap of N nodes: O(MaxN) An array of size MaxN, plus a variable to store the size N Time FindMin: O(1) DeleteMin and Insert: O(log N) BuildHeap from N inputs??? 1//0 Binary Heaps - Lecture

Build Heap BuildHeap { for i = N/ to 1 PercDown(i, A[i]) } N= 1 1 1 1//0 Binary Heaps - Lecture

Build Heap 1 1//0 Binary Heaps - Lecture

Build Heap 1 1 1//0 Binary Heaps - Lecture

Time Complexity Naïve considerations: n/ calls to PercDown, each takes clog)n) Total:cn log n More careful considerations: Only O(n)

Analysis of Build Heap Assume n = h+1 1 where h is height of the tree Thus, level h has h nodes but there is nothing to PercDown At level h-1 there are h-1 nodes, each might percolate down 1 level At level h-j, there are h j nodes, each might percolate down j levels Total Time = O(n) 1//0 Binary Heaps - Lecture

Other Heap Operations Find(X, H): Find the element X in heap H of N elements What is the running time? O(N) FindMax(H): Find the maximum element in H Where FindMin is O(1) What is the running time? O(N) We sacrificed performance of these operations in order to get O(1) performance for FindMin 1//0 Binary Heaps - Lecture 1

Other Heap Operations DecreaseKey(P,Δ,H): Decrease the key value of node at position P by a positive amount Δ, e.g., to increase priority First, subtract Δ from current value at P Heap order property may be violated so percolate up to fix Running Time: O(log N) 1//0 Binary Heaps - Lecture

Other Heap Operations IncreaseKey(P, Δ,H): Increase the key value of node at position P by a positive amount Δ, e.g., to decrease priority First, add Δ to current value at P Heap order property may be violated so percolate down to fix Running Time: O(log N) 1//0 Binary Heaps - Lecture

Other Heap Operations Delete(P,H): E.g. Delete a job waiting in queue that has been preemptively terminated by user Use DecreaseKey(P, Δ,H) followed by DeleteMin Running Time: O(log N) 1//0 Binary Heaps - Lecture

Other Heap Operations Merge(H1,H): Merge two heaps H1 and H of size O(N). H1 and H are stored in two arrays. Can do O(N) Insert operations: O(N log N) time Better: Copy H at the end of H1 and use BuildHeap. Running Time: O(N) 1//0 Binary Heaps - Lecture

Heap Sort Idea: buildheap then call deletemin n times E[] input = buildheap(...); E[] output = new E[n]; for (int i = 0; i < n; i++) { output[i] = deletemin(input); } Runtime? Best-case Worst-case Average-case Stable? In-place? CSE: Data Structures & Algorithms

Heap Sort Idea: buildheap then call deletemin n times E[] input = buildheap(...); E[] output = new E[n]; for (int i = 0; i < n; i++) { output[i] = deletemin(input); } Runtime? Best-case, Worst-case, and Average-case: O(n log(n)) Stable? No In-place? No. But it could be, with a slight trick... CSE: Data Structures & Algorithms

In-place Heap Sort But this reverse sorts how would you fix that? Treat the initial array as a heap (via buildheap) When you delete the i th element, put it at arr[n-i] That array location isn t needed for the heap anymore! 1 heap part sorted part put the min at the end of the heap data arr[n-i]= deletemin() 1 heap part sorted part CSE: Data Structures & Algorithms

AVL sort? Hash sort? AVL Tree: sure, we can also use an AVL tree to: insert each element: total time O(n log n) Repeatedly deletemin: total time O(n log n) Better: in-order traversal O(n), but still O(n log n) overall But this cannot be done in-place and has worse constant factors than heap sort CSE: Data Structures & Algorithms