priority queues - universitetet i oslo · fibonacci heaps very elegant, and in theory efficient,...
TRANSCRIPT
Priority Queues
• Binary heaper• Leftist heaper• Binomial heapsp• Fibonacci heaps
Priority queues are important in, among other things, operating systems (process control i multitasking systems) search algorithms (A A* D* etc )(process control i multitasking systems), search algorithms (A, A , D , etc.), and simulation.
Priority QueuesPriority queues are data stuctures that hold elements with some kind of priority (key) in a queue-like structure, implementing the following operations:
• insert() – Inserting an element into the queue.• deleteMin() – Removing the element with the highest priority.
And maybe also:
b ild () B ild f t ( 1) f l t• buildHeap() – Build a queue from a set (>1) of elements.• increaseKey()/DecreaseKey() – Change priority.• delete() – Removing an element from the queue.• merge() – Merge two queues.
Priority QueuesAn unsorted linked list can be used. insert() inserts an element at the head of the list (O(1)), and deleteMin() searches the list for the element with the highest priority and removes it (O(n))with the highest priority and removes it (O(n)).
A sorted list can also be used (reversed running times).
– Not very efficient implementations.
To make an efficient priority queue, it is enough to keeps the elements “almost sorted”almost sorted .
Binary heapsA binary heap is organized as a complete binary tree. (All leves are full, execptpossibly the last.)
In a binary heap the element in the root must have a key less than or equal to the key of its children, in addition each sub-tree must be a binary heap.
1a
2
3⎣i / 2⎦ b c
d e f g2i 2i+14h i j
a b c d e f g h i j
0 1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4
Binary heaps
13 1insert(14)
21 16 2
24 31 19 68 3
65 26 32 414
13 21 16 24 31 19 68 65 26 32 14
0 1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4
Binary heaps
13 1insert(14)
14 16 2
23 21 19 68 3
65 26 32 431
13 14 16 24 21 19 68 65 26 32 31
0 1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4
”percolateUp()”
Binary heaps
1deleteMin()
14 16 2
19 21 19 68 3
65 26 32 431
14 16 19 21 19 68 65 26 32 31
0 1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4
Binary heaps
31 1deleteMin()
14 16 2
19 21 19 68 3
65 26 32 4
31 14 16 19 21 19 68 65 26 32
0 1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4
Binary heaps
14 1deleteMin()
19 16 2
26 21 19 68 3
65 31 32 4
14 19 16 19 21 26 68 65 31 32
0 1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4
”percolateDown()”
Binary heapsworst case average
insert() O(log N) O(1)
deleteMin() O(log N) O(log N)
buildHeap() O(N)
(Insert elements into the array unsorted, and run percolateDown() on each root in the resulting heap (the tree), bottom up)
(The sum of the heights of a binary tree with N nodes is O(N).)
merge() O(N)
(N = number of elements)(N = number of elements)
Leftist heaps
To implement an efficient merge(), we move away from arrays, and implement so-called leftist heaps as pure trees.
The idea is to make the heap (the tree) as skewed as possible, and do all the work on a short (right) branch, leaving the long (left) branch untouched.
A leftist heap is still a binary tree with the heap structure (key in root is lower than key in children), but with an extra skewness requirement.
For all nodes X in our tree, we define the null-path-length(X) as the distance from X to a node without two children (i.e. 0 or 1).
The skewness requirement is that for every node the null path length of its left child be at least as large as the null path length of the right child.
Leftist heaps
1
1 0
1
0 0
0 LEFTIST
1 0
1
0 1
0 0
NOT LEFTIST
Leftist heapsmerge() 3 6
10 8 12 7
21 14 17 18 24 37 18
23 26 33
Leftist heapsmerge() 3 6
10 8 12 7
21 14 17 18 24 37 18
23 26 33
Leftist heapsmerge() 3 6
10 8 12 7
21 14 17 18 24 37 18
23 26 33
63
7
18
6
108
12
18 24
3721 14
2317
18 24
33
23
26
Leftist heapsmerge() 3 6
10 8 12 7
21 14 17 18 24 37 18
23 26 33
63
7
18
6
108
12
18 24
3721 14
2317
18 24
33
23
26
Leftist heapsmerge() 3 6
10 8 12 7
21 14 17 18 24 37 18
23 26 33
63
7
18
6
108
12
18 24
3721 14
2317
18 24
33
23
26
Leftist heapsmerge() 3 6
10 8 12 7
21 14 17 18 24 37 18
23 26 33
63
7
37
6
10
12
18 2418
821 14
23 1718 24
33
23 17
26
Leftist heapsmerge() 3 6
10 8 12 7
21 14 17 18 24 37 18
23 26 33
3
12 76 10
18 2437
18
8
17
21 14
23
3317
26
23
Leftist heaps
5 3
7 6
insert(3) merge()
10
deleteMin() 1
5 3
7 6 11
merge()
7 6
10
11
Leftist heaps
worst case
merge() O(log N)
insert() O(log N)
deleteMin() O(log N)
buildHeap() O(N)
(N = number of elements)
In a leftist heap with N nodes, the right path is at most ⎣log (N+1)⎦ long.
Binomial heaps
Leftist heaps:merge(), insert() og deleteMin() in O(log N) time w.c.
Binary heaps:insert() in O(1) time on average.
Binomial heapsmerge(), insert() og deleteMin() in O(log N) time w.c.insert() O(1) time on average
Binomial heaps are collections of trees (sometimes called a forest), each tree a hheap.
Binomial heapsBinomial trees
B0B0
Binomial heapsBinomial trees
B0B0B1
Binomial heapsBinomial trees
B0B0B1
B2
Binomial heapsBinomial trees
B0B0B1
B2
B3
Binomial heapsBinomial trees
B0B0B1
B2
B3
B4B4
Binomial heapsBinomial trees
B0B0B1
B2
B3
A t f h i ht k h
B4
A tree of height k has:
2k nodes in total,B4
nodes on level d.⎟⎠⎞⎜
⎝⎛dk
Binomial heapsBinomial heap
16
18
12
21 24
65
Maximum one tree of each size:
6 l t 6 bi 011 (0 2 4) B B BX6 elements: 6 binary = 011 (0+2+4) B0 B1 B2X
Binomial heapsBinomial heap
16
18
12
21 24
65
Maximum one tree of each size:
6 l t 6 bi 011 (0 2 4) B B BX6 elements: 6 binary = 011 (0+2+4) B0 B1 B2X
The length of the root list in a heap of N elements is O(log N).(Doubly linked, circular list.)
Binomial heaps
merge() 16 12 14 2313
18 21 24
65
26 51 24
6565 65
Binomial heaps
merge() 16 12 14 2313
18 21 24
65
26 51 24
6565 65
Binomial heaps
merge() 16 12 14 2313
18 21 24
65
26 51 24
6565 65
1313
Binomial heaps
merge() 16 12 14 2313
18 21 24
65
26 51 24
6565 65
13 1413
16
14
26
18
Binomial heaps
merge() 16 12 14 2313
18 21 24
65
26 51 24
6565 65
13 122313
14
12
21 2424
23
51
16
18
266565
The trees (the root list) is kept sorted on height.
18
Binomial heaps
deleteMin() 13 1223
16
14
26
21 2451 24
16
18
266565
Binomial heaps
deleteMin() 13 1223
16
14
26
21 2451 24
16
18
266565
13 1421 2423merge()
16
18
266551 24
65
Binomial heapsworst case average case
merge() O(log N) O(log N)
insert() O(log N) O(1)
deleteMin() O(log N) O(log N)
buildHeap() O(N) O(N)
(Run N insert() on an initially empty heap.)
(N = number of elements)
Binomial heapsImplementation Doubly linked, circular lists
13
14
12
21 24
23
51 24
16
14
26
21 24
65
51 24
65
18
Binomial heapsImplementation Doubly linked, circular lists
13
14
12
21 24
23
51 24
16
14
26
21 24
65
51 24
65
18
2323
61 27
88
Binomial heapsImplementation Doubly linked, circular lists
13
14
12
21 24
23
51 24
16
14
26
21 24
65
51 24
65
18
2323
61 27
88
Fibonacci heapsVery elegant, and in theory efficient, way to implement heaps: Most operastionshave O(1) amortized running time. (Fredman & Tarjan ’87)
insert(), decreaseKey() og merge() O(1) amortized time
deleteMin() O(log N) amortized time
Combines elements from leftist heaps and binomial heaps.
A bit compicated to implement, and certain hidden constants are a bit high.
Best suited when there are few deleteMin() compared to the other operations. The data structure was developed for a shortest path algorithm (with p p p g (many decreaseKey() operations), also used in spanning tree algorithms.
Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.
2
114
12 1759
188 61018
1131
21
15
21
Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.
2
114
12 1750
188 61018
1131
21
15
21
Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.
2
114
0
12 175
1018
31
188 6
31
Leftist
11
21
15
21
Ikke leftist
Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.
2
114
0
12 175
1018
31
188 6
31
Leftist
11
21
15
21
Ikke leftist
Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.
2
411
0
12 17 5
1018
31
18 8 6
31
Leftist
11
21
15
21
Leftist
Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.
0
2
411
18
12 17 5 10
31
18 8 6
11
21
15
21
Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.
The method must be modified a bit as we wish to use trees that are binomialThe method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.
N d k d th fi t ti hild i d- Nodes are marked the first time child is removed. - The second time a node gets a child removed, it is cut off, and becomes theroot of a separate tree
38 718
min
38
24
7
23 17
18
39 2141
26
35
463052
35
Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.
The method must be modified a bit as we wish to use trees that are binomialThe method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.
N d k d th fi t ti hild i d- Nodes are marked the first time child is removed. - The second time a node gets a child removed, it is cut off, and becomes theroot of a separate tree
38 718
min
38
24
7
23 17
18
39 2141
26
35
53052
35
Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.
The method must be modified a bit as we wish to use trees that are binomialThe method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.
N d k d th fi t ti hild i d- Nodes are marked the first time child is removed. - The second time a node gets a child removed, it is cut off, and becomes theroot of a separate tree
38 5718
min
38
24
57
23 17
18
39 2141
26
35
3052
35
Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.
The method must be modified a bit as we wish to use trees that are binomialThe method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.
N d k d th fi t ti hild i d- Nodes are marked the first time child is removed. - The second time a node gets a child removed, it is cut off, and becomes theroot of a separate tree
38 5718
min
38
24
57
23 17
18
39 2141
13
35
3052
35
Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.
The method must be modified a bit as we wish to use trees that are binomialThe method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.
N d k d th fi t ti hild i d- Nodes are marked the first time child is removed. - The second time a node gets a child removed, it is cut off, and becomes theroot of a separate tree
38 135718
min
38 13
3524
57
23 17
18
39 2141
3052
Fibonacci heapsWe include a smart decreaseKey() method from leftist heaps.
The method must be modified a bit as we wish to use trees that are binomialThe method must be modified a bit, as we wish to use trees that are binomial trees, or partial binomial trees.
N d k d th fi t ti hild i d- Nodes are marked the first time child is removed. - The second time a node gets a child removed, it is cut off, and becomes theroot of a separate tree
38 13 245718
min
38 13
35
2457
23 17
18
39 2141
3052
Fibonacci heapsWe use lazy merging / lazy binomial queue.
18 7
8
5
8 6
8
9
4
9 68 8 6
11
9 9 6
32
Fibonacci heapsWe use lazy merging / lazy binomial queue.
18 7
8
5
8 6
8
9
4
9 68 8 6
11
9 9 6
32
18 7 5 8 4
8 8 6 9 9 6
11 32
Fibonacci heapsThe problem with our decreaseKey()-method and lazy merging is that wehave to clean up afterwards. This is done in by the deleteMin()-method whichbecomes expensive (O(log N) amortized time):becomes expensive (O(log N) amortized time):
All trees are examined we start with the smallest and merge two and two soAll trees are examined, we start with the smallest, and merge two and two, so that we get at most one tree of each size.
Each root has a number of children – this is used as the size of the tree (RecallEach root has a number of children this is used as the size of the tree. (Recallhow we construct binomial trees, and that they may be partial as a result ofdeleteMin()’s)
The trees are put in lists, one per size, and we begin merging, starting with thesmallest.
Fibonacci heapsAmortized time
insert() O(1)
decreaseKey() O(1)
merge() O(1)
deleteMin() O(log N)
buildHeap() O(N)
(Run N insert() on an initially empty heap.)
(N = number of elements)