algorithms: minimum spanning treesamotz/bc-algorithms/presentations/mst.pdf · a spanning tree: a...

73
Algorithms: Minimum Spanning Trees Amotz Bar-Noy CUNY Spring 2012 Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 1 / 59

Upload: phamtram

Post on 27-Apr-2019

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Algorithms: Minimum Spanning Trees

Amotz Bar-Noy

CUNY

Spring 2012

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 1 / 59

Page 2: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Minimum Spanning Trees (MST)

Input:An undirected and connected graph G = (V ,E) with n vertices andm edges.A weight function w : E → < on the edges.

A spanning tree: A connected sub-graph with n − 1 edges.

A minimum spanning tree (MST): A spanning tree for which thesum of the weights of the n − 1 edges is minimum.

Two greedy algorithms:Kruskal - O(m log m)-time, a distributed algorithm.Prim - O(m + n log n)-time, a centralized algorithm.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 2 / 59

Page 3: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Minimum Spanning Trees (MST)

Input:An undirected and connected graph G = (V ,E) with n vertices andm edges.A weight function w : E → < on the edges.

A spanning tree: A connected sub-graph with n − 1 edges.

A minimum spanning tree (MST): A spanning tree for which thesum of the weights of the n − 1 edges is minimum.

Two greedy algorithms:Kruskal - O(m log m)-time, a distributed algorithm.Prim - O(m + n log n)-time, a centralized algorithm.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 2 / 59

Page 4: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Minimum Spanning Trees (MST)

Input:An undirected and connected graph G = (V ,E) with n vertices andm edges.A weight function w : E → < on the edges.

A spanning tree: A connected sub-graph with n − 1 edges.

A minimum spanning tree (MST): A spanning tree for which thesum of the weights of the n − 1 edges is minimum.

Two greedy algorithms:Kruskal - O(m log m)-time, a distributed algorithm.Prim - O(m + n log n)-time, a centralized algorithm.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 2 / 59

Page 5: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: A weighted Graph

BC

D

EF

A

1236

4

3

3

55

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 3 / 59

Page 6: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: A BFS Spanning Tree

BC

D

EF

A

1236

4

3

3

55

W (T ) = 21

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 4 / 59

Page 7: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: A DFS Spanning Tree

BC

D

EF

A

1236

4

3

3

55

W (T ) = 14

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 5 / 59

Page 8: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: An MST

BC

D

EF

A

1236

4

3

3

55

W (T ) = 13

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 6 / 59

Page 9: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Another MST

BC

D

EF

A

1236

4

3

3

55

W (T ) = 13

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 7 / 59

Page 10: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Cuts

A cut 〈S, (V − S)〉 in a graph is a partition of the set of vertices Vinto two disjoint sets: V = S ∪ (V − S).

An edge (u, v) crosses a cut 〈S, (V − S)〉 if(u ∈ S & v ∈ (V − S)) or (v ∈ S & u ∈ (V − S)).

An edge (u, v) is contained in a cut 〈S, (V − S)〉 if(u, v) ∈ S or (u, v) ∈ (V − S).

A set A of edges is contained in a cut 〈S, (V − S)〉 if all of theedges of A are contained in the cut.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 8 / 59

Page 11: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: A Cut

BC

D

EF

A

An 〈ABDE , CF〉 cut

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 9 / 59

Page 12: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Another Cut

BC

D

EF

A

An 〈AEF , BCD〉 cut

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 10 / 59

Page 13: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Minimal Forests

A set A of edges is a minimal forest if it is possible to add edgesto A to become a minimal spanning tree.

An empty set is in particular a minimal forest.

A minimum spanning tree is in particular a minimal forest.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 11 / 59

Page 14: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

A Minimal Forest

A weighted graph:

D

CB

12

1

11

A

Minimal forests:A

D

CB

1 1

A

D

CB

1

1

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 12 / 59

Page 15: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

A Minimal Forest

A weighted graph:

D

CB

12

1

11

A

Not a minimal forest:

D

CB 2

A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 13 / 59

Page 16: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

The Greedy Lemma

Input:A weighted connected graph G = (V ,E).

A minimal forest A ⊂ E .

A cut 〈S, (V − S)〉 that contains A.

An edge (u, v) crossing 〈S, (V − S)〉 with minimum weight.

Lemma: A ∪ (u, v) is also a minimal forest.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 14 / 59

Page 17: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

The Greedy Lemma

Input:A weighted connected graph G = (V ,E).

A minimal forest A ⊂ E .

A cut 〈S, (V − S)〉 that contains A.

An edge (u, v) crossing 〈S, (V − S)〉 with minimum weight.

Lemma: A ∪ (u, v) is also a minimal forest.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 14 / 59

Page 18: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Proof

A vertex in V−S

A vertex in S

x

y

v

u

An edge not in T

An edge in T−A

An edge in A

An MST T that contains A and (u, v) /∈ T .

(x , y) ∈ T crossing 〈S, (V − S)〉 ⇒ w(u, v) ≤ w(x , y).

T ′ = T − (x , y) ∪ (u, v) ⇒ T ′ is also an MST.

⇒ A ∪ (u, v) is a minimal forest.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 15 / 59

Page 19: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

A Schematic Greedy Algorithm

(1) A = ∅(2) while |A| < n − 1 do(3) find (u, v) s.t. A ∪ (u, v) is a minimal forest(4) A = A ∪ (u, v)(5) return(A)

Correctness:Due to the lemma, step (3) is always possible.By definition, A is always a minimal forest.At the end A has n − 1 edges.A forest with n − 1 edges is a tree.

Complexity: Depends on the implementation of step (3).

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 16 / 59

Page 20: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

A Schematic Greedy Algorithm

(1) A = ∅(2) while |A| < n − 1 do(3) find (u, v) s.t. A ∪ (u, v) is a minimal forest(4) A = A ∪ (u, v)(5) return(A)

Correctness:Due to the lemma, step (3) is always possible.By definition, A is always a minimal forest.At the end A has n − 1 edges.A forest with n − 1 edges is a tree.

Complexity: Depends on the implementation of step (3).

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 16 / 59

Page 21: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

A Schematic Greedy Algorithm

(1) A = ∅(2) while |A| < n − 1 do(3) find (u, v) s.t. A ∪ (u, v) is a minimal forest(4) A = A ∪ (u, v)(5) return(A)

Correctness:Due to the lemma, step (3) is always possible.By definition, A is always a minimal forest.At the end A has n − 1 edges.A forest with n − 1 edges is a tree.

Complexity: Depends on the implementation of step (3).

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 16 / 59

Page 22: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Kruskal Algorithm

Sort the edges from the lightest to the heaviest.

Consider the edges following this order.

Start with an empty minimal forest.

Add an edge to the minimal forest if it doesn’t close a cycle.

Terminate when there are n − 1 edges in the minimal forest.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 17 / 59

Page 23: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 18 / 59

Page 24: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 19 / 59

Page 25: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 20 / 59

Page 26: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 21 / 59

Page 27: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 22 / 59

Page 28: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 23 / 59

Page 29: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 24 / 59

Page 30: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 25 / 59

Page 31: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Kruskal Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 26 / 59

Page 32: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Kruskal Algorithm – Data Structure

A collection S of disjoint sets of vertices: S1,S2, . . . ,SkSi ∩ Sj = ∅ for all 1 ≤ i 6= j ≤ k .Initially, the collection is empty: S = ∅.At the end S contains one set of all the vertices: S = V.

Make-Set(x): Creates a new set containing only x and adds it tothe collection S: S = S ∪ x.

Find-Set(x): Finds the set in the collection S that contains x :Si ∈ S such that x ∈ Si .

Union(Si ,Sj ): replaces in the collection S the two sets Si and Sjwith their union: S = S −

Si ,Sj

Si ∪ Sj

.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 27 / 59

Page 33: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Kruskal Algorithm – Code

Variables:A: A minimal forest.Each tree in A is represented by a set of vertices.

Algorithm:(1) A = ∅(2) for all v ∈ V do Make-Set(v )(3) Sort(E) all edges from min to max(4) for each edge (u, v) in sorted order do(5) if Find-Set(u) 6=Find-Set(v ) then(6) A = A ∪ (u, v)(7) Union(Find-Set(u),Find-Set(v ))(8) return (A)

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 28 / 59

Page 34: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Kruskal Algorithm – Correctness

Assume to the contrary that the output set A is not an MST.

Let (u, v) be the first edge that was added to A such thatA ∪ (u, v) is not a minimal forest.

In particular, at that time, A is a minimal forest.

Let C = 〈S, (V − S)〉 be a cut for the set u ∈ S.

(u, v) crosses C since v belongs to another set.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 29 / 59

Page 35: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Kruskal Algorithm – Correctness

Any lighter edge (x , y) is contained in C:If (x , y) 6∈ A, then both x and y belong to the same set before (x , y)was examined.

If (x , y) ∈ A, then both x and y belong to the same set after (x , y)was examined.

Sorting⇒ (u, v) is a minimal crossing edge for the cut C.

Greedy lemma⇒ A ∪ (u, v) is a minimal forest.

A contradiction.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 30 / 59

Page 36: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Kruskal Algorithm – Complexity

Sorting complexity: O(m log m).

Set operations complexity:There are n Make-Set operations.There are O(m) Find-Set operations.There are n − 1 Union operations.Possible to implement in time: O(m · α(m,n)).

α(m, n) is the inverse of the Ackerman’s function.The α(m, n) function grows very slowly.For example, m, n ≈ 1080 ⇒ α(m, n) ≤ 4.

Overall complexity: O(m log m).

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 31 / 59

Page 37: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

The Ackerman’s Function

Ak (n) =

2n for k = 0 and n ≥ 0Ak−1(1) for k ≥ 1 and n = 1Ak−1(Ak (n − 1)) for k ≥ 1 and n ≥ 2

A0(n) = 2 + · · ·+ 2 = 2n – The multiply-by-2 function.

A1(n) = 2× · · · × 2 = 2n – The power-of-2 function.

A2(n) = 22. ..2

– With n 2’s, the tower-of-2 function.

Each recursive level does the previous level’s operation n times.

A2(4) = 2222

= 216 = 65536.

Already A3(4) and A4(4) must be extremely large!

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 32 / 59

Page 38: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

The Ackerman’s Function

Ak (n) =

2n for k = 0 and n ≥ 0Ak−1(1) for k ≥ 1 and n = 1Ak−1(Ak (n − 1)) for k ≥ 1 and n ≥ 2

A0(n) = 2 + · · ·+ 2 = 2n – The multiply-by-2 function.

A1(n) = 2× · · · × 2 = 2n – The power-of-2 function.

A2(n) = 22. ..2

– With n 2’s, the tower-of-2 function.

Each recursive level does the previous level’s operation n times.

A2(4) = 2222

= 216 = 65536.

Already A3(4) and A4(4) must be extremely large!

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 32 / 59

Page 39: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

The Ackerman’s Function

Ak (n) =

2n for k = 0 and n ≥ 0Ak−1(1) for k ≥ 1 and n = 1Ak−1(Ak (n − 1)) for k ≥ 1 and n ≥ 2

A0(n) = 2 + · · ·+ 2 = 2n – The multiply-by-2 function.

A1(n) = 2× · · · × 2 = 2n – The power-of-2 function.

A2(n) = 22. ..2

– With n 2’s, the tower-of-2 function.

Each recursive level does the previous level’s operation n times.

A2(4) = 2222

= 216 = 65536.

Already A3(4) and A4(4) must be extremely large!

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 32 / 59

Page 40: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Very Slow Growing Functions

log∗ n – the inverse of the tower-of-2 function – is the least x such

that 22. ..2

x times is greater or equal to n.

For example, log∗(265536) = 5.

α(n) – the inverse Ackerman’s function – is the least x such thatAx (x) ≥ n.

α(n) is much slower than log∗ n.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 33 / 59

Page 41: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Kruskal Algorithm – A Simple Implementation

A set is represented by the following linked list:

Set Name

Set Size

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 34 / 59

Page 42: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Kruskal Algorithm – A Simple Implementation

A set is represented by the following linked list:

Set Name

Set Size

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 34 / 59

Page 43: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Kruskal Algorithm – A Simple Implementation

The head of the set contains two fields: the name and the size ofthe set.

The head of the set has two pointers: to the head and the tail of alinked list of vertices.

An array of n vertices each has two pointers: to the head of its setand to the next vertex in its linked list.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 35 / 59

Page 44: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Kruskal Algorithm – A Simple Implementation

Make-Set(x)⇒ O(1) complexity.

Sx

1

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 36 / 59

Page 45: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Kruskal Algorithm – A Simple Implementation

Find-Set(x)⇒ O(1) complexity.

xS

s

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 37 / 59

Page 46: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Kruskal Algorithm – A Simple Implementation

Union(R,S)⇒ O(s) complexity.

a b

a b c d

c d

RS

r+s

R

r

S

s

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 38 / 59

Page 47: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

The Union Operation Implementation

Worst case complexity:Connect the larger set to the smaller set.Consider the n − 1 Union operations:

Union(S2,S1), Union(S3,S2), . . ., Union(Sn,Sn−1)The cost of Union(Si+1,Si ) is Ω(i).Total cost for the Union operations:

Ω(1) + Ω(2) + · · ·+ Ω(n − 1) = Ω(n2).

Modification:Connect the smaller set to the larger set.The pointer of each vertex is changed at most log n times, sinceafter each Union operation the pointer points to a set whose size isat least twice the size of the previous set.All together, for the n − 1 Union operations, for all vertices,O(n log n) complexity.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 39 / 59

Page 48: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

The Union Operation Implementation

Worst case complexity:Connect the larger set to the smaller set.Consider the n − 1 Union operations:

Union(S2,S1), Union(S3,S2), . . ., Union(Sn,Sn−1)The cost of Union(Si+1,Si ) is Ω(i).Total cost for the Union operations:

Ω(1) + Ω(2) + · · ·+ Ω(n − 1) = Ω(n2).

Modification:Connect the smaller set to the larger set.The pointer of each vertex is changed at most log n times, sinceafter each Union operation the pointer points to a set whose size isat least twice the size of the previous set.All together, for the n − 1 Union operations, for all vertices,O(n log n) complexity.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 39 / 59

Page 49: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Implementation Complexity

n − 1 Union operations: O(n log n).

n Make-Set operations: n ·Θ(1) = Θ(n).

O(m) Find-Set operations: O(m) ·Θ(1) = Θ(m).

Sorting complexity: Θ(m log m) = Θ(m log n).

Kruskal algorithm complexity: Θ(m log m).

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 40 / 59

Page 50: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Implementation Complexity

n − 1 Union operations: O(n log n).

n Make-Set operations: n ·Θ(1) = Θ(n).

O(m) Find-Set operations: O(m) ·Θ(1) = Θ(m).

Sorting complexity: Θ(m log m) = Θ(m log n).

Kruskal algorithm complexity: Θ(m log m).

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 40 / 59

Page 51: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Prim Algorithm

Start with an arbitrary vertex as a singleton tree.

Maintain a minimal forest initially set to be this tree.

Find the closest vertex to the minimal forest.

Add this vertex and its closest edge to the minimal forest.

Continue until all vertices are in the minimal forest.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 41 / 59

Page 52: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Prim Algorithm

A

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 42 / 59

Page 53: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 43 / 59

Page 54: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 44 / 59

Page 55: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 45 / 59

Page 56: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 46 / 59

Page 57: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 47 / 59

Page 58: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 48 / 59

Page 59: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 49 / 59

Page 60: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 50 / 59

Page 61: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Example: Prim Algorithm

B

H

C

E

D

I

FG

4

8

8 7

9

10

21

76

414

2

11A

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 51 / 59

Page 62: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Prim Algorithm – Implementing the Greedy Idea

Data structure:A minimal tree (forest) A that will be the MST.A starting vertex r that is the root of the MST.A priority queue Q for vertices not yet in A.A distance key(v) from A for vertices in Q.A candidate edge (v ,Π(v)) for any vertex v in Q.

The greedy idea:Repeat adding to A the edge (u,Π(u)) for the vertex u that has theminimum value for key(·) in Q.Update, if necessary, the distance key(v) and the candidate edge(v ,Π(v)) for each neighbor v of u that is still in Q.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 52 / 59

Page 63: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Prim Algorithm – Implementing the Greedy Idea

Data structure:A minimal tree (forest) A that will be the MST.A starting vertex r that is the root of the MST.A priority queue Q for vertices not yet in A.A distance key(v) from A for vertices in Q.A candidate edge (v ,Π(v)) for any vertex v in Q.

The greedy idea:Repeat adding to A the edge (u,Π(u)) for the vertex u that has theminimum value for key(·) in Q.Update, if necessary, the distance key(v) and the candidate edge(v ,Π(v)) for each neighbor v of u that is still in Q.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 52 / 59

Page 64: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Prim Algorithm – Code

(1) initialize Π(r) = nil ; A = ∅; Q = V − r(2) for all u ∈ Q do key(u) =∞(3) u = r(4) Repeat(5) for each neighbor v of u do(6) if (v ∈ Q) and w(u, v) < key(v) then(7) key(v) = w(u, v); Π(v) = u(8) u =Extract-Min(Q)(9) A = A ∪ (u,Π(u))(10) until Q = ∅(11) return (A)

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 53 / 59

Page 65: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Prim Algorithm – Correctness

Assume to the contrary that the output set A is not an MST.

Let (u, v) be the first edge that was added to A such thatA ∪ (u, v) is not a minimal forest.

In particular, at that time, A is a minimal forest.

Let C = 〈Q, (V −Q)〉 be a cut.

The algorithm guarantees that C contains A.

The priority queue implies that (u, v) is a minimal crossing edgefor the cut C.

Greedy lemma⇒ A ∪ (u, v) is a minimal forest.

A contradiction.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 54 / 59

Page 66: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Prim Algorithm – Correctness

Assume to the contrary that the output set A is not an MST.

Let (u, v) be the first edge that was added to A such thatA ∪ (u, v) is not a minimal forest.

In particular, at that time, A is a minimal forest.

Let C = 〈Q, (V −Q)〉 be a cut.

The algorithm guarantees that C contains A.

The priority queue implies that (u, v) is a minimal crossing edgefor the cut C.

Greedy lemma⇒ A ∪ (u, v) is a minimal forest.

A contradiction.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 54 / 59

Page 67: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Prim Algorithm – Complexity I

Queue operations:One time building a priority queue.n − 1 times the operation Extract-Min.At most m times updating a value of a key .

An implementation with an unsorted array:Θ(n) to build a queue.Θ(n) for the Extract-Min operation.Θ(1) for the Update-Queue operation.

Unsorted array total complexity:1×Θ(n) + (n − 1)×Θ(n) + Θ(m)×Θ(1) = Θ(n2)

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 55 / 59

Page 68: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Prim Algorithm – Complexity I

Queue operations:One time building a priority queue.n − 1 times the operation Extract-Min.At most m times updating a value of a key .

An implementation with an unsorted array:Θ(n) to build a queue.Θ(n) for the Extract-Min operation.Θ(1) for the Update-Queue operation.

Unsorted array total complexity:1×Θ(n) + (n − 1)×Θ(n) + Θ(m)×Θ(1) = Θ(n2)

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 55 / 59

Page 69: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Prim Algorithm – Complexity II

Queue operations:One time building a priority queue.n − 1 times the operation Extract-Min.At most m times updating a value of a key .

An implementation with a sorted array:Θ(n) to build a queue.Θ(1) for the Extract-Min operation.Θ(n) for the Update-Queue operation.

Sorted array total complexity:1×Θ(n) + (n − 1)×Θ(1) + Θ(m)×Θ(n) = Θ(nm)

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 56 / 59

Page 70: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Prim Algorithm – Complexity III

Queue operations:One time building a priority queue.n − 1 times the operation Extract-Min.At most m times updating a value of a key .

An implementation with a heap:Θ(n) to build a queue.Θ(log n) for the Extract-Min operation.Θ(log n) for the Update-Queue operation.

Heap total complexity:1×Θ(n) + (n − 1)×Θ(log n) + Θ(m)×Θ(log n) = Θ(m log n)

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 57 / 59

Page 71: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Prim Algorithm – Complexity IV

Queue operations:One time building a priority queue.n − 1 times the operation Extract-Min.At most m times updating a value of a key .

An implementation with a Fibonacci heap:Θ(n) to build a queue.Θ(log n) for the Extract-Min operation.Θ(1) (amortized) for the Update-Queue operation.

Fibonacci heap total complexity:1×Θ(n) + (n − 1)×Θ(log n) + Θ(m)×Θ(1) = Θ(m + n log n)

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 58 / 59

Page 72: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Kruskal vs. Prim

Complexity:Kruskal is an O(m log m) algorithm.

Prim is an O(m + n log n) algorithm.

Implementation:Kruskal is a distributed algorithm.

Prim is a centralized algorithm.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 59 / 59

Page 73: Algorithms: Minimum Spanning Treesamotz/BC-ALGORITHMS/PRESENTATIONS/mst.pdf · A spanning tree: A connected sub-graph with n 1 edges. A minimum spanning tree (MST): A spanning tree

Kruskal vs. Prim

Complexity:Kruskal is an O(m log m) algorithm.

Prim is an O(m + n log n) algorithm.

Implementation:Kruskal is a distributed algorithm.

Prim is a centralized algorithm.

Amotz Bar-Noy (CUNY) Minimum Spanning Trees Spring 2012 59 / 59