HEAPS.

Slides:



Advertisements
Similar presentations
BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
Advertisements

Analysis of Algorithms
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677
Comp 122, Spring 2004 Heapsort. heapsort - 2 Lin / Devi Comp 122 Heapsort  Combines the better attributes of merge sort and insertion sort. »Like merge.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 7 Heapsort and priority queues Motivation Heaps Building and maintaining heaps.
COMP 171 Data Structures and Algorithms Tutorial 5 Heapsort.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
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.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
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.
2IL50 Data Structures Spring 2015 Lecture 3: Heaps.
Binary Heap.
2IL50 Data Structures Fall 2015 Lecture 3: Heaps.
Prepared by- Jatinder Paul, Shraddha Rumade
Introduction to Algorithms
1 Analysis of Algorithms Chapter - 03 Sorting Algorithms.
1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΡΧΕΙΩΝ
Chapter 6: Heapsort Combines the good qualities of insertion sort (sort in place) and merge sort (speed) Based on a data structure called a “binary heap”
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
CSE 5392 Fall 2005 Week 4 Devendra Patel Subhesh Pradhan.
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:
David Luebke 1 2/5/2016 CS 332: Algorithms Introduction to heapsort.
Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.
Sept Heapsort What is a heap? Max-heap? Min-heap? Maintenance of Max-heaps -MaxHeapify -BuildMaxHeap Heapsort -Heapsort -Analysis Priority queues.
Heapsort Lecture 4 Asst. Prof. Dr. İlker Kocabaş.
6.Heapsort. Computer Theory Lab. Chapter 6P.2 Why sorting 1. Sometimes the need to sort information is inherent in a application. 2. Algorithms often.
1 Algorithms CSCI 235, Fall 2015 Lecture 13 Heap Sort.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
Heaps, Heapsort, and Priority Queues
Heaps, Heap Sort and Priority Queues
Algorithms and Data Structures Lecture VI
Heapsort Chapter 6 Lee, Hsiu-Hui
Heapsort.
Heap Sort Example Qamar Abbas.
Introduction to Algorithms
Priority Queues Linked-list Insert Æ Æ head head
The Heap Data Structure
Heaps,heapsort and priority queue
CS 583 Analysis of Algorithms
Heaps, Heapsort, and Priority Queues
CS200: Algorithm Analysis
Ch 6: Heapsort Ming-Te Chi
Heapsort.
Lecture 3 / 4 Algorithm Analysis
Heap Sort The Heap Data Structure
"Teachers open the door, but you must enter by yourself. "
Heap Sort.
Design and Analysis of Algorithms
Binary and Binomial Heaps
Topic 5: Heap data structure heap sort Priority queue
Heapsort Sorting in place
Solving Recurrences Continued The Master Theorem
Priority Queues Supports the following operations. Insert element x.
Binary Heaps and Heapsort
Computer Algorithms CISC4080 CIS, Fordham Univ.
Algorithms CSCI 235, Spring 2019 Lecture 14 Heap Sort Read: Ch. 6
Asst. Prof. Dr. İlker Kocabaş
Algorithms CSCI 235, Spring 2019 Lecture 15 Analysis of Heap Sort
Presentation transcript:

HEAPS

Priority Queues Insert head head Linked-list Æ Æ 9 2 5 8 10 15 2 5 8

Priority Queues Supports the following operations. Insert element x. Return min/max element. Return and delete minimum/maximum element. Increase/Decrease key of element x to k. Max/Min-HEAPIFY BUILD-Max/Min-HEAP HEAPSORT Applications. Dijkstra's shortest path algorithm. Prim's MST algorithm. Event-driven simulation. Huffman encoding. Heapsort. . . . Event driven simulation: generate random inter-arrival times between customers, generate random processing times per customer, Advance clock to next event, skipping intervening ticks

Time Complexity Insert: Delete: n deletions and insertions: O(n) O(1) O(n2)

Heap A max (min) heap is a complete binary tree such that the data stored in each node is greater (smaller) than the data stored in its children, if any.

Heap Types Max-heaps (largest element at root), have the max-heap property: for all nodes i, excluding the root: A[PARENT(i)] ≥ A[i] Min-heaps (smallest element at root), have the min-heap property: A[PARENT(i)] ≤ A[i]

Binary Heap: Definition Almost complete binary tree. filled on all levels, except last, where filled from left to right Min-heap ordered. every child greater than (or equal to) parent 06 14 45 78 18 47 53 83 91 81 77 84 99 64

Binary Heap: Properties Min element is in root. Heap with N elements has height = log2 N. 06 N = 14 Height = 3 14 45 78 18 47 53 83 91 81 77 84 99 64

20 8 15 4 10 7 2 8 7 6 2

Larger than its parent 20 8 15 4 10 7 9 Not a heap

Not a complete tree – Not a heap Missing left child 25 12 10 5 9 7 11 1 6 3 8 Not a complete tree – Not a heap

Where to add the next node? Complete Binary Tree 1 3 2 5 4 7 6 8 9 10 11 12 Where to add the next node? How to find it?

After the first 1 from left: 0 – left; 1 – right 3 2 5 4 7 6 8 9 10 11 12 13 14 15 1 0001 6 0110 11 1011 2 0010 7 0111 12 1100 0011 8 1000 13 1101 0100 9 1001 14 1110 0101 10 1010 15 1111 After the first 1 from left: 0 – left; 1 – right

1 3 2 5 4 7 6 8 9 10 11 12 1 3 1 3 1 1 6 3 7 3 13 6 14 7 Next 13 1101 Next 14 1110

Binary Heaps: Array Implementation Implementing binary heaps. Use an array: no need for explicit parent or child pointers. Parent(i) = i/2 Left(i) = 2i Right(i) = 2i + 1 06 parent and children formula require only simple bit twiddling operations 1 14 45 2 3 78 18 47 53 4 5 6 7 83 91 81 77 84 99 64 8 9 10 11 12 13 14

Storing a Complete Binary Tree in an Array 1 3 2 5 4 7 6 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 for any node at index i parent = i/2 left child = i * 2 right child = i*2+1

Binary Heap: Insertion Insert element x into heap. Insert into next available slot. Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence 06 14 45 78 18 47 53 42 next free slot 83 91 81 77 84 99 64

Binary Heap: Insertion Insert element x into heap. Insert into next available slot. Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence swap with parent 06 14 45 78 18 47 53 42 42 83 91 81 77 84 99 64

Binary Heap: Insertion Insert element x into heap. Insert into next available slot. Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence swap with parent 06 14 45 78 18 47 42 42 83 91 81 77 84 99 64 53

Binary Heap: Insertion Insert element x into heap. Insert into next available slot. Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence stop: heap ordered 06 14 42 78 18 47 45 83 91 81 77 84 99 64 53 O(log N) operations.

Binary Heap: Decrease Key Decrease key of element x to k. Bubble up until it's heap ordered. 06 14 42 78 18 47 45 83 91 81 77 84 99 64 53 O(log N) operations.

Binary Heap: Delete Min Delete minimum element from heap. Exchange root with rightmost leaf. Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted 06 14 42 78 18 47 45 83 91 81 77 84 99 64 53

Binary Heap: Delete Min Delete minimum element from heap. Exchange root with rightmost leaf. Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted 53 14 42 78 18 47 45 83 91 81 77 84 99 64 06

Binary Heap: Delete Min Delete minimum element from heap. Exchange root with rightmost leaf. Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted exchange with left child 53 14 42 78 18 47 45 83 91 81 77 84 99 64

Binary Heap: Delete Min Delete minimum element from heap. Exchange root with rightmost leaf. Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted exchange with right child 14 53 42 78 18 47 45 83 91 81 77 84 99 64

Binary Heap: Delete Min Delete minimum element from heap. Exchange root with rightmost leaf. Bubble root down until it's heap ordered. power struggle principle: better subordinate is promoted stop: heap ordered 14 18 42 78 53 47 45 83 91 81 77 84 99 64 O(log N) operations.

Binary Heap: Delete /Extract Max 25 24 20 18 15 7 16 8 9 10 11 12 22 3 3 25 3 24 22 3 3 16 25 delete Last Node# 14 1110

Binary Heap: Delete /Extract Max

Max-Heapify Example ? 17 98 86 ? 17 86 65 ? 41 ? left child is greater right child is greater 13 65 17 44 32 29 9 10 17 44 23 21 17 children of 2: 2*2, 2*2+1 = 4, 5 children of root: 2*1, 2*1+1 = 2, 3 index 1 2 3 4 5 6 7 8 10 11 12 value 98 86 41 13 65 32 29 9 44 23 21 17 86 65 17 44 17 17

Procedure MaxHeapify MaxHeapify(A, i) 1. l = left(i); 2. r = right(i); 3. if (l  heap-size[A] && A[l] > A[i]) 4. largest = l; 5. else largest = I; 6. if (r  heap-size[A] && A[r] > A[largest]) 7. largest = r; 8. if (largest != i) 9. Swap(A[i], A[largest]) 10. MaxHeapify(A, largest) Assumption: Left(i) and Right(i) are max-heaps.

MaxHeapify – Example MaxHeapify(A, 2) 26 14 24 24 14 20 18 18 14 24 14 17 19 13 12 14 18 14 11

Running Time for MaxHeapify MaxHeapify(A, i) 1. l = left(i); 2. r = right(i); 3. if (l  heap-size[A] && A[l] > A[i]) 4. largest = l; 5. else largest = I; 6. if (r  heap-size[A] && A[r] > A[largest]) 7. largest = r; 8. if (largest != i) 9. Swap(A[i], A[largest]) 10. MaxHeapify(A, largest) Time to fix node i and its children = (1) PLUS Time to fix the subtree rooted at one of i’s children = T(size of subree at largest)

Running Time for MaxHeapify(A, n) T(n) = T(largest) + (1) largest  2n/3 (worst case occurs when the last row of tree is exactly half full) T(n)  T(2n/3) + (1)  T(n) = O(lg n) Alternately, MaxHeapify takes O(h) where h is the height of the node where MaxHeapify is applied.

Building a heap BuildMaxHeap(A) 1. heap-size[A] = length[A] Use MaxHeapify to convert an array A into a max-heap. How? Call MaxHeapify on each element in a bottom-up manner. BuildMaxHeap(A) 1. heap-size[A] = length[A] 2. for i = length[A]/2 down to 1 3. do MaxHeapify(A, i)

BuildMaxHeap – Example Input Array: 24 21 23 22 36 29 30 34 28 27 Initial Heap: (not max-heap) 24 21 23 22 36 29 30 34 28 27

BuildMaxHeap – Example MaxHeapify(10/2 = 5) MaxHeapify(4) 36 24 24 MaxHeapify(3) MaxHeapify(2) 34 24 36 21 21 23 30 23 MaxHeapify(1) 28 34 22 24 22 21 21 36 36 36 27 29 23 30 34 22 28 24 27 21

Running Time of BuildMaxHeap BuildMaxHeap(A) 1. heap-size[A] = length[A] 2. for i = length[A]/2 down to 1 3. do MaxHeapify(A, i) (logn) (n logn) Cost of a MaxHeapify call  No. of calls to MaxHeapify O(lg n)  O(n) = O(nlg n)

Heapsort Goal: Sort an array using heap representations Idea: Build a max-heap from the array Swap the root (the maximum element) with the last element in the array “Discard” this last node by decreasing the heap size Call MAX-HEAPIFY on the new root Repeat this process until only one node remains

Example: A=[7, 4, 3, 1, 2] MAX-HEAPIFY(A, 1, 2) MAX-HEAPIFY(A, 1, 3)

Heapsort(A) HeapSort(A) 1. Build-Max-Heap(A) 2. for i = length[A] downto 2 3. swap( A[1], A[i] ); 4. heap-size[A] = heap-size[A] – 1; 5. MaxHeapify(A, 1);

Algorithm Analysis HeapSort(A) 1. Build-Max-Heap(A) (n) In-place Not Stable Build-Max-Heap takes O(n) and each of the n-1 calls to Max-Heapify takes time O(lg n). Therefore, T(n) = O(n lg n) HeapSort(A) 1. Build-Max-Heap(A) 2. for i = length[A] downto 2 3. swap( A[1], A[i] ); 4. heap-size[A] = heap-size[A] – 1; 5. MaxHeapify(A, 1); (n) (n -1) (logn) (n logn)

Binary Heap: Delete /Extract Max 25 24 20 18 15 7 16 8 9 10 11 12 22 3 3 25 3 24 22 3 3 16 25 delete Last Node# 14 1110

Binary Heap: Delete /Extract Max

Analysis Binary Heap: Delete /Extract Max (1) (logn) ( logn)

Binary Heap: Return/Find Max

Analysis Binary Heap: Return/Find Max (1) (1)

Binary Heap: Increase Key The node i has its key increased to 15.

Binary Heap: Increase Key

Analysis Binary Heap: Increase Key The running time of HEAP-INCREASE-KEY on an n-element heap is O(lg n), since the path traced from the node to the root has length O(lg n).

Binary Heap: Insertion Insert element x into heap. Insert into next available slot. Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence 98 84 75 78 68 67 53 89 next free slot 33 21 51 47 34 49 40

Binary Heap: Insertion Insert element x into heap. Insert into next available slot. Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence swap with parent 98 84 75 78 68 67 53 89 42 33 21 51 47 34 49 40

Binary Heap: Insertion Insert element x into heap. Insert into next available slot. Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence swap with parent 98 84 75 78 68 67 89 53 42 33 21 51 47 34 49 40

Binary Heap: Insertion Insert element x into heap. Insert into next available slot. Bubble up until it's heap ordered. Peter principle: nodes rise to level of incompetence stop: heap ordered 98 84 89 78 68 67 75 42 53 33 21 51 47 34 49 40 O(log N) operations.

Binary Heap: Insertion

Analysis Binary Heap: Insertion The running time of Insert on an n-element heap is O(lg n), since the path traced from the node to the root has length O(lg n).

Priority Queues Operation Build -heap N insert log N find-min 1 delete-min log N union N decrease-key log N delete log N is-empty 1