CS 124 Section 1 Binary Min/Max Heaps

CS 124 Section 1 Binary Min/Max Heaps

CS 124 Section 1 Binary Min/Max Heaps Yash Nair February 2021 Yash Nair Section 1 February 2021 1 / 25 Table of Contents What is a Heap and Why do we Use Them? Heap Basics Representing a Heap Implementing Heap Operations Problems Yash Nair Section 1 February 2021 2 / 25 Purpose of Heaps Data structure to find (and also remove) most extreme (i.e., maximal or minimal) element in a set we can add to quickly MAX-HEAP finds maximal element, MIN-HEAP finds minimal We will consider MAX-HEAP, but note that a MIN-HEAP is just a MAX-HEAP if we multiply the elements by 1 − Yash Nair Section 1 February 2021 3 / 25 Using a Sorted Array is Slower than a Heap Do the following operations using a sorted array: insert 5, insert 3, insert 2, find and remove max, find and remove max, insert 1 Yash Nair Section 1 February 2021 4 / 25 Comparing Operation Times Data Structure Add Element Find Max Find and Remove Max Build from Unsorted Array Sorted Array O(n) O(1) O(1) O(n log n) Unsorted Array O(1) O(n) O(n) O(1) Binary Heap O(log n) O(1) O(log n) O(n) Yash Nair Section 1 February 2021 5 / 25 Heap Operations Overview PEEK(): what is the largest element in the heap? EXTRACTMAX(): remove the largest element from the heap INSERT(x): insert the element x into the heap BUILD-HEAP(A): given an unsorted array, A, build a binary heap Want to be able to do these operations quickly by using a binary tree structure. Yash Nair Section 1 February 2021 6 / 25 CS 124 Section #2 Heaps and Graphs 2/8/16 1 Heaps Heaps are data structures that make it easy to find the element with the most extreme value in a collection of elements. A Min-Heap prioritizes the element with the smallest value, while a Max-Heap prioritizes the element with the largest value. Because of this property, heaps are often used to implement priority queues. In section today, we will focus on the binary max heap. You can find more about heaps by reading pages 151–169 in CLRS. 1.1 What is a heap? Max heaps are a very use data structure which maintains some structure on a list of numbers such that you can do the following operations. The goal is to be able to very quickly find the maximum element in a list, while supporting insertions and deletions to the list. • peek(): what is the largest element in the list? • extractmax(): remove the largest element from the list • insert(x): insert the element x into the list. The goal is to have come up with a way to implement heaps such that all three of the above operations are fast. Heaps will be useful when we talk about Dijkstra’s algorithm for shortest paths. An efficient way to do this is to use a binary tree to store the list of numbers. This won’t be an ordinary binary tree, but one that satisfies the heap property, which is: If x is a parent of y in the binary tree, then x>y 1.2 RepresentingBinary Trees a Heap as Arrays Example One way to represent binary trees more easily is to use an 1-indexed array. For example, the following array will be used to represent the following max-heap [124, 109, 121, 50, 1, 110, 51] 124 109 121 50 1 110 51 We call the first element in the heap element 1. Now, given an element i, we can find its left and right children with a little arithmetic: Yash Nair Section 1 February 2021 7 / 25 1 Binary Trees as Arrays Can order elements of left-aligned binary tree and view them in array Letting the first element (i.e., root) of tree have index 1, what are the following: PARENT(i) = LEFT(i) = RIGHT(i) = Yash Nair Section 1 February 2021 8 / 25 CS 124 Section #2 Heaps and Graphs 2/8/16 1 Heaps Heaps are data structures that make it easy to find the element with the most extreme value in a collection of elements. A Min-Heap prioritizes the element with the smallest value, while a Max-Heap prioritizes the element with the largest value. Because of this property, heaps are often used to implement priority queues. In section today, we will focus on the binary max heap. You can find more about heaps by reading pages 151–169 in CLRS. 1.1 What is a heap? Max heaps are a very use data structure which maintains some structure on a list of numbers such that you can do the following operations. The goal is to be able to very quickly find the maximum element in a list, while supporting insertions and deletions to the list. • peek(): what is the largest element in the list? • extractmax(): remove the largest element from the list • insert(x): insert the element x into the list. The goal is to have come up with a way to implement heaps such that all three of the above operations are fast. Heaps will be useful when we talk about Dijkstra’s algorithm for shortest paths. An efficient way to do this is to use a binary tree to store the list of numbers. This won’t be an ordinary binary tree, but one that satisfies the heap property, which is: If x is a parent of y in the binary tree, then x>y 1.2 RepresentingHeap Representation a Heap One way to represent binary trees more easily is to use an 1-indexed array. For example, the following array will be used to represent the following max-heap Heap property: if x is a parent of y in the binary tree, then x > y A heap is a binary tree[124 that, 109, satisfies121, 50, 1 the, 110 heap, 51] property 124 109 121 50 1 110 51 We call the first element in the heap element 1. Now, given an element i, we can find its left and right children with a little arithmetic: Yash Nair Section 1 February 2021 9 / 25 1 Exercise 1. How would you find the index of the parent, left child and right child of the ith element in the heap? • Parent(i)= • Left(i)= • Right(i)= Solution The array representation is simplified by calling the first element in the heap 1: i • Parent(i)= 2 • Left(i)=2i ⌅ ⇧ • Right(i)=2i +1 Max-Heapify(1.3 Heap operationsH; N) Before we look at how peek, insertion and deletion work, let’s first implement some helper functions thatInput: will beN usefulis fornode maintaining in the the left-aligned heap structure and binary building tree a heapH fromsuch scratch. that the N is 1.3.1the rootMax-Heapify of a MAX-HEAP, except that N may be smaller than its Max-Heapify(H, N): Given that the children of the node N in the Max-Heap H are each the rootchildren of a Max-Heap (i.e.,, but rearranges all lower the tree layers rooted at satisfyN to be atheMax-Heap heap. property) We will call this when weGoal: break the Rearrange heap property nodes in order so to fix that it. tree rooted at N is a MAX-HEAP Max-Heapify(H, N) Require: N is the root of a Max-Heap except, possibly, at N (i.e., N could be smaller than its children) 1: (l, r) (Left(N), Right(N)) 2: if Exists(l)andH[l] >H[N] then 3: largest l 4: else 5: largest N 6: end if 7: if Exists(r)andH[r] >H[largest] then 8: largest r 9: end if 10: if largest = N then 6 11: Swap(H[N],H[largest]) 12: Max-Heapify(H, largest) 13: end if Ensure: N is the root of a Max-Heap Description:Yash Nair First, we set larger to be theSection bigger of1 N and `,thevalueofthenodeFebruaryN or 2021 its left 10 / 25 child. Then, we find the larger of that and the value of N’s right child. We swap H[larger] with the root so that now the root is bigger in value than the two children. We recursively call max-heapify on the side that we swapped with. If no swap occurred, we do not recurse. 2 Exercise 2.Example • Run Max-HeapifyRun Max-Heapifywith withN =1Non= 1 on the below graph. What is the runtime? H = [10, 15, 7, 13, 9, 4, 1, 8, 6] 10 15 7 13 9 4 1 8 6 Yash Nair Section 1 February 2021 11 / 25 Note that every level besides the one with N =1does satisfy the max-heap property. That is an important assumption when calling max-heapify. • What is Max-Heapify’s run-time? Solution • Max-Heapify(H, N) returns a max-heap [15, 13, 7, 11, 9, 4, 1, 8, 10] 15 13 7 11 9 4 1 8 10 • Runs in O(log n) time because node N is moved down at most log n times. 3 Runtime of Max-Heapify O(log n) comparisons are made O(log n) swaps are made Total runtime: O(log n). Yash Nair Section 1 February 2021 12 / 25 Runs in O(log n) time because node N is moved down at most log n times. • Runs1.3.2 in O(log Build-Heapn) time because node N is moved down at most log n times. Building• a Heap Build-Heap(A): Given an unordered array, makes it into a max heap.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    25 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us