Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
Prochain SlideShare
Chargement dans…5
×

# Algorithm: priority queue

24 462 vues

Publié le

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Soyez le premier à commenter

### Algorithm: priority queue

1. 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. 2. Heap and Priority Queue Heap can be used to implement a priority queue.
3. 3. Priority Queue <ul><li>There are two kinds of priority queue </li></ul><ul><li>max-priority queue </li></ul><ul><li>min-priority queue </li></ul>
4. 4. Priority Queue <ul><li>Applications of priority queue </li></ul><ul><li>Job scheduling on a shared computer </li></ul><ul><li>Event-driven simulation </li></ul>
5. 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. 6. Priority Queue HEAP-MAXIMUM(A) return A[1]
7. 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. 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. 9. Priority Queue i (a) 16 2 9 8 3 10 7 14 4 1
10. 10. Priority Queue i (b) 16 2 9 8 3 10 7 14 15 1
11. 11. Priority Queue i (c) 16 2 9 15 3 10 7 14 8 1
12. 12. Priority Queue i (d) 16 2 9 14 3 10 7 15 8 1
13. 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. 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. 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. 16. Quick Sort Combine: Since the sub-arrays are sorted in place, no work is needed to combine them.
17. 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. 18. Quick Sort PARTITION(A, p, r) x  A[r] i  p-1
19. 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. 20. Quick Sort (a) i 4 6 5 3 1 7 8 2 p, j r
21. 21. Quick Sort (b) 4 6 5 3 1 7 8 2 j p, i r
22. 22. Quick Sort (c) 4 6 5 3 1 7 8 2 j p, i r
23. 23. Quick Sort (d) 4 6 5 3 1 7 8 2 j p, i r
24. 24. Quick Sort (e) 4 6 5 3 8 7 1 2 j i p r
25. 25. Quick Sort (f) 4 6 5 7 8 3 1 2 i p r j
26. 26. Quick Sort (g) 4 6 5 7 8 3 1 2 i p r j
27. 27. Quick Sort (h) 4 6 5 7 8 3 1 2 i p r
28. 28. Quick Sort (i) 8 6 5 7 4 3 1 2 i p r