Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of.
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)
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Data Structures Lecture 7 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
The Priority Queue Abstract Data Type. Heaps. Adaptable Priority Queue. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich,
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CSC401 – Analysis of Algorithms Lecture Notes 5 Heaps and Hash Tables Objectives: Introduce Heaps, Heap-sorting, and Heap- construction Analyze the performance.
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.
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
Chapter 8: Priority Queues
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
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.
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.
Chapter 8: Priority Queues and Heaps Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided.
1 Priority Queues CPS212 Gordon College VIP. 2 Introduction to STL Priority Queues Adaptor container - underlying container may be either: – a template.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
§3 Binary Heap 1. Structure Property: 【 Definition 】 A binary tree with n nodes and height h is complete iff its nodes correspond to the nodes numbered.
Chapter 21 Binary Heap.
Priority Queues & Heaps Chapter 9. Iterable Collection Abstract Collection Queue List Abstract Queue Priority Queue Array List Abstract List Vector Stack.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Priority Queues and Heaps. Outline and Reading PriorityQueue ADT (§8.1) Total order relation (§8.1.1) Comparator ADT (§8.1.2) Sorting with a Priority.
Chapter 2.4: Priority Queues and Heaps PriorityQueue ADT (§2.4.1) Total order relation (§2.4.1) Comparator ADT (§2.4.1) Sorting with a priority queue (§2.4.2)
HEAPS • Heaps • Properties of Heaps • HeapSort
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
CS 2468: Assignment 2 (Due Week 9, Tuesday. Drop a hard copy in Mail Box 75 or hand in during the lecture) Use array representation (double a[]) to implement.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Priority Queues Last Update: Oct 23, 2014 EECS2011: Priority Queues1.
1 COMP9024: Data Structures and Algorithms Week Seven: Priority Queues Hui Wu Session 1, 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 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
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,
COMP9024: Data Structures and Algorithms
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Chapter 2: Basic Data Structures
CSCI2100 Data Structures Tutorial 7
Heaps and Priority Queues
Priority Queues and Heaps
Part-D1 Priority Queues
Algorithms Sorting-part2.
Heaps and Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
Tree Representation Heap.
Heaps A heap is a binary tree.
Heaps and Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser
Copyright © Aiman Hanna All rights reserved
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Heaps and Priority Queues
CE 221 Data Structures and Algorithms
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Lecture 9 CS2013.
Heaps and Priority Queues
1 Lecture 10 CS2013.
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Heaps and Priority Queues

2 What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties: Heap-Order: for every internal node v other than the root, key(v)  key(parent(v)) 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 The last node of a heap is the rightmost internal node of depth h  1 last node

Heaps and Priority Queues3 Height of a Heap Theorem: A heap storing n keys has height O(log n) Proof: (we apply the complete binary tree property) Let h be the height of a heap storing n keys Since there are 2 i keys at depth i  0, …, h  2 and at least one key at depth h  1, we have n  1  2  4  …  2 h  2  1 Thus, n  2 h  1, i.e., h  log n  h22h2 1 keys 0 1 h2h2 h1h1 depth

Heaps and Priority Queues4 We can use a heap to implement a priority queue We store an item (key, element) at each internal node We keep track of the position of the last node For simplicity, we show only the keys in the pictures (2, Sue) (6, Mark)(5, Pat) (9, Jeff)(7, Anna)

Heaps and Priority Queues5 Insertion into a Heap For any element in array position i, the left child is in position 2*i, the right child is in the cell after the left child ( 2*i+1). Thus, the parent is in i/2. Heap-order-property: key(v)  key(parent(v)): To keep the smallest element at root, for every node v, the key of the parent of v is smaller than or equal to the key of v. A CB EDF I H J G ABCDEFGHIJ

Heaps and Priority Queues6 Insertion into a Heap insertItem() function of the priority queue ADT corresponds to the insertion of a key k to the heap The insertion algorithm consists of three steps 1. Find the insertion node z (the new last node) 2. Store k at z and expand z into an internal node 3. Restore the heap- order property: key(v)  key(parent(v)) insertion node z z

Heaps and Priority Queues7 Upheap Algorithm After the insertion of a new key k, the heap-order property may be violated Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node (percolate up) Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k Since a heap has height O(log n), upheap runs in O(log n) time z z

Heaps and Priority Queues8 Removal from a Heap removeMin() function of the priority queue ADT corresponds to the removal of the root key from the heap The removal algorithm consists of three steps 1. Replace the root key with the key of the last node w 2. Compress w and its children into a leaf 3. Restore the heap-order property (discussed next) last node w w

Heaps and Priority Queues9 Downheap (percolate down) After replacing the root key with the key k of the last node, the heap-order property may be violated Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root Downheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k Since a heap has height O(log n), downheap runs in O(log n) time w w

Heaps and Priority Queues10 Updating the Last Node The insertion node can be found by traversing a path of O(log n) nodes Go up until a left child or the root is reached If a left child is reached, go to the right child Go down left until a leaf is reached Similar algorithm for updating the last node after a removal

Heaps and Priority Queues root removeMin()  Down Heap Bubbling

Heaps and Priority Queues root

Heaps and Priority Queues root

Heaps and Priority Queues root

Heaps and Priority Queues root

Heaps and Priority Queues root

Heaps and Priority Queues root

Heaps and Priority Queues root

Heaps and Priority Queues root

Heaps and Priority Queues20 Heap-Sort Consider a priority queue with n items implemented by means of a heap the space used is O(n) functions insertItem and removeMin take O(log n) time methods size, isEmpty, minKey, and minElement take time O(1) time Using a heap-based priority queue, we can sort a sequence of n elements in O(n log n) time The resulting algorithm is called heap-sort Heap-sort is much faster than quadratic sorting algorithms, such as insertion-sort and selection-sort

Heaps and Priority Queues21 Implementation using an array of pointers

Heaps and Priority Queues root = 0.tail = 0

Heaps and Priority Queues root = 0.tail = 1 5

Heaps and Priority Queues root = 0.tail =

Heaps and Priority Queues root = 0.tail =

Heaps and Priority Queues root = 0.tail =

Heaps and Priority Queues root = 0.tail = Parent of “3” node is determined by subtracting the index of that node by 1 and dividing by 2. In this case its (3-1)/2 = 1

Heaps and Priority Queues root = 0.tail = Parent of “3” node is determined by subtracting the index of that node by 1 and dividing by 2. In this case its (3-1)/2 = 1 Compare parent with child.

Heaps and Priority Queues root = 0.tail = Parent of “3” node is determined by subtracting the index of that node by 1 and dividing by 2. In this case its (3-1)/2 = 1 Compare parent with child. Move the child up by one level.

Heaps and Priority Queues root = 0.tail = Parent of “3” node is determined by subtracting the index of that node by 1 and dividing by 2. In this case its (1-1)/2 = 0

Heaps and Priority Queues root = 0.tail = Parent of “3” node is determined by subtracting the index of that node by 1 and dividing by 2. In this case its (1-1)/2 = 0 Compare parent and child.

Heaps and Priority Queues root = 0.tail = Parent of “3” node is determined by subtracting the index of that node by 1 and dividing by 2. In this case its (1-1)/2 = 0 Compare parent and child. Move child up one level.

Heaps and Priority Queues root = 0.tail =

Heaps and Priority Queues root = 0.tail =

Heaps and Priority Queues root = 0.tail = Parent of “4” node is determined by subtracting the index of that node by 1 and dividing by 2. In this case its (4-1)/2 = 1

Heaps and Priority Queues root = 0.tail = Parent of “4” node is determined by subtracting the index of that node by 1 and dividing by 2. In this case its (4-1)/2 = 1 Compare parent and child.

Heaps and Priority Queues root = 0.tail = Parent of “4” node is determined by subtracting the index of that node by 1 and dividing by 2. In this case its (4-1)/2 = 1 Compare parent and child. Move child up one level.

Heaps and Priority Queues root = 0.tail = Parent of “4” node is determined by subtracting the index of that node by 1 and dividing by 2. In this case its (4-1)/2 = 1 Compare parent and child. Move child up one level.

Heaps and Priority Queues root = 0.tail = Parent of “4” node is determined by subtracting the index of that node by 1 and dividing by 2. In this case its (1-1)/2 = 0

Heaps and Priority Queues root = 0.tail = Parent of “4” node is determined by subtracting the index of that node by 1 and dividing by 2. In this case its (1-1)/2 = 0 Compare parent and child.

Heaps and Priority Queues root = 0.tail = Parent of “4” node is determined by subtracting the index of that node by 1 and dividing by 2. In this case its (1-1)/2 = 0 Compare parent and child. Leave child alone.

Heaps and Priority Queues42 Now remove the element with the smallest value

Heaps and Priority Queues root = 0.tail = Now to “pop” off the root node

Heaps and Priority Queues root = 0.tail = Now to “pop” off the root node. 3.object

Heaps and Priority Queues root = 0.tail = Now to “pop” off the root node. Move “tail” object to root. 3.object 5

Heaps and Priority Queues root = 0.tail = Now to “pop” off the root node. Move “tail” object to root. Change the “tail” index 3.object 5

Heaps and Priority Queues root = 0.tail = Now to “pop” off the root node. Move “tail” object to root. Change the “tail” index Compare root with two children 3.object 5

Heaps and Priority Queues root = 0.tail = Now to “pop” off the root node. Move “tail” object to root. Change the “tail” index Compare root with two children If greater, swap with smallest of the children 3.object 5

Heaps and Priority Queues root = 0.tail = Now to “pop” off the root node. Move “tail” object to root. Change the “tail” index Compare root with two children If greater, swap with smallest of the children 3.object 4

Heaps and Priority Queues root = 0.tail = Now to “pop” off the root node. Move “tail” object to root. Change the “tail” index Compare root with two children If greater, swap with smallest of the children Compare swapped object with children at indices 1*2+1=3 and 1*2+2=4 3.object 4

Heaps and Priority Queues root = 0.tail = Now to “pop” off the root node. Move “tail” object to root. Change the “tail” index Compare root with two children If greater, swap with smallest of the children Compare swapped object with children at indices 1*2+1=3 and 1*2+2=4 Node is smaller than both: stop 3.object 4

Heaps and Priority Queues root = 0.tail = Now to “pop” off the root node. Move “tail” object to root. Change the “tail” index Compare root with two children If greater, swap with smallest of the children Compare swapped object with children at indices 1*2+1=3 and 1*2+2=4 Node is smaller than both: stop Return the object 4

Implementation in C - HeapStruct 53 #define MinPQSize (10) #define MinData (-32767) typedef int ElementType; struct HeapStruct { int Capacity; int Size; ElementType *Elements; }; typedef struct HeapStruct *PriorityQueue;

Implementation in C - Initialize 54 PriorityQueue Initialize( int MaxElements ) { PriorityQueue H; if( MaxElements < MinPQSize ) Error( "Priority queue size is too small" ); H = malloc( sizeof( struct HeapStruct ) ); if( H ==NULL ) FatalError( "Out of space!!!" ); /* Allocate the array plus one extra for sentinel */ H->Elements = malloc((MaxElements + 1)*sizeof(ElementType)); if( H->Elements == NULL ) FatalError( "Out of space!!!" ); H->Capacity = MaxElements; H->Size = 0; H->Elements[ 0 ] = MinData; return H; }

55 int IsEmpty( PriorityQueue H ) { return H->Size == 0; } int IsFull( PriorityQueue H ) { return H->Size == H->Capacity; } Implementation in C – IsEmpty, IsFull

56 Implementation in C – Insert /* H->Element[ 0 ] is a sentinel */ void Insert( ElementType X, PriorityQueue H ) { int i; if( IsFull( H ) ) { Error( "Priority queue is full" ); return; } for( i = ++H->Size; H->Elements[ i / 2 ] > X; i /= 2 ) H->Elements[ i ] = H->Elements[ i / 2 ]; H->Elements[ i ] = X; }

57 Implementation in C – DeleteMin ElementType DeleteMin( PriorityQueue H ) { ElementType MinElement; if( IsEmpty( H ) ) { Error( "Priority queue is empty" ); return H->Elements[0]; } MinElement = H->Elements[ 1 ]; H->Elements[ 1 ] = H->Elements[ H->Size-- ]; percolateDown( H, 1 ); return MinElement; }

58 Implementation in C – percolateDown void percolateDown( PriorityQueue H, int hole ) { int child; ElementType tmp = H->Elements[ hole ]; for( ; hole * 2 Size; hole = child ) { /* Find smaller child */ child = hole * 2; if (child != H->Size && H->Elements[child+1] Elements[child]) child++; /* Percolate one level */ if( tmp > H->Elements[child] ) H->Elements[ hole ] = H->Elements[ child ]; else break; } H->Elements[ hole ] = tmp; }

Building a Heap Sometimes it is required to construct it from an initial collection of items O(NlogN) in the worst case. But insertions take O(1) on the average. Hence the question: is it possible to do any better? 59

buildHeap Algorithm General Algorithm Place the N items into the tree in any order, maintaining the structure property. Call buildHeap 60 void buildHeap( PriorityQueue H, int N ) { int i; for( i = N / 2; i > 0; i-- ) percolateDown( H, i ); }

Heaps and Priority Queues61 Merging Two Heaps We are given two heaps and a key k We create a new heap with the root node storing k and with the two heaps as subtrees We perform downheap to restore the heap- order property

Heaps and Priority Queues62 We can construct a heap storing n given keys in using a bottom-up construction with log n phases In phase i, pairs of heaps with 2 i  1 keys are merged into heaps with 2 i  1  1 keys Bottom-up Heap Construction 2 i  1 2i112i11

Heaps and Priority Queues63 Example

Heaps and Priority Queues64 Example (contd.)

Heaps and Priority Queues65 Example (contd.)

Heaps and Priority Queues66 Example (end)