fibonacci heaps - stanford universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · the...

220
Fibonacci Heaps

Upload: others

Post on 31-May-2020

24 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Fibonacci Heaps

Page 2: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Outline for Today

● Review from Last Time● Quick refresher on binomial heaps and lazy

binomial heaps.

● The Need for decrease-key● An important operation in many graph algorithms.

● Fibonacci Heaps● A data structure efficiently supporting decrease-

key.

● Representational Issues● Some of the challenges in Fibonacci heaps.

Page 3: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: (Lazy) Binomial Heaps

Page 4: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Building a Priority Queue

● Group nodes into “packets” with the following properties:

● Size must be a power of two.● Can efficiently fuse packets of the same size.● Can efficiently find the minimum element of

each packet.● Can efficiently “fracture” a packet of 2k nodes

into packets of 1, 2, 4, 8, …, 2k-1 nodes.

Page 5: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Binomial Trees

● A binomial tree of order k is a type of tree recursively defined as follows:

A binomial tree of order k is a single node whose children are binomial trees of order 0, 1, 2, …, k – 1.

● Here are the first few binomial trees:

0

0

1

0

0

1

2

0

0

1

2

0

1 0

3

Page 6: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Binomial Trees

● A heap-ordered binomial tree is a binomial tree whose nodes obey the heap property: all nodes are less than or equal to their descendants.

● We will use heap-ordered binomial trees to implement our “packets.”

5

9

2

7

5

3

1

Page 7: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Binomial Heap

● A binomial heap is a collection of heap-ordered binomial trees stored in ascending order of size.

● Operations defined as follows:

● meld(pq₁, pq₂): Use addition to combine all the trees.

– Fuses O(log n) trees. Total time: O(log n).

● pq.enqueue(v, k): Meld pq and a singleton heap of (v, k).

– Total time: O(log n).

● pq.find-min(): Find the minimum of all tree roots.

– Total time: O(log n).

● pq.extract-min(): Find the min, delete the tree root, then meld together the queue and the exposed children.

– Total time: O(log n).

Page 8: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Lazy Binomial Heaps

● A lazy binomial heap is a variation on a standard binomial heap in which melds are done lazily by concatenating tree lists together.

● Tree roots are stored in a doubly-linked list.● An extra pointer is required that points to

the minimum element.● extract-min eagerly coalesces binomial

trees together and runs in amortized time O(log n).

Page 9: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

7

8

7

8

7

8

3

4

8 7

8

7

8

7

8

5

7

1 2 5 7

8

7

8

7

8

6

9

0 7

8

1

7

7

8

1

7

7

8

1

7

8

9

9

7

Page 10: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

7

8

7

8

7

8

3

4

8 7

8

7

8

7

8

5

7

1 2 5 7

8

7

8

7

8

6

9

0 7

8

1

7

7

8

1

7

7

8

1

7

8

9

9

7

Total number of nodes: 15

(Can compute in time Θ(T), where T is the number of trees, if each tree is

tagged with its order)

Bits needed: 4

Total number of nodes: 15

(Can compute in time Θ(T), where T is the number of trees, if each tree is

tagged with its order)

Bits needed: 4

Page 11: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

7

8

7

8

7

8

3

4

8 7

8

7

8

7

8

5

7

1 2 5 7

8

7

8

7

8

6

9

0 7

8

1

7

7

8

1

7

7

8

1

7

8

9

9

7

Page 12: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

8 7

8

7

8

7

8

5

7

1 2 5 7

8

7

8

7

8

6

9

0 7

8

1

7

7

8

1

7

7

8

1

7

8

9

9

7

7

8

7

8

7

8

3

4

Page 13: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

7

8

7

8

7

8

5

7

1 2 5 7

8

7

8

7

8

6

9

0 7

8

1

7

7

8

1

7

7

8

1

7

8

9

9

7

7

8

7

8

7

8

3

4

8

Page 14: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

7

8

7

8

7

8

5

7

1 2 5 7

8

7

8

7

8

6

9

0 7

8

1

7

7

8

1

7

7

8

1

7

8

9

9

7

7

8

7

8

7

8

3

4

8

Page 15: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

1 2 5 6

9

0 8

9

9

7

8

5

7

4

3

Page 16: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

1

2 5 6

9

0 8

9

9

7

8

5

7

4

3

Page 17: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

2 5 6

9

0 8

9

9

7

5

7

4

3 1

8

Page 18: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

5 6

9

0 8

9

9

7

5

7

4

3 1

8

2

Page 19: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

6

9

0 8

9

9

7

5

7

4

3 1

8

2

5

Page 20: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

6

9

0 8

9

9

7

5

7

4

3 1

8

2

5

Page 21: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

6

9

0 8

9

9

7

5

7

4

3

2

5

8

1

Page 22: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

6

9

0 8

9

9

7

2

5

8

5

7

4

3

1

Page 23: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

0 8

9

9

7

2

5

8

5

7

4

3

1 6

9

Page 24: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

8

9

9

7

2

5

8

5

7

4

3

1 6

9

0

Page 25: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Coalescing Trees

2

5

8

5

7

4

3

1 6

9

0

8

9

9

7

Page 26: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Overall Analysis

● Set Φ(D) to be the number of trees in D.● The amortized costs of the operations on a

lazy binomial heap are as follows:● enqueue: O(1)● meld: O(1)● find-min: O(1)● extract-min: O(log n)

● Details are in the previous lecture.● Let's quickly review extract-min's analysis.

Page 27: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Analyzing Extract-Min

● Initially, we expose the children of the minimum element. This takes time O(log n).

● Suppose that at this point there are T trees. The runtime for the coalesce is Θ(T).

● When we're done merging, there will be O(log n) trees remaining, so ΔΦ = -T + O(log n).

● Amortized cost is

= O(log n) + Θ(T) + O(1) · (-T + O(log n))

= O(log n) + Θ(T) – O(1) · T + O(1) · O(log n)

= O(log n).

Page 28: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Detail in the Analysis

● The amortized cost of an extract-min is

O(log n) + Θ(T) + O(1) · (-T + O(log n))

● Where do these O(log n) terms come from?● First O(log n): Removing the minimum element

might expose O(log n) children, since the maximum order of a tree is O(log n).

● Second O(log n): Maximum number of trees after a coalesce is O(log n).

● A different intuition: Let M(n) be the maximum possible order of a tree in a lazy binomial heap.

● Amortized runtime is O(M(n)).

Page 29: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Need for decrease-key

Page 30: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Dijkstra's Algorithm

● Dijkstra's algorithm solves the single-source shortest paths (SSSP) problem in graphs with nonnegative edge weights.

5

2

10

4

1

1

4

Page 31: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Dijkstra's Algorithm

● Dijkstra's algorithm solves the single-source shortest paths (SSSP) problem in graphs with nonnegative edge weights.

0?

∞?

∞?

∞?

∞?

5

2

10

4

1

1

4

Page 32: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Dijkstra's Algorithm

● Dijkstra's algorithm solves the single-source shortest paths (SSSP) problem in graphs with nonnegative edge weights.

0

∞?

∞?

∞?

∞?

5

2

10

4

1

1

4

Page 33: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Dijkstra's Algorithm

● Dijkstra's algorithm solves the single-source shortest paths (SSSP) problem in graphs with nonnegative edge weights.

0

5?

10?

∞?

∞?

5

2

10

4

1

1

4

Page 34: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Dijkstra's Algorithm

● Dijkstra's algorithm solves the single-source shortest paths (SSSP) problem in graphs with nonnegative edge weights.

0

5

10?

∞?

∞?

5

2

10

4

1

1

4

Page 35: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Dijkstra's Algorithm

● Dijkstra's algorithm solves the single-source shortest paths (SSSP) problem in graphs with nonnegative edge weights.

0

5

10?

7?

6?

5

2

10

4

1

1

4

Page 36: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Dijkstra's Algorithm

● Dijkstra's algorithm solves the single-source shortest paths (SSSP) problem in graphs with nonnegative edge weights.

0

5

10?

7?

6

5

2

10

4

1

1

4

Page 37: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Dijkstra's Algorithm

● Dijkstra's algorithm solves the single-source shortest paths (SSSP) problem in graphs with nonnegative edge weights.

0

5

10?

7

6

5

2

10

4

1

1

4

Page 38: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Dijkstra's Algorithm

● Dijkstra's algorithm solves the single-source shortest paths (SSSP) problem in graphs with nonnegative edge weights.

0

5

8?

7

6

5

2

10

4

1

1

4

Page 39: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Dijkstra's Algorithm

● Dijkstra's algorithm solves the single-source shortest paths (SSSP) problem in graphs with nonnegative edge weights.

0

5

8

7

6

5

2

10

4

1

1

4

Page 40: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Dijkstra and Priority Queues

● At each step of Dijkstra's algorithm, we need to do the following:● Find the node at v minimum distance from s.● Update the candidate distances of all the nodes

connected to v. (Distances only decrease in this step.)

● This first step sounds like an extract-min on a priority queue.

● How would we implement the second step?

Page 41: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

Page 42: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

∞? ∞?

∞?∞?

∞?

∞?

∞?

∞?

Page 43: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

∞? 1?

5?∞?

10?

2?

∞?

∞?

Page 44: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

∞?

5?∞?

10?

2?

∞?

∞?

Page 45: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

7?

5?∞?

10?

2?

∞?

∞?

Page 46: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

7?

5?∞?

10? ∞?

∞?

Page 47: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

7?

5?∞?

9? ∞?

8?

Page 48: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

7?

∞?

9? ∞?

8?

Page 49: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

4?

6?

9? ∞?

8?

Page 50: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

6?

9? ∞?

8?

Page 51: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

3?

9? ∞?

8?

Page 52: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

9? ∞?

8?

Page 53: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

9? ∞?

Page 54: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

9? 9?

Page 55: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

9?

Page 56: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

4?

Page 57: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

7

43

6

1

5

10

2

4

9

8

9

Page 58: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Review: Prim's Algorithm

● Prim's algorithm solves the minimum spanning tree (MST) problem in undirected graphs.

43

1

5 2

4

8

9

Page 59: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Prim and Priority Queues

● At each step of Prim's algorithm, we need to do the following:● Find the node v outside of the spanning tree with

the lowest-cost connection to the tree.● Update the candidate distances from v to nodes

outside the set S.

● This first step sounds like an extract-min on a priority queue.

● How would we implement the second step?

Page 60: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The decrease-key Operation

● Some priority queues support the operation pq.decrease-key(v, k), which works as follows:

Given a pointer to an element v in pq, lower its key (priority) to k. It is assumed that k is

less than the current priority of v.● This operation is crucial in efficient

implementations of Dijkstra's algorithm and Prim's MST algorithm.

Page 61: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Dijkstra and decrease-key

● Dijkstra's algorithm can be implemented with a priority queue using

● O(n) total enqueues,

● O(n) total extract-mins, and

● O(m) total decrease-keys.

Page 62: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Dijkstra and decrease-key

● Dijkstra's algorithm can be implemented with a priority queue using

● O(n) total enqueues,

● O(n) total extract-mins, and

● O(m) total decrease-keys.

0?

∞?

∞?

∞?

∞?

5

2

10

4

1

1

4

Page 63: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Dijkstra and decrease-key

● Dijkstra's algorithm can be implemented with a priority queue using

● O(n) total enqueues,

● O(n) total extract-mins, and

● O(m) total decrease-keys.

0

∞?

∞?

∞?

∞?

5

2

10

4

1

1

4

Page 64: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Dijkstra and decrease-key

● Dijkstra's algorithm can be implemented with a priority queue using

● O(n) total enqueues,

● O(n) total extract-mins, and

● O(m) total decrease-keys.

0

5?

10?

∞?

∞?

5

2

10

4

1

1

4

Page 65: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Dijkstra and decrease-key

● Dijkstra's algorithm can be implemented with a priority queue using

● O(n) total enqueues,

● O(n) total extract-mins, and

● O(m) total decrease-keys.

● Dijkstra's algorithm runtime is

O(n Tenq + n Text + m Tdec)

Page 66: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Prim and decrease-key

● Prim's algorithm can be implemented with a priority queue using

● O(n) total enqueues,

● O(n) total extract-mins, and

● O(m) total decrease-keys.

Page 67: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Prim and decrease-key

● Prim's algorithm can be implemented with a priority queue using

● O(n) total enqueues,

● O(n) total extract-mins, and

● O(m) total decrease-keys.

7

43

6

1

5

10

2

4

9

8

9

∞? ∞?

∞?∞?

∞?

∞?

∞?

∞?

Page 68: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Prim and decrease-key

● Prim's algorithm can be implemented with a priority queue using

● O(n) total enqueues,

● O(n) total extract-mins, and

● O(m) total decrease-keys.

7

43

6

1

5

10

2

4

9

8

9

∞? 1?

5?∞?

10?

2?

∞?

∞?

Page 69: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Prim and decrease-key

● Prim's algorithm can be implemented with a priority queue using

● O(n) total enqueues,

● O(n) total extract-mins, and

● O(m) total decrease-keys.

7

43

6

1

5

10

2

4

9

8

9

∞?

5?∞?

10?

2?

∞?

∞?

Page 70: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Prim and decrease-key

● Prim's algorithm can be implemented with a priority queue using

● O(n) total enqueues,

● O(n) total extract-mins, and

● O(m) total decrease-keys.

7

43

6

1

5

10

2

4

9

8

9

7?

5?∞?

10?

2?

∞?

∞?

Page 71: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Prim and decrease-key

● Prim's algorithm can be implemented with a priority queue using

● O(n) total enqueues,

● O(n) total extract-mins, and

● O(m) total decrease-keys.

● Prim's algorithm runtime is

O(n Tenq + n Text + m Tdec)

Page 72: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Standard Approaches

● In a binary heap, enqueue, extract-min, and decrease-key can be made to work in time O(log n) time each.

● Cost of Dijkstra's / Prim's algorithm:

= O(n Tenq + n Text + m Tdec)

= O(n log n + n log n + m log n)

= O(m log n)

Page 73: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Standard Approaches

● In a binomial heap, n enqueues takes time O(n), each extract-min takes time O(log n), and each decrease-key takes time O(log n).

● Cost of Dijkstra's / Prim's algorithm:

= O(n Tenq + n Text + m Tdec)

= O(n + n log n + m log n)

= O(m log n)

Page 74: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Where We're Going● The Fibonacci heap has these runtimes:

● enqueue: O(1)

● meld: O(1)

● find-min: O(1)

● extract-min: O(log n), amortized.

● decrease-key: O(1), amortized.

● Cost of Prim's or Dijkstra's algorithm:

= O(n Tenq + n Text + m Tdec)

= O(n + n log n + m)

= O(m + n log n)

● This is theoretically optimal for a comparison-based priority queue in Dijkstra's or Prim's algorithms.

Page 75: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Challenge of decrease-key

Page 76: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Simple Implementation

● It is possible to implement decrease-key in time O(log n) using lazy binomial heaps.

● Idea: “Bubble” the element up toward the root of the binomial tree containing it and (potentially) update the min pointer.

2

5

8

5

7

4

3

1 6

9

4

8

9

9

7

min

Page 77: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Simple Implementation

● It is possible to implement decrease-key in time O(log n) using lazy binomial heaps.

● Idea: “Bubble” the element up toward the root of the binomial tree containing it and (potentially) update the min pointer.

2

5

8

5

7

4

3

1 6

9

4

8

9

9

7

min

Page 78: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Simple Implementation

● It is possible to implement decrease-key in time O(log n) using lazy binomial heaps.

● Idea: “Bubble” the element up toward the root of the binomial tree containing it and (potentially) update the min pointer.

2

5

8

5

7

4

3

1 6

7

4

8

9

9

7

min

Page 79: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Simple Implementation

● It is possible to implement decrease-key in time O(log n) using lazy binomial heaps.

● Idea: “Bubble” the element up toward the root of the binomial tree containing it and (potentially) update the min pointer.

2

5

8

5

7

4

3

1 6

7

4

8

9

9

7

min

Page 80: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Simple Implementation

● It is possible to implement decrease-key in time O(log n) using lazy binomial heaps.

● Idea: “Bubble” the element up toward the root of the binomial tree containing it and (potentially) update the min pointer.

2

5

8

5

7

4

3

1 6

7

4

8

9

9

7

min

Page 81: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Simple Implementation

● It is possible to implement decrease-key in time O(log n) using lazy binomial heaps.

● Idea: “Bubble” the element up toward the root of the binomial tree containing it and (potentially) update the min pointer.

2

5

8

5

7

4

3

1 6

7

4

8

0

9

7

min

Page 82: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Simple Implementation

● It is possible to implement decrease-key in time O(log n) using lazy binomial heaps.

● Idea: “Bubble” the element up toward the root of the binomial tree containing it and (potentially) update the min pointer.

2

5

8

5

7

4

3

1 6

7

4

0

8

9

7

min

Page 83: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Simple Implementation

● It is possible to implement decrease-key in time O(log n) using lazy binomial heaps.

● Idea: “Bubble” the element up toward the root of the binomial tree containing it and (potentially) update the min pointer.

2

5

8

5

7

4

3

1 6

7

4

7

8

9

0

min

Page 84: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Simple Implementation

● It is possible to implement decrease-key in time O(log n) using lazy binomial heaps.

● Idea: “Bubble” the element up toward the root of the binomial tree containing it and (potentially) update the min pointer.

2

5

8

5

7

4

3

1 6

7

4

7

8

9

0

min

Page 85: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Simple Implementation

● It is possible to implement decrease-key in time O(log n) using lazy binomial heaps.

● Idea: “Bubble” the element up toward the root of the binomial tree containing it and (potentially) update the min pointer.

2

5

8

5

7

4

3

1 6

7

4

7

8

9

0

min

Page 86: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Challenge

● Goal: Implement decrease-key in amortized time O(1).

● Why is this hard?● Lowering a node's priority might break the

heap property.● Correcting the imbalance O(log n) layers

deep in a tree might take time O(log n).

● We will need to change our approach.

Page 87: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Challenge

● Goal: Implement decrease-key in amortized time O(1).

● Why is this hard?● Lowering a node's priority might break the

heap property.● Correcting the imbalance O(log n) layers

deep in a tree might take time O(log n).

● We will need to change our approach.

Page 88: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

3

2 6

7

4

7

8

9

5

2

Page 89: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

3

2 6

7

4

7

8

9

5

2

Page 90: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

3

2 6

7

4

7

8

9

3

2

Page 91: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

3

2 6

7

4

7

8

9

3

2

Page 92: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

3

2 6

7

4

7

8

9

3

2

Page 93: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

0

2 6

7

4

7

8

9

3

2

Page 94: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

0

2 6

7

4

7

8

9

3

2

Page 95: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

0

2 6

7

4

7

8

9

3

2

Page 96: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

0

2 6

7

4

7

8

9

3

2

Page 97: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

0

2 6

7

4

7

8

9

3

2

Page 98: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

0

2 6

7

4

7

8

9

1

2

Page 99: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

0

2 6

7

4

2

7

8

9

1

Page 100: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

0

2 6

7

4

2

7

8

9

1

Page 101: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

0

2 6

7

4

2

7

8

9

1

Page 102: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

0

2 6

7

4

2

0

8

9

1

Page 103: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

0

2 6

7

4

2

0

8

9

1

Page 104: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

0

2 6

7

4

2 0

89

1

Page 105: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

4

5

8

5

7

4

0

2 6

7

4

2 0

89

1

Page 106: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

A Crazy Idea

● To implement decrease-key efficiently:● Lower the key of the specified node.● If its key is greater than or equal to its

parent's key, we're done.● Otherwise, cut that node from its parent and

hoist it up to the root list, optionally updating the min pointer.

● Time required: O(1).● This requires some changes to the tree

representation; more details later.

Page 107: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Tree Sizes and Orders

● Recall: A binomial tree of order k has 2k nodes and the root has k children.

● Going forward, we'll say that the order of a node is the number of children it has.

● Concern: If trees can be cut, a tree of order k might have many fewer than 2k nodes.

Page 108: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Problem

4

585 4

3

2 8 43

2

9

2 6

3

Page 109: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Problem

123k+1 ... 4

Number of nodes: Θ(k2)

Number of trees: Θ(n1/2)

Number of nodes: Θ(k2)

Number of trees: Θ(n1/2)

Page 110: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Problem

● Recall: The amortized cost of an extract-min is O(M(n)), where M(n) is the maximum order of a tree in the heap.

● With true binomial trees, this is O(log n).● With our “damaged” binomial trees, this

can be Θ(n1/2).● We've lost our runtime bounds!

Page 111: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Time-Out for Announcements!

Page 112: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Problem Sets

● Problem Set Three was due at the start of class today.● Want to use late days? Feel free to submit it

by Saturday at 3:00PM.

● The next problem set goes out on Tuesday. Enjoy a little break from the problem sets!

Page 113: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored
Page 114: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Back to CS166!

Page 115: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Problem

● This problem arises because we have lost one of the guarantees of binomial trees:

A binomial tree of order k has 2k nodes.● When we cut low-hanging trees, the root

node won't learn that these trees are missing.

● However, communicating this information up from the leaves to the root might take time O(log n)!

Page 116: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Tradeoff

● If we don't impose any structural constraints on our trees, then trees of large order may have too few nodes.● Leads to M(n) getting too high, wrecking our

runtime bounds for extract-min.

● If we impose too many structural constraints on our trees, then we have to spend too much time fixing up trees.● Leads to decrease-key taking too long.

● How can we strike a balance?

Page 117: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

8

5

7

4

3

2 6

7

4

7

8

9

3

2

Page 118: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

8

5

7

4

3

2 6

7

4

7

8

9

3

2

Page 119: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1

5

7

4

3

2 6

7

4

7

8

9

3

2

Page 120: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1

5

7

4

3

2 6

7

4

7

8

9

3

2

Page 121: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1

5

7

4

3

2 6

7

4

7

8

9

3

2

Page 122: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1

5

7

4

3

2 6

7

4

7

8

9

3

2

Page 123: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1

5

7

4

3

2 6

7

4

7

8

9

3

2

Page 124: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1

2

7

4

3

2 6

7

4

7

8

9

3

2

Page 125: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1

2

7

4

3

2 6

7

4

7

8

9

3

2

Page 126: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1

2

7

4

3

2 6

7

4

7

8

9

3

2

Page 127: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1 2

7

4

3

2 6

7

4

7

8

9

3

2

Page 128: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1 2

7

4

3

2 6

7

4

7

8

9

3

2

Page 129: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1 2

7

0

3

2 6

7

4

7

8

9

3

2

Page 130: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1 2

7

0

3

2 6

7

4

7

8

9

3

2

Page 131: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1 2

7

0

3

2 6

7

4

7

8

9

3

2

Page 132: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1 2

7

0

3

2 6

7

4

7

8

9

3

2

Page 133: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● Every non-root node is allowed to lose at most one child.● If a non-root node loses two children, we cut it from its

parent. (This might trigger more cuts.)● We will mark nodes in the heap that have lost children

to keep track of this fact.

4

5

1 2

7

032

6

7

4

7

8

9

3

2

Page 134: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● To cut node v from its parent p:● Unmark v.● Cut v from p.● If p is not already marked and is not the root

of a tree, mark it.● If p was already marked, recursively cut p

from its parent.

Page 135: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● If we do a few decrease-keys, then the tree won't lose “too many” nodes.

● If we do many decrease-keys, the information slowly propagates to the root.

4

5

8

5

7

4

3

2 6

7

4

7

8

9

3

2

Page 136: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● If we do a few decrease-keys, then the tree won't lose “too many” nodes.

● If we do many decrease-keys, the information slowly propagates to the root.

4

5

8

5 4

3

2 6

7

4

7

8

9

3

2

Page 137: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● If we do a few decrease-keys, then the tree won't lose “too many” nodes.

● If we do many decrease-keys, the information slowly propagates to the root.

8

5 4

3

2 6

7

4

7

8

9

3

2

Page 138: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● If we do a few decrease-keys, then the tree won't lose “too many” nodes.

● If we do many decrease-keys, the information slowly propagates to the root.

5 4

3

2 6

7

4

7

8

9

3

2

Page 139: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● If we do a few decrease-keys, then the tree won't lose “too many” nodes.

● If we do many decrease-keys, the information slowly propagates to the root.

5 4

3

2 6

7

4

7

8

9

3

2

Page 140: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Compromise

● If we do a few decrease-keys, then the tree won't lose “too many” nodes.

● If we do many decrease-keys, the information slowly propagates to the root.

6

7

4

7

8

9

3

2

Page 141: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Assessing the Impact

● The amortized cost of an extract-min is O(M(n)), where M(n) is the maximum possible order of a tree.

● This used to be O(log n) because our trees had exponentially many nodes in them.

● What is it now?

Page 142: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Two Extremes

● If we never do any decrease-keys, then the trees in our data structure are all binomial trees.

● Each tree of order k has 2k nodes in it, the maximum possible order is O(log n).

● On the other hand, suppose that all trees in the binomial heap have lost the maximum possible number of nodes.

● In that case, how many nodes will each tree have?

Page 143: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

Page 144: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0

Page 145: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 1

0

Page 146: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 1

0

We can't cut any nodes from this tree without making the root node

have order 0.

We can't cut any nodes from this tree without making the root node

have order 0.

Page 147: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

2

01

0

0 1

0

Page 148: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

2

01

0

0 1

0

We can't cut any of the root's children without decreasing its order.

We can't cut any of the root's children without decreasing its order.

Page 149: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

2

01

0

0 1

0

We can't cut any of the root's children without decreasing its order.

We can't cut any of the root's children without decreasing its order.

However, we can cut this node, leaving the root

node with two children.

However, we can cut this node, leaving the root

node with two children.

Page 150: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

2

00

0 1

0

Page 151: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 1

0

2

01

0

32

00

0 1

0

Page 152: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 1

0

2

01

0

32

00

0 1

0

As before, we can't cut any of the root's children

without decreasing its order.

As before, we can't cut any of the root's children

without decreasing its order.

Page 153: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 1

0

2

01

0

32

00

0 1

0

However, any nodes below the second layer are fair game to be eliminated.

However, any nodes below the second layer are fair game to be eliminated.

As before, we can't cut any of the root's children

without decreasing its order.

As before, we can't cut any of the root's children

without decreasing its order.

Page 154: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 1

0

2

01

0

32

00

0 1

0

Page 155: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 0 2

01

0

32

00

0 1

0

Page 156: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 0 1

0

32

00

0 1

0

Page 157: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 0 1

0

32

00

0 1

0

We can't cut this node without triggering a

cascading cut, so we're done.

We can't cut this node without triggering a

cascading cut, so we're done.

Page 158: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 0 1

0

32

00

0 1

0

Page 159: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 1

0

2

01

0

0 1

0

2

01

0

3

4

0 0 1

0

32

00

0 1

0

Page 160: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 1

0

2

01

0

0 1

0

2

01

0

3

4

0 0 1

0

32

00

0 1

0

We can start chopping away at these nodes!

We can start chopping away at these nodes!

Page 161: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 1

0

2

01

0

0 1

0

2

01

0

3

4

0 0 1

0

32

00

0 1

0

Page 162: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 0 2

01

0

0 1

0

2

01

0

3

4

0 0 1

0

32

00

0 1

0

Page 163: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 0 1

0 0 1

0

2

01

0

3

4

0 0 1

0

32

00

0 1

0

Page 164: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 0 1

0 0 1

0

2

4

0 0 1

0

32

00

0 1

0

Page 165: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 0 1

0 0 0

2

4

0 0 1

0

32

00

0 1

0

Page 166: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 0 1

0 0 0

2

4

0 0 1

0

32

00

0 1

0

Page 167: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

k

0 0 k-21…

0 0 1

0 0 0

2

4

0 0 1

0

32

00

0 1

0

Page 168: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

A maximally-damaged tree of order k is a node whose children are maximally-damaged trees of orders

0, 0, 1, 2, 3, …, k – 2.

A maximally-damaged tree of order k is a node whose children are maximally-damaged trees of orders

0, 0, 1, 2, 3, …, k – 2.

k

0 0 k-21…

0 0 1

0 0 0

2

4

0 0 1

0

32

00

0 1

0

Page 169: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 0 1

0 0 0

2

4

0 0 1

0

32

00

0 1

0

Page 170: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 0 1

0 0 0

2

4

0 0 1

0

32

00

0 1

0

Page 171: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 0 1

0 0 0

2

4

0 0 1

0

32

00

0 1

0

Page 172: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 0 1

0 0 0

2

4

0 0 1

0

32

00

0 1

0

1 2 3 5 8

Page 173: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

0 0 1

0 0 0

2

4

0 0 1

0

32

00

0 1

0

1 2 3 5 8

Claim: The minimum number of nodes in a tree of order k is Fₖ₊₂

Claim: The minimum number of nodes in a tree of order k is Fₖ₊₂

Page 174: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

● Theorem: The number of nodes in a maximally-damaged tree of order k is Fₖ₊₂.

● Proof: Induction.

Page 175: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

● Theorem: The number of nodes in a maximally-damaged tree of order k is Fₖ₊₂.

● Proof: Induction.

0 1

Page 176: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

● Theorem: The number of nodes in a maximally-damaged tree of order k is Fₖ₊₂.

● Proof: Induction.

0 1

F₂ F₃

Page 177: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

● Theorem: The number of nodes in a maximally-damaged tree of order k is Fₖ₊₂.

● Proof: Induction.

0 1

0 0 k-2

k

1…

F₂ F₃

Page 178: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

● Theorem: The number of nodes in a maximally-damaged tree of order k is Fₖ₊₂.

● Proof: Induction.

0 1

0 0 k-2

k + 1

1…

k-1

F₂ F₃

Page 179: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

● Theorem: The number of nodes in a maximally-damaged tree of order k is Fₖ₊₂.

● Proof: Induction.

0 1

0 0 k-2

k + 1

1…

k-1

F₂ F₃

Page 180: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

● Theorem: The number of nodes in a maximally-damaged tree of order k is Fₖ₊₂.

● Proof: Induction.

0 1

0 0 k-2

k + 1

1…

k-1

Fₖ₊₂F₂ F₃

Page 181: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

● Theorem: The number of nodes in a maximally-damaged tree of order k is Fₖ₊₂.

● Proof: Induction.

0 1

0 0 k-2

k + 1

1…

k-1

Fₖ₊₂ Fₖ₊₁F₂ F₃

Page 182: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

● Theorem: The number of nodes in a maximally-damaged tree of order k is Fₖ₊₂.

● Proof: Induction.

0 1

0 0 k-2

k + 1

1…

k-1

Fₖ₊₂ Fₖ₊₁+F₂ F₃

Page 183: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Maximally-Damaged Trees

● Theorem: The number of nodes in a maximally-damaged tree of order k is Fₖ₊₂.

● Proof: Induction.

0 1

0 0 k-2

k + 1

1…

k-1

Fₖ₊₃F₂ F₃

Page 184: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

φ-bonacci Numbers

● Fact: For n ≥ 2, we have Fₙ ≥ φn-2, where φ is the golden ratio:

φ ≈ 1.61803398875...

● Claim: In our modified data structure, we have M(n) = O(log n).

● Proof: In a tree of order k, there are at leastFₖ₊₂ ≥ φk nodes. Therefore, the maximumorder of a tree in our data structure islogφ n = O(log n). ■

Page 185: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Fibonacci Heaps

● A Fibonacci heap is a lazy binomial heap where decrease-key is implemented using the earlier cutting-and-marking scheme.

● Operation runtimes:● enqueue: O(1)● meld: O(1)● find-min: O(1)● extract-min: O(log n) amortized● decrease-key: Up next!

Page 186: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Analyzing decrease-key

● In the best case, decrease-key takes time O(1) when no cuts are made.

● In the worst case, decrease-key takes time O(C), where C is the number of cuts made.

● What is the amortized cost of a decrease-key?

Page 187: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Refresher: Our Choice of Φ

● In our amortized analysis of lazy binomial heaps, we set Φ to be the number of trees in the heap.

● With this choice of Φ, we obtained these amortized time bounds:● enqueue: O(1)● meld: O(1)● find-min: O(1)● extract-min: O(log n)

Page 188: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Rethinking our Potential

● Intuitively, a cascading cut only occurs if we have a long chain of marked nodes.

● Those nodes were only marked because of previous decrease-key operations.

● Idea: Backcharge the work required to do the cascading cut to each preceding decrease-key that contributed to it.

● Specifically, change Φ as follows:

Φ = #trees + #marked● Note: Since only decrease-key interacts with marked

nodes, our amortized analysis of all previous operations is still the same.

Page 189: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The (New) Amortized Cost

● Using our new Φ, a decrease-key makes C cuts, it● Marks one new node (+1),● Unmarks C nodes (-C), and● Adds C trees to the root list (+C).

● Amortized cost is

= Θ(C) + O(1) · ΔΦ

= Θ(C) + O(1) · (1 – C + C)

= Θ(C) + O(1) · 1

= Θ(C) + O(1)

= Θ(C)● Hmmm... that didn't work.

Page 190: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Trick

● Each decrease-key makes extra work for two future operations:● Future extract-mins that now have more

trees to coalesce, and● Future decrease-keys that might have to do

cascading cuts.

● We can make this explicit in our potential function:

Φ = #trees + 2·#marked

Page 191: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The (Final) Amortized Cost

● Using our new Φ, a decrease-key makes C cuts, it● Marks one new node (+2),● Unmarks C nodes (-2C), and● Adds C trees to the root list (+C).

● Amortized cost is

= Θ(C) + O(1) · ΔΦ

= Θ(C) + O(1) · (2 – 2C + C)

= Θ(C) + O(1) · (2 – C)

= Θ(C) – O(C) + O(1)

= Θ(1)● We now have amortized O(1) decrease-key!

Page 192: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Story So Far

● The Fibonacci heap has the following amortized time bounds:● enqueue: O(1)● find-min: O(1)● meld: O(1)● decrease-key: O(1) amortized● extract-min: O(log n) amortized

● This is about as good as it gets!

Page 193: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Catch: Representation Issues

Page 194: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Representing Trees

● The trees in a Fibonacci heap must be able to do the following:● During a merge: Add one tree as a child of

the root of another tree.● During a cut: Cut a node from its parent in

time O(1).

● Claim: This is trickier than it looks.

Page 195: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Representing Trees

A

B C D E

Page 196: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Representing Trees

A

B C D E

A

B C D E

Page 197: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Representing Trees

A

B C D E

A

B C D E

Page 198: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Representing Trees

A

B C

D

E

A

B C D E

Page 199: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Representing Trees

A

B C

D

E

A

B C D E

Page 200: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Representing Trees

A

B C

D

E

A

B C D E

Page 201: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Representing Trees

A

B C

D

E

A

B C D E

Finding this pointer might take

time Θ(log n)!

Finding this pointer might take

time Θ(log n)!

Page 202: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Solution

Page 203: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Solution

This is going to be weird.

Sorry.

This is going to be weird.

Sorry.

Page 204: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Solution

B C D E

A

Page 205: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Solution

B C D E

AEach node stores a

pointer to its parent.

Each node stores a pointer to its parent.

The parent stores a pointer to an arbitrary

child.

The parent stores a pointer to an arbitrary

child.

The children of each node are in a circularly,

doubly-linked list.

The children of each node are in a circularly,

doubly-linked list.

Page 206: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Solution

B C D E

A

Page 207: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Solution

B C D E

A

Page 208: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Solution

B C E

A

Page 209: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Solution

B C E

A

To cut a node from its parent, if it isn't the representative child, just

splice it out of its linked list.

To cut a node from its parent, if it isn't the representative child, just

splice it out of its linked list.

Page 210: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Solution

B C E

A

Page 211: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Solution

B C E

A

Page 212: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Solution

B C E

A

Page 213: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Solution

B C

A

Page 214: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

The Solution

B C

A

If it is the representative, change the parent's representative child to

be one of the node's siblings.

If it is the representative, change the parent's representative child to

be one of the node's siblings.

Page 215: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Awful Linked Lists

● Trees are stored as follows:● Each node stores a pointer to some child.● Each node stores a pointer to its parent.● Each node is in a circularly-linked list of its siblings.

● Awful, but the following possible are now possible in time O(1):● Cut a node from its parent.● Add another child node to a node.

● This is the main reason Fibonacci heaps are so complex.

Page 216: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Fibonacci Heap Nodes

● Each node in a Fibonacci heap stores● A pointer to its parent.● A pointer to the next sibling.● A pointer to the previous sibling.● A pointer to an arbitrary child.● A bit for whether it's marked.● Its order.● Its key.● Its element.

Page 217: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

In Practice

● In practice, Fibonacci heaps are slower than other heaps with worse asymptotic performance.

● Why?● Huge memory requirements per node.● High constant factors on all operations.● Poor locality of reference and caching.

Page 218: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

In Theory

● That said, Fibonacci heaps are worth knowing about for several reasons:● Clever use of a two-tiered potential function

shows up in lots of data structures.● Implementation of decrease-key forms the

basis for many other advanced priority queues.

● Gives the theoretically optimal comparison-based implementation of Prim's and Dijkstra's algorithms.

Page 219: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Summary

● decrease-key is a useful operation in many graph algorithms.

● Implement decrease-key by cutting a node from its parent and hoisting it up to the root list.

● To make sure trees of high order have lots of nodes, add a marking scheme and cut nodes that lose two or more children.

● Represent the data structure using Awful Linked Lists.

● Can prove that the number of trees is O(log n) by most maximally damaged trees in the heap.

Page 220: Fibonacci Heaps - Stanford Universityweb.stanford.edu/class/archive/cs/cs166/cs166.1166/... · The Binomial Heap A binomial heap is a collection of heap-ordered binomial trees stored

Next Time

● Splay Trees● Amortized-efficient balanced trees.

● Static Optimality● Is there a single best BST for a set of data?

● Dynamic Optimality● Is there a single best BST for a set of data if

that BST can change over time?