fibonacci heap

24
Fibonacci Heap

Upload: kuper

Post on 09-Jan-2016

48 views

Category:

Documents


0 download

DESCRIPTION

Fibonacci Heap. Fibonacci Heaps: A collection of heap-ordered trees. trees : rooted but unordered Each node x : P[x] points to its parent child[x] points to any one of its children,children of x are linked together in a circular doubly linked list - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Fibonacci Heap

Fibonacci Heap

Page 2: Fibonacci Heap

p2.

Procedure

Binary 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.

Fibonacci Heaps:A collection of heap-ordered trees.trees: rooted but unorderedEach node x: P[x] points to its parentchild[x] points to any one of its children,children of x

are linked together in a circular doubly linked listdegree[x]: number of children in the child list of xmark[x]: indicate whether node x has lost a child

since the last time x was mode the child of another node

min[H]: points to the root of the tree containing a minimum key

n[H]: number of nodes in H

Page 4: Fibonacci Heap

p4.

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 5: Fibonacci Heap

p5.

(a) 23 7

4139

24

30

17

3818 52 26

3

46

35

min[ H ]

(b) 23 7

4139

24

30

17

3818 52 26

3

46

35

min[ H ]

Page 6: Fibonacci Heap

p6.

Unordered binomial tree:U0: a single node

Uk: 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.

Page 7: Fibonacci Heap

p7.

Fib-Heap-Insert(H, x){ degree[x] 0

P[x] NILchild[x] NILleft[x] x ; right[x] xmark[x] FALSEconcatenate the root list containing x with

root list Hif min[H] = NIL or key[x]<key[min[H]]

then min[H] xn[H] n[H]+1

}

Page 8: Fibonacci Heap

p8.

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

is not changed

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

{ H Make-Fib-Heap[] min[H] min[H1]

concatenate the root list of H2 with the root list of H

if (min[H1]=NIL) or (min[H2] NIL and min[H2]<min[H1])

then min[H] min[H2]

n[H] n[H1]+n[H2]

free the objects H1 and H2

return H}

Page 9: Fibonacci Heap

p9.

(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 10: Fibonacci Heap

p10.

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

{ z min[H] if z NIL

then { for each child x of z do { add x to the root list of H P[x] NIL }

remove z from the root list of H if z = right[z]

then min[H] NIL else min[H] right[z] Consolidate(H)

n[H] n[H] – 1 }

return z }

Page 11: Fibonacci Heap

p11.

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

Consolidate(H){ for i 0 to D(n[H]) do A[i]=NIL

for each node w in the root list of H do { x w ; d degree[x] ;

while A[d] NIL do { y A[d]

if key[x]>key[y] then exchange xyFib-Heap-Link(H, y, x)A[d] NIL ; d d+1 }

A[d] x } min[H] NIL

for i 0 to D(n[H]) do if A[i] NIL then { add A[i] to the root list of H ;

if min[H]=NIL or key[A[i]]<key[min[H]]

then min[H] A[i] } }

Page 12: Fibonacci Heap

p12.

(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

min[ H ]

min[ H ]

Page 13: Fibonacci Heap

p13.

(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 14: Fibonacci Heap

p14.

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 15: Fibonacci Heap

p15.

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 16: Fibonacci Heap

p16.

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 17: Fibonacci Heap

p17.

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 18: Fibonacci Heap

p18.

35

(m)

23

7

39

18

41

38

30

1724

26 46

21

52

min[ H ]

Page 19: Fibonacci Heap

p19.

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){ if k>key[x] then error “new key is greater than current key” key[x] k

y P[x] if yNIL and key[x]<key[y] then { CUT(H, x, y)

CASCADING-CUT(H, y) } if key[x]<key[min[H]] then min[H] x

}

Page 20: Fibonacci Heap

p20.

CUT(H, x, y){ remove x from the child list of y, decrease degree[y]

add x to the root list of HP[x] NILmark[x] FALSE }

CASCADING-CUT(H, y){ z P[y]

if zNIL then { if mark[y]=FALSE then mark[y] TRUE

else { CUT(H, y, z)CASCADING-CUT(H, z) }

}}

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

Page 21: Fibonacci Heap

p21.

35

(a)

23

7

39

18

41

38

30

1724

26 46

21

52

min[ H ]

(b) 15

35

26

23

7

41

38

30

1724 39

18

21

52

min[ H ]

Page 22: Fibonacci Heap

p22.

(c) 15

26

23

7

41

38

30

1724 39

18

21

52

min[ H ]

5

(d) 15

41

38

39

18

21

52

23

7

30

1724

min[ H ]

5 26

Page 23: Fibonacci Heap

p23.

(e) 15

41

38

39

18

21

52

23

7

30

17

5 26

min[ H ]

24

Page 24: Fibonacci Heap

p24.

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 node

Thus; 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)