68
Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Lecture 14:

Greedy

Algorithms and

the Minimum

Spanning Tree1

These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

Page 2: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Problem du Jour – Network Design

● You have a collection of cities on a map…

2

Page 3: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Problem du Jour – Network Design

● You have a collection of cities on a map…

3

Page 4: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Problem du Jour – Network Design

● You have a collection of cities on a map…

● You want to connect them all into an

electric power grid.

4

Page 5: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Problem du Jour – Network Design

● You have a collection of cities on a map…

● You want to connect them all into an

electric power grid.

● Can string transmission lines

between cities

● Every city must be

connected!5

Page 6: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

6

Page 7: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

7

Page 8: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

8

One possible solution

Page 9: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Abstract Graph Problem

● Cities form set of vertices

● All possible transmission lines are edges between vertices

● Goal is to pick a subset of edges that “spans” graph (that is, subset

that connects all vertices)

● So why not just add all possible edges?

9

Page 10: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Abstract Graph Problem

● Cities form set of vertices

● All possible transmission lines are edges between vertices

● Goal is to pick a subset of edges that “spans” graph (that is, subset

that connects all vertices)

● So why not just add all possible edges?

COST! 10

Page 11: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Adding Construction Costs

● Using edge between vertices u,v has cost w(u,v) ≥ 0

● Want to minimize total cost to connect all vertices

● Hence, pick a set T of edges that spans graph s.t.

𝑊 𝑇 = σ𝑒∈𝑇𝑤(𝑒) is minimized.

11

Page 12: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

12

Page 13: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

13

We probably don’t

want to do this!

Page 14: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Observation: Desired T is a Tree!

● We just need to connect all vertices.

● If any cycle exists, some edge can be removed without disconnecting

any vertex.

● Since edges have non-negative cost, this can only improve W(T).

● Hence, T is an (undirected) acyclic graph, also known as a tree.

14

Page 15: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

15

Possible edge set T

Page 16: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

16

T contains a cycle…

Page 17: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

17

One acyclic subset

Page 18: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

18

Another acyclic subset

(which seems better?)

Page 19: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Formal Problem: Minimum Spanning Tree

● Given undirected graph G = (V,E) with weights w(e) ≥ 0 for all e ϵ E

● Find a tree T that spans G, s.t.

𝑊 𝑇 = σ𝑒∈𝑇𝑤(𝑒) is minimized.

● T is called a minimum spanning tree of G.

19

Page 20: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Other Applications of Minimum Spanning Tree

● Other network design problems (phone, Internet, road, …)

● Clustering data points by proximity

[remove k-1 largest MST edges to form k clusters]

● Approximate answers to much harder problems (e.g.

travelling salesperson problem)

20

Page 21: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

General Approach

● Start with empty edge set T

● Keep adding edges to T, without creating a cycle, until T

spans G.

● Question: how do we know which edge to add next to

ensure that W(T) ends up being minimal?

21

Page 22: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Greedy Principle

● Define a “local” criterion to apply when picking each edge

● At each step, pick the edge that is currently best by this

criterion and add it to T.

● Keep picking edges until T spans G.

22

Page 23: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Greedy Principle Applied to MST (Prim’s Algo)

● Prim’s criterion: pick the edge e of minimum w(e) that

connects a vertex in T to a vertex not yet in T.

23

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

Page 24: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Greedy Principle Applied to MST (Prim’s Algo)

● Prim’s criterion: pick the edge e of minimum w(e) that

connects a vertex in T to a vertex not yet in T.

24

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

start

Starting vertex for

building T is arbitrary.

Page 25: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Greedy Principle Applied to MST (Prim’s Algo)

● Prim’s criterion: pick the edge e of minimum w(e) that

connects a vertex in T to a vertex not yet in T.

25

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

start

Page 26: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Greedy Principle Applied to MST (Prim’s Algo)

● Prim’s criterion: pick the edge e of minimum w(e) that

connects a vertex in T to a vertex not yet in T.

26

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

start

Page 27: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Greedy Principle Applied to MST (Prim’s Algo)

● Prim’s criterion: pick the edge e of minimum w(e) that

connects a vertex in T to a vertex not yet in T.

27

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

start

Break ties arbitrarily

between edges of

equal weight.

Page 28: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Greedy Principle Applied to MST (Prim’s Algo)

● Prim’s criterion: pick the edge e of minimum w(e) that

connects a vertex in T to a vertex not yet in T.

28

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

start

Page 29: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Greedy Principle Applied to MST (Prim’s Algo)

● Prim’s criterion: pick the edge e of minimum w(e) that

connects a vertex in T to a vertex not yet in T.

29

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

start

Page 30: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Greedy Principle Applied to MST (Prim’s Algo)

● Prim’s criterion: pick the edge e of minimum w(e) that

connects a vertex in T to a vertex not yet in T.

30

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

start

Page 31: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Greedy Principle Applied to MST (Prim’s Algo)

● Prim’s criterion: pick the edge e of minimum w(e) that

connects a vertex in T to a vertex not yet in T.

31

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

start

Page 32: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Greedy Principle Applied to MST (Prim’s Algo)

● Prim’s criterion: pick the edge e of minimum w(e) that

connects a vertex in T to a vertex not yet in T.

32

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

start

Page 33: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Greedy Principle Applied to MST (Prim’s Algo)

● Prim’s criterion: pick the edge e of minimum w(e) that

connects a vertex in T to a vertex not yet in T.

33

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

start

Page 34: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Greedy Principle Applied to MST (Prim’s Algo)

● Prim’s criterion: pick the edge e of minimum w(e) that

connects a vertex in T to a vertex not yet in T.

34

A

B C D

H G F

EI

48 7

24

9

1 2

unconnected

connected

W(T) = 37

Page 35: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Greedy Principle Applied to MST (Prim’s Algo)

● Prim’s criterion: pick the edge e of minimum w(e) that

connects a vertex in T to a vertex not yet in T.

35

A

B C D

H G F

EI

47

24

9

1 2

unconnected

connected

W(T) = 37

8

Note that G may

have multiple MSTs!

Page 36: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Why Does Prim’s Greedy Criterion Work?

● Claim: After any number of edges are chosen, algorithm’s current

edge set T is a subset of some minimum spanning tree for G.

● (Hence, once T spans all of G, T is itself an MST for G.)

36

Page 37: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Why Does Prim’s Greedy Criterion Work?

● Claim: After any number of edges are chosen, algorithm’s current

edge set T is a subset of some minimum spanning tree for G.

● (Hence, once T spans all of G, T is itself an MST for G.)

● Pf: by induction on # of edges chosen so far.

● Base: before any edges are chosen, T is empty, so is a subset of

every MST for G.

37

Page 38: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Why Does Prim’s Greedy Criterion Work?

● Claim: After any number of edges are chosen, algorithm’s current

edge set T is a subset of some minimum spanning tree for G.

● Ind: Suppose Prim’s criterion picks a next edge e.

● Let C and N be the connected and unconnected vertices of G after

picking edge set T.

38

Page 39: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Why Does Prim’s Greedy Criterion Work?

● Claim: After any number of edges are chosen, algorithm’s current

edge set T is a subset of some minimum spanning tree for G.

39

C N

vertices connected to T vertices not connected to T

e

Page 40: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Why Does Prim’s Greedy Criterion Work?

● Claim: After any number of edges are chosen, algorithm’s current

edge set T is a subset of some minimum spanning tree for G.

● By IH, T is a subset of some MST T* for G.

● Some edge e’ of T* connects C and N, as does edge e.

40

C N

vertices connected to T vertices not connected to T

e

e’

Page 41: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Why Does Prim’s Greedy Criterion Work?

● Claim: After any number of edges are chosen, algorithm’s current

edge set T is a subset of some minimum spanning tree for G.

● By IH, T is a subset of some MST T* for G.

● Some edge e’ of T* connects C and N, as does edge e.

41

C N

vertices connected to T vertices not connected to T

e = e’

If e = e’, then T U {e}

is a subset of T*,

and we are done.

Page 42: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Why Does Prim’s Greedy Criterion Work?

● Some edge e’ of T* connects C and N, as does edge e.

● If e ≠ e’, then T* U {e} (spanning tree + 1 edge) forms a cycle in G.

42

C N

vertices connected to T vertices not connected to T

e

e’

Page 43: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Why Does Prim’s Greedy Criterion Work?

● Some edge e’ of T* connects C and N, as does edge e.

● If e ≠ e’, then T* U {e} (spanning tree + 1 edge) forms a cycle in G.

● Hence, T’ = T* U {e} – {e’} is another spanning tree for G.

43

C N

vertices connected to T vertices not connected to T

e

Page 44: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Why Does Prim’s Greedy Criterion Work?

● Some edge e’ of T* connects C and N, as does edge e.

● If e ≠ e’, then T* U {e} (spanning tree + 1 edge) forms a cycle in G.

● Hence, T’ = T* U {e} – {e’} is another spanning tree for G.

● Prim’s criterion picked e instead of e’, so w(e) ≤ w(e’).

● Conclude that W(T’) = W(T*) – w(e’) + w(e) ≤ W(T*), and so T’ is a

minimum spanning tree that contains T U {e}, as claimed. QED44

Page 45: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Implementing Prim’s Algorithm

● Maintain set of unconnected vertices.

● For each unconnected vertex v, maintain v.conn, weight of lowest-

weight edge connecting v to any vertex in T.

● When we add an edge (u,v) to T, update connections to each x

adjacent to v:

If w(v,x) < x.conn, then x.conn w(v,x)

45

Page 46: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Prim’s MST Algorithm (Adding to T Not Shown)

46

● starting vertex gets v.conn = 0, all other u get u.conn = ∞

● mark all vertices as unconnected

● while (any vertex unconnected)

● v <- unconnected vertex with smallest v.conn

● for each edge (v, u)

● if (w(v, u) < u.conn)

● u.conn = w(v, u)

● mark v connected // augment partial MST with edge from T to v

Page 47: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Prim’s MST Algorithm (Adding to T Not Shown)

47

● starting vertex gets v.conn = 0, all other u get u.conn = ∞

● mark all vertices as unconnected

● while (any vertex unconnected)

● v <- unconnected vertex with smallest v.conn

● for each edge (v, u)

● if (w(v, u) < u.conn)

● u.conn = w(v, u)

● mark v connected // augment partial MST with edge from T to v

Does this

pseudocode

look familiar?

Page 48: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Dijkstra’s Shortest Path Algorithm

● starting vertex v gets v.dist 0; all other u get u.dist∞● mark all vertices as unfinished

● while (any vertex unfinished)

● v unfinished vertex with smallest v.dist

● for each edge (v,u)

● if (v.dist + w(u,v) < u.dist)

● u.dist v.dist + w(u,v) // relax!

● mark v finished

48

Page 49: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Prim vs Dijkstra

● Prim’s MST algorithm is nearly identical to Dijkstra’s shortest-path

algorithm

● Only difference is in greedy criterion for next vertex to process.

○ Dijkstra – total weight of path from start to unfinished vertex v

○ Prim – weight of last edge on path from start to unconnected vertex v

● We can use same min-first priority queue trick to efficiently select next

vertex to connect to T; for Prim’s algo, use u.conn as vertex’s key.

49

Page 50: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Prim’s MST Algorithm w/Queue

50

● v.conn = 0, D[v] = PQ.insert(start vertex v)

● For all other u:

● u.conn = ∞ , D[u] = PQ.insert(u)

● mark all vertices as unconnected

● while (PQ not empty)

● v = PQ.extractMin()

● for each edge (v, u)

● if (w(v, u) < u.conn)

● u.conn = w(v, u)

● D[u].decrease(u)

Page 51: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Prim’s MST Algorithm w/Queue

51

● v.conn = 0, D[v] = PQ.insert(start vertex v)

● For all other u:

● u.conn = ∞ , D[u] = PQ.insert(u)

● mark all vertices as unconnected

● while (PQ not empty)

● v = PQ.extractMin()

● for each edge (v, u)

● if (w(v, u) < u.conn)

● u.conn = w(v, u)

● D[u].decrease(u)

Note: book’s

pseudocode uses

common variable

names, so that Prim &

Dijkstra code, including

tree maintenance, differ

by only one line.

Page 52: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Running Time of Prim’s Algorithm

● Exactly the same analysis as for Dijkstra’s algorithm!

● Dominant cost is again heap operations.

● Algorithm runs in time Θ((|V| + |E|) log |V|) using a binary heap.

52

Page 53: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Another Greedy Criterion for MST

● Kruskal’s criterion: add to T the edge e of minimum w(e)

that does not form a cycle when combined with edges

already in T.

53

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

Page 54: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Another Greedy Criterion for MST

● Kruskal’s criterion: add to T the edge e of minimum w(e)

that does not form a cycle when combined with edges

already in T.

54

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

Page 55: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Another Greedy Criterion for MST

● Kruskal’s criterion: add to T the edge e of minimum w(e)

that does not form a cycle when combined with edges

already in T.

55

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

Page 56: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Another Greedy Criterion for MST

● Kruskal’s criterion: add to T the edge e of minimum w(e)

that does not form a cycle when combined with edges

already in T.

56

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

Page 57: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Another Greedy Criterion for MST

● Kruskal’s criterion: add to T the edge e of minimum w(e)

that does not form a cycle when combined with edges

already in T.

57

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

Page 58: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Another Greedy Criterion for MST

● Kruskal’s criterion: add to T the edge e of minimum w(e)

that does not form a cycle when combined with edges

already in T.

58

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

Page 59: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Another Greedy Criterion for MST

● Kruskal’s criterion: add to T the edge e of minimum w(e)

that does not form a cycle when combined with edges

already in T.

59

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connectedEdge I-G

forms a cycle,

so we can’t

pick it.

Page 60: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Another Greedy Criterion for MST

● Kruskal’s criterion: add to T the edge e of minimum w(e)

that does not form a cycle when combined with edges

already in T.

60

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

Page 61: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Another Greedy Criterion for MST

● Kruskal’s criterion: add to T the edge e of minimum w(e)

that does not form a cycle when combined with edges

already in T.

61

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

Page 62: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Another Greedy Criterion for MST

● Kruskal’s criterion: add to T the edge e of minimum w(e)

that does not form a cycle when combined with edges

already in T.

62

A

B C D

H G F

EI

4

8

11

8 7

2

7 64

14

9

10

1 2

unconnected

connected

Page 63: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Running time of Kruskal's Algorithm

● Stages: sort edges (O(|E| log |E|)), run main loop

63

Page 64: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Running time of Kruskal's Algorithm

● Stages: sort edges, run main loop

● Main loop: add next-smallest edge provided it does not

create cycle○ Equivalently: add next-smallest edge provided its endpoints aren't

in same connected component, i.e. their components are disjoint

○ Need disjoint-component data structure

■ Union-Find: check disjointness in O(log |V|) time

○ Main loop running time: O(|E| log |V|) <-- we can cut off after we

add |V|-1 edges

64

Page 65: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Running time of Kruskal's Algorithm

● Stages: sort edges, run main loop

● Main loop: add next-smallest edge provided it does not

create cycle○ Equivalently: add next-smallest edge provided its endpoints aren't

in same connected component, i.e. their components are disjoint

○ Need disjoint-component data structure

■ Union-Find: check disjointness in O(log |V|) time

○ Main loop running time: O(|E| log |V|) <-- we can cut off after we

add |V|-1 edges

● Total cost: O(|E|log|E| + |E|log|V|) = O(|E|log |V|)○ Last equality holds b/c E = O(|V|^2) and log(|V|^2) = 2 log |V|

○ Same as Prim's since |E| = Ω(|V|) or else MST wouldn't exist 65

Page 66: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

A Few More Words on Greedy Algorithms

● Greedy choice is a design principle for algorithms.

● Many different problems can be solved using it.

● Does it always work?

● Tune in to Studio 14 to find out!66

Page 67: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Course wrap-up: what to do next?

● Take more CSE classes (no matter your degree program)

● Join the WashU chapter of the ACM (Association

for Computing Machinery)○ Programming competitions, tech talks, course registration

discussions, social events...

● Apply to be a TA

● Be an active, CSE-literate member of society

67

Page 68: Lecture 14: Greedy Algorithms and the Minimum Spanning Tree · Lecture 14: Greedy Algorithms and the Minimum Spanning Tree 1 These slides include material originally prepared by Dr

Course wrap-up: thank you!

● Getting to know you as CSE thinkers and as people has

been a pleasure

● We've seen you work hard, grow intellectually, work

together in studio, graciously help each other and us

● We look forward to seeing you around the department and

having you as CSE colleagues

● All the best!

Thank you for a great semester!68