Download - Fibonacci Heap

Transcript
Page 1: Fibonacci Heap

Fibonacci Heap

Page 2: Fibonacci Heap

p2.

ProcedureBinary heap

(worst-case)

Binomial heap

(worst-case)

Fibonacci heap

(amortized)

MAKE-HEAP (1) (1) (1)

INSERT (lg n) O(lg n) (1)

MINIMUM (1) O(lg n) (1)

EXTRACT-MIN (lg n) (lg n) O(lg n)

UNION (n) O(lg n) (1)DECREASE-KEY (lg n) (lg n) (1)

DELETE (lg n) (lg n) O(lg n)

Page 3: Fibonacci Heap

p3.

(a) 23 7

4139

24

30

17

3818 52 26

3

46

35

H.min

(b) 23 7

4139

24

30

17

3818 52 26

3

46

35

H.min

Page 4: Fibonacci Heap

p4.

(a) 23 7

4139

24

30

17

3818 52 26

3

46

35

H.min

H.min(b) 23 7

4139

24

30

17

3818 52 26

3

46

35

21

Insert key 21

Page 5: Fibonacci Heap

p5.

Fibonacci Heaps:A collection of heap-ordered trees.trees: rooted but unorderedEach node x: x.p points to its parent x.child points to any one of its childrenchildren of x are linked together in a circular doubly

linked listx.Left, x.right: points to its left and right siblings.x.degree: number of children in the child list of xx.mark: indicate whether node x has lost a child since

the last time x was mode the child of another nodeH.min: points to the root of the tree containing a

minimum keyH.n: number of nodes in H

Page 6: Fibonacci Heap

p6.

Potential function:

D(n): upper bound on the max degree of any node in an n-node Fibonacci heap

Fibonacci heap # of trees in the rooted list of H

# of marked nodes in H (H) = t(H) + 2m(H)

Page 7: Fibonacci Heap

p7.

Unordered binomial tree:U0: a single nodeUk: consists of 2 unordered binomial trees Uk-1 for

which the root of one is made into any child of the root of the other

若只用 Make-Heap, Insert, Minimum, Extract-Min & Union.

Fibonacci heap 只含 unordered binomial trees.

Make-Heap, Make-Fib-Heap(H):Allocate and return the Fibonacci heap object H with H.n=0 and H.min=nil t(H)=0 , m(H)=0 so (H)=0

The amortized cost of Make-Fib-Heap is equal to its O(1) actual cost.

Page 8: Fibonacci Heap

p8.

Fib-Heap-Insert(H, x)1. x.degree = 02. x.p = NIL3. x.child = NIL4. x.mark = FALSE5. if H.min == NIL 6. create a root list for H containing just x7. H.min = x8. else insert x into H’s root list9. if x.key < H.min.key10. H.min = x11. H.n = H.n +1 Actual cost: O(1); Amortized cost: O(1) + 1

Page 9: Fibonacci Heap

p9.

Finding the minimum node:min[H] O(1) Amortized cost O(1)

is not changed

Uniting 2 Fibonacci heaps:Fib-Heap-Union(H1, H2)

1. H = Make-Fib-Heap( )2. H.min = H1.min3. concatenate the root list of H2 with the root list of H4. if (H1.min == NIL) or (H2.min NIL and

H2.min.key<H1.min.key)5. H.min = H2.min6. H.n = H1.n + H2.n7. return H

Page 10: Fibonacci Heap

p10.

(H) - ((H1)+(H2))= (t(H)+2m(H)) – ((t(H1)+2m(H1)) +

(t(H2)+2m(H2))) = 0t(H) = t(H1) + t(H2) , m(H) = m(H1) + m(H2)

Thus the amortized cost of Fib-Heap-Union is therefore O(1) actual cost

Page 11: Fibonacci Heap

p11.

Extracting the minimum node:Fib-Heap-Extract-Min(H)

1. z = H.min2. if z NIL3. for each child x of z4. do { add x to the root list of H5. x.p = NIL }6. remove z from the root list of H7. if z == z.right8. H. min = NIL9. else H.min = z.right10. Consolidate(H)11. H.n = H.n – 112. return z

Page 12: Fibonacci Heap

p12.

Fib-Heap-Link(H, y, x) {1. remove y from the root list of H; 2. make y a child of x; x.degree =x.degree+1; 3. y.mark = FALSE; }

Consolidate(H)1. let A[0..D(H.n)] be a new array2. for i = 0 to D(n[H]) do A[i]=NIL3. for each node w in the root list of H 4. do { x = w ; d = x.degree;5. while A[d] NIL6. do { y = A[d]7. if x.key > y.key exchange xy8. Fib-Heap-Link(H, y, x)9. A[d] = NIL ; d = d+1; }10. A[d] = x; }11. H.min = NIL12. for i = 0 to D(H.n) do13. if A[i] NIL 14. if H.min==NIL15. create a root list for H containing just A[i]; H.min

=A[i];16. else insert A[i] into H’s root list17. if A[i].key < H.min.key H.min = A[i]

Page 13: Fibonacci Heap

p13.

(a) 23 7

4139

24

30

17

3818 52 26

3

46

35

21

(b) 23 7 24

26 46

35

39

18

41

38

30

1721 52

H.min

H.min

Page 14: Fibonacci Heap

p14.

(c) 23 7 24

26 46

35

39

18

41

38

30

1721 52

0 1 2 3 4A

w,x

0 1 2 3 4A

(d) 23 7 24

26 46

35

39

18

41

38

30

1721 52w,x

Page 15: Fibonacci Heap

p15.

0 1 2 3 4A

(e) 23 7 24

26 46

35

39

18

41

38

30

1721 52w,x

(f)

23

7

0 1 2 3 4A

24

26 46

35

39

18

41

38

30

1721 52x

w

Page 16: Fibonacci Heap

p16.

0 1 2 3 4A

(g)

23

7 24

26 46

35

39

18

41

38

30

17

21 52x

w

35

(h)

0 1 2 3 4A

23

7

39

18

41

38

30

1724

26 46

21 52x

w

Page 17: Fibonacci Heap

p17.

0 1 2 3 4

35

(i)

A

23

7

39

18

41

38

30

1724

26 46

21 52w, x

0 1 2 3 4

35

(j)

A

23

7

39

18

41

38

30

1724

26 46

21 52w, x

Page 18: Fibonacci Heap

p18.

0 1 2 3 4

35

(k)

A

23

7

39

18

41

38

30

1724

26 46

21

52

w, x

0 1 2 3 4

35

(l)

A

23

7

39

18

41

38

30

1724

26 46

21

52

w, x

Page 19: Fibonacci Heap

p19.

35

(m)

23

7

39

18

41

38

30

1724

26 46

21

52

H.min

Page 20: Fibonacci Heap

p20.

Decreasing a key and deleting a node:do not preserve the property that all trees in

theFibonacci heap are unordered binomial trees.

Fib-Heap-Decrease-key(H, x, k)1. if k>x.key 2. error “new key is greater than current key”3. x.key = k4. y x.p5. if yNIL and x.key< y.key6. { CUT(H, x, y)7. CASCADING-CUT(H, y) }8. if x.key< H.min.key9. H.min = x

Page 21: Fibonacci Heap

p21.

CUT(H, x, y)1. remove x from the child list of y, decrease y.degree2. add x to the root list of H3. x.p = NIL4. x.mark = FALSE

CASCADING-CUT(H, y)1. z y.p2. if zNIL 3. if y.mark == FALSE 4. y.mark= TRUE5. else CUT(H, y, z)6. CASCADING-CUT(H, z)

Fib-Heap-Delete(H, x) { Fib-Heap-Decrease-key(H, x, -) Fib-Heap-Extract-Min(H) }

Page 22: Fibonacci Heap

p22.

35

(a)

23

7

39

18

41

38

30

1724

26 46

21

52

H.min

(b) 15

35

26

23

7

41

38

30

1724 39

18

21

52

H.min

Page 23: Fibonacci Heap

p23.

(c) 15

26

23

7

41

38

30

1724 39

18

21

52

H.min

5

(d) 15

41

38

39

18

21

52

23

7

30

1724

H.min

5 26

Page 24: Fibonacci Heap

p24.

(e) 15

41

38

39

18

21

52

23

7

30

17

5 26

H.min

24

Page 25: Fibonacci Heap

p25.

Analysis of Decrease-key:Actual cost : O(c) suppose CASCADING-CUT is called

c times Each recursive call of CASCADING-CUT except for

the last one, cuts a marked node and clears the mark bit.

After Decrease-key, there are at most t(H)+c trees, and at most m(H)-c+2 marked nodes. Last call of CASCADING-CUT may

have marked a nodeThus; the potential change is :[t(H)+c+2(m(H)-c+2)] - [t(H)+2m(H)]= 4-cAmortized cost: O(c)+4-c = O(1)

By scaling up the units of potential to dominate the constant hidden in O(c)

Page 26: Fibonacci Heap

p26.

Analysis of Fib-Heap-Extract-Min:H : n-node Fib-HeapActual cost :

O(D(n)) : for-loop in Fib-Heap-Extract-MinD(n)+t(H)-1 : size of the root list

Total actual cost:O(D(n))+t(H)

Potential before extracting : t(H)+2m(H)Potential after extracting : D(n)+1+2m(H)

At most D(n)+1 nodes remain on the list and no nodes become marked

Thus the amortized cost is at most:O(D(n))+t(H)+[(D(n)+1+2m(H)) – (t(H)+2m(H))]= O(D(n)+t(H)-t(H))= O(D(n))

Page 27: Fibonacci Heap

p27.

Bounding the maximum degree:

Goal : D(n) logn , =

Let size(x) be the number of nodes, including x itself, in the subtree rooted at x

(1 5) / 2

Page 28: Fibonacci Heap

p28.

Lemma 1x : any node in a Fibonacci heap and x.degree=ky1, …, yk : children of x in the order in which they were

linked to x. (from the earliest to the latest)Then, y1.degree 0 and yi.degree i-2 for i=2,3,…,k

Pf:Clearly, y1.degree 0For i 2, note that when yi was linked to x, all of y1, …, yi-1

were children of x, so we MUST have had x.degree i-1.Node yi is linked to x only if x.degree = yi.degree,thus yi.degree i-1 at that time.Since then, node yi has lost at most ONE child, since it would have been cut from x if it had lost two children.We conclude that yi.degree i-2

x

y1 y2yk

Page 29: Fibonacci Heap

p29.

Lemma 2:For all integer k0,

pf:By induction on kk=0, F2=F1+F0=1 = 1+F0

Suppose

Fibonacci number:

k

k 1 k 2

0 if k 0F 1 if k 1

F F if k 2

k

k 2 ii 0

F 1 F

k 1

k 1 ii 0

F 1 F

k 1 k

k 2 k k 1 k i ii 0 i 0

F F F F 1 F 1 F

k

k 2F , (1 5) / 2 1.618

Page 30: Fibonacci Heap

p30.

Lemma 3:x: any node in a Fibonacci heap, and let

k=x.degreeThen, size(x) Fk+2 k

pf:Sk : denote the min possible value of

size(z) over all nodes z such that z.degree=k.Trivially, S0=1, S1=2, and S2=3Sk size(x) , size(y1) 1size(x) Sk

2+i=2,…,k Si-2

By induction on k that SkFk+2 Clearly for k=0 and 1

Assume that k2 and that SiFi+2 for i=0,…,k-1

We have Sk 2+i=2,…,k Si-2 2+i=2,…,k Fi

= 1+ i=0,…,k Fi = Fk+2

Thus, size(x) Sk Fk+2 k

x

y1 y2yk

S0

Sk-2

Page 31: Fibonacci Heap

p31.

Corollary 4:The max degree D(n) of any node in an n-

nodeFibonacci heap is O(lg n)

pf:x: any node in an n-node Fibonacci heapk=degree[x]n size(x) k

logn kThus the max degree D(n) of any node is O(lg

n)


Top Related