fibonacci heap

25
Fibonacci Heap Fibonacci Heap BH&FH BH&FH

Upload: leone

Post on 09-Jan-2016

78 views

Category:

Documents


2 download

DESCRIPTION

Fibonacci Heap. BH&FH. Why BH&FH -- 程式目的. MaxPQ. MinPQ. Symmetric Max Data Structures. Min Heap. DEPQ. Deap. MinMax. Mergeable MinPQ. Min-Leftist. Min-Skew. Min-B-Heap. Min-F-Heap. Inheritance. ADT. 實作方法. Why BH&FH -- 舉例. Server for one priority shuts down 。 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Fibonacci Heap

Fibonacci HeapFibonacci HeapBH&FHBH&FH

Page 2: Fibonacci Heap

Why BH&FH -- Why BH&FH -- 程式目的程式目的MinPQMinPQ

Mergeable MinPQMergeable MinPQ

MaxPQMaxPQ

DEPQDEPQ

Min-Leftist Min-Skew Min-B-Heap

Min-F-Heap

Min Heap

Deap MinMaxSymmetric

Max

Data

Structures

Symmetric

Max

Data

Structures

Inheritance ADT 實作方法

Page 3: Fibonacci Heap

Why BH&FH -- Why BH&FH -- 舉例舉例

Server for one priority shuts downServer for one priority shuts down 。。

Efficient algorithms for Shortest-path prEfficient algorithms for Shortest-path problem and finding minimum spanning troblem and finding minimum spanning treesees 。。 (Improved network optimization)(Improved network optimization)

Page 4: Fibonacci Heap

What’s BH&FH -- DefinitionWhat’s BH&FH -- Definition A binomial heap is a collection of binomial treA binomial heap is a collection of binomial tre

es.es. The The binomial tree binomial tree Bk Bk is an ordered tree defineis an ordered tree define

d recursively. the binomial tree d recursively. the binomial tree BB0 consists of 0 consists of a single node. The binomial tree a single node. The binomial tree Bk Bk consists of consists of two binomial trees two binomial trees BkBk-1 that are -1 that are linked linked togethtogether: the root of one is the leftmost child of the rer: the root of one is the leftmost child of the rootoot

of the other.of the other. Like a binomial heap, a Like a binomial heap, a Fibonacci heap Fibonacci heap is a cois a co

llection of min-heap-ordered trees. The trees illection of min-heap-ordered trees. The trees in a Fibonacci heap are not constrained to be bn a Fibonacci heap are not constrained to be binomial trees. inomial trees.

Page 5: Fibonacci Heap

BH BH 圖解圖解

Page 6: Fibonacci Heap

FHFH 圖解圖解

Page 7: Fibonacci Heap

提供的函式提供的函式 MakeHeap()MakeHeap() Insert(H,x)Insert(H,x) Minimum(H)Minimum(H) Extract-min(H)Extract-min(H) Union(H1,H2)Union(H1,H2) DecreaceKey(H,x,k)DecreaceKey(H,x,k) Delete(H,x)Delete(H,x) 範例網站範例網站

Binomial Heap

Binary Min-Heap

Fibonacci Heap

Page 8: Fibonacci Heap

How Fibonacci InsertHow Fibonacci Insert FIB-HEAP-INSERT(FIB-HEAP-INSERT(HH, , xx)) 1 1 degreedegree[[xx] ← 0] ← 0 2 2 pp[[xx] ← NIL] ← NIL 3 3 childchild[[xx] ← NIL] ← NIL 4 4 leftleft[[xx] ← ] ← xx 5 5 rightright[[xx] ← ] ← xx 6 6 markmark[[xx] ← FALSE] ← FALSE 7 7 concatenate the root list containing concatenate the root list containing x x with root list with root list HH 8 8 if if minmin[[HH] = NIL or ] = NIL or keykey[[xx] < ] < keykey[[minmin[[HH]]]] 9 9 then then minmin[[HH] ← ] ← xx 10 10 nn[[HH] ← ] ← nn[[HH] + 1] + 1

Page 9: Fibonacci Heap

Fibonacci Insert 21Fibonacci Insert 21

Page 10: Fibonacci Heap

How Fibonacci UnionHow Fibonacci Union FIB-HEAP-UNION(FIB-HEAP-UNION(HH1, 1, HH2)2) 1 1 H H ← MAKE-FIB-HEAP()← MAKE-FIB-HEAP() 2 2 minmin[[HH] ← ] ← minmin[[HH1]1] 3 3 concatenate the root list of concatenate the root list of HH2 with the root list of 2 with the root list of HH 4 4 if if ((minmin[[HH1] = NIL) or (1] = NIL) or (minmin[[HH2] ≠ NIL and 2] ≠ NIL and minmin

[[HH2] < 2] < minmin[[HH1])1]) 5 5 then then minmin[[HH] ← ] ← minmin[[HH2]2] 6 6 nn[[HH] ← ] ← nn[[HH1] + 1] + nn[[HH2]2] 7 free the objects 7 free the objects HH1 and 1 and HH22 8 8 return return HH

Page 11: Fibonacci Heap

How Fibonacci Extract-minHow Fibonacci Extract-min FIB-HEAP-EXTRACT-MIN(FIB-HEAP-EXTRACT-MIN(HH)) 1 1 z z ← ← minmin[[HH]] 2 2 if if z z ≠ NIL≠ NIL 3 3 then for then for each child each child x x of of zz 4 4 do do add add x x to the root list of to the root list of HH 5 5 pp[[xx] ← NIL] ← NIL 6 remove 6 remove z z from the root list of from the root list of HH 7 7 if if z z = = rightright[[zz]] 8 8 then then minmin[[HH] ← NIL] ← NIL 9 9 else else minmin[[HH] ← ] ← rightright[[zz]] 10 CONSOLIDATE(10 CONSOLIDATE(HH)) 11 11 nn[[HH] ← ] ← nn[[HH] - 1] - 1 12 12 return return zz

Page 12: Fibonacci Heap

Extract-min 1Extract-min 1

Page 13: Fibonacci Heap

Extract-min 2Extract-min 2

Page 14: Fibonacci Heap

Extract-min 3Extract-min 3

Page 15: Fibonacci Heap

Extract-min FinalExtract-min Final

Page 16: Fibonacci Heap

How Fibonacci ConsolidateHow Fibonacci Consolidate CONSOLIDATE(CONSOLIDATE(HH)) 1 1 for for i i ← 0 ← 0 to to DD((nn[[HH])]) 2 2 do do AA[[ii] ← NIL] ← NIL 3 3 for for each node each node w w in the root list of in the root list of HH 4 4 do do x x ← ← ww 5 5 d d ← ← degreedegree[[xx]] 6 6 while while AA[[dd] ≠ NIL] ≠ NIL 7 7 do do y y ← ← AA[[dd] Another node with the same degree as ▹] Another node with the same degree as ▹ xx.. 8 8 if if keykey[[xx] > ] > keykey[[yy]] 9 9 then then exchange exchange x x ↔ ↔ yy 10 FIB-HEAP-LINK(10 FIB-HEAP-LINK(HH, , yy, , xx)) 11 11 AA[[dd] ← NIL] ← NIL 12 12 d d ← ← d d + 1+ 1 13 13 AA[[dd] ← ] ← xx 14 14 minmin[[HH] ← NIL] ← NIL 15 15 for for i i ← 0 ← 0 to to DD((nn[[HH])]) 16 16 do if do if AA[[ii] ≠ NIL] ≠ NIL 17 17 then then add add AA[[ii] to the root list of ] to the root list of HH 18 18 if if minmin[[HH] = NIL or ] = NIL or keykey[[AA[[ii]] < ]] < keykey[[minmin[[HH]]]] 19 19 then then minmin[[HH] ← ] ← AA[[ii]] FIB-HEAP-LINK(FIB-HEAP-LINK(HH, , yy, , xx)) 1 remove 1 remove y y from the root list of from the root list of HH 2 make 2 make y y a child of a child of xx, incrementing , incrementing degreedegree[[xx]] 3 3 markmark[[yy] ← FALSE] ← FALSE

Page 17: Fibonacci Heap

How Fibonacci Decrease-KeyHow Fibonacci Decrease-Key FIB-HEAP-DECREASE-KEY(FIB-HEAP-DECREASE-KEY(HH, , xx, , kk)) 1 1 if if k k > > keykey[[xx]] 2 2 then error then error "new key is greater than current key""new key is greater than current key" 3 3 keykey[[xx] ← ] ← kk 4 4 y y ← ← pp[[xx]] 5 5 if if y y ≠ NIL and ≠ NIL and keykey[[xx] < ] < keykey[[yy]] 6 6 then then CUT(CUT(HH, , xx, , yy)) 7 CASCADING-CUT(7 CASCADING-CUT(HH, , yy)) 8 8 if if keykey[[xx] < ] < keykey[[minmin[[HH]]]] 9 9 then then minmin[[HH] ← ] ← xx CUT(CUT(HH, , xx, , yy)) 1 remove 1 remove x x from the child list of from the child list of yy, decrementing , decrementing degreedegree[[yy]] 2 add 2 add x x to the root list of to the root list of HH 3 3 pp[[xx] ← NIL] ← NIL 4 4 markmark[[xx] ← FALSE] ← FALSE CASCADING-CUT(CASCADING-CUT(HH, , yy)) 1 1 z z ← ← pp[[yy]] 2 2 if if z z ≠ NIL≠ NIL 3 3 then if then if markmark[[yy] = FALSE] = FALSE 4 4 then then markmark[[yy] ← TRUE] ← TRUE 5 5 else else CUT(CUT(HH, , yy, , zz)) 6 CASCADING-CUT(6 CASCADING-CUT(HH, , zz))

Page 18: Fibonacci Heap

Fibonacci Decrease-KeyFibonacci Decrease-Key

Page 19: Fibonacci Heap

How Fibonacci DeleteHow Fibonacci Delete

FIB-HEAP-DELETE(FIB-HEAP-DELETE(HH, , xx)) 1 FIB-HEAP-DECREASE-KEY(1 FIB-HEAP-DECREASE-KEY(HH, , xx, -∞), -∞) 2 FIB-HEAP-EXTRACT-MIN(2 FIB-HEAP-EXTRACT-MIN(HH))

Page 20: Fibonacci Heap

How Binomial DeleteHow Binomial Delete

BINOMIAL-HEAP-DELETE(BINOMIAL-HEAP-DELETE(HH, , xx)) 1 BINOMIAL-HEAP-DECREASE-KEY(1 BINOMIAL-HEAP-DECREASE-KEY(HH, , xx, -∞), -∞) 2 BINOMIAL-HEAP-EXTRACT-MIN(2 BINOMIAL-HEAP-EXTRACT-MIN(HH))

Page 21: Fibonacci Heap

Note -- Time ComplexityNote -- Time Complexity

Page 22: Fibonacci Heap

Note -- DifferenceNote -- Difference

1 Delete1 Delete DecreaseKeyDecreaseKey 2 Binomial2 Binomial :: OrderedOrdered FibonacciFibonacci :: UnorderedUnordered

Page 23: Fibonacci Heap

Note -- ApplicationNote -- Application

Shortest-pathShortest-path

Combine PQCombine PQ

Page 24: Fibonacci Heap

Fibonacci Heap Fibonacci Heap

習題習題

Page 25: Fibonacci Heap

1.1. 請建立一個空的請建立一個空的 Fibonacci HeapFibonacci Heap ,並,並 insertinsert 以以下數值為以以下數值為 keykey 的節點的節點 7070 、、 8080 、、 9090 、、 3030 、、 4040 、、 5050 、、 2020 、、 6060 (hint: insert(hint: insert 至哪個位置並無差別至哪個位置並無差別 ))2.extractMin2.extractMin3.3. 將將 8080 的的 keykey ,, Decrease KeyDecrease Key 為為 10104.4. 給一給一 Fibonacci HeapFibonacci Heap 如圖,請將兩者如圖,請將兩者 UnionUnion