priority queues min priority queue
play

Priority Queues Min Priority Queue Collection of elements. Each - PDF document

Priority Queues Min Priority Queue Collection of elements. Each element has a priority or key. Two kinds of priority queues: Supports following operations: Min priority queue. isEmpty Max priority queue. size


  1. Priority Queues Min Priority Queue • Collection of elements. • Each element has a priority or key. Two kinds of priority queues: • Supports following operations: • Min priority queue. � isEmpty • Max priority queue. � size � add/put an element into the priority queue � get element with min priority � remove element with min priority Max Priority Queue Complexity Of Operations • Collection of elements. Two good implementations are heaps • Each element has a priority or key. and leftist trees. • Supports following operations: � isEmpty isEmpty, size, and get => O(1) time � size � add/put an element into the priority queue put and remove => O(log n) time � get element with max priority where n is the size of the priority � remove element with max priority queue Applications Sorting Example Sorting Sort five elements whose keys are 6, 8, 2, 4, 1 using a max priority queue. • use element key as priority � Put the five elements into a max priority queue. • put elements to be sorted into a priority queue � Do five remove max operations placing removed • extract elements in priority order elements into the sorted array from right to left. � if a min priority queue is used, elements are extracted in ascending order of priority (or key) � if a max priority queue is used, elements are extracted in descending order of priority (or key)

  2. After Putting Into Max Priority Queue After First Remove Max Operation 8 4 6 4 6 Max Priority Max Priority 1 1 2 2 Queue Queue 8 Sorted Array Sorted Array After Second Remove Max Operation After Third Remove Max Operation 4 Max Priority Max Priority 1 1 2 2 Queue Queue 6 8 4 6 8 Sorted Array Sorted Array After Fourth Remove Max Operation After Fifth Remove Max Operation Max Priority Max Priority 1 Queue Queue 2 4 6 8 1 2 4 6 8 Sorted Array Sorted Array

  3. Heap Sort Complexity Of Sorting Sort n elements. Uses a max priority queue that is implemented as a heap. � n put operations => O(n log n) time. � n remove max operations => O(n log n) time. � total time is O(n log n). Initial put operations are replaced by a heap � compare with O(n 2 ) for sort methods of initialization step that takes O(n) time. Chapter 2. Machine Scheduling Machine Scheduling Example � m identical machines (drill press, cutter, sander, etc.) 3 machines and 7 jobs � n jobs/tasks to be performed job times are [6, 2, 3, 5, 10, 7, 14] � assign jobs to machines so that the time at which the last job completes is minimum possible schedule 6 13 A 2 7 21 B 3 13 C time -----------> Machine Scheduling Example LPT Schedules 6 13 Longest Processing Time first. A Jobs are scheduled in the order 2 7 21 B 14, 10, 7, 6, 5, 3, 2 3 13 Each job is scheduled on the machine C on which it finishes earliest. time -----------> Finish time = 21 Objective: Find schedules with minimum finish time.

  4. LPT Schedule LPT Schedule • LPT rule does not guarantee minimum finish [14, 10, 7, 6, 5, 3, 2] time schedules. • (LPT Finish Time)/(Minimum Finish Time) <= 4/3 - 1/(3m) 14 16 where m is number of machines. A 10 15 • Usually LPT finish time is much closer to B minimum finish time. 7 13 16 • Minimum finish time scheduling is NP-hard. C Finish time is 16! NP-hard Problems NP-hard Problems • Since even polynomial time algorithms with • Infamous class of problems for which no one degree k > 3 (say) are not practical for large n, has developed a polynomial time algorithm. we must change our expectations of the • That is, no algorithm whose complexity is algorithm that is used. O(n k ) for any constant k is known for any NP- • Usually develop fast heuristics for NP-hard hard problem. problems. • The class includes thousands of real-world � Algorithm that gives a solution close to best. problems. � Runs in acceptable amount of time. • Highly unlikely that any NP-hard problem can • LPT rule is good heuristic for minimum finish be solved by a polynomial time algorithm. time scheduling. Complexity Of LPT Scheduling Using A Min Priority Queue • Sort jobs into decreasing order of task time. � O(n log n) time (n is number of jobs) • Min priority queue has the finish times of the • Schedule jobs in this order. m machines. � assign job to machine that becomes available first • Initial finish times are all 0. � must find minimum of m (m is number of machines) • To schedule a job remove machine with finish times minimum finish time from the priority queue. � takes O(m) time using simple strategy • Update the finish time of the selected machine � so need O(mn) time to schedule all n jobs. and put the machine back into the priority queue.

  5. Huffman Codes Using A Min Priority Queue Useful in lossless compression. • m put operations to initialize priority queue May be used in conjunction with LZW method. • 1 remove min and 1 put to schedule each job Read from text. • each put and remove min operation takes O(log m) time • time to schedule is O(n log m) • overall time is O(n log n + n log m) = O(n log (mn)) Min Tree Definition Min Tree Example Each tree node has a value. Value in any node is the minimum value in 2 the subtree for which that node is the root. 4 9 3 Equivalently, no descendent has a smaller value. 4 8 7 9 9 Root has minimum element. Min Heap Definition Max Tree Example • complete binary tree 9 • min tree 4 9 8 4 2 7 3 1 Root has maximum element.

  6. Min Heap With 9 Nodes Min Heap With 9 Nodes 2 4 3 6 7 9 3 8 6 Complete binary tree with 9 nodes. Complete binary tree with 9 nodes that is also a min tree. Max Heap With 9 Nodes Heap Height 9 Since a heap is a complete binary 8 7 tree, the height of an n node heap is log 2 (n+1). 6 7 2 6 5 1 Complete binary tree with 9 nodes that is also a max tree. Moving Up And Down A Heap A Heap Is Efficiently Represented As An Array 1 9 9 2 3 8 7 8 7 4 7 5 6 6 7 2 6 6 7 2 6 5 1 5 1 8 9 9 8 7 6 7 2 6 5 1 0 1 2 3 4 5 6 7 8 9 10

  7. Putting An Element Into A Max Heap Putting An Element Into A Max Heap 9 9 8 7 8 7 6 7 2 6 6 7 2 6 5 1 7 5 1 7 5 Complete binary tree with 10 nodes. New element is 5. Putting An Element Into A Max Heap Putting An Element Into A Max Heap 9 9 8 7 8 7 6 2 6 6 2 6 7 5 1 7 7 5 1 7 7 7 New element is 20. New element is 20. Putting An Element Into A Max Heap Putting An Element Into A Max Heap 9 20 7 9 7 6 8 2 6 6 8 2 6 5 1 7 7 5 1 7 7 7 7 New element is 20. New element is 20.

  8. Putting An Element Into A Max Heap Putting An Element Into A Max Heap 20 20 9 7 9 7 6 8 2 6 6 8 2 6 5 1 7 7 5 1 7 7 7 7 Complete binary tree with 11 nodes. New element is 15. Putting An Element Into A Max Heap Putting An Element Into A Max Heap 20 20 9 7 7 15 6 2 6 6 9 2 6 5 1 7 7 8 8 5 1 7 7 8 8 7 7 New element is 15. New element is 15. Complexity Of Put Removing The Max Element 20 20 7 7 15 15 6 9 2 6 6 9 2 6 5 1 7 7 8 8 5 1 7 7 8 8 7 7 Complexity is O(log n), where n is Max element is in the root. heap size.

  9. Removing The Max Element Removing The Max Element 7 7 15 15 6 9 2 6 6 9 2 6 5 1 7 7 8 8 5 1 7 7 8 8 7 7 After max element is removed. Heap with 10 nodes. Reinsert 8 into the heap. Removing The Max Element Removing The Max Element 15 7 7 15 6 9 2 6 6 9 2 6 5 1 7 7 5 1 7 7 7 7 Reinsert 8 into the heap. Reinsert 8 into the heap. Removing The Max Element Removing The Max Element 15 15 9 7 9 7 6 2 6 6 2 6 8 8 5 1 7 7 5 1 7 7 7 7 Reinsert 8 into the heap. Max element is 15.

  10. Removing The Max Element Removing The Max Element 9 7 9 7 6 2 6 6 2 6 8 8 5 1 7 7 5 1 7 7 7 7 After max element is removed. Heap with 9 nodes. Removing The Max Element Removing The Max Element 9 9 7 7 6 2 6 6 2 6 8 8 5 1 5 1 Reinsert 7. Reinsert 7. Removing The Max Element Complexity Of Remove Max Element 9 9 8 8 7 7 6 7 2 6 6 7 2 6 5 1 5 1 Reinsert 7. Complexity is O(log n).

Recommend


More recommend