Heaps, Heap Sort, and Priority Queues

Slides:



Advertisements
Similar presentations
DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
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.
Heaps, Heap Sort, and Priority Queues. Sorting III / Slide 2 Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees, Binary Trees, and Binary Search Trees. 2 Trees * Linear access time of linked lists is prohibitive n Does there exist any simple data structure.
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.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
CS 315 Lecture 18 Nov 15 Goals Complete heap operations review insert, deletemin decreaseKey, increaseKey heap building Heap sorting Some applications.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Lecture 7 Heaps and Priority Queues. Motivating Example 3 jobs have been submitted to a printer, the jobs have sizes 100, 10, 1 page. Average waiting.
Lecture 7 Heaps and Priority Queues. Motivating Example 3 jobs have been submitted to a printer, the jobs have sizes 100, 10, 1 page. Average waiting.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
1 Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
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.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort or bubble sort 1. Find the minimum value in the list 2. Swap it with the value.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 21 Binary Heap.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Trees, Binary Trees, and Binary Search Trees COMP171.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Priority Queues (Heaps)
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
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.
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.
1 Priority Queues (Heaps)  Sections 6.1 to Priority Queues  Regular queues which supports –First In, First Out –Enqueue(): add a new element.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
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:
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)
"Teachers open the door, but you must enter by yourself. "
CS 201 Data Structures and Algorithms
Priority Queues (Heaps)
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Heaps What is a Binary Heap?
Heaps, Heap Sort, and Priority Queues
Trees, Binary Trees, and Binary Search Trees
CMSC 341: Data Structures Priority Queues – Binary Heaps
Binary Heaps Priority Queues
CMSC 341 Lecture 14 Priority Queues & Heaps
Heaps and the Heapsort Heaps and priority queues
Binary Heaps Priority Queues
"Teachers open the door, but you must enter by yourself. "
CE 221 Data Structures and Algorithms
Binary Trees, Binary Search Trees
Priority Queues (Heaps)
Heaps & Multi-way Search Trees
Binary Trees, Binary Search Trees
Priority Queues (Heaps)
Presentation transcript:

Heaps, Heap Sort, and Priority Queues

Trees A tree T is a collection of nodes T can be empty (recursive definition) If not empty, a tree T consists of a (distinguished) node r (the root), and zero or more nonempty subtrees T1, T2, ...., Tk

Some Terminologies Child and Parent Leaves Sibling Every node except the root has one parent  A node can have an zero or more children Leaves Leaves are nodes with no children Sibling nodes with same parent

More Terminologies Path Length of a path Depth of a node A sequence of edges Length of a path number of edges on the path Depth of a node length of the unique path from the root to that node Height of a node length of the longest path from that node to a leaf all leaves are at height 0 The height of a tree = the height of the root = the depth of the deepest leaf Ancestor and descendant If there is a path from n1 to n2 n1 is an ancestor of n2, n2 is a descendant of n1 Proper ancestor and proper descendant

Example: UNIX Directory

Example: Expression Trees Leaves are operands (constants or variables) The internal nodes contain operators Will not be a binary tree if some operators are not binary

Background: Binary Trees root Has a root at the topmost level Each node has zero, one or two children A node that has no child is called a leaf For a node x, we denote the left child, right child and the parent of x as left(x), right(x) and parent(x), respectively. Parent(x) x leaf leaf left(x) right(x) leaf

A binary tree can be naturally implemented by pointers. Struct Node { double element; // the data Node* left; // left child Node* right; // right child // Node* parent; // parent } class Tree { public: Tree(); // constructor Tree(const Tree& t); ~Tree(); // destructor bool empty() const; double root(); // decomposition (access functions) Tree& left(); Tree& right(); // Tree& parent(double x); // … update … void insert(const double x); // compose x into a tree void remove(const double x); // decompose x from a tree private: Node* root; A binary tree can be naturally implemented by pointers.

Height (Depth) of a Binary Tree The number of edges on the longest path from the root to a leaf. Height = 4

Background: Complete Binary Trees A complete binary tree is the tree Where a node can have 0 (for the leaves) or 2 children and All leaves are at the same depth No. of nodes and height A complete binary tree with N nodes has height O(logN) A complete binary tree with height d has, in total, 2d+1-1 nodes height no. of nodes 1 1 2 2 4 3 8 d 2d

Proof: O(logN) Height Proof: a complete binary tree with N nodes has height of O(logN) Prove by induction that number of nodes at depth d is 2d Total number of nodes of a complete binary tree of depth d is 1 + 2 + 4 +…… 2d = 2d+1 - 1 Thus 2d+1 - 1 = N d = log(N+1)-1 = O(logN) Side notes: the largest depth of a binary tree of N nodes is O(N)

(Binary) Heap Heaps are “almost complete binary trees” All levels are full except possibly the lowest level If the lowest level is not full, then nodes must be packed to the left Pack to the left

1 4 2 5 2 5 4 3 6 1 3 6 A heap Not a heap Heap-order property: the value at each node is less than or equal to the values at both its descendants --- Min Heap It is easy (both conceptually and practically) to perform insert and deleteMin in heap if the heap-order property is maintained

Structure properties Has 2h to 2h+1-1 nodes with height h The structure is so regular, it can be represented in an array and no links are necessary !!! Use of binary heap is so common for priority queue implemen-tations, thus the word heap is usually assumed to be the implementation of the data structure

Heap Properties Heap supports the following operations efficiently Insert in O(logN) time Locate the current minimum in O(1) time Delete the current minimum in O(log N) time

Array Implementation of Binary Heap C A B C D E F G H I J 1 2 3 4 5 6 7 8 … D E F G H I J For any element in array position i The left child is in position 2i The right child is in position 2i+1 The parent is in position floor(i/2) A possible problem: an estimate of the maximum heap size is required in advance (but normally we can resize if needed) Note: we will draw the heaps as trees, with the implication that an actual implementation will use simple arrays Side notes: it’s not wise to store normal binary trees in arrays, because it may generate many holes

class Heap { public: Heap(); // constructor Heap(const Heap& t); ~Heap(); // destructor bool empty() const; double root(); // access functions Heap& left(); Heap& right(); Heap& parent(double x); // … update … void insert(const double x); // compose x into a heap void deleteMin(); // decompose x from a heap private: double* array; int array-size; int heap-size; }

Insertion Algorithm Add the new element to the next available position at the lowest level Restore the min-heap property if violated General strategy is percolate up (or bubble up): if the parent of the element is larger than the element, then interchange the parent and child. 1 1 1 2 5 2 5 2 2.5 swap 4 3 6 4 3 6 2.5 4 3 6 5 Insert 2.5 Percolate up to maintain the heap property

Insertion Complexity Time Complexity = O(height) = O(logN) A heap! 7 9 8 17 16 14 10 20 18 A heap! Time Complexity = O(height) = O(logN)

deleteMin: First Attempt Algorithm Delete the root. Compare the two children of the root Make the lesser of the two the root. An empty spot is created. Bring the lesser of the two children of the empty spot to the empty spot. A new empty spot is created. Continue

Example for First Attempt 1 2 5 4 3 6 2 5 4 3 6 2 5 4 3 6 1 3 5 4 6 Heap property is preserved, but completeness is not preserved!

deleteMin Copy the last number to the root (i.e. overwrite the minimum element stored there) Restore the min-heap property by percolate down (or bubble down)

An Implementation Trick (see Weiss book) Implementation of percolation in the insert routine by performing repeated swaps: 3 assignment statements for a swap. 3d assignments if an element is percolated up d levels An enhancement: Hole digging with d+1 assignments (avoiding swapping!) 7 7 7 4 9 8 9 4 8 8 17 16 14 10 17 14 10 17 9 14 10 4 20 18 20 18 16 20 18 16 Dig a hole Compare 4 with 16 Compare 4 with 9 Compare 4 with 7

Insertion PseudoCode void insert(const Comparable &x) { //resize the array if needed if (currentSize == array.size()-1 array.resize(array.size()*2) //percolate up int hole = ++currentSize; for (; hole>1 && x<array[hole/2]; hole/=2) array[hole] = array[hole/2]; array[hole]= x; }

deleteMin with ‘Hole Trick’ The same ‘hole’ trick used in insertion can be used here too 2 5 4 3 6 2. Compare children and tmp bubble down if necessary 2 5 4 3 6 1. create hole tmp = 6 (last element) 2 5 3 4 6 3. Continue step 2 until reaches lowest level 2 5 3 4 6 4. Fill the hole

deleteMin PseudoCode void deleteMin() { if (isEmpty()) throw UnderflowException(); //copy the last number to the root, decrease array size by 1 array[1] = array[currentSize--] percolateDown(1); //percolateDown from root } void percolateDown(int hole) //int hole is the root position int child; Comparable tmp = array[hole]; //create a hole at root for( ; hold*2 <= currentSize; hole=child){ //identify child position child = hole*2; //compare left and right child, select the smaller one if (child != currentSize && array[child+1] <array[child] child++; if(array[child]<tmp) //compare the smaller child with tmp array[hole] = array[child]; //bubble down if child is smaller else break; //bubble stops movement array[hole] = tmp; //fill the hole

Heap is an efficient structure Array implementation ‘hole’ trick Access is done ‘bit-wise’, shift, bit+1, …

Heapsort (1) Build a binary heap of N elements the minimum element is at the top of the heap (2)   Perform N DeleteMin operations the elements are extracted in sorted order (3) Record these elements in a second array and then copy the array back

Build Heap Input: N elements Output: A heap with heap-order property Method 1: obviously, N successive insertions Complexity: O(NlogN) worst case

Heapsort – Running Time Analysis (1) Build a binary heap of N elements repeatedly insert N elements  O(N log N) time (there is a more efficient way, check textbook p223 if interested) (2) Perform N DeleteMin operations Each DeleteMin operation takes O(log N)  O(N log N) (3) Record these elements in a second array and then copy the array back O(N) Total time complexity: O(N log N) Memory requirement: uses an extra array, O(N)

Heapsort: in-place, no extra storage Observation: after each deleteMin, the size of heap shrinks by 1 We can use the last cell just freed up to store the element that was just deleted  after the last deleteMin, the array will contain the elements in decreasing sorted order To sort the elements in the decreasing order, use a min heap To sort the elements in the increasing order, use a max heap the parent has a larger element than the child

Sort in increasing order: use max heap Delete 97

Delete 16 Delete 14 Delete 10 Delete 9 Delete 8

One possible Heap ADT Template <typename Comparable> class BinaryHeap { public: BinaryHeap(int capacity=100); explicit BinaryHeap(const vector<comparable> &items); bool isEmpty() const; void insert(const Comparable &x); void deleteMin(); void deleteMin(Comparable &minItem); void makeEmpty(); private: int currentSize; //number of elements in heap vector<Comparable> array; //the heap array void buildHeap(); void percolateDown(int hole); }

Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job A – 100 pages Job B – 10 pages Job C -- 1 page Average waiting time with FIFO service: (100+110+111) / 3 = 107 time units Average waiting time for shortest-job-first service: (1+11+111) / 3 = 41 time units A queue be capable to insert and deletemin? Priority Queue

Priority Queue Priority queue is a data structure which allows at least two operations insert deleteMin: finds, returns and removes the minimum elements in the priority queue Applications: external sorting, greedy algorithms deleteMin insert Priority Queue

Possible Implementations Linked list Insert in O(1) Find the minimum element in O(n), thus deleteMin is O(n) Binary search tree (AVL tree, to be covered later) Insert in O(log n) Delete in O(log n) Search tree is an overkill as it does many other operations Eerr, neither fit quite well…

It’s a heap!!!