Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

117
Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th

Transcript of Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Page 1: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Minimum Spanning Trees

Prim’s Algorithm

Kruskal’s Algorithm

Wed, July 9th

Page 2: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Outline For Today

1. Graph Theory Part 1: Trees & Properties of Trees

2. Minimum Spanning Trees

3. Prim’s Algorithm And Run-time Analysis

4. Graph Theory Part 2: Cuts & Properties of Cuts

5. Prim’s Algorithm’s Correctness & “The Cut Property”

6. Kruskal’s Algorithm

Page 3: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Outline For Today

1. Graph Theory Part 1: Trees & Properties of Trees

2. Minimum Spanning Trees

3. Prim’s Algorithm And Run-time Analysis

4. Graph Theory Part 2: Cuts & Properties of Cuts

5. Prim’s Algorithm’s Correctness & “The Cut Property”

6. Kruskal’s Algorithm

Page 4: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Outline For Today

1. Graph Theory Part 1: Trees & Properties of Trees

2. Minimum Spanning Trees

3. Prim’s Algorithm And Run-time Analysis

4. Graph Theory Part 2: Cuts & Properties of Cuts

5. Prim’s Algorithm’s Correctness & “The Cut Property”

6. Kruskal’s Algorithm

Page 5: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Tree

Definition: An undirected graph G(V, E) is a tree iff

1. G is connected

2. G is acyclic

Page 6: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Example: Tree (1)

Page 7: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Example: Not a Tree (1)

Page 8: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Example: Not a Tree (2)

Page 9: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Example: Tree (2)

Page 10: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Example: Not a Tree (3)

Page 11: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Example: Tree (3)

Page 12: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Example: Not a Tree (3)

Page 13: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Breaking a Tree Lemma

Removing any edge (u, v) from a tree

T disconnects T!

Why?No u v path can exist! Assume it did…⤳

u x1 x2 v

Contradicts acyclicity of T!

Page 14: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Reverse is Also True

Let G be a connected graph and assume

removing any edge from G disconnects it.

Then G is acyclic and hence a tree.

Why?

x1

xi xi+1

xkClaim: G cannot

contain a cycle

C

Page 15: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Proof That G Cannot Contain a Cycle C

x1

xi xi+1xk

Breaking a Cycle Lemma: Removing any

edge from a cycle cannot disconnect a

connected graph!

Take (xi xi+1), and any path P that was

using it S txi xi+1

Page 16: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Proof That G Cannot Contain a Cycle C

x1

xi xi+1xk

Breaking a Cycle Lemma: Removing any

edge from a cycle cannot disconnect a

connected graph!

Take (xi xi+1), and any path P that was

using it S txi xi+1

Page 17: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Proof That G Cannot Contain a Cycle C

x1

xi xi+1xk

Breaking a Cycle Lemma: Removing any

edge from a cycle cannot disconnect a

connected graph!

Take (xi xi+1), and any path P that was

using it S txi xi+1

xi-1

x1

xks and t are

still

connected!

Page 18: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cycle Creation Lemma

Adding any edge (u, v) to a tree T

creates a cycle!

Page 19: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cycle Creation Lemma

Adding any edge (u, v) to a tree T

creates a cycle!

Page 20: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cycle Creation Lemma

Adding any edge (u, v) to a tree T

creates a cycle!

Proof: B/c T is connected, ∃path P from u to

v.

u v

adding (u, v) closes the cycle.

Page 21: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Theorems

Every tree of n vertices contains n-1

edges!

Every n-1 acyclic set of edges is a tree

=> i.e., they connect V!

Page 22: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Outline For Today

1. Graph Theory Part 1: Trees & Properties of Trees

2. Minimum Spanning Trees

3. Prim’s Algorithm And Run-time Analysis

4. Graph Theory Part 2: Cuts & Properties of Cuts

5. Prim’s Algorithm’s Correctness & “The Cut Property”

6. Kruskal’s Algorithm

Page 23: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Minimum Spanning Tree

Input: undirected G(V, E) and arbitrary weights on the

edges

Output: A tree T* of V such that w(T*) ≤ any other T of

V

w(T) = sum of the weights of all n-1 edges in T

“spanning” tree of G(V, E) means T* has to connect

all of V

Assumptions:

1. G is connected (minor)

2. Edge weights are distinct (all of our algorithms

work w/o this assumption)

Page 24: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Spanning Tree Example

A

D

B

C

1

2

4

2.5

3

5

Page 25: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Spanning Tree Example

A

D

B

C

1

2

4

2.5

3

5

Page 26: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Spanning Tree Example

A

D

B

C

1 4

2.5

W((A,C), (C,B), (B,D)) = 1 + 2.5 + 4 = 7.5

Page 27: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Spanning Tree Example

A

D

B

C

1

2

4

2.5

3

5

Page 28: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Spanning Tree Example

A

D

B

C

1

2

4

W((A,C), (C,D), (B,D)) = 1 + 2 + 4 = 7

Page 29: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

MST Applications

Designing all kinds of networks:

datacenter networks

road networks

phone networks

Circuit Design

Clustering

Image Segmentation

Page 30: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Outline For Today

1. Graph Theory Part 1: Trees & Properties of Trees

2. Minimum Spanning Trees

3. Prim’s Algorithm And Run-time Analysis

4. Graph Theory Part 2: Cuts & Properties of Cuts

5. Prim’s Algorithm’s Correctness & “The Cut Property”

6. Kruskal’s Algorithm

Page 31: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (1)

?

Page 32: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (1)A

B C

1

46

D

Page 33: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (1)

B C

1

46

2

5

A D E

Page 34: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (1)

B C

1

46

2

5

A D E

F

3

Page 35: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (1)

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

Page 36: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (1)

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

Page 37: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (1)

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

Page 38: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (1)

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

Page 39: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (1)

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

Page 40: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (1)

B C

1

4

2A D E

F

3

G

2.5

7.5

H

7

Final Tree!

Page 41: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm

procedure prim(G(V, E)): choose any s∈V let L={s}, R=V-{s} T = {} // final tree edges

for i = 1 to n: 1. let (u, v) be the min cost edge crossing L-R 2. remove v from R and add to L

3. add (u, v) to T return T

Page 42: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

?

Page 43: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

∞∞

A

B C

1

46

D

Page 44: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

1

64

A

B C

1

46

D

Page 45: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

64

B C

1

46

2

5

A D E

Page 46: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

54

2

B C

1

46

2

5

A D E

Page 47: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

54

B C

1

46

2

5

A D E

F∞

3

Page 48: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

54

B C

1

46

2

5

A D E

F3

3

Page 49: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

54

B C

1

46

2

5

A D E

F

3

∞ G

2.5

7.5

Page 50: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

2.54

B C

1

46

2

5

A D E

F

3

7.5 G

2.5

7.5

Page 51: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

4

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

∞H

8

7

7.5

Page 52: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

4

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

7H

8

7

7.5

Page 53: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

7H

8

7

7.5

Page 54: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9 7.5

Page 55: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

Page 56: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Simulation (2)

B C

1

4

2A D E

F

3

G

2.5

7.5

H

7

Final Tree!

Page 57: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm procedure prim(G(V, E)): choose any s∈V let L={s}, R=V-{s} let mWE store for each vertex in R, its min weight edge to L. T = {} // final tree edges

for i = 1 to n: 1. let v be min mWE vertex let (u, v) be the edge minimizing v’s mWE 2. remove v from R & mWE;add to L

3. add (u, v) to T 4. update mWE for each neighbor of v in Rreturn T

Page 58: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Prim’s Algorithm Running Time

Exactly the same As Dijkstra’s

Algorithm! O((n+m)log(n)) = O(mlog(n)) when using a

priority queue backed with a heap to store “mWE”

values

When a vertex y is moved from R to L, For each (x,

y):

Dijkstra Update: val[y]=min{val[y],

shortDist[x] +

w((x, y))}

Prim Update: val[y]=min{val[y], w((x, y))}

Page 59: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Outline For Today

1. Graph Theory Part 1: Trees & Properties of Trees

2. Minimum Spanning Trees

3. Prim’s Algorithm And Run-time Analysis

4. Graph Theory Part 2: Cuts & Properties of Cuts

5. Prim’s Algorithm’s Correctness & “The Cut Property”

6. Kruskal’s Algorithm

Page 60: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cuts

A cut of G(V, E) is a slice of V into 2 non-empty

sets

A

D

B

C

1

2

4

2.5

3

5

Page 61: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cuts

A cut of G(V, E) is a slice of V into 2 non-empty

setsA

D

B

C

1

2

4

2.5

3

5

Page 62: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cuts

A cut of G(V, E) is a slice of V into 2 non-empty

setsA

D

B

C

1

2

4

2.5

3

5

Page 63: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Empty Cut Lemma

A graph G(V,

E) is not

connected

⇔∃ cut (X, Y)

with no

crossing edges

[prove as

exercise]

Page 64: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Double Cut Crossing Lemma of Cycles

Suppose a cycle C⊆ E has an edge e

crossing a cut (X, Y)

Claim: Then there is another e` ≠ e of

C that also crosses (X, Y)

Page 65: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Double Cut Crossing Lemma of Cycles

u ve

u`

v`

e`

X Y

Page 66: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Lonely Cut Corollary

Suppose there is a cut (X, Y) which

has only one edge e crossing it

Claim: e cannot be part of any cycle

C!

Page 67: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Lonely Cut Corollary

u ve

Claim: e cannot

be part of any

cycle C!

X Y

Page 68: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Outline For Today

1. Graph Theory Part 1: Trees & Properties of Trees

2. Minimum Spanning Trees

3. Prim’s Algorithm And Run-time Analysis

4. Graph Theory Part 2: Cuts & Properties of Cuts

5. Prim’s Algorithm’s Correctness & “The Cut Property”

6. Kruskal’s Algorithm

Page 69: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

For Correctness We Need To Prove 2 Things1. Outputs a Spanning Tree Tprim

2. Tprim is a minimum spanning tree

Page 70: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

1: Prim Outputs a Spanning Tree

Proof: At each iteration i, Tprim,i spans Li AND is

acyclic

By induction: Holds for base case s

Ind. Hyp. (IH).: Tprim,k spans L after kth iteration

Prim picks the min edge (u, v) from Lk to Rk.

By IH: u has a path to every vertex in Lk and vice

versa

=> with addition of (u, v), v has a path to

every

vertex in Lk and vice versa => Tprim,k+1 is

spanning

Tprim,k+1 is acyclic b/c (u, v) is the first edge from Lk

to Rk (by the lonely cut corollary)

Q.E.D

Page 71: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Plan for 2: Tprim is a Minimum Spanning Tree1. “Cut Property” of Edges

2. Argue that the Cut Property Implies Tprim is an MST

Page 72: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property of Edges

Consider any cut (X, Y) of G, and

suppose e is the minimum edge

crossing (X, Y)

Claim: e belongs to every MST

Page 73: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property of Edges

1

8

3

4

5

X Y

Page 74: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property of Edges

u v1

8

3

4

5

(u,v) belongs to

every MST!X Y

Page 75: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property => Tprim is a MST

?

Page 76: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property => Tprim is a MSTA

B C

1

46

D

Page 77: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property => Tprim is a MST

B C

1

46

2

5

A D E

Page 78: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property => Tprim is a MST

B C

1

46

2

5

A D E

F

3

Page 79: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property => Tprim is a MST

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

Page 80: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property => Tprim is a MST

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

Page 81: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property => Tprim is a MST

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

Page 82: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property => Tprim is a MST

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

Page 83: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property => Tprim is a MST

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

Page 84: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property => Tprim is a MST

Prim looks at n-1 different cuts.

Add n-1 edges that every MST

includes.

Since any spanning tree include n-1

edges

=> Tprim is “the” MST.

(with distinct edge weights this proof

also proves the uniqueness of MST)

Page 85: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Proof of Cut Property (Exchange Argument)

u ve=min

X Y

internal edges and vertices of X

internal edges and vertices of Y

Page 86: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Proof of Cut Property (Exchange Argument)

u ve=min

X YLet T be any MST that

doesn’t contain e

internal edges and vertices of X

internal edges and vertices of Y

Page 87: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Proof of Cut Property (Exchange Argument)

u v

X YLet T be any MST that

doesn’t contain e

internal edges of T in Y

internal edges of T in X

Page 88: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Proof of Cut Property (Exchange Argument)

u v

X Yadd e to T

internal edges of T in Y

internal edges of T in X

By cycle creation lemma we created a cycle.

Page 89: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Proof of Cut Property (Exchange Argument)

u

a

b

v

X Yadd e to T

internal edges of T in Y

internal edges of T in X

By Double Crossing Lemma ∃(a, b) that also crosses (X, Y)

Page 90: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Proof of Cut Property (Exchange Argument)

u

a

b

v

X Y

internal edges of T in Y

internal edges of T in X

Claim: Can safely remove (a, b) and still get a spanning tree. Why?

Page 91: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Proof of Cut Property (Exchange Argument)

u

a

b

v

X Y

internal edges of T in Y

internal edges of T in X

By Breaking a Cycle Lemma we can’t disconnect T ∪ (u, v)!

Page 92: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Proof of Cut Property (Exchange Argument)

u

a

b

v

X YT` = T + (u, v) – (a,

b)

internal edges of T in Y

internal edges of T in X

T` is still a spanning

tree and w(T`) < w(T)

Page 93: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Proof of Cut Property (Exchange Argument)Started with (X, Y) cut, and e=(u, v): min edge crossing

(X, Y)

Took any minimum spanning tree T that did not include

e.

Added e to T => created a cycle C (by cycle-creation-

lemma)

By DCL, there is another edge e` that crosses (X, Y)

Removed e` => T`

By Breaking A Cycle Lemma: T` is still connected

Therefore w(T`) < w(T) => T was not an MST.

=> Every MST has to include e!Q.E.D.

Page 94: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Summary of Prim’s Algorithm’s CorrectnessFirst proved Tprim is a spanning tree

By induction on Li

Second proved Tprim is a an minimum spanning tree

Note this version of Prim’s Correctness Proof contains:

• Exchange Argument: Cut Property

• Greedy Stays Ahead Argument: Prim adds n-1 edges

and stay ahead at each addition.

Page 95: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Outline For Today

1. Graph Theory Part 1: Trees & Properties of Trees

2. Minimum Spanning Trees

3. Prim’s Algorithm And Run-time Analysis

4. Graph Theory Part 2: Cuts & Properties of Cuts

5. Prim’s Algorithm’s Correctness & “The Cut Property”

6. Kruskal’s Algorithm

Page 96: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Simulation

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

1, 2, 2.5, 3, 4, 5, 6, 7, 7.5, 8, 9

Page 97: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Simulation

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

1, 2, 2.5, 3, 4, 5, 6, 7, 7.5, 8, 9

Page 98: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Simulation

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

1, 2, 2.5, 3, 4, 5, 6, 7, 7.5, 8, 9

Page 99: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Simulation

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

1, 2, 2.5, 3, 4, 5, 6, 7, 7.5, 8, 9

Page 100: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Simulation

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

1, 2, 2.5, 3, 4, 5, 6, 7, 7.5, 8, 9

Page 101: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Simulation

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

1, 2, 2.5, 3, 4, 5, 6, 7, 7.5, 8, 9

Creates a cycle

Page 102: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Simulation

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

1, 2, 2.5, 3, 4, 5, 6, 7, 7.5, 8, 9

Creates a cycle

Page 103: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Simulation

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

1, 2, 2.5, 3, 4, 5, 6, 7, 7.5, 8, 9

Page 104: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Simulation

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

1, 2, 2.5, 3, 4, 5, 6, 7, 7.5, 8, 9

Page 105: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Simulation

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

1, 2, 2.5, 3, 4, 5, 6, 7, 7.5, 8, 9

Creates a cycle

Page 106: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Simulation

B C

1

46

2

5

A D E

F

3

G

2.5

7.5

H

8

7

9

1, 2, 2.5, 3, 4, 5, 6, 7, 7.5, 8, 9

Creates a cycle

Page 107: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Simulation

B C

1

4

2A D E

F

3

G

2.5

7.5

H

7

Final Tree!

Same as Tprim

Page 108: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Pseudocode

procedure kruskal(G(V, E)): sort E in order of increasing

weights rename E so w(e1) < w(e2) < … < w(em) T = {} // final tree edges for i = 1 to m: if T ∪ ei=(u,v) doesn’t create cycle add ei to T return T

Page 109: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

For Correctness We Need To Prove 2 Things1. Outputs a Spanning Tree Tkrsk

2. Tkrsk is a minimum spanning tree

Page 110: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

1: Kruskal Outputs a Spanning Tree (1)

Need to prove Tkrsk is spanning AND is acyclic

Acyclic is by definition of the algorithm.

Why is Tkrsk spanning (i.e., connected)?

Recall Empty Cut Lemma:

A graph is not connected iff ∃ cut (X, Y) with no

crossing edges

If all cuts have a crossing edge -> graph is

connected!

Page 111: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

1: Kruskal Outputs a Spanning Tree (2)Consider any cut (X, Y)

Since input G is connected, ∃ edges crossing (X, Y)

Let e* be the min-weight edge

Kruskal inspects all edges

Consider the time when Kruskal inspects e*

Claim: No edge crossing (X, Y) is in Tkrsk

And adding e* to Tkrsk cannot create a cycle =>

Why?

(because of lonely cut corallary)

Therefore Kruskal will add e*, so for each cut, there

is an edge in Tkrsk crossing it => Tkrsk spans V.

Page 112: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Plan for 2: Tkrsk is a Minimum Spanning Tree1. Argue that the Cut Property Implies Tkrsk is an

MST

In particular we will argue that each edge

(u, v) added to Tkrsk is a min-edge in a cut (X,

Y) in G.

Page 113: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property => Tkrsk is a MST (1)Let (u, v) be any edge added by Kruskal’s Algorithm.

u and v are in different comp. (b/c Kruskal checks for

cycles)

ux

y

v

t

zw

Claim: (u, v) is min-edge crossing this cut!

Page 114: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Cut Property => Tkrsk is a MST (2)

(u, v) is the min edge crossing the cut b/c

Kruskal looks at edges in increasing weights.

By the Cut Property => (u, v) is in every MST.

Therefore all edges added to Tkrsk is in every

MST

=> Tkrsk is “the” MST.

Q.E.D.

Page 115: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Summary of Kruskal’s Correctness

First proved Tkrsk is a spanning tree

Acyclicity was by definition

Connectedness followed from showing that there is

an edge of Tkrsk crossing any cut (X, Y) of G, which

by the empty cut lemma implied that Tkrsk was

connected

Second proved Tkrsk was a minimum spanning tree by

arguing that any edge (u, v) if Tkrsk is a min-edge in

some cut (X, Y)

Note this version of Kruskal’s Correctness Proof

contains:

• Exchange Argument: Cut Property

• Greedy Stays Ahead Argument: Krusk adds n-1

edges and all are justified.

Page 116: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Pseudocode

procedure kruskal(G(V, E)): sort E in order of increasing

weights rename E so w(e1) < w(e2) < … < w(em) T = {} // final tree edges for i = 1 to m: if T ∪ ei=(u,v) doesn’t create cycle add ei to T return T

Page 117: Minimum Spanning Trees Prim’s Algorithm Kruskal’s Algorithm Wed, July 9th.

Kruskal’s Algorithm Naïve Run-time

O(mlog(n)) to sort the edges

m iterations

O(n) time to check for a cycle (BFS/DFS)

Total: O(mn) total time

On Monday will improve to O(mlog(n))