Priority Queues Two kinds of priority queues Min


















![LPT Schedule [14, 10, 7, 6, 5, 3, 2] 14 16 A 10 15 LPT Schedule [14, 10, 7, 6, 5, 3, 2] 14 16 A 10 15](https://slidetodoc.com/presentation_image/05f17c482feb7f8fdff5effb5bc8b71b/image-19.jpg)









































- Slides: 60

Priority Queues Two kinds of priority queues: • Min priority queue. • Max priority queue.

Min Priority Queue • Collection of elements. • Each element has a priority or key. • Supports following operations: § § § is. Empty size add/put an element into the priority queue get element with min priority remove element with min priority

Max Priority Queue • Collection of elements. • Each element has a priority or key. • Supports following operations: § § § is. Empty size add/put an element into the priority queue get element with max priority remove element with max priority

Complexity Of Operations Two good implementations are heaps and leftist trees. is. Empty, size, and get => O(1) time put and remove => O(log n) time where n is the size of the priority queue

Applications Sorting • use element key as priority • put elements to be sorted into a priority queue • extract elements in priority order § 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)

Sorting Example Sort five elements whose keys are 6, 8, 2, 4, 1 using a max priority queue. § Put the five elements into a max priority queue. § Do five remove max operations placing removed elements into the sorted array from right to left.

After Putting Into Max Priority Queue 8 4 1 6 2 Sorted Array Max Priority Queue

After First Remove Max Operation 4 1 6 Max Priority Queue 2 8 Sorted Array

After Second Remove Max Operation 4 1 Max Priority Queue 2 6 Sorted Array 8

After Third Remove Max Operation 1 Max Priority Queue 2 4 6 Sorted Array 8

After Fourth Remove Max Operation Max Priority Queue 1 2 4 6 Sorted Array 8

After Fifth Remove Max Operation Max Priority Queue 1 2 4 6 Sorted Array 8

Complexity Of Sorting Sort n elements. § § n put operations => O(n log n) time. n remove max operations => O(n log n) time. total time is O(n log n). compare with O(n 2) for sort methods of Chapter 2.

Heap Sort Uses a max priority queue that is implemented as a heap. Initial put operations are replaced by a heap initialization step that takes O(n) time.

Machine Scheduling § m identical machines (drill press, cutter, sander, etc. ) § n jobs/tasks to be performed § assign jobs to machines so that the time at which the last job completes is minimum

Machine Scheduling Example 3 machines and 7 jobs job times are [6, 2, 3, 5, 10, 7, 14] possible schedule 6 13 A 2 7 21 B 3 13 C time ------>

Machine Scheduling Example 6 13 A 2 7 21 B 3 13 C time ------> Finish time = 21 Objective: Find schedules with minimum finish time.

LPT Schedules Longest Processing Time first. Jobs are scheduled in the order 14, 10, 7, 6, 5, 3, 2 Each job is scheduled on the machine on which it finishes earliest.
![LPT Schedule 14 10 7 6 5 3 2 14 16 A 10 15 LPT Schedule [14, 10, 7, 6, 5, 3, 2] 14 16 A 10 15](https://slidetodoc.com/presentation_image/05f17c482feb7f8fdff5effb5bc8b71b/image-19.jpg)
LPT Schedule [14, 10, 7, 6, 5, 3, 2] 14 16 A 10 15 B 7 C Finish time is 16! 13 16

LPT Schedule • LPT rule does not guarantee minimum finish time schedules. • (LPT Finish Time)/(Minimum Finish Time) <= 4/3 - 1/(3 m) where m is number of machines. • Usually LPT finish time is much closer to minimum finish time. • Minimum finish time scheduling is NP-hard.

NP-hard Problems • Infamous class of problems for which no one has developed a polynomial time algorithm. • That is, no algorithm whose complexity is O(nk) for any constant k is known for any NPhard problem. • The class includes thousands of real-world problems. • Highly unlikely that any NP-hard problem can be solved by a polynomial time algorithm.

NP-hard Problems • Since even polynomial time algorithms with degree k > 3 (say) are not practical for large n, we must change our expectations of the algorithm that is used. • Usually develop fast heuristics for NP-hard problems. § Algorithm that gives a solution close to best. § Runs in acceptable amount of time. • LPT rule is good heuristic for minimum finish time scheduling.

Complexity Of LPT Scheduling • Sort jobs into decreasing order of task time. § O(n log n) time (n is number of jobs) • Schedule jobs in this order. § assign job to machine that becomes available first § must find minimum of m (m is number of machines) finish times § takes O(m) time using simple strategy § so need O(mn) time to schedule all n jobs.

Using A Min Priority Queue • Min priority queue has the finish times of the m machines. • Initial finish times are all 0. • To schedule a job remove machine with minimum finish time from the priority queue. • Update the finish time of the selected machine and put the machine back into the priority queue.

Using A Min Priority Queue • m put operations to initialize priority queue • 1 remove min and 1 put to schedule each job • 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))

Huffman Codes Useful in lossless compression. May be used in conjunction with LZW method. Read from text.

Min Tree Definition Each tree node has a value. Value in any node is the minimum value in the subtree for which that node is the root. Equivalently, no descendent has a smaller value.

Min Tree Example 2 4 4 9 3 8 7 9 9 Root has minimum element.

Max Tree Example 9 4 4 9 8 2 7 3 1 Root has maximum element.

Min Heap Definition • complete binary tree • min tree

Min Heap With 9 Nodes Complete binary tree with 9 nodes.

Min Heap With 9 Nodes 2 4 6 8 3 7 9 6 Complete binary tree with 9 nodes that is also a min tree. 3

Max Heap With 9 Nodes 9 8 6 5 7 7 2 1 Complete binary tree with 9 nodes that is also a max tree. 6

Heap Height Since a heap is a complete binary tree, the height of an n node heap is log 2 (n+1).

A Heap Is Efficiently Represented As An Array 9 8 7 6 5 7 2 1 0 9 8 7 6 7 2 6 5 1 1 2 3 4 5 6 7 8 9 10 6

Moving Up And Down A Heap 1 9 2 3 8 7 4 5 6 7 5 1 8 9 7 6 2 6

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

Putting An Element Into A Max Heap 9 8 7 6 5 7 1 75 New element is 5. 2 6

Putting An Element Into A Max Heap 9 8 7 6 5 7 1 7 New element is 20. 2 6

Putting An Element Into A Max Heap 9 8 7 6 5 2 1 7 New element is 20. 6

Putting An Element Into A Max Heap 9 7 6 5 8 1 7 New element is 20. 2 6

Putting An Element Into A Max Heap 20 9 7 6 5 8 1 7 New element is 20. 2 6

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

Putting An Element Into A Max Heap 20 9 7 6 5 8 1 7 New element is 15. 2 6

Putting An Element Into A Max Heap 20 9 7 6 5 2 1 7 8 New element is 15. 6

Putting An Element Into A Max Heap 20 7 15 6 5 9 1 7 2 8 New element is 15. 6

Complexity Of Put 20 7 15 6 5 9 1 7 2 8 Complexity is O(log n), where n is heap size. 6

Removing The Max Element 20 7 15 6 5 9 1 7 2 8 Max element is in the root. 6

Removing The Max Element 7 15 6 5 9 1 7 2 8 After max element is removed. 6

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

Removing The Max Element 7 15 6 5 9 1 2 7 Reinsert 8 into the heap. 6

Removing The Max Element 15 7 6 5 9 1 2 7 Reinsert 8 into the heap. 6

Removing The Max Element 15 9 7 6 5 8 1 2 7 Reinsert 8 into the heap. 6

Removing The Max Element 15 9 7 6 5 8 1 7 Max element is 15. 2 6

Removing The Max Element 9 7 6 5 8 1 2 7 After max element is removed. 6

Removing The Max Element 9 7 6 5 8 1 7 Heap with 9 nodes. 2 6

Removing The Max Element 9 6 5 7 8 1 Reinsert 7. 2 6

Removing The Max Element 9 7 6 5 8 1 Reinsert 7. 2 6

Removing The Max Element 9 8 6 5 7 7 1 Reinsert 7. 2 6

Complexity Of Remove Max Element 9 8 6 5 7 7 2 1 Complexity is O(log n). 6
Queue quiz
Adaptable priority queues
Applications of priority queues
Min-priority queue
Burman's priority list gives priority to
Priority mail vs priority mail express
Stacks and queues in python
Java stacks and queues
Representation of queues
Java stack exercises
Message queues in unix
Pipes in rtos
Jenis jenis queue
Queue head tail
Erisone queues
What are stacks
Descriptive kind of quantitative research
Two kinds of main memory are
Different market forms of shellfish
Two kinds of questions
Two kinds of cultural identity activity 1.5 answer key
Two important kinds of metaergs are
Verbal irony meaning
Kinds of letter
Two kinds of real numbers
Moral saint
Konstruktor wykrywacza min
Min arbeidsplan st olavs
Min max normalization example
Tentukan nilai minimum
Ludus sde
Gak syngende din ven i møde
Wei min shen
Absolute max vs local max
Richard k min
Parts of a parabola
Quadratic maximum and minimum word problems worksheet
Min jin ha
Persoonapronominit
Select ename min(sal) from emp
Relative vs absolute max and min
Cardinality ratio in dbms
Cmin dur
Duett min side
Min max theorem
Hur ser min framtid ut
Gtt/min formula
Opgang pia juul
Sulalah min thin
Kai-min chung
Iv drop factor formula
Drops per minute formula
Intensiewe vorm vir suur
Hash table visualization
Gak ud min sjæl
Solucion cristaloide
Symmetric min max heap
Dosage calculations formula
Double ended priority queue
Dopamine drip calculation
Dimensional analysis calculator