COL 106 Shweta Agrawal and Amit Kumar

Slides:



Advertisements
Similar presentations
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)
Advertisements

CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
Trees Types and Operations
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2010.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
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.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
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.
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
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 Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Binary Heap.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
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.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
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.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CS 367 Introduction to Data Structures Lecture 8.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
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)
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Catie Baker Spring 2015.
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2012.
Nov 2, 2001CSE 373, Autumn Hash Table example marking deleted items + choice of table size.
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.
CSE373: Data Structures & Algorithms Priority Queues
Partially Ordered Data ,Heap,Binary Heap
CSE373: Data Structures & Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Source: Muangsin / Weiss
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
CMSC 341 Lecture 14 Priority Queues & Heaps
CSE332: Data Abstractions Lecture 4: Priority Queues
Tree Representation Heap.
CSE 326: Data Structures Priority Queues & Binary Heaps
Priority Queues CSE 373 Data Structures.
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Presentation transcript:

COL 106 Shweta Agrawal and Amit Kumar Binary Heaps COL 106 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: What if we use… 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

These are all valid binary heaps (minimum) 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 2 1 1 4 6 2 6 7 5 8 4 7 These are all valid binary heaps (minimum)

Binary Heap vs Binary Search Tree min value 5 94 10 94 10 97 min value 97 24 5 24 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 6 2 4 2 4 6 5 2 2 4 6 5 6 7 5 7 4 complete tree, heap order is "max" 5 not complete 2 2 4 6 5 6 7 5 7 4 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[2i], A[2i + 1] Parent of A[j] = A[j/2] Keep track of current size N (number of nodes) 1 2 2 3 value - 2 4 6 7 5 4 6 index 1 2 3 4 5 6 7 7 5 4 5 N = 5

FindMin and DeleteMin FindMin: Easy! DeleteMin: Return root value A[1] Run time = ? DeleteMin: Delete (and return) value at root node 2 4 3 7 5 8 10 11 9 6 14

DeleteMin Delete (and return) value at root node 4 3 7 5 8 10 11 9 6 14

Maintain the Structure Property We now have a “Hole” at the root Need to fill the hole with another value When we get done, the tree will have one less node and must still be complete 4 3 7 5 8 10 11 9 6 14 4 3 7 5 8 10 11 9 6 14

Maintain the Heap Property The last value has lost its node we need to find a new place for it 14 4 3 7 5 8 10 11 9 6

DeleteMin: Percolate Down 14 ? 14 3 ? 3 4 3 4 4 8 7 5 8 10 7 5 8 10 7 5 14 10 11 9 6 11 9 6 11 9 6 Keep comparing with children A[2i] and A[2i + 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 2 3 4 5 6 Percolate Down 6 | 10 | 8 | 13 | 14 | 25 PercDown(i:integer, x: integer): { // N is the number elements, i is the hole, x is the value to insert Case{ 2i > N : A[i] := x; //at bottom// 2i = N : if A[2i] < x then A[i] := A[2i]; A[2i] := x; else A[i] := x; 2i < N : if A[2i] < A[2i+1] then j := 2i; else j := 2i+1; if A[j] < x then A[i] := A[j]; PercDown(j,x); }} no children one child at the end 2 children 12/26/03 Binary Heaps - Lecture 11

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 = log2(N) Run time of DeleteMin is O(log N)

Insert Add a value to the tree Structure and heap order properties must still be correct when we are done 2 3 4 8 7 5 14 10 11 9 6

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 2 3 4 8 7 5 14 10 11 9 6

Maintain the Heap Property The new value goes where? 3 4 8 7 5 14 10 11 9 6 2

Insert: Percolate Up 2 3 3 ? 3 4 8 4 8 8 7 5 14 10 7 14 10 7 4 14 10 ? ? 11 9 6 11 9 6 5 11 9 6 5 2 2 Start at last node and keep comparing with parent A[i/2] If parent larger, copy parent down and go up one level Done if parent ≤ item or reached top node A[1]

Insert: Done 2 3 8 7 4 14 10 11 9 6 5 Run time?

Binary Heap Analysis Space needed for heap of N nodes: O(MaxN) Time 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 ??? 12/26/03 Binary Heaps - Lecture 11

Build Heap BuildHeap { for i = N/2 to 1 PercDown(i, A[i]) } N=11 1 11 11 2 3 5 10 5 10 4 5 6 7 9 4 8 12 9 3 8 12 2 7 6 3 2 7 6 4 8 11 9 10 12/26/03 Binary Heaps - Lecture 11

Build Heap 11 11 5 10 5 8 2 3 8 9 2 3 10 12 9 7 6 4 9 7 6 4 12/26/03 Binary Heaps - Lecture 11

Build Heap 11 2 2 8 3 8 5 3 10 12 5 4 10 12 9 7 6 4 9 7 6 11 12/26/03 Binary Heaps - Lecture 11

Time Complexity Naïve considerations: More careful considerations: n/2 calls to PercDown, each takes clog)n) Total:𝐜𝐧 𝐥𝐨𝐠 𝐧 More careful considerations: Only O(n)

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

Analysis of Build Heap Assume N = 2K –1 Level 1: k -1 steps for 1 item Level 2: k - 2 steps for 2 items Level 3: k - 3 steps for 4 items Level 4: k - 4 steps for 8 items Level i : k - i steps for 2i-1 items 12/26/03 Binary Heaps - Lecture 11

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) We sacrificed performance of these operations in order to get O(1) performance for FindMin 12/26/03 Binary Heaps - Lecture 11

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) 12/26/03 Binary Heaps - Lecture 11

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) 12/26/03 Binary Heaps - Lecture 11

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) 12/26/03 Binary Heaps - Lecture 11

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