94
CSCB63 WINTER 2021 WEEK 5LECTURE 2-MINIMUM COST SPANNING TREES Anna Bretscher February 8, 2021 1 / 20

CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

CSCB63 WINTER 2021WEEK 5 LECTURE 2 - MINIMUM COST SPANNING TREES

Anna Bretscher

February 8, 2021

1 / 20

Page 2: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

TODAY

Kruskals Algorithm

Prims Algorithm

Dijkstra’s Algorithm

2 / 20

Page 3: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

INTRODUCTION: (EDGE-)WEIGHTED GRAPHS

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7

These are computers and costs of direct connections. What is a cheapestway to network them?

3 / 20

Page 4: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

(EDGE-)WEIGHTED GRAPH

I Many useful graphs have numbers or weights assigned to edges.

I Think of each edge e having a price tag w(e).I Usually w(e) ≥ 0. Some cases have w(e) < 0.

A weighted (edge-weighted) graph consists of:

I a set of vertices VI a set of edges E

I weights: a map from edges to numbers w : E → R

I undirected graphs: {u,v }= {v ,u}, same weightI directed graphs: (u,v) and (v ,u) may have different weights

I Notation: w(u,v) or w(e) or weight(u,v) etc.

4 / 20

Page 5: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

(EDGE-)WEIGHTED GRAPH

I Many useful graphs have numbers or weights assigned to edges.I Think of each edge e having a price tag w(e).

I Usually w(e) ≥ 0. Some cases have w(e) < 0.

A weighted (edge-weighted) graph consists of:

I a set of vertices VI a set of edges E

I weights: a map from edges to numbers w : E → R

I undirected graphs: {u,v }= {v ,u}, same weightI directed graphs: (u,v) and (v ,u) may have different weights

I Notation: w(u,v) or w(e) or weight(u,v) etc.

4 / 20

Page 6: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

(EDGE-)WEIGHTED GRAPH

I Many useful graphs have numbers or weights assigned to edges.I Think of each edge e having a price tag w(e).I Usually w(e) ≥ 0. Some cases have w(e) < 0.

A weighted (edge-weighted) graph consists of:

I a set of vertices VI a set of edges E

I weights: a map from edges to numbers w : E → R

I undirected graphs: {u,v }= {v ,u}, same weightI directed graphs: (u,v) and (v ,u) may have different weights

I Notation: w(u,v) or w(e) or weight(u,v) etc.

4 / 20

Page 7: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

(EDGE-)WEIGHTED GRAPH

I Many useful graphs have numbers or weights assigned to edges.I Think of each edge e having a price tag w(e).I Usually w(e) ≥ 0. Some cases have w(e) < 0.

A weighted (edge-weighted) graph consists of:

I a set of vertices VI a set of edges E

I weights: a map from edges to numbers w : E → R

I undirected graphs: {u,v }= {v ,u}, same weightI directed graphs: (u,v) and (v ,u) may have different weights

I Notation: w(u,v) or w(e) or weight(u,v) etc.

4 / 20

Page 8: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

(EDGE-)WEIGHTED GRAPH

I Many useful graphs have numbers or weights assigned to edges.I Think of each edge e having a price tag w(e).I Usually w(e) ≥ 0. Some cases have w(e) < 0.

A weighted (edge-weighted) graph consists of:I a set of vertices V

I a set of edges E

I weights: a map from edges to numbers w : E → R

I undirected graphs: {u,v }= {v ,u}, same weightI directed graphs: (u,v) and (v ,u) may have different weights

I Notation: w(u,v) or w(e) or weight(u,v) etc.

4 / 20

Page 9: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

(EDGE-)WEIGHTED GRAPH

I Many useful graphs have numbers or weights assigned to edges.I Think of each edge e having a price tag w(e).I Usually w(e) ≥ 0. Some cases have w(e) < 0.

A weighted (edge-weighted) graph consists of:I a set of vertices VI a set of edges E

I weights: a map from edges to numbers w : E → R

I undirected graphs: {u,v }= {v ,u}, same weightI directed graphs: (u,v) and (v ,u) may have different weights

I Notation: w(u,v) or w(e) or weight(u,v) etc.

4 / 20

Page 10: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

(EDGE-)WEIGHTED GRAPH

I Many useful graphs have numbers or weights assigned to edges.I Think of each edge e having a price tag w(e).I Usually w(e) ≥ 0. Some cases have w(e) < 0.

A weighted (edge-weighted) graph consists of:I a set of vertices VI a set of edges E

I weights: a map from edges to numbers w : E → R

I undirected graphs: {u,v }= {v ,u}, same weightI directed graphs: (u,v) and (v ,u) may have different weights

I Notation: w(u,v) or w(e) or weight(u,v) etc.

4 / 20

Page 11: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

(EDGE-)WEIGHTED GRAPH

I Many useful graphs have numbers or weights assigned to edges.I Think of each edge e having a price tag w(e).I Usually w(e) ≥ 0. Some cases have w(e) < 0.

A weighted (edge-weighted) graph consists of:I a set of vertices VI a set of edges E

I weights: a map from edges to numbers w : E → R

I undirected graphs: {u,v }= {v ,u}, same weight

I directed graphs: (u,v) and (v ,u) may have different weightsI Notation: w(u,v) or w(e) or weight(u,v) etc.

4 / 20

Page 12: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

(EDGE-)WEIGHTED GRAPH

I Many useful graphs have numbers or weights assigned to edges.I Think of each edge e having a price tag w(e).I Usually w(e) ≥ 0. Some cases have w(e) < 0.

A weighted (edge-weighted) graph consists of:I a set of vertices VI a set of edges E

I weights: a map from edges to numbers w : E → R

I undirected graphs: {u,v }= {v ,u}, same weightI directed graphs: (u,v) and (v ,u) may have different weights

I Notation: w(u,v) or w(e) or weight(u,v) etc.

4 / 20

Page 13: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

(EDGE-)WEIGHTED GRAPH

I Many useful graphs have numbers or weights assigned to edges.I Think of each edge e having a price tag w(e).I Usually w(e) ≥ 0. Some cases have w(e) < 0.

A weighted (edge-weighted) graph consists of:I a set of vertices VI a set of edges E

I weights: a map from edges to numbers w : E → R

I undirected graphs: {u,v }= {v ,u}, same weightI directed graphs: (u,v) and (v ,u) may have different weights

I Notation: w(u,v) or w(e) or weight(u,v) etc.

4 / 20

Page 14: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

STORING A WEIGHTED GRAPH

A B

C

D

E

4

1

5

2

Adjacency matrix:

A B C D EA 0 4 2 ∞ ∞

B 4 0 1 5 ∞

C 2 1 0 ∞ ∞

D ∞ 5 ∞ 0 ∞

E ∞ ∞ ∞ ∞ 0

Adjacency lists:

adjacency listA (B,4), (C,2)B (A,4), (C,1), (D,5)C (A,2), (B,1)D (B,5)E

5 / 20

Page 15: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

COMMON TASK #1 ON WEIGHTED GRAPHS - MINIMUM

COST SPANNING TREES

Let G = (V ,E) be a connected, undirected graph with edge weights w(e) foreach edge e ∈ E .

A spanning tree is a tree A such that every vertex v ∈ V is an endpoint of atleast one edge in A.Q. Which algorithms have we seento construct a spanning tree?

A.

A minimum cost spanning tree (MST) is a spanning tree A such that the sumof the weights is minimum for all possible spanning trees B.

w(A) =∑e∈A

w(e) ≤ w(B)

6 / 20

Page 16: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

COMMON TASK #1 ON WEIGHTED GRAPHS - MINIMUM

COST SPANNING TREES

Let G = (V ,E) be a connected, undirected graph with edge weights w(e) foreach edge e ∈ E .A spanning tree is a tree A such that every vertex v ∈ V is an endpoint of atleast one edge in A.Q. Which algorithms have we seento construct a spanning tree?

A.

A minimum cost spanning tree (MST) is a spanning tree A such that the sumof the weights is minimum for all possible spanning trees B.

w(A) =∑e∈A

w(e) ≤ w(B)

6 / 20

Page 17: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

COMMON TASK #1 ON WEIGHTED GRAPHS - MINIMUM

COST SPANNING TREES

Let G = (V ,E) be a connected, undirected graph with edge weights w(e) foreach edge e ∈ E .A spanning tree is a tree A such that every vertex v ∈ V is an endpoint of atleast one edge in A.Q. Which algorithms have we seento construct a spanning tree?

A.

A minimum cost spanning tree (MST) is a spanning tree A such that the sumof the weights is minimum for all possible spanning trees B.

w(A) =∑e∈A

w(e) ≤ w(B)

6 / 20

Page 18: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

COMMON TASK #1 ON WEIGHTED GRAPHS - MINIMUM

COST SPANNING TREES

Let G = (V ,E) be a connected, undirected graph with edge weights w(e) foreach edge e ∈ E .A spanning tree is a tree A such that every vertex v ∈ V is an endpoint of atleast one edge in A.Q. Which algorithms have we seento construct a spanning tree?

A.

A minimum cost spanning tree (MST) is a spanning tree A such that the sumof the weights is minimum for all possible spanning trees B.

w(A) =∑e∈A

w(e) ≤ w(B)

6 / 20

Page 19: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

EXAMPLE

A B

C

D4

1

5

2

Usually just for undirected, connected graphs.

Q. How might we find a minimum spanning tree?

A.

7 / 20

Page 20: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

SAMPLE GRAPH

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7

8 / 20

Page 21: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM: PROOF OF CORRECTNESS

Kruskal’s algorithm finds an MST by

Proof by Contradiction.I

I

I

I

I

I

SAMPLE GRAPH

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7

Anna Bretscher and Albert Lai CSCB63 Winter 2018 February 7, 2018 9 / 25

9 / 20

Page 22: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM: PROOF OF CORRECTNESS

Kruskal’s algorithm finds an MST by

Proof by Contradiction.I

I

I

I

I

I

SAMPLE GRAPH

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7

Anna Bretscher and Albert Lai CSCB63 Winter 2018 February 7, 2018 9 / 25

9 / 20

Page 23: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM: PROOF OF CORRECTNESS

Kruskal’s algorithm finds an MST by

Proof by Contradiction.I

I

I

I

I

I

SAMPLE GRAPH

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7

Anna Bretscher and Albert Lai CSCB63 Winter 2018 February 7, 2018 9 / 25

9 / 20

Page 24: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM: PROOF OF CORRECTNESS

Kruskal’s algorithm finds an MST by

Proof by Contradiction.I

I

I

I

I

I

SAMPLE GRAPH

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7

Anna Bretscher and Albert Lai CSCB63 Winter 2018 February 7, 2018 9 / 25

9 / 20

Page 25: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM: PROOF OF CORRECTNESS

Kruskal’s algorithm finds an MST by

Proof by Contradiction.I

I

I

I

I

I

SAMPLE GRAPH

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7

Anna Bretscher and Albert Lai CSCB63 Winter 2018 February 7, 2018 9 / 25

9 / 20

Page 26: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM: PROOF OF CORRECTNESS

Kruskal’s algorithm finds an MST by

Proof by Contradiction.I

I

I

I

I

I

SAMPLE GRAPH

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7

Anna Bretscher and Albert Lai CSCB63 Winter 2018 February 7, 2018 9 / 25

9 / 20

Page 27: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM: PROOF OF CORRECTNESS

Kruskal’s algorithm finds an MST by

Proof by Contradiction.I

I

I

I

I

I

SAMPLE GRAPH

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7

Anna Bretscher and Albert Lai CSCB63 Winter 2018 February 7, 2018 9 / 25

9 / 20

Page 28: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM: PROOF OF CORRECTNESS

Kruskal’s algorithm finds an MST by repeatedly adding the least weight edgethat does not induce a cycle.

Proof by Contradiction.I

I

Case 1. w(e′) = wi .

Case 2. w(e′) > wi .

SAMPLE GRAPH

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7

Anna Bretscher and Albert Lai CSCB63 Winter 2018 February 7, 2018 9 / 25

10 / 20

Page 29: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM: PROOF OF CORRECTNESS

Kruskal’s algorithm finds an MST by repeatedly adding the least weight edgethat does not induce a cycle.

Proof by Contradiction.I

I

Case 1. w(e′) = wi .

Case 2. w(e′) > wi .

SAMPLE GRAPH

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7

Anna Bretscher and Albert Lai CSCB63 Winter 2018 February 7, 2018 9 / 25

10 / 20

Page 30: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM: PROOF OF CORRECTNESS

Kruskal’s algorithm finds an MST by repeatedly adding the least weight edgethat does not induce a cycle.

Proof by Contradiction.I

I

Case 1. w(e′) = wi .

Case 2. w(e′) > wi .

SAMPLE GRAPH

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7

Anna Bretscher and Albert Lai CSCB63 Winter 2018 February 7, 2018 9 / 25

10 / 20

Page 31: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM: PROOF OF CORRECTNESS

Kruskal’s algorithm finds an MST by repeatedly adding the least weight edgethat does not induce a cycle.

Proof by Contradiction.I

I

Case 1. w(e′) = wi .

Case 2. w(e′) > wi .

SAMPLE GRAPH

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7

Anna Bretscher and Albert Lai CSCB63 Winter 2018 February 7, 2018 9 / 25

10 / 20

Page 32: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM

Q. How should we store the edges sorted by non-decreasing weight?

A.

Q. How can we add edges and make sure that no cycle is induced.

A.

Kruskal(E, V)

S := new container() for chosen edges

PQ := min priority queue of edges and weights

for each vertex v:

v.cluster := {v}

while not PQ.is_empty():{u,v} = PQ.extract_min():

if u.cluster , v.cluster:

S.add({u,v})

union(u.cluster, v.cluster)

return S

11 / 20

Page 33: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM

Q. How should we store the edges sorted by non-decreasing weight?

A.

Q. How can we add edges and make sure that no cycle is induced.

A.

Kruskal(E, V)

S := new container() for chosen edges

PQ := min priority queue of edges and weights

for each vertex v:

v.cluster := {v}

while not PQ.is_empty():{u,v} = PQ.extract_min():

if u.cluster , v.cluster:

S.add({u,v})

union(u.cluster, v.cluster)

return S

11 / 20

Page 34: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM

Q. How should we store the edges sorted by non-decreasing weight?

A.

Q. How can we add edges and make sure that no cycle is induced.

A.

Kruskal(E, V)

S := new container() for chosen edges

PQ := min priority queue of edges and weights

for each vertex v:

v.cluster := {v}

while not PQ.is_empty():{u,v} = PQ.extract_min():

if u.cluster , v.cluster:

S.add({u,v})

union(u.cluster, v.cluster)

return S

11 / 20

Page 35: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

STORING CLUSTERS: EASY WAY - LINKED LISTS

Idea.I each cluster is a linked list

I v.cluster is pointer to v ’s own linked listI u.cluster , v .cluster is pointer equality, Θ(1) timeI merging two clusters is merging two linked lists, BUT:

→ a lot of vertices need their cluster pointers updatedLuckily, if you move the smaller list to the larger one, then:

I

I If cluster size doubles, at most how many cluster updates can we do?I

We will see a faster way later in this course.

12 / 20

Page 36: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

STORING CLUSTERS: EASY WAY - LINKED LISTS

Idea.I each cluster is a linked listI v.cluster is pointer to v ’s own linked list

I u.cluster , v .cluster is pointer equality, Θ(1) timeI merging two clusters is merging two linked lists, BUT:

→ a lot of vertices need their cluster pointers updatedLuckily, if you move the smaller list to the larger one, then:

I

I If cluster size doubles, at most how many cluster updates can we do?I

We will see a faster way later in this course.

12 / 20

Page 37: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

STORING CLUSTERS: EASY WAY - LINKED LISTS

Idea.I each cluster is a linked listI v.cluster is pointer to v ’s own linked listI u.cluster , v .cluster is pointer equality, Θ(1) time

I merging two clusters is merging two linked lists, BUT:

→ a lot of vertices need their cluster pointers updatedLuckily, if you move the smaller list to the larger one, then:

I

I If cluster size doubles, at most how many cluster updates can we do?I

We will see a faster way later in this course.

12 / 20

Page 38: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

STORING CLUSTERS: EASY WAY - LINKED LISTS

Idea.I each cluster is a linked listI v.cluster is pointer to v ’s own linked listI u.cluster , v .cluster is pointer equality, Θ(1) timeI merging two clusters is merging two linked lists, BUT:

→ a lot of vertices need their cluster pointers updated

Luckily, if you move the smaller list to the larger one, then:

I

I If cluster size doubles, at most how many cluster updates can we do?I

We will see a faster way later in this course.

12 / 20

Page 39: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

STORING CLUSTERS: EASY WAY - LINKED LISTS

Idea.I each cluster is a linked listI v.cluster is pointer to v ’s own linked listI u.cluster , v .cluster is pointer equality, Θ(1) timeI merging two clusters is merging two linked lists, BUT:

→ a lot of vertices need their cluster pointers updated

Luckily, if you move the smaller list to the larger one, then:

I

I If cluster size doubles, at most how many cluster updates can we do?I

We will see a faster way later in this course.

12 / 20

Page 40: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

STORING CLUSTERS: EASY WAY - LINKED LISTS

Idea.I each cluster is a linked listI v.cluster is pointer to v ’s own linked listI u.cluster , v .cluster is pointer equality, Θ(1) timeI merging two clusters is merging two linked lists, BUT:

→ a lot of vertices need their cluster pointers updatedLuckily, if you move the smaller list to the larger one, then:I

I If cluster size doubles, at most how many cluster updates can we do?I

We will see a faster way later in this course.

12 / 20

Page 41: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

STORING CLUSTERS: EASY WAY - LINKED LISTS

Idea.I each cluster is a linked listI v.cluster is pointer to v ’s own linked listI u.cluster , v .cluster is pointer equality, Θ(1) timeI merging two clusters is merging two linked lists, BUT:

→ a lot of vertices need their cluster pointers updatedLuckily, if you move the smaller list to the larger one, then:I

I If cluster size doubles, at most how many cluster updates can we do?

I

We will see a faster way later in this course.

12 / 20

Page 42: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

STORING CLUSTERS: EASY WAY - LINKED LISTS

Idea.I each cluster is a linked listI v.cluster is pointer to v ’s own linked listI u.cluster , v .cluster is pointer equality, Θ(1) timeI merging two clusters is merging two linked lists, BUT:

→ a lot of vertices need their cluster pointers updatedLuckily, if you move the smaller list to the larger one, then:I

I If cluster size doubles, at most how many cluster updates can we do?I

We will see a faster way later in this course.

12 / 20

Page 43: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

STORING CLUSTERS: EASY WAY - LINKED LISTS

Idea.I each cluster is a linked listI v.cluster is pointer to v ’s own linked listI u.cluster , v .cluster is pointer equality, Θ(1) timeI merging two clusters is merging two linked lists, BUT:

→ a lot of vertices need their cluster pointers updatedLuckily, if you move the smaller list to the larger one, then:I

I If cluster size doubles, at most how many cluster updates can we do?I

We will see a faster way later in this course.

12 / 20

Page 44: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

STORING CLUSTERS: EASY WAY - LINKED LISTS

Idea.I each cluster is a linked listI v.cluster is pointer to v ’s own linked listI u.cluster , v .cluster is pointer equality, Θ(1) timeI merging two clusters is merging two linked lists, BUT:

→ a lot of vertices need their cluster pointers updatedLuckily, if you move the smaller list to the larger one, then:I

I If cluster size doubles, at most how many cluster updates can we do?I

We will see a faster way later in this course.

12 / 20

Page 45: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM TIME COMPLEXITY

Complexity

I Building PQ and removing edges:

I v .cluster updates:

I the rest is Θ(1) per vertex or edge

Total O(n lgn + m lgm) time worst case.

Q. What do we know about lgm and lgn?

A.

Therefore,

Faster if faster cluster implementation.

13 / 20

Page 46: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM TIME COMPLEXITY

ComplexityI Building PQ and removing edges:

I v .cluster updates:

I the rest is Θ(1) per vertex or edge

Total O(n lgn + m lgm) time worst case.

Q. What do we know about lgm and lgn?

A.

Therefore,

Faster if faster cluster implementation.

13 / 20

Page 47: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM TIME COMPLEXITY

ComplexityI Building PQ and removing edges:

I v .cluster updates:

I the rest is Θ(1) per vertex or edge

Total O(n lgn + m lgm) time worst case.

Q. What do we know about lgm and lgn?

A.

Therefore,

Faster if faster cluster implementation.

13 / 20

Page 48: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM TIME COMPLEXITY

ComplexityI Building PQ and removing edges:

I v .cluster updates:

I the rest is Θ(1) per vertex or edge

Total O(n lgn + m lgm) time worst case.

Q. What do we know about lgm and lgn?

A.

Therefore,

Faster if faster cluster implementation.

13 / 20

Page 49: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM TIME COMPLEXITY

ComplexityI Building PQ and removing edges:

I v .cluster updates:

I the rest is Θ(1) per vertex or edge

Total O(n lgn + m lgm) time worst case.

Q. What do we know about lgm and lgn?

A.

Therefore,

Faster if faster cluster implementation.

13 / 20

Page 50: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM TIME COMPLEXITY

ComplexityI Building PQ and removing edges:

I v .cluster updates:

I the rest is Θ(1) per vertex or edge

Total O(n lgn + m lgm) time worst case.

Q. What do we know about lgm and lgn?

A.

Therefore,

Faster if faster cluster implementation.

13 / 20

Page 51: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

KRUSKAL’S ALGORITHM TIME COMPLEXITY

ComplexityI Building PQ and removing edges:

I v .cluster updates:

I the rest is Θ(1) per vertex or edge

Total O(n lgn + m lgm) time worst case.

Q. What do we know about lgm and lgn?

A.

Therefore,

Faster if faster cluster implementation.

13 / 20

Page 52: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM AGAIN

Prim’s algorithm finds an MST by something similar to breadth-first search,but with a twist:

The queue is changed to a min priority queue.

The algorithm grows a tree T one edge at a time.

Priority of vertex v =

Let’s step through the example again...

14 / 20

Page 53: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM AGAIN

Prim’s algorithm finds an MST by something similar to breadth-first search,but with a twist:

The queue is changed to a min priority queue.

The algorithm grows a tree T one edge at a time.

Priority of vertex v =

Let’s step through the example again...

14 / 20

Page 54: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM AGAIN

Prim’s algorithm finds an MST by something similar to breadth-first search,but with a twist:

The queue is changed to a min priority queue.

The algorithm grows a tree T one edge at a time.

Priority of vertex v =

Let’s step through the example again...

14 / 20

Page 55: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM AGAIN

Prim’s algorithm finds an MST by something similar to breadth-first search,but with a twist:

The queue is changed to a min priority queue.

The algorithm grows a tree T one edge at a time.

Priority of vertex v =

Let’s step through the example again...

14 / 20

Page 56: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM: A FEW EXAMPLE STEPS

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7

vertex a b c d e f g h ipriority 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

pred

15 / 20

Page 57: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM: A FEW EXAMPLE STEPS

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7a

vertex b h c d e f g ipriority 4 8 ∞ ∞ ∞ ∞ ∞ ∞

pred a a

15 / 20

Page 58: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM: A FEW EXAMPLE STEPS

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7a

b

vertex h c d e f g ipriority 8 8 ∞ ∞ ∞ ∞ ∞

pred a b

15 / 20

Page 59: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM: A FEW EXAMPLE STEPS

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7a

b

h

vertex g i c d e fpriority 1 7 8 ∞ ∞ ∞

pred h h b

15 / 20

Page 60: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM: A FEW EXAMPLE STEPS

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7a

b

h

g

vertex f i c d epriority 2 6 8 ∞ ∞

pred g g b

15 / 20

Page 61: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM: A FEW EXAMPLE STEPS

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7a

b

h

g

f

vertex c i e dpriority 4 6 10 14pred f g f f

15 / 20

Page 62: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM

a

b

h

c

i

g

d

f

e

4

8

11

8 7

4

29

14

10

2

6

1

7a

b

h

g

f

vertex c i e dpriority 4 6 10 14pred f g f f

10 / 25

Prim(V, E)S := new container() for edgesPQ := new min-heap()start := pick a vertexPQ.insert(start, 0)for each vertex v , start:

# initialize pqPQ.insert(v, ∞)

while not PQ.is_empty():# add least edge to grow the treeu := PQ.extract_min()S.add({u.pred, u})

for each z in u’s adjacency list:# update priorities based on u now in Sif z in PQ && weight(u,z) < priority of z:

PQ.decrease_priority(z, weight(u,z))z.pred := u

return S

16 / 20

Page 63: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM TIME COMPLEXITY

Q. How many times does a vertex enter/leave the min-heap?

A.

Q. How many times can a vertex’s priority decrease?

A.

I Everything else, can be done in Θ(1) per vertex or per edgeI Total O((n + m) lgn) time worst case.

17 / 20

Page 64: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM TIME COMPLEXITY

Q. How many times does a vertex enter/leave the min-heap?

A.

Q. How many times can a vertex’s priority decrease?

A.

I Everything else, can be done in Θ(1) per vertex or per edgeI Total O((n + m) lgn) time worst case.

17 / 20

Page 65: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM TIME COMPLEXITY

Q. How many times does a vertex enter/leave the min-heap?

A.

Q. How many times can a vertex’s priority decrease?

A.

I Everything else, can be done in Θ(1) per vertex or per edgeI Total O((n + m) lgn) time worst case.

17 / 20

Page 66: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM TIME COMPLEXITY

Q. How many times does a vertex enter/leave the min-heap?

A.

Q. How many times can a vertex’s priority decrease?

A.

I Everything else, can be done in Θ(1) per vertex or per edgeI Total O((n + m) lgn) time worst case.

17 / 20

Page 67: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM TIME COMPLEXITY

Q. How many times does a vertex enter/leave the min-heap?

A.

Q. How many times can a vertex’s priority decrease?

A.

I Everything else, can be done in Θ(1) per vertex or per edge

I Total O((n + m) lgn) time worst case.

17 / 20

Page 68: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S ALGORITHM TIME COMPLEXITY

Q. How many times does a vertex enter/leave the min-heap?

A.

Q. How many times can a vertex’s priority decrease?

A.

I Everything else, can be done in Θ(1) per vertex or per edgeI Total O((n + m) lgn) time worst case.

17 / 20

Page 69: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S CORRECTNESS PROOF

To begin with we will first prove a useful property:

Cut Property: Let S be a nontrivial subset of V in G (i.e. S , ∅ and S , V ). If(u,v) is the lowest-cost edge crossing (S,V −S), then (u,v) is in every MSTof G.

Proof.I Suppose there exists an MST T that does not contain (u,v).

I Consider the sets S and V −S.I

I

I

I Therefore, T is not an MST.

18 / 20

Page 70: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S CORRECTNESS PROOF

To begin with we will first prove a useful property:

Cut Property: Let S be a nontrivial subset of V in G (i.e. S , ∅ and S , V ). If(u,v) is the lowest-cost edge crossing (S,V −S), then (u,v) is in every MSTof G.

Proof.I Suppose there exists an MST T that does not contain (u,v).I Consider the sets S and V −S.

I

I

I

I Therefore, T is not an MST.

18 / 20

Page 71: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S CORRECTNESS PROOF

To begin with we will first prove a useful property:

Cut Property: Let S be a nontrivial subset of V in G (i.e. S , ∅ and S , V ). If(u,v) is the lowest-cost edge crossing (S,V −S), then (u,v) is in every MSTof G.

Proof.I Suppose there exists an MST T that does not contain (u,v).I Consider the sets S and V −S.I

I

I

I Therefore, T is not an MST.

18 / 20

Page 72: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S CORRECTNESS PROOF

To begin with we will first prove a useful property:

Cut Property: Let S be a nontrivial subset of V in G (i.e. S , ∅ and S , V ). If(u,v) is the lowest-cost edge crossing (S,V −S), then (u,v) is in every MSTof G.

Proof.I Suppose there exists an MST T that does not contain (u,v).I Consider the sets S and V −S.I

I

I

I Therefore, T is not an MST.

18 / 20

Page 73: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S CORRECTNESS PROOF

To begin with we will first prove a useful property:

Cut Property: Let S be a nontrivial subset of V in G (i.e. S , ∅ and S , V ). If(u,v) is the lowest-cost edge crossing (S,V −S), then (u,v) is in every MSTof G.

Proof.I Suppose there exists an MST T that does not contain (u,v).I Consider the sets S and V −S.I

I

I

I Therefore, T is not an MST.

18 / 20

Page 74: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

PRIM’S CORRECTNESS PROOF

To begin with we will first prove a useful property:

Cut Property: Let S be a nontrivial subset of V in G (i.e. S , ∅ and S , V ). If(u,v) is the lowest-cost edge crossing (S,V −S), then (u,v) is in every MSTof G.

Proof.I Suppose there exists an MST T that does not contain (u,v).I Consider the sets S and V −S.I

I

I

I Therefore, T is not an MST.

18 / 20

Page 75: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

CORRECTNESS OF PRIM’S ALGORITHM

The correctness of Prim’s Algorithm follows...from the Cut Property.

Q. How would the argument go?

A.

I Consider optimal MST O and Prim’s Algorithm tree T .I Order edges of T according to order they are selected.I Consider the first edge e = (u,v) in the ordering that is in T but not in O.I

I

I

I

I

I

19 / 20

Page 76: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

CORRECTNESS OF PRIM’S ALGORITHM

The correctness of Prim’s Algorithm follows...from the Cut Property.

Q. How would the argument go?

A.I Consider optimal MST O and Prim’s Algorithm tree T .

I Order edges of T according to order they are selected.I Consider the first edge e = (u,v) in the ordering that is in T but not in O.I

I

I

I

I

I

19 / 20

Page 77: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

CORRECTNESS OF PRIM’S ALGORITHM

The correctness of Prim’s Algorithm follows...from the Cut Property.

Q. How would the argument go?

A.I Consider optimal MST O and Prim’s Algorithm tree T .I Order edges of T according to order they are selected.

I Consider the first edge e = (u,v) in the ordering that is in T but not in O.I

I

I

I

I

I

19 / 20

Page 78: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

CORRECTNESS OF PRIM’S ALGORITHM

The correctness of Prim’s Algorithm follows...from the Cut Property.

Q. How would the argument go?

A.I Consider optimal MST O and Prim’s Algorithm tree T .I Order edges of T according to order they are selected.I Consider the first edge e = (u,v) in the ordering that is in T but not in O.

I

I

I

I

I

I

19 / 20

Page 79: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

CORRECTNESS OF PRIM’S ALGORITHM

The correctness of Prim’s Algorithm follows...from the Cut Property.

Q. How would the argument go?

A.I Consider optimal MST O and Prim’s Algorithm tree T .I Order edges of T according to order they are selected.I Consider the first edge e = (u,v) in the ordering that is in T but not in O.I

I

I

I

I

I

19 / 20

Page 80: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

CORRECTNESS OF PRIM’S ALGORITHM

The correctness of Prim’s Algorithm follows...from the Cut Property.

Q. How would the argument go?

A.I Consider optimal MST O and Prim’s Algorithm tree T .I Order edges of T according to order they are selected.I Consider the first edge e = (u,v) in the ordering that is in T but not in O.I

I

I

I

I

I

19 / 20

Page 81: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

CORRECTNESS OF PRIM’S ALGORITHM

The correctness of Prim’s Algorithm follows...from the Cut Property.

Q. How would the argument go?

A.I Consider optimal MST O and Prim’s Algorithm tree T .I Order edges of T according to order they are selected.I Consider the first edge e = (u,v) in the ordering that is in T but not in O.I

I

I

I

I

I

19 / 20

Page 82: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

CORRECTNESS OF PRIM’S ALGORITHM

The correctness of Prim’s Algorithm follows...from the Cut Property.

Q. How would the argument go?

A.I Consider optimal MST O and Prim’s Algorithm tree T .I Order edges of T according to order they are selected.I Consider the first edge e = (u,v) in the ordering that is in T but not in O.I

I

I

I

I

I

19 / 20

Page 83: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

CORRECTNESS OF PRIM’S ALGORITHM

The correctness of Prim’s Algorithm follows...from the Cut Property.

Q. How would the argument go?

A.I Consider optimal MST O and Prim’s Algorithm tree T .I Order edges of T according to order they are selected.I Consider the first edge e = (u,v) in the ordering that is in T but not in O.I

I

I

I

I

I

19 / 20

Page 84: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

CORRECTNESS OF PRIM’S ALGORITHM

The correctness of Prim’s Algorithm follows...from the Cut Property.

Q. How would the argument go?

A.I Consider optimal MST O and Prim’s Algorithm tree T .I Order edges of T according to order they are selected.I Consider the first edge e = (u,v) in the ordering that is in T but not in O.I

I

I

I

I

I

19 / 20

Page 85: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

COMMON THEME FOR GREEDY CORRECTNESS PROOFS

I Let R be our greedy rule for selecting edges.

I Consider our edge set sorted according to R.

I Let O be an optimal solution that differs from our algorithm solution T .I Consider our first edge {u,v } in the edge set ordering that differs

between O and T .I Show that by definition of R, {u,v } ∈ T .I Consider the set of selected vertices S ⊂ V (T ) when {u,v } is chosen.

By construction, u ∈ S and v ∈ V −S.I Consider the path p from u to v in O and the edge e ∈ p that crosses

from S to V −S.I Show that swapping e and {u,v } in O maintains the MST properties of

O either improves or maintains the optimality of O.

? You may find it helpful to know that many greedy algorithm proofs (forother types of problems) follow a similar template.

? L02’s notes have a different but similar template - another perspective.

20 / 20

Page 86: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

COMMON THEME FOR GREEDY CORRECTNESS PROOFS

I Let R be our greedy rule for selecting edges.

I Consider our edge set sorted according to R.

I Let O be an optimal solution that differs from our algorithm solution T .I Consider our first edge {u,v } in the edge set ordering that differs

between O and T .I Show that by definition of R, {u,v } ∈ T .I Consider the set of selected vertices S ⊂ V (T ) when {u,v } is chosen.

By construction, u ∈ S and v ∈ V −S.I Consider the path p from u to v in O and the edge e ∈ p that crosses

from S to V −S.I Show that swapping e and {u,v } in O maintains the MST properties of

O either improves or maintains the optimality of O.

? You may find it helpful to know that many greedy algorithm proofs (forother types of problems) follow a similar template.

? L02’s notes have a different but similar template - another perspective.

20 / 20

Page 87: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

COMMON THEME FOR GREEDY CORRECTNESS PROOFS

I Let R be our greedy rule for selecting edges.

I Consider our edge set sorted according to R.

I Let O be an optimal solution that differs from our algorithm solution T .

I Consider our first edge {u,v } in the edge set ordering that differsbetween O and T .

I Show that by definition of R, {u,v } ∈ T .I Consider the set of selected vertices S ⊂ V (T ) when {u,v } is chosen.

By construction, u ∈ S and v ∈ V −S.I Consider the path p from u to v in O and the edge e ∈ p that crosses

from S to V −S.I Show that swapping e and {u,v } in O maintains the MST properties of

O either improves or maintains the optimality of O.

? You may find it helpful to know that many greedy algorithm proofs (forother types of problems) follow a similar template.

? L02’s notes have a different but similar template - another perspective.

20 / 20

Page 88: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

COMMON THEME FOR GREEDY CORRECTNESS PROOFS

I Let R be our greedy rule for selecting edges.

I Consider our edge set sorted according to R.

I Let O be an optimal solution that differs from our algorithm solution T .I Consider our first edge {u,v } in the edge set ordering that differs

between O and T .

I Show that by definition of R, {u,v } ∈ T .I Consider the set of selected vertices S ⊂ V (T ) when {u,v } is chosen.

By construction, u ∈ S and v ∈ V −S.I Consider the path p from u to v in O and the edge e ∈ p that crosses

from S to V −S.I Show that swapping e and {u,v } in O maintains the MST properties of

O either improves or maintains the optimality of O.

? You may find it helpful to know that many greedy algorithm proofs (forother types of problems) follow a similar template.

? L02’s notes have a different but similar template - another perspective.

20 / 20

Page 89: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

COMMON THEME FOR GREEDY CORRECTNESS PROOFS

I Let R be our greedy rule for selecting edges.

I Consider our edge set sorted according to R.

I Let O be an optimal solution that differs from our algorithm solution T .I Consider our first edge {u,v } in the edge set ordering that differs

between O and T .I Show that by definition of R, {u,v } ∈ T .

I Consider the set of selected vertices S ⊂ V (T ) when {u,v } is chosen.By construction, u ∈ S and v ∈ V −S.

I Consider the path p from u to v in O and the edge e ∈ p that crossesfrom S to V −S.

I Show that swapping e and {u,v } in O maintains the MST properties ofO either improves or maintains the optimality of O.

? You may find it helpful to know that many greedy algorithm proofs (forother types of problems) follow a similar template.

? L02’s notes have a different but similar template - another perspective.

20 / 20

Page 90: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

COMMON THEME FOR GREEDY CORRECTNESS PROOFS

I Let R be our greedy rule for selecting edges.

I Consider our edge set sorted according to R.

I Let O be an optimal solution that differs from our algorithm solution T .I Consider our first edge {u,v } in the edge set ordering that differs

between O and T .I Show that by definition of R, {u,v } ∈ T .I Consider the set of selected vertices S ⊂ V (T ) when {u,v } is chosen.

By construction, u ∈ S and v ∈ V −S.

I Consider the path p from u to v in O and the edge e ∈ p that crossesfrom S to V −S.

I Show that swapping e and {u,v } in O maintains the MST properties ofO either improves or maintains the optimality of O.

? You may find it helpful to know that many greedy algorithm proofs (forother types of problems) follow a similar template.

? L02’s notes have a different but similar template - another perspective.

20 / 20

Page 91: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

COMMON THEME FOR GREEDY CORRECTNESS PROOFS

I Let R be our greedy rule for selecting edges.

I Consider our edge set sorted according to R.

I Let O be an optimal solution that differs from our algorithm solution T .I Consider our first edge {u,v } in the edge set ordering that differs

between O and T .I Show that by definition of R, {u,v } ∈ T .I Consider the set of selected vertices S ⊂ V (T ) when {u,v } is chosen.

By construction, u ∈ S and v ∈ V −S.I Consider the path p from u to v in O and the edge e ∈ p that crosses

from S to V −S.

I Show that swapping e and {u,v } in O maintains the MST properties ofO either improves or maintains the optimality of O.

? You may find it helpful to know that many greedy algorithm proofs (forother types of problems) follow a similar template.

? L02’s notes have a different but similar template - another perspective.

20 / 20

Page 92: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

COMMON THEME FOR GREEDY CORRECTNESS PROOFS

I Let R be our greedy rule for selecting edges.

I Consider our edge set sorted according to R.

I Let O be an optimal solution that differs from our algorithm solution T .I Consider our first edge {u,v } in the edge set ordering that differs

between O and T .I Show that by definition of R, {u,v } ∈ T .I Consider the set of selected vertices S ⊂ V (T ) when {u,v } is chosen.

By construction, u ∈ S and v ∈ V −S.I Consider the path p from u to v in O and the edge e ∈ p that crosses

from S to V −S.I Show that swapping e and {u,v } in O maintains the MST properties of

O either improves or maintains the optimality of O.

? You may find it helpful to know that many greedy algorithm proofs (forother types of problems) follow a similar template.

? L02’s notes have a different but similar template - another perspective.

20 / 20

Page 93: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

COMMON THEME FOR GREEDY CORRECTNESS PROOFS

I Let R be our greedy rule for selecting edges.

I Consider our edge set sorted according to R.

I Let O be an optimal solution that differs from our algorithm solution T .I Consider our first edge {u,v } in the edge set ordering that differs

between O and T .I Show that by definition of R, {u,v } ∈ T .I Consider the set of selected vertices S ⊂ V (T ) when {u,v } is chosen.

By construction, u ∈ S and v ∈ V −S.I Consider the path p from u to v in O and the edge e ∈ p that crosses

from S to V −S.I Show that swapping e and {u,v } in O maintains the MST properties of

O either improves or maintains the optimality of O.

? You may find it helpful to know that many greedy algorithm proofs (forother types of problems) follow a similar template.

? L02’s notes have a different but similar template - another perspective.

20 / 20

Page 94: CSCB63 Winter 2021 - Week 5 Lecture 2 - Minimum Cost Spanning Trees · 2021. 2. 8. · A spanning tree is a tree A such that every vertex v 2V is an endpoint of at least one edge

COMMON THEME FOR GREEDY CORRECTNESS PROOFS

I Let R be our greedy rule for selecting edges.

I Consider our edge set sorted according to R.

I Let O be an optimal solution that differs from our algorithm solution T .I Consider our first edge {u,v } in the edge set ordering that differs

between O and T .I Show that by definition of R, {u,v } ∈ T .I Consider the set of selected vertices S ⊂ V (T ) when {u,v } is chosen.

By construction, u ∈ S and v ∈ V −S.I Consider the path p from u to v in O and the edge e ∈ p that crosses

from S to V −S.I Show that swapping e and {u,v } in O maintains the MST properties of

O either improves or maintains the optimality of O.

? You may find it helpful to know that many greedy algorithm proofs (forother types of problems) follow a similar template.

? L02’s notes have a different but similar template - another perspective.

20 / 20