algorithm: priority queue
TRANSCRIPT
![Page 1: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/1.jpg)
Priority Queue
A priority queue is a data structure for
maintaining a set S of elements, each with
an associated value called a key.
![Page 2: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/2.jpg)
Heap and Priority Queue
Heap can be used to implement a priority
queue.
![Page 3: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/3.jpg)
Priority Queue
There are two kinds of priority queue
• max-priority queue
• min-priority queue
![Page 4: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/4.jpg)
Priority Queue
Applications of priority queue
• Job scheduling on a shared computer
• Event-driven simulation
![Page 5: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/5.jpg)
Priority Queue
A max-priority queue supports the following
operations
INSERT(S,x), MAXIMUM(S)
EXTRACT-MAX(S), INCREASE-KEY(S,x,k)
![Page 6: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/6.jpg)
Priority Queue
HEAP-MAXIMUM(A)
return A[1]
![Page 7: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/7.jpg)
Priority Queue
HEAP-EXTRACT-MAX(A)
if heap-size[A] < 1
then error “heap underflow”
max A[1]
A[1] A[heap-size[A]]
heap-size[A] heap-size[A]-1
MAX-HEAPIFY(A,1)
return max
![Page 8: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/8.jpg)
Priority Queue
HEAP-INCREASE-KEY(A, i, key)
if key < A[i]
then error “new key is smaller than current key”
A[i] key
while i > 1 and A[PARENT(i)] < A[i]
do exchange A[i] A[PARENT(i)]
i PARENT(i)
![Page 9: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/9.jpg)
Priority Queue
16
2
98
3
10
7
14
4 1
i
(a)
![Page 10: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/10.jpg)
Priority Queue
16
2
98
3
10
7
14
15 1
i
(b)
![Page 11: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/11.jpg)
Priority Queue
16
2
915
3
10
7
14
8 1
i
(c)
![Page 12: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/12.jpg)
Priority Queue
16
2
914
3
10
7
15
8 1
i
(d)
![Page 13: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/13.jpg)
Priority Queue
MAX-HEAP-INSERT(A, key)
heap-size[A] heap-size[A]+1
A[heap-size[A]] -∞
HEAP-INCREASE-KEY (A, heap-size[A], key)
![Page 14: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/14.jpg)
Quick Sort
Divide: Partition the array into two sub-arrays
A[p . . q-1] and A[q+1 . . r] such that each element of
A[p . . q-1] is less than or equal to A[q], which in turn
less than or equal to each element of A[q+1 . . r]
![Page 15: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/15.jpg)
Quick Sort
Conquer: Sort the two sub-arrays A[p . . q-1] and
A[q+1 . . r] by recursive calls to quick sort.
![Page 16: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/16.jpg)
Quick Sort
Combine: Since the sub-arrays are sorted in place, no
work is needed to combine them.
![Page 17: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/17.jpg)
Quick Sort
QUICKSORT(A, p, r)
if p< r
then q PARTITION(A, p, r)
QUICKSORT(A, p, q-1)
QUICKSORT(A, q+1, r)
![Page 18: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/18.jpg)
Quick Sort
PARTITION(A, p, r)
x A[r]
i p-1
![Page 19: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/19.jpg)
Quick Sort
for j p to r-1
do if A[j] <= x
then i i+1
exchange A[i] A[j]
exchange A[i+1] A[r]
return i+1
![Page 20: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/20.jpg)
Quick Sort
i p, j r
2 8 7 1 3 5 6 4
(a)
![Page 21: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/21.jpg)
Quick Sort
p, i j r
2 8 7 1 3 5 6 4
(b)
![Page 22: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/22.jpg)
Quick Sort
p, i j r
2 8 7 1 3 5 6 4
(c)
![Page 23: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/23.jpg)
Quick Sort
p, i j r
2 8 7 1 3 5 6 4
(d)
![Page 24: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/24.jpg)
Quick Sort
p i j r
2 1 7 8 3 5 6 4
(e)
![Page 25: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/25.jpg)
Quick Sort
p i j r
2 1 3 8 7 5 6 4
(f)
![Page 26: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/26.jpg)
Quick Sort
p i j r
2 1 3 8 7 5 6 4
(g)
![Page 27: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/27.jpg)
Quick Sort
p i r
2 1 3 8 7 5 6 4
(h)
![Page 28: Algorithm: priority queue](https://reader036.vdocument.in/reader036/viewer/2022081505/554f5ebfb4c905bb178b4595/html5/thumbnails/28.jpg)
Quick Sort
p i r
2 1 3 4 7 5 6 8
(i)