Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05-2: Analysis of time Complexity of Priority.

Similar presentations


Presentation on theme: "Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05-2: Analysis of time Complexity of Priority."— Presentation transcript:

1 Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05-2: Analysis of time Complexity of Priority Queue Dr.Surasak Mungsing Feb-19

2 Priority Queue Collection of elements.
Each element has a priority or key. Supports following operations: isEmpty size add/put an element into the priority queue get element with min/max priority remove element with min/max priority Feb-19

3 Min Tree Example 2 4 9 3 8 7 Root is the minimum element Feb-19

4 Max Tree Example 9 4 8 2 7 3 1 Root is the maximum element Feb-19

5 Min Heap Definition complete binary tree min tree
2 4 6 7 9 3 8 Complete binary tree with 9 nodes that is also a min tree. Feb-19

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

7 Heap Height What is the height of an n node heap ?
Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1). Feb-19

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

33 Complexity Of Remove Max Element
6 2 5 1 7 9 8 Complexity is O(log n).

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

35 Practical Complexities
109 instructions/second Teraflop computer the 32yr time becomes approx 10 days. 2/24/2019

36 Impractical Complexities
109 instructions/second 2/24/2019

37 Summary O(n2) O(n7/6) O(n log n) 10 0.00044 0.00041 0.00057 0.00052
Insertion Sort O(n2) Shellsort O(n7/6) Heapsort O(n log n) Quicksort 10 100 1000 10000 58864 100000 NA 5.7298 8.8591 4.2298 71.164 104.68 47.065 2/24/2019

38 Faster Computer Vs Better Algorithm
Algorithmic improvement more useful than hardware improvement. E.g. 2n to n3 2/24/2019

39 24-Feb-19


Download ppt "Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05-2: Analysis of time Complexity of Priority."

Similar presentations


Ads by Google