Data structures Binomial Heaps - Binomial Trees B0B0 BkBk B k-1
Data structures Binomial Heaps - Binomial Trees B0B0 B2B2 B1B1 B3B3
Data structures Binomial Heaps - Binomial Trees B4B4
Data structures Binomial Heaps - Binomial Trees BkBk B k-1 B k-2 B2B2 B1B1 B0B0
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
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
Data structures Binomial Heaps - Example
Data structures Binomial Heaps - Example KeyDataDegParentLeftNext
Data structures Binomial Heaps - Example [Adapted from T.Cormen, C.Leiserson, R. Rivest]
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
Data structures Binomial Heaps - Union - Example H1H1 H2H2
Data structures Binomial Heaps - Union - Example H1H2H1H2
Data structures Binomial Heaps - Union - Example H1H2H1H2
Data structures Binomial Heaps - Union - Example H1H2H1H2
Data structures Binomial Heaps - Union - Example H1H2H1H2
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
Data structures Binomial Heaps - Union [Adapted from T.Cormen, C.Leiserson, R. Rivest]
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
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)
Data structures Binomial Heaps - ExtractMin - Example
Data structures Binomial Heaps - ExtractMin - Example
Data structures Binomial Heaps - ExtractMin - Example
Data structures Binomial Heaps - ExtractMin - Example
Data structures Binomial Heaps - ExtractMin - Example
Data structures Binomial Heaps - ExtractMin - Example
Data structures Binomial Heaps - ExtractMin - Example
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
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)
Data structures Binomial Heaps - DecreaseKey - Example
Data structures Binomial Heaps - DecreaseKey - Example
Data structures Binomial Heaps - DecreaseKey - Example
Data structures Binomial Heaps - DecreaseKey - Example
Data structures Binomial Heaps - DecreaseKey - Example
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
Data structures Binomial Heaps - Delete procedure DeleteKey(BinomialHeap H, Node P): BinomialHeapDecreaseKey(H,P, ) BinomialHeapExtractMin(H)
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)
Data structures Fibonacci Heaps [Adapted from T.Cormen, C.Leiserson, R. Rivest]
Data structures Fibonacci Heaps - Example min(H)
Data structures Fibonacci Heaps - Example min(H) KeyDataDegParentLeftNextPrevious
Data structures Fibonacci Heaps - Example [Adapted from T.Cormen, C.Leiserson, R. Rivest]
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)
Data structures Fibonacci Heaps - Min [Adapted from T.Cormen, C.Leiserson, R. Rivest]
Data structures Fibonacci Heaps - Insert [Adapted from T.Cormen, C.Leiserson, R. Rivest]
Data structures Fibonacci Heaps - Insert - Example min(H) 1
Data structures Fibonacci Heaps - Insert - Example min(H) 1
Data structures Fibonacci Heaps - Insert - Example min(H) 1
Data structures Fibonacci Heaps - Union [Adapted from T.Cormen, C.Leiserson, R. Rivest]
Data structures Fibonacci Heaps - ExtractMin [Adapted from T.Cormen, C.Leiserson, R. Rivest]
Data structures Fibonacci Heaps - ExtractMin [Adapted from T.Cormen, C.Leiserson, R. Rivest]
Data structures Fibonacci Heaps - ExtractMin - Example min(H) 21
Data structures Fibonacci Heaps - ExtractMin - Example min(H) 21
Data structures Fibonacci Heaps - ExtractMin - Example
Data structures Fibonacci Heaps - ExtractMin - Example
Data structures Fibonacci Heaps - ExtractMin - Example
Data structures Fibonacci Heaps - ExtractMin - Example
Data structures Fibonacci Heaps - ExtractMin - Example
Data structures Fibonacci Heaps - ExtractMin - Example
Data structures Fibonacci Heaps - ExtractMin - Example min(H)
Data structures Fibonacci Heaps - DecreaseKey [Adapted from T.Cormen, C.Leiserson, R. Rivest]
Data structures Fibonacci Heaps - DecreaseKey - Example min(H) 15
Data structures Fibonacci Heaps - DecreaseKey - Example min(H)
Data structures Fibonacci Heaps - DecreaseKey - Example min(H) 5
Data structures Fibonacci Heaps - DecreaseKey - Example min(H) 5
Data structures Fibonacci Heaps - DecreaseKey - Example min(H) 5
Data structures Fibonacci Heaps - DecreaseKey - Example min(H) 5
Data structures Fibonacci Heaps - Delete [Adapted from T.Cormen, C.Leiserson, R. Rivest]
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)
Data structures Fibonacci Heaps - Complexity Insert Min [Adapted from T.Cormen, C.Leiserson, R. Rivest]
Data structures Fibonacci Heaps - Complexity Union [Adapted from T.Cormen, C.Leiserson, R. Rivest]
Data structures Fibonacci Heaps - Complexity ExtractMin [Adapted from T.Cormen, C.Leiserson, R. Rivest]
Data structures Fibonacci Heaps - Complexity DecreaseKey Delete - just sum of the costs for DecreaseKey and ExtractMin [Adapted from T.Cormen, C.Leiserson, R. Rivest]
Data structures Fibonacci Heaps - Degree and size D(n) - maximal possible node degree in Fibonacci heap with n elements Theorem D(n) = (log n)
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).
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]
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
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]
Data structures Fibonacci Heaps - Degree and size [Adapted from T.Cormen, C.Leiserson, R. Rivest]
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).
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)