Your SlideShare is downloading. ×
Algorithm: priority queue
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Algorithm: priority queue

12,074

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
12,074
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
260
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Priority Queue A priority queue is a data structure for maintaining a set S of elements, each with an associated value called a key.
  • 2. Heap and Priority Queue Heap can be used to implement a priority queue.
  • 3. Priority Queue
    • There are two kinds of priority queue
    • max-priority queue
    • min-priority queue
  • 4. Priority Queue
    • Applications of priority queue
    • Job scheduling on a shared computer
    • Event-driven simulation
  • 5. Priority Queue A max-priority queue supports the following operations INSERT(S,x), MAXIMUM(S) EXTRACT-MAX(S), INCREASE-KEY(S,x,k)
  • 6. Priority Queue HEAP-MAXIMUM(A) return A[1]
  • 7. 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
  • 8. 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)
  • 9. Priority Queue i (a) 16 2 9 8 3 10 7 14 4 1
  • 10. Priority Queue i (b) 16 2 9 8 3 10 7 14 15 1
  • 11. Priority Queue i (c) 16 2 9 15 3 10 7 14 8 1
  • 12. Priority Queue i (d) 16 2 9 14 3 10 7 15 8 1
  • 13. 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)
  • 14. 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]
  • 15. Quick Sort Conquer: Sort the two sub-arrays A[p . . q-1] and A[q+1 . . r] by recursive calls to quick sort.
  • 16. Quick Sort Combine: Since the sub-arrays are sorted in place, no work is needed to combine them.
  • 17. 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)
  • 18. Quick Sort PARTITION(A, p, r) x  A[r] i  p-1
  • 19. 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
  • 20. Quick Sort (a) i 4 6 5 3 1 7 8 2 p, j r
  • 21. Quick Sort (b) 4 6 5 3 1 7 8 2 j p, i r
  • 22. Quick Sort (c) 4 6 5 3 1 7 8 2 j p, i r
  • 23. Quick Sort (d) 4 6 5 3 1 7 8 2 j p, i r
  • 24. Quick Sort (e) 4 6 5 3 8 7 1 2 j i p r
  • 25. Quick Sort (f) 4 6 5 7 8 3 1 2 i p r j
  • 26. Quick Sort (g) 4 6 5 7 8 3 1 2 i p r j
  • 27. Quick Sort (h) 4 6 5 7 8 3 1 2 i p r
  • 28. Quick Sort (i) 8 6 5 7 4 3 1 2 i p r

×