cs223 advanced data structures and algorithms 1 priority queue and binary heap neil tang 02/09/2010
TRANSCRIPT
![Page 1: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/1.jpg)
CS223 Advanced Data Structures and Algorithms 1
Priority Queue and Binary HeapPriority Queue and Binary Heap
Neil TangNeil Tang02/09/201002/09/2010
![Page 2: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/2.jpg)
CS223 Advanced Data Structures and Algorithms 2
Class OverviewClass Overview
Priority queue
Binary heap
Heap operations: insert, deleteMin, de/increaseKey, delete, buildHeap
Application
![Page 3: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/3.jpg)
CS223 Advanced Data Structures and Algorithms 3
Priority QueuePriority Queue
A priority queue is a queue in which each element has a
priority and elements with higher priorities are supposed to
be removed before the elements with lower priorities.
![Page 4: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/4.jpg)
CS223 Advanced Data Structures and Algorithms 4
Possible SolutionsPossible Solutions
Linked list: Insert at the front (O(1)) and traverse the list to delete (O(N)).
Linked list: Keep it always sorted. traverse the list to insert (O(N)) and delete the first element (O(1)).
Binary search tree
![Page 5: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/5.jpg)
CS223 Advanced Data Structures and Algorithms 5
Binary HeapBinary Heap
A binary heap is a binary tree that is completely filled, with possible exception of the bottom level and in which for every node X, the key in the parent of X is smaller than (or equal to) the key in X.
![Page 6: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/6.jpg)
CS223 Advanced Data Structures and Algorithms 6
Binary HeapBinary Heap
A complete binary tree of height h has between 2h and 2h+1 -1 nodes. So h = logN.
For any element in array position i, its left child in position 2i and the right child is in position (2i+1), and the parent is in i/2.
![Page 7: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/7.jpg)
CS223 Advanced Data Structures and Algorithms 7
Insert 14Insert 14
![Page 8: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/8.jpg)
CS223 Advanced Data Structures and Algorithms 8
Insert (Percolate Up)Insert (Percolate Up)
Time complexity: O(logN)
![Page 9: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/9.jpg)
CS223 Advanced Data Structures and Algorithms 9
deleteMindeleteMin
![Page 10: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/10.jpg)
CS223 Advanced Data Structures and Algorithms 10
deleteMin deleteMin (Percolate Down)(Percolate Down)
Time complexity: O(logN)
![Page 11: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/11.jpg)
CS223 Advanced Data Structures and Algorithms 11
Other OperationsOther Operations
decreaseKey(p,)
increaseKey(p, )
delete(p)?
delete(p)=decreaseKey(p,)+deleteMin()
![Page 12: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/12.jpg)
CS223 Advanced Data Structures and Algorithms 12
buildHeapbuildHeap
![Page 13: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/13.jpg)
CS223 Advanced Data Structures and Algorithms 13
buildHeapbuildHeap
![Page 14: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/14.jpg)
CS223 Advanced Data Structures and Algorithms 14
buildHeapbuildHeap
![Page 15: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/15.jpg)
CS223 Advanced Data Structures and Algorithms 15
buildHeapbuildHeap
Theorem: For the perfect binary tree of height h with (2h+1-1) nodes the sum of the heights of the nodes is (2h+1-1-(h+1)).
Time complexity: 2h+1-1-(h+1) = O(N).
![Page 16: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/16.jpg)
CS223 Advanced Data Structures and Algorithms 16
ApplicationsApplications
Problem: find the kth smallest element.
Algorithm: buildHeap, then deleteMin k times.
Time complexity: O(N+klogN) = O(NlogN).
![Page 17: CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010](https://reader036.vdocument.in/reader036/viewer/2022062519/5697c0101a28abf838ccb021/html5/thumbnails/17.jpg)
CS223 Advanced Data Structures and Algorithms 17
ApplicationsApplications
Problem: find the kth largest element.
Algorithm: buildHeap with the first k elements, check the rest one by one. In each step, if the new element is larger than the element in the root node, deleteMin and insert the new one.
Time complexity: O(k+(N-k)logk) = O(NlogN).