fibonacci heaps priority queues - purdue universitycs180/spring2010web/... · 2010. 4. 26. · heap...
TRANSCRIPT
Fibonacci HeapsPriority Queues
John Ross Wallrabenstein
Updates
• Project 4 Grades Released
• Project 5 Questions?
Review
*This usually refers to the size of the structure
Review
• What is a static data structure?
*This usually refers to the size of the structure
Review
• What is a static data structure?
• Data Structures that do not change* over the lifetime of their use
*This usually refers to the size of the structure
Review
Review
• What is a dynamic data structure?
Review
• What is a dynamic data structure?
• Data Structures that change over their lifetime, allowing for efficient updates
Review
• Can you describe the Union/Find algorithm and its applications?
• Can you describe the Union By Rank heuristic?
• Can you describe Path Compression?
Union By Rank/** * Combines the set that contains a with the set that contains b. */ public void union(int a, int b) { Node na = findNode(a); Node nb = findNode(b);
if (na == nb) { return; }
// Link the smaller tree under the larger. if (na.rank > nb.rank) { // Delete nb. nb.child.parent = na.child; na.value = b; } else { // Delete na. na.child.parent = nb.child; nb.value = b;
if (na.rank == nb.rank) { nb.rank++; } } }
Path Compression/** * Finds the set containing a given Node. */ private Node findNode(Node node) { int top = 0;
// Find the child of the root element. while (node.parent.child == null) { stack[top++] = node; node = node.parent; }
// Do path compression on the way back down. Node rootChild = node;
while (top > 0) { node = stack[--top]; node.parent = rootChild; }
return rootChild.parent; }
Today’s Goal
• What is a Queue?
• What is a Priority Queue?
• We will implement a Priority Queue efficiently using a Fibonacci Heap
Overview
• Tree Theory
• Heaps
• Fibonacci Heaps
• Priority Queues
Tree Theory
• A free tree is a connected, acyclic undirected graph
• A graph is connected if every pair of vertices is connected by some path
• A graph is acyclic if it contains no cycles
Free Tree
Rooted Trees
• A rooted tree is a free tree where one vertex is distinguished from the others as the root
Rooted Tree
Root
Terminology
• An ancestor of node x is any node on the path from x to the root
• Similarly, if node y is an ancestor of x, then x is a descendent of y
• A leaf node has no descendants
Binary Tree
• A binary tree satisfies the property that every node has at most two children
Overview
• Tree Theory
• Heaps
• Fibonacci Heaps
• Priority Queues
Heaps
• A heap is a complete binary tree satisfying one of the following heap properties:
• Max-Heap: the parent of every node has a rank greater than or equal to all of its children
• Min-Heap: the parent of every node has a rank less than or equal to all of its children
Aside
• Which data structure do you use to organize your priorities?
• e.g. If you use a max-heap, you visit cracked.com before doing CS 180 work
• Assumption: Your last priority has a “higher” rank than your #1 priority
Max-Heap
Maintaining the Heap Property
• We will assume for now that the tree is self-balancing
• e.g. The height is kept at a minimum of log n
• When a node is inserted, the heap may no longer satisfy the {min,max} property
Maintaining the Heap Property
• To “fix” the heap, we recursively heapify so that the {min,max}-property is maintained
• Yes, heapify is the actual term!
Maintaining the Heap Property
• Max-Heapify Invariant:
• We will assume the binary trees rooted at Left(i) and Right(i) are max-heaps
• We will assume the inserted node x may be smaller than its children
• This violates the property
Maintaining the Heap Property
• Basic Idea:
• Let node i “float down” to its proper position so that the max-heap property is satisfied
Max-Heapify
Max-Heapify
Overview
• Tree Theory
• Heaps
• Fibonacci Heaps
• Priority Queues
Fibonacci Heaps
• A Fibonacci Heap is a forest of min-heap trees
• Their name is derived from the Fibonacci Numbers, which are used in their asymptotic analysis
Operations
• We will only concern ourselves with the following operations:
• Insert
• Extract-Min
• There are others, but the additional complexity is not necessary for queues*
*You should thank me for not covering this
Motivation
• Fibonacci Heaps have fast amortized cost relative to other data structures
• Heaps in general support priority queues
Motivation
• While Fibonacci Heaps are min-heaps, we can easily use them as priority queues
• How?
• Define lesser values to have higher priority (this is somewhat natural)
Underlying Structure
• Fibonacci Heaps are commonly implemented using a circular doubly-linked list
• Brief review of standard operations over a doubly-linked list
• See board
WARNING
• The following image has been found to induce disgust, confusion and seizures
• I promise things won’t seem as bad as they look*!
*By not covering your eyes, you absolve me of all legal liability with respect to your mental/emotional well-being
C-DLL Structure
*Ignore the dark, or “marked” nodes
Fibonacci Heaps
Lecture slides adapted from:
Chapter 19
Kevin Wayne’s slides from the 2007 Princeton University Algorithms y y gclass
Priority Queues Performance
make-heap
Operation
1
BinaryHeap
1
LinkedList
1
FibonacciHeap †
insert
delete min
log n
log n
1
n
is-empty 11
1
log n
1
delete-min
decrease-key
delete
log n
log n
log n
n
n
n
log n
1
log n
find-min
union
1
n
n
1
1
1
† amortizedn = number of elements in priority queue † amortizedn = number of elements in priority queue
2
Hopeless challenge. O(1) insert, delete- min and decrease- key. Why?
Fibonacci Heaps: Structure
Fibonacci heap.Set of heap- ordered trees.Maintain pointer to minimum element.Set of marked nodes.
find- min takes O(1) time
min
17 72324 3
30
35
26 46
39
4118 52
44
4
Heap H
39 44
Fibonacci Heaps: Structure
Fibonacci heap.Set of heap- ordered trees.Maintain pointer to minimum element.Set of marked nodes.
roots heap-ordered tree
72317 24 3
30
35
26 464118 52
44
5
Heap H
39 44
Fibonacci Heaps: Notation
Notation
n = number of nodes in heap.
rank(x) = number of children of node x.
rank(H) = max rank of any node in heap H.
trees(H) = number of trees in heap H.
marks(H) = number of marked nodes in heap H.
(Note: terms different in text)
k i( ) k (H) 3 14
72317 24 3
rank = 3 mintrees(H) = 5 marks(H) = 3 n = 14
723
30
17
26 46
24
4118 52
3
7
3539 44
Heap H marked
Insert
9
Fibonacci Heaps: Insert
Insert.
Create a new singleton tree.
Add to root list; update min pointer (if necessary).
21
insert 21
i
72317 24 3
min
723
30
17
26 46
24
4118 52
3
10
3539 44
Heap H
Fibonacci Heaps: Insert
Insert.
Create a new singleton tree.
Add to root list; update min pointer (if necessary).
i
insert 21
723 317 24 21
min
41
723
18 52
3
30
17
26 46
24 21
11
3935
44Heap H
Fibonacci Heaps: Insert Analysis
Actual cost. O(1)
Change in potential. + 1
(H) = trees(H) + 2 marks(H)
potential of heap H
Amortized cost. O(1)
i
7 317 24 2123
min
41
7
18 52
3
30
17
26 46
24 2123
12
3935
44Heap H
Delete Min
13
Linking Operation
Linking operation. Make larger root a child of smaller root.
smaller rootlarger root still heap-ordered
315 3
4118 5256 24 4118 5215
39 4477
tree T1 tree T2
39 44
77
56 24
14
tree T'
Fibonacci Heaps: Delete Min
Delete minDelete min.Delete minMeld its children into the root list and update min.Consolidate trees so that no two roots have same rankConsolidate trees so that no two roots have same rank.
min
317237 24
39
4118 52
44
30
35
26 46
15
39 4435
Fibonacci Heaps: Delete Min
Delete min.Delete minMeld its children into the root list and update min.Consolidate trees so that no two roots have same rank.
min
411723 18 527 24
3930 26 46 44
35
16
Fibonacci Heaps: Delete Min
Delete min.Delete minMeld its children into the root list and update min.Consolidate trees so that no two roots have same rank:– Repeatedly link two trees of the same rank until no pair
of roots has the same rank (= degree)
mincurrent
411723 18 527 24
3930
35
26 46 44
17
35
Fibonacci Heaps: Delete Min
Delete minDelete min.
Delete min. Meld its children into the root list and update min.
Consolidate trees so that no two roots have same rank.
0 1 2 3
rank
currentmin
411723 18 527 24
3930
35
26 46 44
18
35
Fibonacci Heaps: Delete Min
Delete min.
Delete min. Meld its children into the root list and update min.
Consolidate trees so that no two roots have same rank.
0 1 2 3
rank
mincurrent
411723 18 527 24
3930
35
26 46 44
19
35
Fibonacci Heaps: Delete Min
Delete min.
Delete min. Meld its children into the root list and update min.
Consolidate trees so that no two roots have same rank.
0 1 2 3
rank
min
411723 18 527 24
3930
35
26 46 44current
20
35
Fibonacci Heaps: Delete Min
Delete min.
Delete min
Meld its children into the root list and update min.
Consolidate trees so that no two roots have same rank.
0 1 2 3
rank
min
411723 18 527 24
3930
35
26 46 44current
21
35
link 23 into 17
Fibonacci Heaps: Delete Min
Delete min.
Delete min. Meld its children into the root list and update min.
Consolidate trees so that no two roots have same rank.
0 1 2 3
rank
min
4117 18 527 24
392330
35
26 46 44current
22
35
link 17 into 7
Fibonacci Heaps: Delete Min
Delete min.
Delete min. Meld its children into the root list and update min.
Consolidate trees so that no two roots have same rank.
0 1 2 3
rank
current
417 18 5224
min
393017
35
26 46 44
23
23
35 23
link 24 into 7
Fibonacci Heaps: Delete Min
Delete min.
Delete min. Meld its children into root list; update min.
Consolidate trees so that no two roots have same rank.
0 1 2 3
rank
current
417 18 52
min
3930
23
17
26 46
24 44
24
23
35
26 46
Fibonacci Heaps: Delete Min
Delete min.
Delete min; meld its children into root list; update min.
Consolidate trees so that no two roots have same rank.
0 1 2 3
rank
current
417 18 52
min
3930
23
17
26 46
24 44
25
23
35
26 46
Fibonacci Heaps: Delete Min
Delete min.
Delete min. Meld its children into root list; update min.
Consolidate trees so that no two roots have same rank.
0 1 2 3
rank
current
417 18 52
min
3930
23
17
26 46
24 44
26
23
35
26 46
Fibonacci Heaps: Delete Min
Delete min.
Delete min. Meld its children into root list; update min.
Consolidate trees so that no two roots have same rank.
0 1 2 3
rank
current
417 18 52
min
3930
23
17
26 46
24 44
27
23
35
26 46
link 41 into 18
Fibonacci Heaps: Delete Min
Delete min.
Delete min; meld its children into root list; update min.
Consolidate trees so that no two roots have same rank.
0 1 2 3
rank
current
7 1852
min
394130
23
17
26 46
24
44
28
23
35
26 46 44
Fibonacci Heaps: Delete Min
Delete min.
Delete min; meld its children into root list; update min.
Consolidate trees so that no two roots have same rank.
0 1 2 3
rank
current
7 52
min
18
30
23
17
26 46
24 3941
44
29
23
35
26 46 44
Fibonacci Heaps: Delete Min
Delete min.
Delete min. Meld its children into root list; update min.
Consolidate trees so that no two roots have same rank.
7 52
min
18
301724 3941
23
35
26 46 44
30
35
Before delete - min
317237 24
4118 52
31723
30
7
26 46
24
39
4118 52
44
30
35
26 46
minAfter consolidation, no7
30
52
1724 3941
18After consolidation, no two roots have the same degree and the largest degree in O(log n)
30
23
17
26 46
24 3941
44
31
35After delete - min