Download presentation
Presentation is loading. Please wait.
Published byAlvin Singleton Modified over 8 years ago
1
Data structures Binomial Heaps - Binomial Trees B0B0 BkBk B k-1
2
Data structures Binomial Heaps - Binomial Trees B0B0 B2B2 B1B1 B3B3
3
Data structures Binomial Heaps - Binomial Trees B4B4
4
Data structures Binomial Heaps - Binomial Trees BkBk B k-1 B k-2 B2B2 B1B1 B0B0
5
Data structures Binomial Heaps - Binomial Trees Properties of binomial tree B k it has 2 k nodes the height of the tree is k it has exactly C k i nodes at depth i for i = 0, 1, , k C k i = n ! / (i ! (n – i) !) the root has degree k
6
Data structures Binomial Heaps - Definition Binomial heap H is an ordered list of binomial trees that satisfies the following properties: Each binomial tree is heap-ordered, i.e. for each node its key is greater or equal to the key of its parent There is at most one binomial tree in H whose root has a given degree Binomial trees are contained in the list in the order of increasing degrees
7
Data structures Binomial Heaps - Example 38 1429 6 27 1117 8 18 1225 110
8
Data structures Binomial Heaps - Example 38 - 0 14 - 129 - 0 6 - 3 27 - 0 11 - 117 - 0 8 - 2 18 - 0 12 - 125 - 0 1 - 2 10 - 0 KeyDataDegParentLeftNext
9
Data structures Binomial Heaps - Example [Adapted from T.Cormen, C.Leiserson, R. Rivest]
10
Data structures Binomial Heaps - Min procedure Min(BinomialHeap H): min P H.Head while P 0 do if P.Key < min then min P.Key P P.Next return min
11
Data structures Binomial Heaps - Union - Example 17 1044 6 50 4831 2937 318 24 2322 8 55 4532 30 25 712 33 15 41 28 H1H1 H2H2
12
Data structures Binomial Heaps - Union - Example 17 1044 6 50 4831 2937 318 24 2322 8 55 4532 30 25 712 33 15 41 28 H1H2H1H2
13
Data structures Binomial Heaps - Union - Example 17 1044 6 50 4831 2937 3 18 24 2322 8 55 4532 30 25 712 33 15 41 28 H1H2H1H2
14
Data structures Binomial Heaps - Union - Example 17 1044 6 50 4831 2937 3 18 24 2322 8 55 4532 3025 7 12 33 15 41 28 H1H2H1H2
15
Data structures Binomial Heaps - Union - Example 17 1044 6 50 4831 2937 3 18 24 2322 8 55 4532 3025 7 12 33 15 41 28 H1H2H1H2
16
Data structures Binomial Heaps - Union procedure Union(BinomialHeap H1,H2): H BinomialHeapMakeEmpty() P H.Head; P1 H1.Head; P2 H2.Head while P1 0 and P2 0 do if P1.Deg < P2.Deg then P.Next P1; P1 P1.Next else P.Next P2; P2 P2.Next while P1 0 do P.Next P1; P1 P1.Next while P2 0 do P.Next P2; P2 P2.Next
17
Data structures Binomial Heaps - Union [Adapted from T.Cormen, C.Leiserson, R. Rivest]
18
Data structures Binomial Heaps - Union (continued) P1 H.Head; P2 P1; P3 P1; P4 P1 merge 0 while P1.Next 0 do P4 P3; P3 P2; P2 P1; P1 P1.Next if P1.Deg = P2.Deg then merge 1 if P1.Deg < P2.Deg and merge = 1 then if P2.Key < P3.Key then P2 P3 if P4 = H.Head then H.Head P3 else P4.Next P3 P2.Next P3.Left; P2.Parent P3 P3.Left P2; P3.Next P1; P3.Deg P3.Deg + 1 merge 0 return H
19
Data structures Binomial Heaps - Insert procedure Insert(BinomialHeap H, int Key, int Data): H1 BinomialHeapMakeEmpty() E NewNode() H1.Head E; E.Next 0 E.Parent 0; E.Left 0 E.Deg 0 E.Key Key; E.Data Data; H Union(H,H1)
20
Data structures Binomial Heaps - ExtractMin - Example 17 1044 6 50 4831 2918 24 2322 8 55 4532 30 12 33 15 41 28
21
Data structures Binomial Heaps - ExtractMin - Example 17 1044 6 50 4831 2918 24 2322 8 55 4532 30 12 33 15 41 28
22
Data structures Binomial Heaps - ExtractMin - Example 17 1044 50 4831 29 18 24 2322 8 55 4532 30 12 33 15 41 28
23
Data structures Binomial Heaps - ExtractMin - Example 17 1044 50 4831 29 18 24 2322 8 55 4532 30 12 33 15 41 28
24
Data structures Binomial Heaps - ExtractMin - Example 17 1044 50 4831 29 1824 2322 8 55 4532 301233 15 41 28
25
Data structures Binomial Heaps - ExtractMin - Example 17 1044 50 4831 29 1824 2322 8 55 4532 301233 15 41 28
26
Data structures Binomial Heaps - ExtractMin - Example 17 1044 50 4831 291824 2322 8 55 4532 3012 33 15 41 28
27
Data structures Binomial Heaps - ExtractMin procedure ExtractMin(BinomialHeap H): min ; M 0; MP 0 P H.Head; PP H.Head while P 0 do if P.Key < min then min P.Key; M P; MP PP PP P; P P.Next if M = 0 then return fail
28
Data structures Binomial Heaps - ExtractMin (continued) H1 BinomialHeapMakeEmpty() if MP H.Head then MP.Next M.Next else H.Head M.Next P M.Left while P 0 do Next P.Next P.Next H1.Head; H1.Head P P.Parent 0 P Next H Union(H,H1)
29
Data structures Binomial Heaps - DecreaseKey - Example 17 1044 50 4831 291824 2322 8 55 4532 3012 33 15 41 28 10
30
Data structures Binomial Heaps - DecreaseKey - Example 17 1044 10 4831 291824 2322 8 55 4532 3012 33 15 41 28
31
Data structures Binomial Heaps - DecreaseKey - Example 17 1044 48 1031 291824 2322 8 55 4532 3012 33 15 41 28
32
Data structures Binomial Heaps - DecreaseKey - Example 17 1044 48 2931 101824 2322 8 55 4532 3012 33 15 41 28
33
Data structures Binomial Heaps - DecreaseKey - Example 17 1044 48 2931 151824 2322 8 55 4532 3012 33 10 41 28
34
Data structures Binomial Heaps - DecreaseKey procedure DecreaseKey(BinomialHeap H, Node P, int Key): Parent P.Parent while Parent 0 and P.Key < Parent.Key do P.Key Parent.Key P.Data Parent.Data P Parent Parent Parent.Parent
35
Data structures Binomial Heaps - Delete procedure DeleteKey(BinomialHeap H, Node P): BinomialHeapDecreaseKey(H,P, ) BinomialHeapExtractMin(H)
36
Data structures Binomial Heaps - Summary Binomial HeapsHeaps Min (log n) or (1) (1) ExtractMin (log n) DecreaseKey (log n) Union (log n) (n)(n) Insert (log n) Delete (log n) MakeEmpty (1) IsEmpty (1)
37
Data structures Fibonacci Heaps [Adapted from T.Cormen, C.Leiserson, R. Rivest]
38
Data structures Fibonacci Heaps - Example 39 1852 3237 41 3830 17 35 2646 24 min(H)
39
Data structures Fibonacci Heaps - Example 39 1852 3237 41 3830 17 35 2646 24 min(H) KeyDataDegParentLeftNextPrevious
40
Data structures Fibonacci Heaps - Example [Adapted from T.Cormen, C.Leiserson, R. Rivest]
41
Data structures Fibonacci Heaps - Degree and Marked nodes A degree D(x) of a node x in Fibonacci heap is defined as a number of children of node x (unlike as it is for binomial heaps, D(x) may not be equal with height of the tree rooted at x). A node x in Fibonacci Heap is marked if and only if it has lost a child since the last time x was made the child of another node (or since the last time x was made the root)
42
Data structures Fibonacci Heaps - Min [Adapted from T.Cormen, C.Leiserson, R. Rivest]
43
Data structures Fibonacci Heaps - Insert [Adapted from T.Cormen, C.Leiserson, R. Rivest]
44
Data structures Fibonacci Heaps - Insert - Example 39 1852 3237 41 3830 17 35 2646 24 min(H) 1
45
Data structures Fibonacci Heaps - Insert - Example 39 1852 3237 41 3830 17 35 2646 24 min(H) 1
46
Data structures Fibonacci Heaps - Insert - Example 39 1852 3237 41 3830 17 35 2646 24 min(H) 1
47
Data structures Fibonacci Heaps - Union [Adapted from T.Cormen, C.Leiserson, R. Rivest]
48
Data structures Fibonacci Heaps - ExtractMin [Adapted from T.Cormen, C.Leiserson, R. Rivest]
49
Data structures Fibonacci Heaps - ExtractMin [Adapted from T.Cormen, C.Leiserson, R. Rivest]
50
Data structures Fibonacci Heaps - ExtractMin - Example 39 1852 3237 41 3830 17 35 2646 24 min(H) 21
51
Data structures Fibonacci Heaps - ExtractMin - Example 39 1852237 41 38 30 17 35 2646 24 min(H) 21
52
Data structures Fibonacci Heaps - ExtractMin - Example 39 1852237 41 38 30 17 35 2646 2421 01234
53
Data structures Fibonacci Heaps - ExtractMin - Example 39 1852237 41 38 30 17 35 2646 2421 01234
54
Data structures Fibonacci Heaps - ExtractMin - Example 39 1852 23 7 41 38 30 17 35 2646 2421 01234
55
Data structures Fibonacci Heaps - ExtractMin - Example 39 1852 23 7 41 38 30 17 35 2646 2421 01234
56
Data structures Fibonacci Heaps - ExtractMin - Example 39 1852 23 7 41 38 30 17 35 2646 24 21 01234
57
Data structures Fibonacci Heaps - ExtractMin - Example 39 18 52 23 7 41 38 30 17 35 2646 2421 01234
58
Data structures Fibonacci Heaps - ExtractMin - Example 39 18 52 23 7 41 38 30 17 35 2646 2421 min(H)
59
Data structures Fibonacci Heaps - DecreaseKey [Adapted from T.Cormen, C.Leiserson, R. Rivest]
60
Data structures Fibonacci Heaps - DecreaseKey - Example 1 39 18 52 23 7 41 38 30 17 35 2646 2421 min(H) 15
61
Data structures Fibonacci Heaps - DecreaseKey - Example 1 39 18 52 23 7 41 38 30 17 35 26 15 2421 min(H)
62
Data structures Fibonacci Heaps - DecreaseKey - Example 2 39 18 52 23 7 41 38 30 17 35 26 15 2421 min(H) 5
63
Data structures Fibonacci Heaps - DecreaseKey - Example 2 39 18 52 23 7 41 38 30 17 26 15 2421 min(H) 5
64
Data structures Fibonacci Heaps - DecreaseKey - Example 2 39 18 52 23 7 41 38 30 17 2615 2421 min(H) 5
65
Data structures Fibonacci Heaps - DecreaseKey - Example 2 39 18 52 23 7 41 38 30 17 261524 21 min(H) 5
66
Data structures Fibonacci Heaps - Delete [Adapted from T.Cormen, C.Leiserson, R. Rivest]
67
Data structures Fibonacci Heaps - Complexity Heap potential P(H) = t(H) + 2m(H), where t(H) - number of nodes in root level, m(H) - number of marked nodes. Amortised complexity T a (H) = T(H) + P(H)
68
Data structures Fibonacci Heaps - Complexity Insert Min [Adapted from T.Cormen, C.Leiserson, R. Rivest]
69
Data structures Fibonacci Heaps - Complexity Union [Adapted from T.Cormen, C.Leiserson, R. Rivest]
70
Data structures Fibonacci Heaps - Complexity ExtractMin [Adapted from T.Cormen, C.Leiserson, R. Rivest]
71
Data structures Fibonacci Heaps - Complexity DecreaseKey Delete - just sum of the costs for DecreaseKey and ExtractMin [Adapted from T.Cormen, C.Leiserson, R. Rivest]
72
Data structures Fibonacci Heaps - Degree and size D(n) - maximal possible node degree in Fibonacci heap with n elements Theorem D(n) = (log n)
73
Data structures Fibonacci Heaps - Degree and size Observation If only operations Min,Union, Insert, ExtractMin, MakeEmpty and IsEmpty are used, then the resulting Fibonacci heap is just a collection of unordered Binomial trees, thus D(n) = (log n).
74
Data structures Fibonacci Heaps - Degree and size Lemma Let x be a node in a Fibonacci heap with degree(x) = k. Let y 1,..., y k be the children of x in the order in which they were linked to x, from the earliest to the latest. Then degree(y 1 ) 0 and degree(y i ) i – 2 for i = 2, 3,..., k. [Adapted from T.Cormen, C.Leiserson, R. Rivest]
75
Data structures Fibonacci Heaps - Degree and size Fibonacci numbers F 0 = 0F 1 = 1F k = F k – 1 + F k – 2 Lemma For all k 0: F k + 2 = 1 + i = 0...k F i
76
Data structures Fibonacci Heaps - Degree and size Lemma Let x be a node in a Fibonacci heap and let degree(x) = k. Then size(x) F k + 2 k, where k = (1 + 5)/2. [Adapted from T.Cormen, C.Leiserson, R. Rivest]
77
Data structures Fibonacci Heaps - Degree and size [Adapted from T.Cormen, C.Leiserson, R. Rivest]
78
Data structures Fibonacci Heaps - Degree and size Corollary The maximum degree D(n) of any node in an n-node Fibonacci heap is O(log n).
79
Data structures Fibonacci Heaps - Summary Fibonacci Heaps Binomial HeapsHeaps Min (1) (log n) or (1) (1) ExtractMin (log n) DecreaseKey (1) (log n) Union (1) (log n) (n)(n) Insert (1) (log n) Delete (log n) MakeEmpty (1) IsEmpty (1)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.