1 / 35

Priority Queue

Priority Queue. A Priority Queue Set S is made up of n elements: x 0 x 1 x 2 x 3 … x n-1 Functions: createEmptySet() returns a newly created empty priority queue findMin(S) returns the minimum node with respect to ordering insert(S, x) returns S with x added

Télécharger la présentation

Priority Queue

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Priority Queue A Priority Queue Set S is made up of n elements: x0 x1 x2 x3 … xn-1 Functions: createEmptySet() returns a newly created empty priority queue findMin(S) returns the minimum node with respect to ordering insert(S, x) returns S with x added deleteMin(S) returns S with the minimum node removed isEmptySet(S) returns true if S is empty and false if it is not

  2. Homework 6 • Describe how to implement a priority queue that has a worst case findMin in O(1) time and insert and deleteMin in no more than O(lg n) time. You can assume that n is always less than 128. In other words, there is a max of 127 elements that can be stored in the queue. • Do the five Priority Queue functions.

  3. Priority Queue – Array List • Ordered Array List • findMin in constant time. • insert and deleteMin in O(n). • Unordered Array List • insert in constant time. • findMin and deleteMin in O(n).

  4. Priority Queue – Linked List • Ordered Linked List • findMin and deleteMin in constant time. • insert in O(n). • Unordered List • insert in constant time. • findMin and deleteMin in O(n).

  5. Priority Queue Trees • Binary Search Tree • Find can be more than O(lg n) if out of balance. • Insert and delete can be more than O(lg n). • AVL Tree • Find is O(lg n) time. • Insert and delete are O(lg n).

  6. Priority Queue Trees • AVL Tree with pointer to smallest • Find is O(1) time. • Insert and delete are O(lg n). • Works, but is way too complicated for the task • We need a simpler solution

  7. Binary Tree

  8. Binary Tree -- Array 0 1 11 2 3 4 5 6 7 8 9 10 2 * i + 1 is the left child 2 * i + 2 is the right child

  9. Binary Tree -- Array 0 1 11 2 3 4 5 6 7 8 9 10 2 * i + 1 is the left child 2 * i + 2 is the right child

  10. Binary Tree -- Array 0 1 11 2 3 4 5 6 7 8 9 10 2 * i + 1 is the left child 2 * i + 2 is the right child

  11. Priority Queue 4 5 7 9 12 10 17 21 11 25 15 13 14

  12. 4 5 13 7 9 12 10 17 21 11 25 15 14 Priority Queue 4 5 7 9 12 10 17 21 11 25 15 13 14

  13. createEmptySet() Declare an array of type node. Declare an int n that is the number of elements in the queue. node s[128] n = 0 return s

  14. isEmpty(s) isEmpty(s) return (n == 0)

  15. findMin(s) findMin(s) return s[0]

  16. insert(s, x) 4 5 7 9 12 10 17 21 11 25 15 13 14 insert(s, 19)

  17. insert(s, x) 4 5 7 9 12 10 17 19 21 11 25 15 13 14 s(n) = 19 n = n + 1

  18. insert(s, x) 4 5 7 9 12 10 17 21 11 25 15 13 14 insert(s, 6)

  19. insert(s, x) 4 5 7 9 12 10 17 6 21 11 25 15 13 14 s(n) = 6 n = n + 1

  20. insert(s, x) 4 5 7 9 12 10 6 17 21 11 25 15 13 14 m = parent-of(n-1) if s[n-1] < s[m] swap(s[n-1], s[m])

  21. insert(s, x) 4 5 6 9 12 10 7 17 21 11 25 15 13 14

  22. deleteMin(x) 4 5 7 9 12 10 17 21 11 25 15 13 14

  23. deleteMin(x) 14 5 7 9 12 10 17 21 11 25 15 13 4 n = n - 1 swap(s[0], s[n])

  24. deleteMin(x) 5 14 7 9 12 10 17 21 11 25 15 13 4 if s[0] < either of its children swap with the least of its children

  25. deleteMin(x) 5 9 7 14 12 10 17 21 11 25 15 13 4

  26. deleteMin(x) 5 9 7 11 12 10 17 21 14 25 15 13 4 return s[n]

  27. Dictionaries A Dictionary is a set S made up of n elements: x0 x1 x2 x3 … xn-1 that has the following functions. Functions: createEmptySet() returns a newly created empty set lookUp(S, k) returns the node in S that has the key k insert(S, x) returns S with x added delete(S, k) returns S with x (found using x.k) removed isEmptySet(S) returns true if S is empty and false if it is not

  28. A Node NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123341112 Grade: 95

  29. Name Some Dictionaries

  30. Name Some Dictionaries • Lists • Binary Search Trees • AVL Trees

  31. Can we create a dictionary that takes constant time, in the worst case, to do lookUp, insert, and delete?

  32. A Node NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123341112 Grade: 95

  33. Use the key as an index • Create an array large enough to hold all the key possibilities. • If we used SSN this would be: node s[1000000000] • With this array we could look up a node using SSN as the key in constant time. • Insert and delete will also be constant.

  34. Key Direct • Do we want to use an array of a billion elements to store 21 nodes? • Is there a way to sacrifice some loss in speed to reduce the size of the array? • Can we still maintain O(1) for the average time of findMin, deleteMin, and insert? • What will the worst case time be?

  35. Homework 7 • Describe how to implement a dictionary that has an average lookUp, insert, and delete time that is constant, but uses an array of no more than 2*n elements. • Do the five Dictionary functions.

More Related