Download presentation
Presentation is loading. Please wait.
Published byようた たけくま Modified over 5 years ago
1
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Priority Queue ADT Linked list implementations Heap implementation Heapsort CSE 373, Copyright S. Tanimoto, Priority Queues -
2
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Priority Queue ADT A priority queue holds a bag of (priority, data) pairs. Priority values are totally ordered. [(2, Sue), (5, Joe), (9, Amy), (9, Sam), (9, Amy)] IsEmpty( ) fIsEmpty: Q {true, false} Insert(priority, data) fInsert: Q P D Q DeleteMin( ) fDeleteMin: Q Q P D Q = space of priority queues P = space of priority values D = space of data values CSE 373, Copyright S. Tanimoto, Priority Queues -
3
PQ Implementations with Linked Lists
Method 1: Always insert at the head of the list: O(1) For DeleteMin, perform a linear search for the pair holding the smallest priority value: O(n). Method 2: Maintain the list in sorted order of increasing priority. Always insert an element at its correct position in the list: O(n). For DeleteMin, simply remove the first element: O(1) (2, Sue), (5, Joe), (9, Amy), (9, Sam) CSE 373, Copyright S. Tanimoto, Priority Queues -
4
PQ Implementation with Heaps
Keep the pairs in the nodes of a partially ordered binary tree called a heap. Insertion: O(log n) For DeleteMin, remove the root of the heap, then update it to maintain the heap property: O(log n). CSE 373, Copyright S. Tanimoto, Priority Queues -
5
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heaps A min heap is a complete binary tree with a value V(N) at each node from a totally ordered set (such as integers or strings), such that if C is a child of N then V(N) V(C). (2, Sue) (5, Joe) (9, Sam) (9, Amy) CSE 373, Copyright S. Tanimoto, Priority Queues -
6
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Insertion into a Heap Insert (1, Jim): Begin by placing (1, Jim) in the next available leaf position. Then correct any violations of the min heap property by “bubbling” light elements up towards the root. (2, Sue) (5, Joe) (9, Sam) (9, Amy) (1, Jim) CSE 373, Copyright S. Tanimoto, Priority Queues -
7
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Bubbling Up A node trades places with one of its children. (2, Sue) (1, Jim) (9, Sam) (9, Amy) (5, Joe) CSE 373, Copyright S. Tanimoto, Priority Queues -
8
Bubbling Up (Second exchange)
A node trades places with one of its children. (1, Jim) (2, Sue) (9, Sam) (9, Amy) (5, Joe) CSE 373, Copyright S. Tanimoto, Priority Queues -
9
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
DeleteMin Root is removed. Tree must be updated. (1, Jim) (2, Sue) (9, Sam) (9, Amy) (5, Joe) CSE 373, Copyright S. Tanimoto, Priority Queues -
10
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
DeleteMin (cont) Root is actually exchanged with the last leaf, but removed from the tree. We now have to perform “bubble down.” (5, Joe) (2, Sue) (9, Sam) (9, Amy) (1, Jim) CSE 373, Copyright S. Tanimoto, Priority Queues -
11
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
DeleteMin (cont) Bubble down: Heavy node trades places with its lighter child. (2, Sue) (5, Joe) (9, Sam) (9, Amy) (1, Jim) CSE 373, Copyright S. Tanimoto, Priority Queues -
12
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Time Complexity Each step of a bubble up or bubble down takes O(1) time. There are at most log2n steps in a bubble up or bubble down operation. Insertion requires 1 bubble up. It’s O(log n) DeleteMin requires 1 bubble down: It’s also O(log n) CSE 373, Copyright S. Tanimoto, Priority Queues -
13
Heapsort Assume that n elements to be sorted are in an array. 7 4 12 3
35 8 2 20 6 1 CSE 373, Copyright S. Tanimoto, Priority Queues -
14
Heapsort (2) Treat the array as a complete binary tree. 7 12 4 3 35 8
20 6 1 CSE 373, Copyright S. Tanimoto, Priority Queues -
15
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (3) 1. Heapify the tree: beginning with the last parent and working toward the root, perform Bubble down. (if the node has a value greater than that of a child, change places with the smaller child, and if necessary, repeat this until the value sinks as far down as necessary to achieve the heap property.) 7 12 4 3 35 8 2 20 6 1 CSE 373, Copyright S. Tanimoto, Priority Queues -
16
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (4) 1. Heapify the tree: beginning with the last parent and working toward the root, perform Bubble down. (if the node has a value greater than that of a child, change places with the smaller child, and if necessary, repeat this until the value sinks as far down as necessary to achieve the heap property.) 7 12 4 3 1 8 2 20 6 35 CSE 373, Copyright S. Tanimoto, Priority Queues -
17
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (5) 1. Heapify the tree: beginning with the last parent and working toward the root, perform Bubble down. (if the node has a value greater than that of a child, change places with the smaller child, and if necessary, repeat this until the value sinks as far down as necessary to achieve the heap property.) 7 12 4 3 1 8 2 20 6 35 CSE 373, Copyright S. Tanimoto, Priority Queues -
18
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (6) 1. Heapify the tree: beginning with the last parent and working toward the root, perform Bubble down. (if the node has a value greater than that of a child, change places with the smaller child, and if necessary, repeat this until the value sinks as far down as necessary to achieve the heap property.) 7 12 4 3 1 8 2 20 6 35 CSE 373, Copyright S. Tanimoto, Priority Queues -
19
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (7) 1. Heapify the tree: beginning with the last parent and working toward the root, perform Bubble down. (if the node has a value greater than that of a child, change places with the smaller child, and if necessary, repeat this until the value sinks as far down as necessary to achieve the heap property.) 7 2 4 3 1 8 12 20 6 35 CSE 373, Copyright S. Tanimoto, Priority Queues -
20
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (8) 1. Heapify the tree: beginning with the last parent and working toward the root, perform Bubble down. (if the node has a value greater than that of a child, change places with the smaller child, and if necessary, repeat this until the value sinks as far down as necessary to achieve the heap property.) 7 2 4 3 1 8 12 20 6 35 CSE 373, Copyright S. Tanimoto, Priority Queues -
21
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (9) 1. Heapify the tree: beginning with the last parent and working toward the root, perform Bubble down. (if the node has a value greater than that of a child, change places with the smaller child, and if necessary, repeat this until the value sinks as far down as necessary to achieve the heap property.) 7 2 1 3 4 8 12 20 6 35 CSE 373, Copyright S. Tanimoto, Priority Queues -
22
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (10) 1. Heapify the tree: beginning with the last parent and working toward the root, perform Bubble down. (if the node has a value greater than that of a child, change places with the smaller child, and if necessary, repeat this until the value sinks as far down as necessary to achieve the heap property.) 1 2 7 3 4 8 12 20 6 35 CSE 373, Copyright S. Tanimoto, Priority Queues -
23
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (11) 1. Heapify the tree: beginning with the last parent and working toward the root, perform Bubble down. (if the node has a value greater than that of a child, change places with the smaller child, and if necessary, repeat this until the value sinks as far down as necessary to achieve the heap property.) 1 2 3 7 4 8 12 20 6 35 CSE 373, Copyright S. Tanimoto, Priority Queues -
24
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (12) Heapification is now complete. The elements are partially ordered in a heap. 1 2 3 6 4 8 12 20 7 35 CSE 373, Copyright S. Tanimoto, Priority Queues -
25
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (13) Step 2. Now perform n successive DELETEMIN operations. After each one, store the deleted element in the array space freed up at the end of the current tree... CSE 373, Copyright S. Tanimoto, Priority Queues -
26
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (14) As we delete the minimum, we replace the root by the last value in the tree, and bubble it down... 35 2 3 6 4 8 12 20 7 1 CSE 373, Copyright S. Tanimoto, Priority Queues -
27
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (15) As we delete the minimum, we replace the root by the last value in the tree, and bubble it down... 2 35 3 6 4 8 12 20 7 1 CSE 373, Copyright S. Tanimoto, Priority Queues -
28
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (16) The heap property has been restored. It’s time to perform the second DELETEMIN. 2 8 3 6 4 35 12 20 7 1 CSE 373, Copyright S. Tanimoto, Priority Queues -
29
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (17) The heap property has been restored. It’s time to perform the second DELETEMIN. 7 8 3 6 4 35 12 20 2 1 CSE 373, Copyright S. Tanimoto, Priority Queues -
30
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heapsort (final) This continues on until the last element has been removed from the tree. The elements are now sorted in decreasing order. 35 12 20 8 7 6 4 3 2 1 CSE 373, Copyright S. Tanimoto, Priority Queues -
31
Heapsort Time and Space
Step 1 requires no more than O(log n) comparison and exchanges per parent, and there are no more than n/2 parents. Step one’s time is therefore O(n log n). Step 2 requires n DELETEMIN operations each of which involves O(log n) comparisons and exchanges. Thus step two’s time is also O(n log n) The time required by Heapsort is O(n log n). Heapsort is an “in-place” sorting method, requiring no extra arrays or buffers for the input data. CSE 373, Copyright S. Tanimoto, Priority Queues -
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.