Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 G64ADS Advanced Data Structures Priority Queue.

Similar presentations


Presentation on theme: "1 G64ADS Advanced Data Structures Priority Queue."— Presentation transcript:

1 1 G64ADS Advanced Data Structures Priority Queue

2 2 Why Priority Queue oQueues are a standard mechanism for ordering tasks on a first-come, first-served basis oHowever, some tasks may be more important or timely than others (higher priority) oPriority queues oStore tasks using a partial ordering based on priority oEnsure highest priority task at head of queue oHeaps are the underlying data structure of priority queues

3 3 Priority Queue oMain operations oInsert (i.e., enqueue) odeleteMin (i.e., dequeue) oFinds the minimum element in the queue, deletes it from the queue, and returns it oPerformance oGoal is for operations to be fast oWill be able to achieve O(logN) time insert/deleteMin amortized over multiple operations oWill be able to achieve O(1) time inserts amortized over multiple insertions

4 4 Simple Implementations oUnordered list oO(1) insert oO(N) deleteMin oOrdered list oO(N) insert oO(1) deleteMin oBalanced BST oO(logN) insert and deleteMin oObservation: We don’t need to keep the priority queue completely ordered

5 5 Binary Heap oA binary heap is a binary tree with two properties oStructure property oA binary heap is a complete binary tree oEach level is completely filled oBottom level may be partially filled from left to right oHeight of a complete binary tree with N elements is Floor[logN]

6 6 Binary Heap Example

7 7 Binary Heap oHeap-order property oFor every node X, key(parent(X)) ≤ key(X) oExcept root node, which has no parent oThus, minimum key always at root oOr, maximum, if you choose oInsert and deleteMin must maintain heap- order property

8 8 Implementing Complete Binary Trees as Arrays oGiven element at position i in the array oi’s left child is at position 2i oi’s right child is at position 2i+1 oi’s parent is at position Floor[i/2]

9 9 Implementing Complete Binary Trees as Arrays

10 10 Heap Insert oInsert new element into the heap at the next available slot (“hole”) oAccording to maintaining a complete binary tree oThen, “percolate” the element up the heap while heap-order property not satisfied

11 11 Heap Insert Insert 14: Creating the hole and building the hole up

12 12 Heap DeleteMin oMinimum element is always at the root o  Heap decreases by one in size o  Move last element into hole at root oPercolate down while heap-order property not satisfied

13 13 Heap DeleteMin Creation of the hole at the root

14 14 Heap DeleteMin Next two steps in DeleteMin

15 15 Heap DeleteMin Last two steps in DeleteMin

16 16 Heap DeleteMin Implementation

17 17 Other Heap Operations odecreaseKey(p,v) oLowers value of item p to v oNeed to percolate up oe.g., change job priority oincreaseKey(p,v) oIncreases value of item p to v oNeed to percolate down oremove(p) oFirst, decreaseKey(p,-∞) oThen, deleteMin oe.g., terminate job

18 18 Building a Heap oConstruct heap from initial set of N items oSolution 1 oPerform N inserts oO(N) average case, but O(NlogN) worst-case oSolution 2 oAssume initial set is a heap oPerform a percolate-down from each internal node (H[size/2] to H[1])

19 19 Building a Heap: Example Initial heap After percolateDown(7)

20 20 Building a Heap: Example After percolateDown(6)After percolateDown(5)

21 21 Building a Heap: Example After percolateDown(4)After percolateDown(3)

22 22 Building a Heap: Example After percolateDown(2)After percolateDown(1)

23 23 BuildingHeap Implementation

24 24 BuildHeap Analysis oRunning time of buildHeap proportional to sum of the heights of the nodes oTheorem 6.1 oFor the perfect binary tree of height h containing 2 h+1 –1 nodes, the sum of heights of the nodes is 2 h+1 –1 –(h + 1) oSince N = 2 h+1 –1, then sum of heights is O(N) oSlightly better for complete binary tree

25 25 Binary Heap Operations Worst- case Analysis oHeight of heap is Floor[logN] oinsert: O(logN) o2.607 comparisons on average, i.e., O(1) odeleteMin: O(logN) odecreaseKey: O(logN) oincreaseKey: O(logN) oremove: O(logN) obuildHeap: O(N)

26 26 Applications oOperating system scheduling oProcess jobs by priority oGraph algorithms oFind the least-cost, neighboring vertex oEvent simulation oInstead of checking for events at each time click, look up next event to happen

27 27 Priority Queues: Alternatives to Binary Heaps od-Heap oEach node has d children oinsert in O(logdN) time odeleteMin in O(d logdN) time oBinary heaps are 2-Heaps

28 28 Mergeable Heaps oHeap merge operation oUseful for many applications oMerge two (or more) heaps into one oIdentify new minimum element oMaintain heap-order property oMerge in O(log N) time oStill support insert and deleteMin in O(log N) time oInsert = merge existing heap with one-element heap od-Heaps require O(N) time to merge

29 29 Leftist Heaps oNull path length npl(X) of node X oLength of the shortest path from X to a node without two children oLeftist heap property oFor every node X in heap, npl(leftChild(X)) ≥ npl(rightChild(X)) oLeftist heaps have deep left subtrees and shallow right subtrees oThus if operations reside in right subtree, they will be faster

30 30 Leftist Heaps Leftist heap Not a leftist heap npl(X) shown in nodes

31 31 Leftist Heaps oTheorem 6.2 oA leftist tree with r nodes on the right path must have at least 2 r – 1 nodes. oThus, a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes

32 32 Leftist Heaps oMerge heaps H1 and H2 oAssume root(H1) > root(H2) oRecursively merge H1 with right subheap of H2 oIf result is not leftist, then swap the left and right subheaps oRunning time O(log N) oDeleteMin oDelete root and merge children

33 33 Leftist Heaps: Example

34 34 Leftist Heaps: Example Result of merging H2 with H1’s right subheap

35 35 Leftist Heaps: Example Result of attaching leftist heap of previous figure as H1’s right child

36 36 Leftist Heaps: Example Result of attaching leftist heap of previous figure as H1’s right child

37 37 Leftist Heaps: Example Result of swapping children of H1’s root

38 38 Skew Heaps oSelf-adjusting version of leftist heap oSkew heaps are to leftist heaps as splay trees are to AVL trees oSkew merge same as leftist merge, except we always swap left and right subheaps oWorst case is O(N) oAmortized cost of M operations is O(M log N)

39 39 Binomial Queues oSupport all three operations in O(log N) worst-case time per operation oInsertions take O(1) average-case time oKey idea oKeep a collection of heap-ordered trees to postpone merging

40 40 Binomial Queues oA binomial queue is a forest of binomial trees oEach in heap order oEach of a different height oA binomial tree B k of height k consists two B k-1 binomial trees oThe root of one B k-1 tree is the child of the root of the other B k-1 tree

41 41 Binomial Trees

42 42 Binomial Trees oBinomial trees of height k have exactly 2 k nodes oNumber of nodes at depth d is, the binomial coefficient oA priority queue of any size can be represented by a binomial queue oBinary representation of B k

43 43 Binomial Queue Operations oMinimum element found by checking roots of all trees oAt most (log2N) of them, thus O(log N) oOr, O(1) by maintaining pointer to minimum element

44 44 Binomial Queue Operations oMerge (H1,H2) → H3 oAdd trees of H1 and H2 into H3 in increasing order by depth oTraverse H3 oIf find two consecutive B k trees, then create a B k+1 tree oIf three consecutive B k trees, then leave first, combine last two oNever more than three consecutive B k trees oKeep binomial trees ordered by height omin(H3) = min(min(H1),min(H2)) oRunning time O(log N)

45 45 Binomial Queue Merge Example

46 46 Binomial Queue Operations oInsert (x, H1) oCreate single-element queue H2 oMerge (H1, H2) oRunning time proportional to minimum k such that B k not in heap oO(log N) worst case oProbability B k not present is 0.5 oThus, likely to find empty B k after two tries on average oO(1) average case

47 47 Binomial Queue Operations odeleteMin (H1) oRemove min(H1) tree from H1 oCreate heap H2 from the children of min(H) oMerge (H1,H2) oRunning time O(log N)

48 48 Binomial Queue Delete Example

49 49 Binomial Queue Implementation oArray of binomial trees oTrees use first-child, right-sibling representation

50 50 Priority Queue in Standard Library

51 51 Summary


Download ppt "1 G64ADS Advanced Data Structures Priority Queue."

Similar presentations


Ads by Google