Heaps,heapsort and priority queue

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
September 19, Algorithms and Data Structures Lecture IV Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
Data Structures Dynamic Sets Heaps Binary Trees & Sorting Hashing.
Heapsort Chapter 6. Heaps A data structure with  Nearly complete binary tree  Heap property: A[parent(i)] ≥ A[i] eg. Parent(i) { return } Left(i) {
Ch. 6: Heapsort n nodes. Heap -- Nearly binary tree of these n nodes (not just leaves) Heap property If max-heap, the max-heap property is that for every.
Binary Heap.
Data Structure & Algorithm Lecture 5 Heap Sort & Binary Tree JJCAO.
1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.
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.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Priority Queues and Binary Heaps Algorithms.
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”
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.
Sept Heapsort What is a heap? Max-heap? Min-heap? Maintenance of Max-heaps -MaxHeapify -BuildMaxHeap Heapsort -Heapsort -Analysis Priority queues.
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.
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
Heapsort Chapter 6 Lee, Hsiu-Hui
Heapsort.
Heap Sort Example Qamar Abbas.
Introduction to Algorithms
The Heap Data Structure
US Treasury & Its Borrowing
Hashing and Hash Tables
Design and Analysis of Algorithms Heapsort
مرتب سازی هرمی Heap Sort تهیه کنندگان: مریم هاشمی و ندا شیخی پاییز 88.
CS 583 Analysis of Algorithms
Heaps, Heapsort, and Priority Queues
CS200: Algorithm Analysis
Ch 6: Heapsort Ming-Te Chi
درخت دودويي و مرتب سازي با آن Binary Trees & Heap sort
Binhai Zhu Computer Science Department, Montana State University
Brief Review of Proof Techniques
AOE/ESM 4084 Engineering Design Optimization
Heapsort.
Technology Update Kris Young Director of Technology
Numerical Methods Charudatt Kadolkar 12/9/2018
Heap Sort The Heap Data Structure
"Teachers open the door, but you must enter by yourself. "
Heap Sort.
Design and Analysis of Algorithms
2015/16 Evaluation Summary October 4, 2016 Jordan Harris
2016 State Assessment Results
Topic 5: Heap data structure heap sort Priority queue
HEAPS.
Heapsort Sorting in place
Solving Recurrences Continued The Master Theorem
TROY SCHOOL DISTRICT ENROLLMENT PROJECTIONS February 7, 2017
2015/16 Evaluation Summary October 18, 2016 Jordan Harris
Binary Heaps and Heapsort
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
Binhai Zhu Computer Science Department, Montana State University
Business Services Update Board of Education Workshop March 7, 2017
Binhai Zhu Computer Science Department, Montana State University
Binhai Zhu Computer Science Department, Montana State University
Binhai Zhu Computer Science Department, Montana State University
CSE5311 Design and Analysis of Algorithms
Presentation transcript:

Heaps,heapsort and priority queue Binhai Zhu Computer Science Department, Montana State University Frequently, presenters must deliver material of a technical nature to an audience unfamiliar with the topic or vocabulary. The material may be complex or heavy with detail. To present technical material effectively, use the following guidelines from Dale Carnegie Training®.   Consider the amount of time available and prepare to organize your material. Narrow your topic. Divide your presentation into clear segments. Follow a logical progression. Maintain your focus throughout. Close the presentation with a summary, repetition of the key steps, or a logical conclusion. Keep your audience in mind at all times. For example, be sure data is clear and information is relevant. Keep the level of detail and vocabulary appropriate for the audience. Use visuals to support key points or steps. Keep alert to the needs of your listeners, and you will have a more receptive audience. 11/21/2018

Definition A heap is a binary tree with the following properties: (1) The value of each node is not less than the values stored in each of its children. //heap property (2) The tree is perfectly balanced and leaves in the last level are all in the leftmost positions. 11/21/2018

Example 16 14 10 8 7 9 3 2 4 1 11/21/2018

Example 16 14 10 7 9 3 8 2 4 1 i 1 2 3 4 5 6 7 8 9 10 A[i] 16 14 10 8 7 9 3 2 4 1 11/21/2018

Definition A---array length[A]---number of elements in the array heap-size[A]---number of elements in the heap stored in the array A root of the tree/heap---A[1] Q. Given index i, how do we find its parent/children? i 1 2 3 4 5 6 7 8 9 10 A[i] 16 14 10 8 7 9 3 2 4 1 11/21/2018

Example Q. Given index i, how do we find its parent/children? 16 14 10 3 8 7 9 2 4 1 i 1 2 3 4 5 6 7 8 9 10 A[i] 16 14 10 8 7 9 3 2 4 1 11/21/2018

Example Q. Given index i, how do we find its parent/children? parent(i): 16 14 10 8 7 9 3 2 4 1 i 1 2 3 4 5 6 7 8 9 10 A[i] 16 14 10 8 7 9 3 2 4 1 11/21/2018

Example Q. Given index i, how do we find its parent/children? parent(i): return └ i/2 ┘ 16 14 10 8 7 9 3 2 4 1 i 1 2 3 4 5 6 7 8 9 10 A[i] 16 14 10 8 7 9 3 2 4 1 11/21/2018

Example Q. Given index i, how do we find its parent/children? parent(i): return └ i/2 ┘ 16 left(i): 14 10 8 7 9 3 2 4 1 i 1 2 3 4 5 6 7 8 9 10 A[i] 16 14 10 8 7 9 3 2 4 1 11/21/2018

Example Q. Given index i, how do we find its parent/children? parent(i): return └ i/2 ┘ 16 left(i): return 2i 14 10 8 7 9 3 2 4 1 i 1 2 3 4 5 6 7 8 9 10 A[i] 16 14 10 8 7 9 3 2 4 1 11/21/2018

Example Q. Given index i, how do we find its parent/children? parent(i): return └ i/2 ┘ 16 left(i): return 2i 14 10 right(i): return 2i+1 8 7 9 3 2 4 1 i 1 2 3 4 5 6 7 8 9 10 A[i] 16 14 10 8 7 9 3 2 4 1 11/21/2018

Heap property: 16 14 10 8 7 9 3 2 4 1 i 1 2 3 4 5 6 7 8 9 10 A[i] 16 14 10 8 7 9 3 2 4 1 11/21/2018

Heap property: A[parent(i)]≥A[i] 16 14 10 8 7 9 3 2 4 1 i 1 2 3 4 5 6 7 8 9 10 A[i] 16 14 10 8 7 9 3 2 4 1 11/21/2018

The height of a node is the number of edges on the longest simple downward path from the node to a leaf. The height of a tree is the height of its root. Take-home exercise: An n-element heap has height └ log n ┘. Prove it! 16 14 10 3 8 7 9 2 4 1 11/21/2018

Now we cover a fundamental procedure regarding heaps: the heapify procedure. Condition: binary trees rooted at left(i) and right(i) are heaps, but at A[i] the heap property is violated; i.e., A[i] maybe smaller than its children. 16 4 10 14 7 9 3 2 8 1 11/21/2018

Now we cover a fundamental procedure regarding heaps: the heapify procedure. Condition: binary trees rooted at left(i) and right(i) are heaps, but at A[i] the heap property is violated; i.e., A[i] maybe smaller than its children. 16 X 4 10 14 7 9 3 2 8 1 11/21/2018

X Heapify(A,i) l ← left (i), r ← right(i) If l ≤ heap-size[A] and A[l] > A[i] then largest ← l else largest ← i //find the largest index among A[i],A[left(i)],A[right(i)] If r ≤ heap-size[A] and A[r] > A[largest] then largest ← r If largest ≠ i then exchange A[i] ↔ A[largest] Heapify(A,largest) 16 X 4 10 14 7 9 3 11/21/2018 2 8 1

X Heapify(A,i) l ← left (i), r ← right(i) If l ≤ heap-size[A] and A[l] > A[i] then largest ← l else largest ← i //find the largest index among A[i],A[left(i)],A[right(i)] If r ≤ heap-size[A] and A[r] > A[largest] then largest ← r If largest ≠ i then exchange A[i] ↔ A[largest] Heapify(A,largest) 16 14 10 X 7 9 3 4 11/21/2018 2 8 1

If l ≤ heap-size[A] and A[l] > A[i] then largest ← l Heapify(A,i) l ← left (i), r ← right(i) If l ≤ heap-size[A] and A[l] > A[i] then largest ← l else largest ← i //find the largest index among A[i],A[left(i)],A[right(i)] If r ≤ heap-size[A] and A[r] > A[largest] then largest ← r If largest ≠ i then exchange A[i] ↔ A[largest] Heapify(A,largest) Runnting time: O(log n) 16 14 10 7 9 3 8 11/21/2018 2 1 4

We can use Heapify to convert an unorganized array into a heap. Build-Heap(A) 1.heap-size[A] ← length[A] 2.for i ← └ length[A]/2 ┘ downto 1 3 do Heapify(A,i) 7 i 1 2 3 4 5 6 7 8 9 10 A[i] 4 1 3 2 16 9 10 14 8 7 11/21/2018

Q: Where do we start? 4 1 3 2 16 9 10 14 8 7 i 1 2 3 4 5 6 7 8 9 10 A[i] 4 1 3 2 16 9 10 14 8 7 11/21/2018

Q: Where do we start? i=5 4 i=5 1 3 2 16 9 10 14 8 7 i 1 2 3 4 5 6 7 8 A[i] 4 1 3 2 16 9 10 14 8 7 11/21/2018

i=4 4 1 3 i=4 2 16 9 10 14 8 7 i 1 2 3 4 5 6 7 8 9 10 Old A[i] 4 1 3 2 16 9 10 14 8 7 11/21/2018

i=4, after heapify 4 1 3 i=4 16 9 10 14 2 8 7 i 1 2 3 4 5 6 7 8 9 10 Old A[i] 4 1 3 2 16 9 10 14 8 7 11/21/2018

i=3 4 1 3 i=3 16 9 10 14 2 8 7 i 1 2 3 4 5 6 7 8 9 10 Old A[i] 4 1 3 2 16 9 10 14 8 7 11/21/2018

i=3, after Heapify 4 1 10 i=3 16 9 3 14 2 8 7 i 1 2 3 4 5 6 7 8 9 10 Old A[i] 4 1 3 2 16 9 10 14 8 7 11/21/2018

i=2 4 i=2 1 10 16 9 3 14 2 8 7 i 1 2 3 4 5 6 7 8 9 10 Old A[i] 4 1 3 2 16 9 10 14 8 7 11/21/2018

i=2, run Heapify 4 i=2 16 10 1 9 3 14 2 8 7 i 1 2 3 4 5 6 7 8 9 10 Old A[i] 4 1 3 2 16 9 10 14 8 7 11/21/2018

i=2, after Heapify 4 i=2 16 10 7 9 3 14 2 8 1 i 1 2 3 4 5 6 7 8 9 10 Old A[i] 4 1 3 2 16 9 10 14 8 7 11/21/2018

i=1 i=1 4 16 10 7 9 3 14 2 8 1 i 1 2 3 4 5 6 7 8 9 10 Old A[i] 4 1 3 2 16 9 10 14 8 7 11/21/2018

i=1 i=1, after Heapify 16 14 10 7 9 3 8 2 4 1 i 1 2 3 4 5 6 7 8 9 10 Old A[i] 4 1 3 2 16 9 10 14 8 7 11/21/2018

i=1 i=1, after Heapify 16 14 10 7 9 3 8 2 4 1 i 1 2 3 4 5 6 7 8 9 10 New A[i] 16 14 10 8 7 9 3 2 4 1 11/21/2018

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) 16 14 10 3 8 7 9 4 2 1 11/21/2018

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) 16 14 10 8 7 9 3 4 2 1 11/21/2018 i=10

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) 1 14 10 heap-size=9 8 7 9 3 4 2 16 11/21/2018 i=10

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) After Heapify(A,1) 14 8 10 heap-size=9 4 7 9 3 2 1 16 11/21/2018 i=10

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) 14 8 10 4 7 9 3 2 1 16 11/21/2018 i=9

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) 1 8 10 heap-size=8 4 7 9 3 2 14 16 11/21/2018 i=9

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) After Heapify(A,1) 10 8 9 heap-size=8 7 1 3 4 2 14 16 11/21/2018 i=9

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) 10 8 9 7 1 3 4 2 14 16 11/21/2018 i=8

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) 2 8 9 heap-size=7 7 1 3 4 10 14 16 11/21/2018 i=8

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) After Heapify(A,1) 9 8 3 heap-size=7 7 1 4 2 10 14 16 11/21/2018 i=8

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) After Heapify(A,1) 9 8 3 heap-size=7 7 1 4 2 10 14 16 11/21/2018 i=8

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) 2 i=2 1 3 8 4 7 9 After a few rounds… 10 14 16 11/21/2018

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) After Heapify(A,1) 1 i=2 2 3 heap-size=1 8 9 4 7 10 14 16 11/21/2018

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) 1 2 3 8 9 The array is sorted! 4 7 10 14 16 11/21/2018

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) 1 2 3 8 9 Running time? 4 7 10 14 16 11/21/2018

Heapsort(A) Build-Heap(A) for i=length[A] down to 2 exchange A[1] and A[i] heap-size[A]=heap-size[A]-1 Heapify(A,1) 1 2 3 8 Running time? O(n log n)! 9 4 7 10 14 16 11/21/2018

Priority Queue A priority queue is a data structure for maintaining a set S of elements, each with an associated value called key. //think of scheduling a set of jobs It supports the following operations Insert(S,x) Max(S) Delete-Max(S) //Of course Min is the symmetric case, depending on real application 11/21/2018

Heap-Delete-Max(A) If heap-size[A] < 1 then print “heap underflow” A[1] ← A[heap-size[A]] heap-size[A] ← heap-size[A]-1 Heapify(A,1) return max 11/21/2018

Heap-Delete-Max(A) If heap-size[A] < 1 then print “heap underflow” A[1] ← A[heap-size[A]] heap-size[A] ← heap-size[A]-1 Heapify(A,1) return max Running time? 11/21/2018

Heap-Insert(A,key) heap-size[A] ← heap-size[A] + 1 i ← heap-size[A] While i > 1 and A[parent(i)] < key do A[i] ← A[parent(i)] i ← parent(i) A[i] ← key 11/21/2018

Heap-Insert(A,key) heap-size[A] ← heap-size[A] + 1 i ← heap-size[A] While i > 1 and A[parent(i)] < key do A[i] ← A[parent(i)] i ← parent(i) A[i] ← key 16 14 10 3 8 7 9 2 4 1 15 11/21/2018

Heap-Insert(A,key) heap-size[A] ← heap-size[A] + 1 i ← heap-size[A] While i > 1 and A[parent(i)] < key do A[i] ← A[parent(i)] i ← parent(i) A[i] ← key 16 14 10 3 8 15 9 2 4 1 7 11/21/2018

Heap-Insert(A,key) heap-size[A] ← heap-size[A] + 1 i ← heap-size[A] While i > 1 and A[parent(i)] < key do A[i] ← A[parent(i)] i ← parent(i) A[i] ← key 16 15 10 8 14 9 3 2 4 1 7 Running time? 11/21/2018

Heap-Insert(A,key) heap-size[A] ← heap-size[A] + 1 i ← heap-size[A] While i > 1 and A[parent(i)] < key do A[i] ← A[parent(i)] i ← parent(i) A[i] ← key 16 15 10 8 14 9 3 Question: What if we have to increase the key 15 to 18? 2 4 1 7 11/21/2018