Download presentation

Presentation is loading. Please wait.

Published byGunner Garnsey Modified over 3 years ago

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

3
Min Priority Queue Collection of elements. Each element has a priority or key. Supports following operations: empty size insert an element into the priority queue (push) get element with min priority (top) remove element with min priority (pop)

4
Max Priority Queue Collection of elements. Each element has a priority or key. Supports following operations: empty size insert an element into the priority queue (push) get element with max priority (top) remove element with max priority (pop)

5
Complexity Of Operations Use a heap or a leftist tree (both are defined later). empty, size, and top => O(1) time insert (push) and remove (pop) => O(log n) time where n is the size of the priority queue

6
Applications Sorting use element key as priority insert elements to be sorted into a priority queue remove/pop 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)

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

8
After Inserting Into Max Priority Queue Sorted Array 68 2 4 1 Max Priority Queue

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

10
After Second Remove Max Operation Sorted Array 2 4 1 86 Max Priority Queue

11
After Third Remove Max Operation Sorted Array 2 1 864 Max Priority Queue

12
After Fourth Remove Max Operation Sorted Array 1 8642 Max Priority Queue

13
After Fifth Remove Max Operation Sorted Array 86421 Max Priority Queue

14
Complexity Of Sorting Sort n elements. n insert 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 insertion sort of Lecture 1.

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

16
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

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

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

19
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.

20
LPT Schedule [14, 10, 7, 6, 5, 3, 2] A B C 14 10 713 15 16 Finish time is 16!

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

22
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(n k ) for any constant k is known for any NP- hard problem. The class includes thousands of real-world problems. Highly unlikely that any NP-hard problem can be solved by a polynomial time algorithm.

23
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.

24
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.

25
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 insert the machine back into the priority queue.

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

27
Huffman Codes Discussed in Chapter 7 of text.

28
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.

29
Min Tree Example 2 493 4 87 99 Root has minimum element.

30
Max Tree Example 9 498 4 27 31 Root has maximum element.

31
Min Heap Definition complete binary tree min tree

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

33
Min Heap With 9 Nodes Complete binary tree with 9 nodes that is also a min tree. 2 4 6793 86 3

34
Max Heap With 9 Nodes Complete binary tree with 9 nodes that is also a max tree. 9 8 6726 51 7

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

36
987672651 123456789100 A Heap Is Efficiently Represented As An Array 9 8 6726 51 7

37
Moving Up And Down A Heap 9 8 6726 51 7 1 23 4 56 7 89

38
Inserting An Element Into A Max Heap Complete binary tree with 10 nodes. 9 8 6726 51 7 7

39
Inserting An Element Into A Max Heap New element is 5. 9 8 6726 51 7 75

40
Inserting An Element Into A Max Heap New element is 20. 9 8 6 7 26 51 7 7 7

41
Inserting An Element Into A Max Heap New element is 20. 9 8 6 7 26 51 7 7 7

42
Inserting An Element Into A Max Heap New element is 20. 9 86 7 26 51 7 7 7

43
Inserting An Element Into A Max Heap New element is 20. 9 86 7 26 51 7 7 7 20

44
Inserting An Element Into A Max Heap Complete binary tree with 11 nodes. 9 86 7 26 51 7 7 7 20

45
Inserting An Element Into A Max Heap New element is 15. 9 86 7 26 51 7 7 7 20

46
Inserting An Element Into A Max Heap New element is 15. 9 8 6 7 26 51 7 7 7 20 8

47
Inserting An Element Into A Max Heap New element is 15. 8 6 7 26 51 7 7 7 20 8 9 15

48
Complexity Of Insert Complexity is O(log n), where n is heap size. 8 6 7 26 51 7 7 7 20 8 9 15

49
Removing The Max Element Max element is in the root. 8 6 7 26 51 7 7 7 20 8 9 15

50
Removing The Max Element After max element is removed. 8 6 7 26 51 7 7 7 8 9 15

51
Removing The Max Element Heap with 10 nodes. 8 6 7 26 51 7 7 7 8 9 15 Reinsert 8 into the heap.

52
Removing The Max Element Reinsert 8 into the heap. 6 7 26 51 7 7 7 9 15

53
Removing The Max Element Reinsert 8 into the heap. 6 7 26 51 7 7 7 9 15

54
Removing The Max Element Reinsert 8 into the heap. 6 7 26 51 7 7 7 9 15 8

55
Removing The Max Element Max element is 15. 6 7 26 51 7 7 7 9 15 8

56
Removing The Max Element After max element is removed. 6 7 26 51 7 7 7 9 8

57
Removing The Max Element Heap with 9 nodes. 6 7 26 51 7 7 7 9 8

58
Removing The Max Element Reinsert 7. 626 51 79 8

59
Removing The Max Element Reinsert 7. 626 51 7 9 8

60
Removing The Max Element Reinsert 7. 626 51 7 9 8 7

61
Complexity Of Remove Max Element Complexity is O(log n). 626 51 7 9 8 7

Similar presentations

Presentation is loading. Please wait....

OK

CS16: Introduction to Data Structures & Algorithms

CS16: Introduction to Data Structures & Algorithms

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on unemployment in india free download Ppt on heterotrophic mode of nutrition in plants Ppt on abo blood grouping images Ppt on effect of global warming on weather oregon Ppt on e-commerce applications Ppt on budget deficit Vector scan display ppt on tv Ppt on class 9 motion powerpoint Ppt on operating system linux Ppt on sources of water supply