1 Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
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.
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.
Heaps, Heap Sort, and Priority Queues
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Heaps Heaps are used to efficiently implement two operations:
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on MinHeap: Insert Delete Converting an array.
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 11 Binary Heap King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
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.
Chapter 21 Binary Heap.
data ordered along paths from root to leaf
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
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.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
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.
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:
ICS 353: Design and Analysis of Algorithms Heaps and the Disjoint Sets Data Structures King Fahd University of Petroleum & Minerals Information & Computer.
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.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
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.
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.
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.
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.
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.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
CS 367 Introduction to Data Structures Lecture 8.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
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). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
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)
Source: Muangsin / Weiss
Binary Heaps What is a Binary Heap?
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Binary Heaps What is a Binary Heap?
Binary Heaps What is a Binary Heap?
- Alan Perlis Heaps "You think you know when you can learn,
CSCI2100 Data Structures Tutorial 7
Heaps, Heap Sort, and Priority Queues
- Alan Perlis Topic 24 Heaps "You think you know when you can learn,
Tree Representation Heap.
Priority Queues (Heaps)
Heaps & Multi-way Search Trees
Presentation transcript:

1 Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an arbitrary key changing the priority of a key Building a binary heap top down approach bottom up approach Heap Applications: Heap Sort Heap as a priority queue

2 What is a Binary Heap? A binary heap is a complete binary tree with one (or both) of the following heap order properties: MinHeap property: Each node must have a key that is less or equal to the key of each of its children. MaxHeap property: Each node must have a key that is greater or equal to the key of each of its children. A binary heap satisfying the MinHeap property is called a MinHeap. A binary heap satisfying the MaxHeap property is called a MaxHeap. A binary heap with all keys equal is both a MinHeap and a MaxHeap. Recall: A complete binary tree may have missing nodes only on the right side of the lowest level. All levels except the bottom one must be fully populated with nodes All missing nodes, if any, must be on the right side of the lowest level

3 MinHeap and non-MinHeap examples A MinHeap Violates MinHeap property 21> Not a Heap Violates heap structural property Not a Heap Violates heap structural property Not a Heap

4 MaxHeap and non-MaxHeap examples A MaxHeap Violates MaxHeap property 65 < 67 Violates heap structural property Not a Heap Not a Heap Not a Heap

5 Array Representation of a Binary Heap A heap is a dynamic data structure that is represented and manipulated more efficiently using an array. Since a heap is a complete binary tree, its node values can be stored in an array, without any gaps, in a breadth-first order, where: Value(node i+1 ) array[ i ], for i > The root is array[0] The parent of array[i] is array  (i – 1)/2 , where i > 0 The left child, if any, of array[i] is array[2i+1]. The right child, if any, of array[i] is array[2i+2].

6 Array Representation of a Binary Heap (contd.) We shall use an implementation in which the heap elements are stored in an array starting at index 1. Value(node i )array[i], for i > The root is array[1]. The parent of array[i] is array  i/2 , where i > 1 The left child, if any, of array[i] is array[2i]. The right child, if any, of array[i] is array[2i+1].

7 Array Representation of a Binary Heap (contd.) The representation of a binary heap in an array makes several operations very fast: –add a new node at the end: O(1) –from a node, find its parent: O(1) –swap parent and child: O(1) –a lot of dynamic memory allocation of tree nodes is avoided

8 MinHeap Implementation public interface PriorityQueue extends Container{ public abstract void enqueue(Comparable comparable); public abstract Comparable findMin(); public abstract Comparable dequeueMin(); } A binary heap can serve as a priority queue Our MinHeap class will implement the following PriorityQueue interface:

9 MinHeap Implementation (contd.) public class BinaryHeap extends AbstractContainer implements PriorityQueue { protected Comparable array[]; public BinaryHeap(int i){ array = new Comparable[i + 1]; } public BinaryHeap(Comparable[] comparable) { this(comparable.length); for(int i = 0; i < comparable.length; i++) array[i + 1] = comparable[i]; count = comparable.length; buildHeapBottomUp(); }

10 Percolate Up In a MinHeap, if the key at a node, other than the root, becomes less than its parent, the heap property can be restored by swapping the current node and its parent, repeating this process for the parent if necessary, until – the key at the node is greater than or equal to that of the parent. – we reach the root. Procedure percolateUp Input: H[1..n], i where 1  i  n. Output: H, where no node is less than its parent on the path from node i to the root. done = false; while (!done && (i != 1)) { if H[i].key < H[i/2].key swap(H[i],H[i/2]); else done = true; i := i/2; } What is the complexity of PercolateUp?

11 Percolate Down In a MinHeap, if the value at a node becomes greater than the key of any of its children, the heap property can be restored by swapping the current node and the child with minimum key value, repeating this process if necessary until – the key at the node is less than or equal to the keys of both children. – we reach a leaf. Procedure percolateDown Input: H[1..n], i where 1  i  n. Output: H[i] is percolated down, if needed, so that it’s not greater than its children. done = false; while ( (2*i <= n) && !done) { i = 2*i; if ((i+1  n) and (H[i+1].key < H[i].key)) i = i+1; if (H[i/2].key > H[i].key) swap(H[i],H[i/2]); else done := true; } What is the complexity of percolateDown?

12 MinHeap enqueue The pseudo code algorithm for enqueing a key in a MinHeap is: Algorithm enqueue Input: A heap H[1..n] & a heap element x. Output: A new heap H[1..n+1] with x being one of its elements. 1. if (Heap is full) throw an exception; 2. n = n + 1; 3. H[n] = x; 4. percolateUp(H, n); Thus, the steps for enqueue are: 1.Enqueue the key at the end of the heap. 2.As long as the heap order property is violated, percolate up. What is the complexity of enqueue method?

13 MinHeap Insertion Example Insert Percolate up Percolate up

14 MinHeap enqueue implementation public void enqueue(Comparable key){ if(isFull()) throw new ContainerFullException(); int hole = ++count; // percolate up via a hole while(hole > 1 && key.compareTo(array[hole / 2])<0){ array[hole] = array[hole / 2]; hole = hole / 2 ; } array[hole] = key; } public boolean isFull(){ return count == array.length - 1; } To have better efficiency, we avoid repeated swapping We find a place (hole) for the new key, move the hole upward when needed, and at the end, put the key into the hole

15 MinHeap Insertion via hole Example To Insert 18 create a hole at index (heapSize + 1) < 31, Percolate hole up < 21, Percolate hole up 18 >= 13, insert 18 in the hole

16 Deleting an Arbitrary Key Algorithm Delete Input: A nonempty heap H[1..n] and i where 1  i  n. Output: H[1..n-1] after H[i] is removed. 1. if (Heap is empty) throw an exception 2. x = H[i]; y = H[n]; 3. n = n – 1; 4. if i == n+1 then return; // Heap consists of 1 node 5. H[i] = y; 6. if y.key  <= x.key then 7. percolateUp(H, i); 8. else percolateDown(H, i); What about dequeueMin()? What is the complexity of Delete method?

17 Example: Deleting an Arbitrary Key

18 MinHeap Dequeue Example 3.delete last node Delete min element 2. Replace by value of last node Percolate down Percolate down

19 MinHeap Dequeue via hole (Example) 3. delete last node Delete min element 2. Store 31,value of last node, in a temp variable > 18, Percolate hole down > 21, Percolate hole down 31 <= 32, insert 31 in hole

20 MinHeap dequeue Implementation public Comparable dequeueMin(){ if(isEmpty()) throw new ContainerEmptyException(); Comparable minItem = array[1]; array[1] = array[count]; count--; percolateDown(1); return minItem; } private void percolateDown(int hole){ int minChildIndex; Comparable temp = array[hole]; while(hole * 2 <= count){ //determine which child of current hole at 2i or 2i+1 is smaller minChildIndex = hole * 2; // assume it is at index 2i if(minChildIndex + 1 <= count && array[minChildIndex + 1].compareTo(array[minChildIndex])<0) minChildIndex++; // change to 2i + 1 if the assumption is wrong // if minChild is less than current hole if(array[minChildIndex].compareTo(temp)<0){ array[hole] = array[minChildIndex]; // percolate hole down hole = minChildIndex; // } else break; } array[hole] = temp; }

21 Changing the priority of a key There are three possibilities when the priority of a key x is changed: 1.The heap property is not violated. 2.The heap property is violated and x has to be percolated up to restore the heap property. 3. The heap property is violated and x has to be percolated down to restore the heap property. Example:

22 Building a heap (top down) A heap is built top-down by inserting one key at a time in an initially empty heap. After each key insertion, if the heap property is violated, it is restored by percolating the inserted key upward. The algorithm is: for(int i=1; i <= heapSize; i++){ read key; binaryHeap.enqueue(key); } Example: Insert the keys 4, 6, 10, 20, and 8 in this order in an originally empty max-heap Complexity: O(n log n)

23 Converting an array into a MinHeap (top down) (Example)

24 Converting an array into a Binary heap (bottom-up) The algorithm to convert an array into a binary heap is: 1.Start at the level containing the last non-leaf node (i.e., array[n/2], where n is the array size). 2.Make the subtree rooted at the last non-leaf node into a heap by invoking percolateDown. 3.Move in the current level from right to left, making each subtree, rooted at each encountered node, into a heap by invoking percolateDown. 4.If the levels are not finished, move to a lower level then go to step 3. The above algorithm can be refined to the following method of the BinaryHeap class: private void buildHeapBottomUp() { for(int i = count / 2; i >= 1; i--) percolateDown(i); }

25 Converting an array into a MinHeap (Example)

26 Converting an array into a MinHeap (Example) At each stage convert the highlighted tree into a MinHeap by percolating down starting at the root of the highlighted tree

27 Heap Application: Heap Sort A MinHeap or a MaxHeap can be used to implement an efficient sorting algorithm called Heap Sort. The following algorithm uses a MinHeap: Because the dequeueMin algorithm is O(log n), heapSort is an O(n log n) algorithm. Apart from needing the extra storage for the heap, heapSort is among the efficient sorting algorithms. public static void heapSort(Comparable[] array){ BinaryHeap heap = new BinaryHeap(array) ; for(int i = 0; i < array.length; i++) array[i] = heap.dequeueMin() ; }

28 Heap Sort with no extra storage Observation: after each dequeueMin, 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 dequeueMin, 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 Example (Adopted from another course): Max heap after the buildHeap phase for the input sequence 59,36,58,21,41,97,31,16,26,53

29 Heap Sort with no extra storage Heap after the first deleteMax operation:

30 Heap Sort with no extra storage Heap after the second deleteMax operation:

31 Heap Applications: Priority Queue A heap can be used as the underlying implementation of a priority queue. A priority queue is a data structure in which the items to be inserted have associated priorities. Items are withdrawn from a priority queue in order of their priorities, starting with the highest priority item first. Minimum priority queues treat low-valued keys as high-priority; maximum priority queues prioritizes high-valued keys Priority queues are often used in resource management, simulations, and in the implementation of some algorithms (e.g., some graph algorithms, some backtracking algorithms). Several data structures can be used to implement priority queues. Below is a comparison of some: Dequeue MinFind MinEnqueueData structure O(n) O(1)Unsorted List O(1) O(n)Sorted List O(log n) AVL Tree O(log n)O(1)O(log n)MinHeap