09 fibonacci heaps - tum · fibonacci heaps „lazy-meld“ version of binomial queues: the melding...

29
09 Fibonacci Heaps

Upload: others

Post on 09-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

09 – Fibonacci Heaps

Page 2: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

2WS 2018/19

Priority queues: operations

Priority queue Q

Data structure for maintaining a set of elements, each having a priority

from a totally ordered universe (U,≤). The following operations are

supported.

Operations:

Q.initialize(): initializes an empty queue Q

Q.isEmpty(): returns true iff Q is empty

Q.insert(e): inserts element e into Q and returns a pointer to the node

containing e

Q.deletemin(): returns the element of Q with minimum key and deletes it

Q.min(): returns the element of Q with minimum key

Q.decreasekey(v,k): decreases the value of v‘s key to the new value k

© S. Albers

Page 3: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

3WS 2018/19

Priority queues: operations

Additional operations:

Q.delete(v): deletes node v and its element from Q

(without searching for v)

Q.meld(Q´): unites Q and Q´ (concatenable queue)

Q.search(k): searches for the element with key k in Q

(searchable queue)

And many more, e.g. predecessor, successor, max, deletemax

© S. Albers

Page 4: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

4

Priority queues: implementations

List Heap Bin. – Q. Fib.-Hp.

insert O(1) O(log n) O(log n) O(1)

min O(n) O(1) O(log n) O(1)

delete-

minO(n) O(log n) O(log n) O(log n)*

meld

(mn)O(1)

O(n) or

O(m log n)O(log n) O(1)

decr.-key O(1) O(log n) O(log n) O(1)*

*= amortized costQ.delete(e) = Q.decreasekey(e, - ) + Q.deletemin( )

WS 2018/19 © S. Albers

Page 5: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

5

Reminder: Binomial queues

Binomial tree Bn of order n (n 0)

B0 Bn+1

Bn

Bn

WS 2018/19 © S. Albers

Page 6: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

6

Binomial trees

B1 B2 B3B0

Binomial queue Q:

Set of heap ordered binomial trees of different order to store keys.

WS 2018/19 © S. Albers

Page 7: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

7

Fibonacci heaps

„Lazy-meld“ version of binomial queues:

The melding of trees having the same order is delayed until the next

deletemin operation.

Definition

A Fibonacci heap Q is a collection heap-ordered trees.

Variables

Q.min: root of the tree containing the minimum key

Q.rootlist: circular, doubly linked, unordered list containing the roots

of all trees

Q.size: number of nodes/elements currently in Q

WS 2018/19 © S. Albers

Page 8: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

8WS 2018/19

Trees in Fibonacci heaps

Let B be a heap-ordered tree in Q.rootlist:

B.childlist: circular, doubly linked and unordered list of the children of B

Every node in a Fibonacci heap has a pointer to one child, if it exists.

Children are stored in circular, doubly linked, unordered list

Structure of a node left

parent

rightkey degree

child mark

Advantages of circular, doubly linked lists:

1. Deleting an element takes constant time.

2. Concatenating two lists takes constant time.© S. Albers

Page 9: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

9

Implementation: Example

WS 2018/19 © S. Albers

71

71

Page 10: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

10WS 2018/19

Operations on Fibonacci heaps

Q.initialize()

Q.rootlist := null; Q.min := null; Q.size := 0;

Q.min()

return Q.min.key;

Q.insert(e)

Generate a new node with element e;

Insert the node into the rootlist of Q and update Q.min;

Q.meld(Q´)

Concatenate Q.rootlist and Q´.rootlist;

Update Q.min;

© S. Albers

Page 11: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

11WS 2018/19

Operation ‘deletemin’

Q.deletemin()

/*Delete the node with minimum key from Q and return its element.*/

1. m := Q.min();

2. if Q.size() > 0 then

3. Remove Q.min() from Q.rootlist;

4. Add Q.min.childlist to Q.rootlist;

5. Q.consolidate();

/*Repeatedly meld nodes in the root list having the same

degree. Then determine the element with minimum key. */

6. return m;

© S. Albers

Page 12: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

12WS 2018/19

Maximum degree of a node

rank(v) = degree/number of children of node v in Q

rank(Q) = maximum degree of any node in Q

Assumption:

rank(Q) 2 log n

if Q.size = n.

© S. Albers

Page 13: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

13

Operation ‘link’

B B´

link

B

1. rank(B) := rank(B) + 1

2. B´.mark := false

rank(B) = degree of the root of B

Heap-ordered trees B,B´ with rank(B) = rank(B´)

WS 2018/19 © S. Albers

Page 14: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

14

Consolidation of the root list

WS 2018/19 © S. Albers

71

71

Page 15: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

15

Consolidation of the root list

WS 2018/19 © S. Albers

71

71

Page 16: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

16

Operation ‘deletemin’

Find roots having the same rank:

Array A:

0 1 2 log n

Q.consolidate()

1. A = array of length 2 log n pointing to roots of trees in the

Fibonacci heap;

2. for i = 0 to 2 log n do A[i] = null;

3. while Q.rootlist do

4. B := Q.delete-first();

5. while A[rank(B)] null do

6. B´ := A[rank(B)]; A[rank(B)] := null; B := link(B,B´);

7. end while;

8. A[rank(B)] = B;

9. end while;

10. determine Q.min;WS 2018/19 © S. Albers

Page 17: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

17

Operation ‘decreasekey’: example

WS 2018/19 © S. Albers

Page 18: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

18

Operation ‘decreasekey’

Q.decreasekey(v,k)

1. if k > v.key then return;

2. v.key := k;

3. update Q.min;

4. if v Q.rootlist or k v.parent.key then return;

5. repeat /* cascading cuts */

6. parent := v.parent;

7. Q.cut(v);

8. v := parent;

9. until v.mark = false or v ∈ Q.rootlist;

10. if v Q.rootlist then v.mark = true;

WS 2018/19 © S. Albers

Page 19: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

19WS 2018/19

Operation ‘cut’

Q.cut(v)

1. if v Q.rootlist

2. then /* cut the link between v and its parent */

3. rank (v.parent) := rank (v.parent) – 1;

4. Remove v from v.parent.childlist;

5. v.parent := null;

6. Add v to Q.rootlist;

© S. Albers

Page 20: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

20WS 2018/19

Marks

History of a node:

v is being linked to a node v.mark := false

a child of v is cut v.mark := true

a second child of v is cut cut v

The boolean value mark indicates whether node v has

lost a child since the last time v was made the child of

another node.

© S. Albers

Page 21: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

21WS 2018/19

Rank of the children of a node

Lemma

Let v be a node in a Fibonacci-Heap Q. Let u1,...,uk denote the childrenof v in the order in which they were linked to v. Then

rank(ui ) i - 2.

Proof:

At the time when ui was linked to v:

# children of v (rank(v)): i - 1

# children of ui (rank(ui)): i - 1

# children ui may have lost: 1

© S. Albers

Page 22: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

22WS 2018/19

Maximum rank of a node

Theorem

Let v be a node in a Fibonacci heap Q, and let rank(v) = k . Then v is

the root of a subtree that has at least Fk+2 nodes.

F0 = 0 F1 = 1 Fk+1 = Fk-1 + Fk Fk+2 ≥ Φk Φ = (1 + 5)/2 ≈ 1.618

Golden Ratio

The number of descendants of a node grows exponentially in the

number of children.

Implication: The maximum rank k of any node v in a Fibonacci heap Q

with n nodes is upper bounded by 2 log n.

Φk ≤ n ⇒ k ≤ log2n / log2Φ < 1.45 log2n

© S. Albers

Page 23: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

23WS 2018/19

Maximum rank of a node

Proof of the Theorem:

Sk = minimum possible size of a subtree whose root has rank k

S0 = 1 = F2

S1 = 2 = F3

There holds:

Fibonacci numbers:

(1) + (2) + induction ⇒ Sk Fk+2

2

0

(1) 2for 2k

iik

kSS

k

k

i

ik

FFF

FF

10

0

2

1

(2) 1

© S. Albers

Page 24: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

24WS 2018/19

Analysis of Fibonacci heaps

Potential method to analyze Fibonacci heap operations.

Potential Q of Fibonacci heap Q:

Q = rQ + 2 mQ

where

rQ = number of nodes in Q.rootlist

mQ= number of all marked nodes in Q

that are not in the root list.

© S. Albers

Page 25: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

25WS 2018/19

Amortized analysis

ai : amortized cost of the i-th operation

ti : actual cost of the i-th operation

ai = ti + i - i-1

= ti + (ri –ri-1) + 2(mi – mi-1)

In the following we assume that a constant number of constant-time

instructions (such as a key comparison, a pointer update, the cut of

a link or the marking of a node) incurs an actual cost of 1. Otherwise

we can simply scale up the potential function.

© S. Albers

Page 26: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

26WS 2018/19

Analysis of ‘insert’

insert

ti = 1

ri - ri-1 = 1

mi - mi-1 = 0

ai = 1 + 1 + 0 = O(1)

© S. Albers

Page 27: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

27WS 2018/19

Analysis of ‘deletemin’

deletemin:

ti ri-1 + 2 log n + 2 log n

By deleting the element with minimum key, at most 2 log n children

join the root list. Hence at most ri-1 + 2 log n link operations can be

performed. After consolidation at most 2 log n roots have to be

inspected to determine the new minimum. Thus the actual cost, up to

a constant factor, is upper bounded by the above right-hand side

expression.

ri - ri-1 2 log n - ri-1

mi – mi-1 0

ai ri-1 + 4 log n + 2 log n - ri-1 + 0

= O(log n)

© S. Albers

Page 28: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

28

Analysis of ‘decreasekey’

decreasekey:

Let c denote the number of cut operations.

ti = c + 1 In addition to the cut operations, there is constant cost for

possibly marking a new node and updating the min-pointer.

ri – ri-1 = c

mi – mi-1 - (c - 1) +1

ai c + 1 + c + 2 (-c + 2)

= O(1)

WS 2018/19 © S. Albers

Page 29: 09 Fibonacci Heaps - TUM · Fibonacci heaps „Lazy-meld“ version of binomial queues: The melding of trees having the same order is delayed until the next deletemin operation. Definition

29

Priority queues: comparison

List Heap Bin. – Q. Fib.-Hp.

insert O(1) O(log n) O(log n) O(1)

min O(n) O(1) O(log n) O(1)

delete-

minO(n) O(log n) O(log n) O(log n)*

meld

(mn)O(1)

O(n) or

O(m log n)O(log n) O(1)

decr.-key O(1) O(log n) O(log n) O(1)*

*= amortized costQ.delete(e) = Q.decreasekey(e, - ) + Q.deletemin( )

WS 2018/19 © S. Albers