Uploaded by 楊証淇

(ch9) lec19-fheap-33p

advertisement
Ch. 9: Fibonacci Heaps
(B-Heaps serve as Special Cases of F-Heaps)
Fibonacci Heaps
有consolidation的版本 (與書本不同)
Insert
Actual
O(1)
Amortized
O(1)
Delete min (or max)
O(n)
O(log n)
Meld
O(1)
O(1)
Delete “any”
O(n)
O(log n)
Decrease key (or
increase)
O(n)
O(1)
額
外
增
加
的
操
作
Min Fibonacci Heap
• Collection of min trees.
• The min trees need NOT be Binomial trees.
 沒有一定
 但,也可以是!
•不支援search!!!
Node Structure
• Data
• Child
• Left and Right Sibling
 Used for circular doubly linked list of siblings.
• Parent
 Pointer to parent node.
• ChildCut flag
 True if node has lost a child since it became a child of its
current parent.
 Set to false by remove min, which is the only operation that
makes one node a child of another.
 Undefined for a root node.
Fibonacci Heap Representation
2
1
4
4
6
5
7
9
7
6
8
5
5
8
6
9
9
• Parent and ChildCut fields not shown.
3
A
Delete(theNode)
• theNode points to the Fibonacci heap node that
contains the element that is to be deleted. //假設有個方
式 (是甚麼方式?) 可以找到要被delete的theNode
• theNode points to min element => do a delete min.
 In this case, complexity is the same as that for delete
min.
Delete(theNode)
6
1
5
theNode
2
4
6
5
7
6
7
4
9
10
5
3
8
9
8
Remove theNode from its doubly linked sibling list.
9
Delete(theNode)
1
7
2
4
6
5
7
9
6
5
3
8
6
8
Combine top-level list and children of theNode.
9
4
10
5
9
Delete(theNode)
10
1
5
6
8
9
2
4
5
7
6
7
9
8
6
3
5
9
DecreaseKey(theNode, theAmount)
6
1
5
theNode
2
4
6
8
5
7
6
7
4
9
10
5
3
9
8
9
If theNode is not a root and new key < parent
key, remove subtree rooted at theNode from its
doubly linked sibling list.
Insert into top-level list.
DecreaseKey(theNode, theAmount)
0
10
1
5
2
9
4
6
8
7
5
7
6
9
8
6
3
5
9
Cascading Cut
• When theNode is cut out of its sibling list in a remove or
decrease key operation, follow path from parent of theNode to
the root.
• Encountered nodes (other than root) with ChildCut = true are cut
from their sibling lists and inserted into top-level list.
• Stop at first node with ChildCut = false.
 For this node, set ChildCut = true.
• Note: ChildCut becomes “false” if being merged due to delete
min (or delete)
 The ChildCut is associated with the root of some F-heap in the top-level
list (這個root是當時被cut出來的,當然其整個子樹也都跟著被帶出來)
Cascading Cut Example
1
2 F
3
7
8
T
5
T 7
6
4
6
9
8
theNode
9
8
9
Decrease key by 2.
Cascading Cut Example
1
6
2 F
3
7
8
T
5
T 7
6
4
6
9
9
8
9
Cascading Cut Example
1
6
2 F
3
7
8
4
6
T
5
6
9
9
7
8
9
Cascading Cut Example
1
7
8
3
6
2 F
5
6
9
9
4
7
8
9
6
Cascading Cut Example
1
7
8
3
6
2 T
5
9
9
4
7
8
6
9
6
Actual complexity of cascading cut is O(h) = O(n).
//Why?
或者,正比於insert+decrease op’s的個數 (自從上
一次delete min或delete後) // amortized cost analytic
的基礎
Do ops to create a B2 (height = 3).
Now do a decrease key to get a chain
of height (length) 3 and a B0. Do 2
inserts, a remove min, and a decrease
key to get a chain of height 4 and B0.
We can increase height by 1 each time
we do 2 inserts, a remove min, and a
decrease key. So, height is O(n), where
n is number of operations (or number
of elements in tree).
B2
最近的一次delete min
這
裡
的
樹
最
高
有
多
高
這
裡
的
樹
最
高
有
多
高
Ans.: O(n)
5 nodes當中竟有
一個歪斜的樹含
有四個節點
假設這裡還有一次delete min
底下討論要花一點時間…
• 為甚麼Fibonacci?
• 為甚麼一個internal node被cut兩個children (為甚麼不是
一個,三個) 就也要跟著被cut?
• 這一切都是因為要維持如下質量:
k的degree的F-heap需要至少有(some base number)Ө(k)的
節點個數,i.e., 指數數量的節點個數; here, Ө(k)=ak+b
(where a=1, b=-2)
補充: O(k)是指小於等於ak+b這個等級的數量,而Ө(k)是指等於ak+b,這裡k
可以任意大,但a,b是兩個常數
Fibonacci Number
• Fibonacci number: definition
• Examples
• Closed from:
黃金比例
關鍵項
• 大約F3項之後以黃金比例成長
• F0=0, F1=1, F2=1, F3=2
• Fn近似F3*1.618n-3,當n大於3
Fn指數成長
Why We Name it “Fibonacci” Heap?
問:考慮一b-heap,當其經歷若干個delete (any), decrease key之後,則該bheap (degree不變下) 所形成的min tree,至少會有幾個節點?
答:經歷上述操作,考慮degree分別為0, 1, 2, 3,…的min trees如下
例如:degree為3的b-heap (B3), 執行上述動作前為
節點數
1
F2
2
F3
3
F4
5
F5
8
F6
13
F7
再看一個F-Heap (starting from B4)
被移出若干個節點後的樣子…
T
T
B4
T
y
x
y
x T
F6
T
問:考慮一b-heap,當其經歷若干個delete (any), decrease key之後,則該bheap (degree不變下) 所形成的min tree,至少會有幾個節點?
答:Fibonacci number
F6
• Fibonacci numbers的特性:
 F6=F5+F4 (by definition)
 F7=F5+F4+F3+F2+F1+1 (自行證明,目測見圖右)
近似B-Heap在節點 “數量” 上
的質量
F4
F3
F2
B2
T
F3 F2
F3
存在F4的“子”結構
F3
F2
F2
F2
Max Degree?
Fn近似F3*1.618n-3,當n大於3
• …
• F6的degree=4
• F7的degree=5
• …
n=max degree最大多少?
 n 近似於(log1.618(系統總結點的個數))-2
 O(log系統總結點的個數)
More on Cascading Cut
• F-Heap (starting from B4) 被移出若干個節點後的樣子…(下圖左)
• 下圖右,除了root以外的節點,只要當中任一個再被移出一次,則原來的
F6馬上變F5
這個家族至少會有的節點數,例如從F6移出x,則y也會相
應移出,得到了至少F5會有的節點數
• 目的:Cascading cut試圖控制每一個生成在top-level中F?的節點數的質量
,如下圖右,要不F6家族要不F5家族,被移出的子樹也會是某一F?家族
T
T
B4
T
y
x
y
x T
F6
T
More on Cascading Cut (cont’d)
• When a parent p removes one of its child y1 (a subtree
rooted at x), the subtree y2 of p are also cut if any p’s
descendant subtree (say, x) is further eliminated
Resulting in 3rd cut
p
1st cut
T
2nd cut
T
y1
T
x
Fk-1=Fk-2+Fk-3
y1
T
y2
x T
為什麼cut兩次subtrees (y1 and
y2) 後,就cut the entire p’s
subtree? “因為萬一最大的兩
個p’s subtrees被cut…則p剩餘
的節點數將不足原來的1/2!”
Recall: Fi≈1.618Fi-1 (for any i)
p的節點數Fk+1=Fk+Fk-1
y1的節點數Fk-1
y2的節點數Fk-2
y1+y2的節點數Fk =Fk-1+Fk-2
Fk=Fk-2+Fk-3+…+F1+1 到此為止,陸續生成了Fk-1及
y2
Fk-2兩個top-level F-heaps
最後,p可能是p’s parent最大的
subtree,則我們就移除p (from p’s
parent),使p’s parent從某個Ft變成
Ft-1
//從y1的角度 (為了方便藉由同個案例討論
)
//觀察y1何以失去兩個 children就需被移開
比如我們觀察p和y1,p將失去y1
。反之,如果p仍持續與y1保持父
子關係,當y1可能失去了Fk-2的節
點數,y1剩下的Fk-3節點數對右側
橘色處Fk並不會產生關鍵性的貢
獻,因為…
• 當y1失去大於1/2的節點 //目前是這種狀況 (y1不適合當p的subtree)
Fk-2+Fk-3 +Fk-3 +…=Fk-1 +Fk-1=Fk-1+1.618Fk-2=Fk+0.618Fk-2 <<Fk+1 ( “未達” p這個subtree期待
的節點數)
• 當y1失去小於1/2的節點 //不是這種狀況 (y1仍可以是p的subtree)
Fk-2+Fk-2 +Fk-3 +…>(1.618)2 Fk-2+Fk-3 +… =(1.618)2 Fk-2+Fk-1 ≈(1.618)Fk-1+Fk-1 ≈ Fk +Fk-1=Fk+1
( “達” p這個subtree期待的節點數)
補充
• F-heap的機械化操作 (e.g., cascading cut)
不難被實現
• 背後的道理是有學問的地方
• 碼農:從事沒有價值的程式碼,或代工
去按規格完成的程式碼的工作
Consolidation
due to Delete Min
• Recall: merge 2 Bi’s with identical degree
in B-heap, iteratively
• How about consolidation in F-heap?
 Say, merge F5 and F4, resulting in F6
• Refer to “Fibonacci number def.”
 In fact, you may also merge 2 F5’s, resulting in
something larger than F6 (in terms of # of nodes)
• Prior merging algorithm remains to work
Fibonacci Heaps (B-Heaps are
Special Cases of F-Heaps)
Insert
Actual
O(1)
Amortized
O(1)
Delete min (or max)
O(n)
O(log n)
Meld
O(1)
O(1)
Delete any
O(n)
Decrease key (or
increase)
O(n)
// due to
cascading
cut
O(log n)
// 同 delete min
O(1)
// see P. 17
Height of F-Heap
• O(log n)
 “若” 存在透過若干個subtrees (每個都是Fheap) 來分割 (disjoint partitions) 整個Fheap
 大部分的時候是O(log n)
• 經過若干如前述操作 (insert, delete (min)
或decrease等動作混雜),則試問the
probability of height of O(n)?
 在兩次delete (或delete min)之間
• 節點來來 (insert) 去去 (decrease),整體需
進出Ө(n)的節點數
• 期間最好不要發生cascading cut
Download