Lecture 15 - Stanford...

123
Lecture 15 Minimum Spanning Trees 1

Transcript of Lecture 15 - Stanford...

Page 1: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Lecture 15

Minimum Spanning Trees

1

Page 2: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Announcements

• HW7 Due Wednesday

• HW8 Out Wednesday

• HW8 is short! Nearly there!

2

Page 3: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Last time

• Greedy algorithms

• Make a series of choices.

• Choose this activity, then that one, ..

• Never backtrack.

• Show that, at each step, your choice does not rule out

success.

• At every step, there exists an optimal solution consistent with the choices we’ve made so far.

• At the end of the day:

• you’ve built only one solution,

• never having ruled out success,

• so your solution must be correct.

3

Page 4: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Today

• Greedy algorithms for Minimum Spanning Tree.

• Agenda:

1. What is a Minimum Spanning Tree?

2. Short break to introduce some graph theory tools

3. Prim’s algorithm

4. Kruskal’s algorithm

4

Page 5: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Minimum Spanning TreeSay we have an undirected weighted graph

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

A spanning tree is a tree that connects all of the vertices.

A tree is a

connected graph

with no cycles!

5

For today, we will focus on

connected graphs!

Page 6: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Minimum Spanning TreeSay we have an undirected weighted graph

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

A spanning tree is a tree that connects all of the vertices.

A tree is a

connected graph

with no cycles!

This is a

spanning tree.

The cost of a

spanning tree is

the sum of the

weights on the

edges.

It has cost 67

6

Page 7: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Minimum Spanning TreeSay we have an undirected weighted graph

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

A spanning tree is a tree that connects all of the vertices.

A tree is a

connected graph

with no cycles!

This is also a

spanning tree.

It has cost 37

7

Page 8: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Minimum Spanning TreeSay we have an undirected weighted graph

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

A spanning tree is a tree that connects all of the vertices.

minimum of minimum cost

8

Page 9: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Minimum Spanning TreeSay we have an undirected weighted graph

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

A spanning tree is a tree that connects all of the vertices.

This is a minimum

spanning tree.

It has cost 37

minimum of minimal cost

9

Page 10: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Why MSTs?

• Network design

• Connecting cities with roads/electricity/telephone/…

• cluster analysis

• eg, genetic distance

• image processing

• eg, image segmentation

• Useful primitive

• for other graph algs

Figure 2: Fully parsimonious minimal spanning tree of 933 SNPs for 282 isolates of Y. pestis colored by location.

Morelli et al. Nature genetics 201010

Page 11: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

How to find an MST?

• Today we’ll see two greedy algorithms.

• In order to prove that these greedy algorithms work, we’ll show something like:

Suppose that our choices so far

are consistent with an MST.

Then the next greedy choice that we make

is still consistent with an MST.

• This is not the only way to prove that these algorithms work!• See a different way in the Algorithms Illuminated reading.

11

Page 12: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Let’s brainstorm some greedy algorithms!

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

Think-pair-share!(You already did the thinking,

so go ahead and pair+share).

Following your pre-lecture exercise…

12

Page 13: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Brief asidefor a discussion of cuts in graphs!

13

Page 14: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Cuts in graphs

• A cut is a partition of the vertices into two parts:

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

This is the cut “{A,B,D,E} and {C,I,H,G,F}”14

Page 15: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Cuts in graphs

• One or both of the two parts might be disconnected.

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

This is the cut “{B,C,E,G,H} and {A,D,I,F}” 15

Page 16: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Cuts in graphs

• This is not a cut. Cuts are partitions of vertices.

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

16

Page 17: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Let S be a set of edges in G

• We say a cut respects S if no edges in S cross the cut.

• An edge crossing a cut is called light if it has the smallest weight of any edge crossing the cut.

DCB

A

H G F

I E

79

10

144

2

2

1

7 68

11

8

4

S is the set of thick orange edges17

Page 18: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Let S be a set of edges in G

• We say a cut respects S if no edges in S cross the cut.

• An edge crossing a cut is called light if it has the smallest weight of any edge crossing the cut.

DCB

A

H G F

I E

79

10

144

2

2

1

7 68

11

8

4

S is the set of thick orange edges

This edge is light

18

Page 19: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Lemma

• Let S be a set of edges, and consider a cut that respects S.

• Suppose there is an MST containing S.

• Let {u,v} be a light edge.

• Then there is an MST containing S ∪ {{u,v}}

DCB

A

H G F

I E

79

10

144

2

2

1

7 68

11

8

4

S is the set of thick orange edges

This edge is light

19

Page 20: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Lemma

• Let S be a set of edges, and consider a cut that respects S.

• Suppose there is an MST containing S.

• Let {u,v} be a light edge.

• Then there is an MST containing S ∪ {{u,v}}

DCB

A

H G F

I E

79

10

144

2

2

1

7 68

11

8

4

S is the set of thick orange edges

It’s ”safe” to add this edge!Aka:

If we haven’t ruled

out the possibility of

success so far, then

adding a light edge

still won’t rule it out.

20

Page 21: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Proof of Lemma

• Assume that we have:

• a cut that respects S

21

Page 22: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Proof of Lemma

• Assume that we have:

• a cut that respects S

• S is part of some MST T.

• Say that {u,v} is light.

• lowest cost crossing the cut

22

Page 23: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Proof of Lemma

• Assume that we have:

• a cut that respects S

• S is part of some MST T.

• Say that {u,v} is light.

• lowest cost crossing the cut

• If {u,v} is in T, we are done.

• T is an MST containing both {u,v} and S.

vu

23

Page 24: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Proof of Lemma

• Assume that we have:

• a cut that respects S

• S is part of some MST T.

• Say that {u,v} is light.

• lowest cost crossing the cut

• Say {u,v} is not in T.

• Note that adding

{u,v} to T will make a cycle.

u

v

Claim: Adding any additional edge to

a spanning tree will create a cycle.

Proof: Both endpoints are already in

the tree and connected to each other.

24

Page 25: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Proof of Lemma

• Assume that we have:

• a cut that respects S

• S is part of some MST T.

• Say that {u,v} is light.

• lowest cost crossing the cut

• Say {u,v} is not in T.

• Note that adding

{u,v} to T will make a cycle.

• There is at least one other edge, {x,y}, in this cycle

crossing the cut.

yx

u

v

Claim: Adding any additional edge to

a spanning tree will create a cycle.

Proof: Both endpoints are already in

the tree and connected to each other.

25

Page 26: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Proof of Lemma ctd.

• Consider swapping {u,v} for {x,y} in T.

• Call the resulting tree T’.

yx

u

v

26

Page 27: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

• Claim: T’ is still an MST.• It is still a spanning tree (why?)

• It has cost at most that of T

• because {u,v} was light.

• T had minimal cost.

• So T’ does too.

Proof of Lemma ctd.

• Consider swapping {u,v} for {x,y} in T.• Call the resulting tree T’.

yx

u

v

• So T’ is an MST

containing S and {u,v}.• This is what we wanted.

27

Page 28: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Lemma

• Let S be a set of edges, and consider a cut that respects S.

• Suppose there is an MST containing S.

• Let {u,v} be a light edge.

• Then there is an MST containing S ∪ {{u,v}}

DCB

A

H G F

I E

79

10

144

2

2

1

7 68

11

8

4

S is the set of thick orange edges

This edge is light

29

Page 29: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

End asideBack to MSTs!

30

Page 30: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Back to MSTs

• How do we find one?

• Today we’ll see two greedy algorithms.

• The strategy:

• Make a series of choices, adding edges to the tree.

• Show that each edge we add is safe to add:

• we do not rule out the possibility of success

• we will choose light edges crossing cuts and use the Lemma.

• Keep going until we have an MST.

31

Page 31: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Idea 1Start growing a tree, greedily add the shortest edge we can to grow the tree.

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

32

Page 32: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Idea 1Start growing a tree, greedily add the shortest edge we can to grow the tree.

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

33

Page 33: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Idea 1Start growing a tree, greedily add the shortest edge we can to grow the tree.

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

34

Page 34: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Idea 1Start growing a tree, greedily add the shortest edge we can to grow the tree.

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

35

Page 35: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Idea 1Start growing a tree, greedily add the shortest edge we can to grow the tree.

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

36

Page 36: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Idea 1Start growing a tree, greedily add the shortest edge we can to grow the tree.

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

37

Page 37: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Idea 1Start growing a tree, greedily add the shortest edge we can to grow the tree.

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

38

Page 38: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Idea 1Start growing a tree, greedily add the shortest edge we can to grow the tree.

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

39

Page 39: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Idea 1Start growing a tree, greedily add the shortest edge we can to grow the tree.

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

40

Page 40: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

We’ve discovered

Prim’s algorithm!

• slowPrim( G = (V,E), starting vertex s ):

• Let (s,u) be the lightest edge coming out of s.

• MST = { (s,u) }

• verticesVisited = { s, u }

• while |verticesVisited| < |V|:

• find the lightest edge {x,v} in E so that:

• x is in verticesVisited

• v is not in verticesVisited

• add {x,v} to MST

• add v to verticesVisited

• return MST Naively, the running time is O(nm):

• For each of ≤n iterations of the while loop:

• Go through all the edges.

At most n

iterations of this

while loop.

Time at most m to

go through all the

edges and find the

lightest.

41

Page 41: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Two questions

1. Does it work?

• That is, does it actually return a MST?

2. How do we actually implement this?

• the pseudocode above says “slowPrim”…

42

Page 42: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Does it work?

• We need to show that our greedy choices don’t

rule out success.

• That is, at every step:

• If there exists an MST that contains all of the edges S we have added so far…

• …then when we make our next choice {u,v}, there is still

an MST containing S and {u,v}.

• Now it is time to use our lemma!

43

Page 43: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Lemma

• Let S be a set of edges, and consider a cut that respects S.

• Suppose there is an MST containing S.

• Let {u,v} be a light edge.

• Then there is an MST containing S ∪ {{u,v}}

DCB

A

H G F

I E

79

10

144

2

2

1

7 68

11

8

4

S is the set of thick orange edges

This edge is light

44

Page 44: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Partway through Prim

• Assume that our choices S so far don’t rule out success

• There is an MST consistent with those choices

78DCB

A

H G F

I E

9

10

144

2

2

1

7 68

11

4

S is the set of

edges selected so far.

How can we use our lemma to show that our

next choice also does not rule out success?

Think-Pair-Share Terrapins

1 min. think

1 min. pair+share

45

Page 45: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Partway through Prim

• Assume that our choices S so far don’t rule out success

• There is an MST consistent with those choices

• Consider the cut {visited, unvisited}

• This cut respects S.

78DCB

A

H G F

I E

9

10

144

2

2

1

7 68

11

4

S is the set of

edges selected so far.

46

Page 46: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Partway through Prim

• Assume that our choices S so far don’t rule out success

• There is an MST consistent with these choices

• Consider the cut {visited, unvisited}

• This cut respects S.

• The edge we add next is a light edge.

• Least weight of any edge crossing the cut.

78DCB

A

H G F

I E

9

10

144

2

2

1

7 68

11

4

S is the set of

edges selected so far.

add this one next

• By the Lemma, that

edge is safe to add.

• There is still an

MST consistent

with the new

set of edges.

47

Page 47: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Hooray!

• Our greedy choices don’t rule out success.

• This is enough (along with an argument by

induction) to guarantee correctness of Prim’s algorithm.

48

Page 48: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Formally(ish)

• Inductive hypothesis:• After adding the t’th edge, there exists an MST with the

edges added so far.

• Base case:• After adding the 0’th edge, there exists an MST with the

edges added so far. YEP.

• Inductive step:• If the inductive hypothesis holds for t (aka, the choices so far

are safe), then it holds for t+1 (aka, the next edge we add is safe).

• That’s what we just showed.

• Conclusion:• After adding the n-1’st edge, there exists an MST with the

edges added so far.

• At this point we have a spanning tree, so it better be minimal.

SLIDE SKIPPED IN CLASS

49

Page 49: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Two questions

1. Does it work?

• That is, does it actually return a MST?

•Yes!

2. How do we actually implement this?

• the pseudocode above says “slowPrim”…

50

Page 50: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

How do we actually implement this?

• Each vertex keeps:

• the distance from itself to the growing spanning tree

• how to get there.

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

I’m 7 away.

C is the closest.

I can’t get to the

tree in one edge

if you can get there in one edge.

51

Page 51: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

How do we actually implement this?

• Each vertex keeps:

• the distance from itself to the growing spanning tree

• how to get there.

• Choose the closest vertex, add it.

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

I’m 7 away.

C is the closest.

I can’t get to the

tree in one edge

if you can get there in one edge.

52

Page 52: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

How do we actually implement this?

• Each vertex keeps:

• the distance from itself to the growing spanning tree

• how to get there.

• Choose the closest vertex, add it.

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

I’m 7 away.

C is the closest.

I can’t get to the

tree in one edge

if you can get there in one edge.

53

Page 53: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

How do we actually implement this?

• Each vertex keeps:

• the distance from itself to the growing spanning tree

• how to get there.

• Choose the closest vertex, add it.

• Update stored info.

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

I’m 7 away.

C is the closest.

I’m 10 away. F is

the closest.

if you can get there in one edge.

54

Page 54: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

∞∞

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

∞∞

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

x is “active”

Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

x

x

55

Page 55: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

∞∞

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

∞∞

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

x x is “active”

x Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

56

Page 56: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

∞8

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

∞4

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

x is “active”

Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

x

x

57

Page 57: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

∞8

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

∞4

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

• Mark u as reached, and add (p[u],u) to MST.

x is “active”

Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

x

x

58

Page 58: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

∞8

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

∞4

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

• Mark u as reached, and add (p[u],u) to MST.

x is “active”

Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

x

x

59

Page 59: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

∞8

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

84

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

• Mark u as reached, and add (p[u],u) to MST.

x is “active”

Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

x

x

60

Page 60: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

∞8

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

84

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

• Mark u as reached, and add (p[u],u) to MST.

x is “active”

Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

x

x

61

Page 61: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

∞8

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

84

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

• Mark u as reached, and add (p[u],u) to MST.

x is “active”

Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

x

x

62

Page 62: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

∞8

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

7

4

84

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

• Mark u as reached, and add (p[u],u) to MST.

x is “active”

Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

2

x

x

63

Page 63: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

∞8

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

7

4

84

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

• Mark u as reached, and add (p[u],u) to MST.

x is “active”

Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

2

x

x

64

Page 64: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

∞8

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

7

4

84

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

• Mark u as reached, and add (p[u],u) to MST.

x is “active”

Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

2

x

x

65

Page 65: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

67

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

7

4

84

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

• Mark u as reached, and add (p[u],u) to MST.

x is “active”

Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

2

x

x

66

Page 66: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

67

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

7

4

84

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

• Mark u as reached, and add (p[u],u) to MST.

x x is “active”

x Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

2

67

Page 67: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

67

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

7

4

84

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

• Mark u as reached, and add (p[u],u) to MST.

x x is “active”

x Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

2

68

Page 68: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

27

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

7

10

4

84

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

• Mark u as reached, and add (p[u],u) to MST.

x x is “active”

x Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

2

69

Page 69: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

27

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

7

10

4

84

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

• Mark u as reached, and add (p[u],u) to MST.

x x is “active”

x Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

2

70

Page 70: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Efficient implementationEvery vertex has a key and a parent

27

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

7

10

4

84

0

k[x]

x

k[x] is the distance of x

from the growing tree

Can’t reach x yet

• Activate the unreached vertex u with the smallest key.

• for each of u’s unreached neighbors v:

• k[v] = min( k[v], weight(u,v) )

• if k[v] updated, p[v] = u

• Mark u as reached, and add (p[u],u) to MST.

x x is “active”

x Can reach x

a b p[b] = a, meaning that

a was the vertex that

k[b] comes from.

Until all the vertices are reached:

2

etc.71

Page 71: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

This should look pretty familiar

• Very similar to Dijkstra’s algorithm!

• Differences:

1. Keep track of p[v] in order to return a tree at the end

• But Dijkstra’s can do that too, that’s not a big difference.

2. Instead of d[v] which we update by

• d[v] = min( d[v], d[u] + w(u,v) )

we keep k[v] which we update by

• k[v] = min( k[v], w(u,v) )

• To see the difference, consider:

Thing 2 is the

big difference.

U

S T

3

22

72

Page 72: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

One thing that is similar:

Running time

• Exactly the same as Dijkstra:

• O(mlog(n)) using a Red-Black tree as a priority queue.

• O(m + nlog(n)) amortized time if we use a Fibonacci Heap*.

*See CS16673

Page 73: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Two questions

1. Does it work?

• That is, does it actually return a MST?

•Yes!

2. How do we actually implement this?

• the pseudocode above says “slowPrim”…

• Implement it basically the same way

we’d implement Dijkstra!• See IPython notebook for an implementation.

74

Page 74: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

What have we learned?

• Prim’s algorithm greedily grows a tree

• smells a lot like Dijkstra’s algorithm

• It finds a Minimum Spanning Tree!

• in time O(mlog(n)) if we implement it with a Red-Black Tree.

• In amortized time O(m + nlog(n)) with a Fibonacci heap.

• To prove it worked, we followed the same recipe for greedy algorithms we saw last time.

• Show that, at every step, we don’t rule out success.

75

Page 75: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

That’s not the only greedy algorithm for MST!

76

Page 76: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

That’s not the only greedy algorithmwhat if we just always take the cheapest edge?

whether or not it’s connected to what we have so far?

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

77

Page 77: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

That’s not the only greedy algorithmwhat if we just always take the cheapest edge?

whether or not it’s connected to what we have so far?

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

78

Page 78: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

That’s not the only greedy algorithmwhat if we just always take the cheapest edge?

whether or not it’s connected to what we have so far?

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

79

Page 79: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

That’s not the only greedy algorithmwhat if we just always take the cheapest edge?

whether or not it’s connected to what we have so far?

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

80

Page 80: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

That’s not the only greedy algorithmwhat if we just always take the cheapest edge?

whether or not it’s connected to what we have so far?

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

81

Page 81: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

That’s not the only greedy algorithmwhat if we just always take the cheapest edge?

whether or not it’s connected to what we have so far?

DCB

A

H G F

I E

7

9

10

14

4

2

2

1

7 68

11

8

4

82

Page 82: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

That’s not the only greedy algorithmwhat if we just always take the cheapest edge?

whether or not it’s connected to what we have so far?

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

!!!!!

That won’t

cause a cycle

83

Page 83: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

That’s not the only greedy algorithmwhat if we just always take the cheapest edge?

whether or not it’s connected to what we have so far?

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

That won’t

cause a cycle

84

Page 84: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

That’s not the only greedy algorithmwhat if we just always take the cheapest edge?

whether or not it’s connected to what we have so far?

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

That won’t

cause a cycle

85

Page 85: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

That’s not the only greedy algorithmwhat if we just always take the cheapest edge?

whether or not it’s connected to what we have so far?

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

That won’t

cause a cycle

86

Page 86: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

That’s not the only greedy algorithmwhat if we just always take the cheapest edge?

whether or not it’s connected to what we have so far?

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

That won’t

cause a cycle

87

Page 87: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

We’ve discovered

Kruskal’s algorithm!

• slowKruskal(G = (V,E)):

• Sort the edges in E by non-decreasing weight.

• MST = {}

• for e in E (in sorted order):

• if adding e to MST won’t cause a cycle:

• add e to MST.

• return MST

Naively, the running time is ???:

• For each of m iterations of the for loop:

• Check if adding e would cause a cycle…

m iterations through this loop

How do we check this?

How would you

figure out if added e

would make a cycle

in this algorithm?

88

Page 88: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Two questions

1. Does it work?

• That is, does it actually return a MST?

2. How do we actually implement this?

• the pseudocode above says “slowKruskal”…

Let’s do this

one first

89

Page 89: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

A forest is a

collection of

disjoint trees

At each step of Kruskal’s,

we are maintaining a forest.

90

Page 90: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

A forest is a

collection of

disjoint trees

At each step of Kruskal’s,

we are maintaining a forest.

91

Page 91: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

At each step of Kruskal’s,

we are maintaining a forest.

A forest is a

collection of

disjoint trees

When we add an edge, we merge two trees:

92

Page 92: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

At each step of Kruskal’s,

we are maintaining a forest.

A forest is a

collection of

disjoint trees

When we add an edge, we merge two trees:

93

Page 93: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

At each step of Kruskal’s,

we are maintaining a forest.

A forest is a

collection of

disjoint trees

When we add an edge, we merge two trees:

We never add an edge within a tree since that would create a cycle.94

Page 94: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Keep the trees in a special data structure

“treehouse”?

95

Page 95: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Union-find data structurealso called disjoint-set data structure

• Used for storing collections of sets

• Supports:

• makeSet(u): create a set {u}

• find(u): return the set that u is in

• union(u,v): merge the set that u is in with the set that v is in.

makeSet(x)

makeSet(y)

makeSet(z)

union(x,y)

xy

z

96

Page 96: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Union-find data structurealso called disjoint-set data structure

• Used for storing collections of sets

• Supports:

• makeSet(u): create a set {u}

• find(u): return the set that u is in

• union(u,v): merge the set that u is in with the set that v is in.

makeSet(x)

makeSet(y)

makeSet(z)

union(x,y)

x y

z

97

Page 97: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Union-find data structurealso called disjoint-set data structure

• Used for storing collections of sets

• Supports:

• makeSet(u): create a set {u}

• find(u): return the set that u is in

• union(u,v): merge the set that u is in with the set that v is in.

makeSet(x)

makeSet(y)

makeSet(z)

union(x,y)

find(x)

x y

z

98

Page 98: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Kruskal pseudo-code

• kruskal(G = (V,E)):

• Sort E by weight in non-decreasing order

• MST = {} // initialize an empty tree

• for v in V:

• makeSet(v) // put each vertex in its own tree in the forest

• for (u,v) in E: // go through the edges in sorted order

• if find(u) != find(v): // if u and v are not in the same tree

• add (u,v) to MST

• union(u,v) // merge u’s tree with v’s tree

• return MST

99

Page 99: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Once more…

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

To start, every vertex is in its own tree.

100

Page 100: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Once more…

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

Then start merging.

101

Page 101: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Once more…

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

Then start merging.

102

Page 102: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Once more…

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

Then start merging.

103

Page 103: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Once more…

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

Then start merging.

104

Page 104: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Once more…

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

Then start merging.

105

Page 105: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Once more…

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

Then start merging.

106

Page 106: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Once more…

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

Then start merging.

107

Page 107: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Once more…

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

Then start merging.

Stop when we have one big tree!

108

Page 108: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Running time

• Sorting the edges takes O(m log(n))• In practice, if the weights are small integers we can use

radixSort and take time O(m)

• For the rest:• n calls to makeSet

• put each vertex in its own set

• 2m calls to find• for each edge, find its endpoints

• n-1 calls to union• we will never add more than n-1 edges to the tree,

• so we will never call union more than n-1 times.

• Total running time:• Worst-case O(mlog(n)), just like Prim with an RBtree.

• Closer to O(m) if you can do radixSort

In practice, each of

makeSet, find, and union

run in constant time*

(See book for a simpler way

which does find and union in

time O(log n)).

*technically, they run in amortized time O(!(#)), where !(#) is the inverse Ackerman function.

! # ≤ 4 provided that n is smaller than the number of atoms in the universe. 109

Page 109: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Two questions

1. Does it work?

• That is, does it actually return a MST?

2. How do we actually implement this?

• the pseudocode above says “slowKruskal”…

• Worst-case running time O(mlog(n)) using a union-find data structure.

Now that we

understand this

“tree-merging” view,

let’s do this one.

110

Page 110: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Does it work?

• We need to show that our greedy choices don’t

rule out success.

• That is, at every step:

• There exists an MST that contains all of the edges we

have added so far.

• Now it is time to use our lemma!again!

111

Page 111: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Lemma

• Let S be a set of edges, and consider a cut that respects S.

• Suppose there is an MST containing S.

• Let {u,v} be a light edge.

• Then there is an MST containing S ∪ {{u,v}}

DCB

A

H G F

I E

79

10

144

2

2

1

7 68

11

8

4

S is the set of thick orange edges

This edge is light

112

Page 112: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Partway through Kruskal

• Assume that our choices S so far don’t rule out success.

• There is an MST extending them

• The next edge we add will merge two trees, T1, T2

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

S is the set of

edges selected so far. 113

Page 113: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Partway through Kruskal

• Assume that our choices S so far don’t rule out success.

• There is an MST extending them

• The next edge we add will merge two trees, T1, T2

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

S is the set of

edges selected so far.

This is the

next edge

114

How can we use our lemma to

show that our next choice also

does not rule out success?

Think-Pair-Share Terrapins

1 minute think

1 minute pair+share

Page 114: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Partway through Kruskal

• Assume that our choices S so far don’t rule out success.

• There is an MST extending them

• The next edge we add will merge two trees, T1, T2

• Consider the cut {T1, V – T1}.

• This cut respects S

• Our new edge is light for the cut

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

S is the set of

edges selected so far.

This is the

next edge

115

Page 115: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Partway through Kruskal

• Assume that our choices S so far don’t rule out success.

• There is an MST extending them

• The next edge we add will merge two trees, T1, T2

• Consider the cut {T1, V – T1}.

• This cut respects S

• Our new edge is light for the cut

DCB

A

H G F

I E

7

9

10

144

2

2

1

7 68

11

8

4

S is the set of

edges selected so far.

This is the

next edge

• By the Lemma, that

edge is safe to add.

• There is still an

MST extending

the new set

116

Page 116: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Hooray!

• Our greedy choices don’t rule out success.

• This is enough (along with an argument by

induction) to guarantee correctness of Kruskal’salgorithm.

117

Page 117: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Two questions

1. Does it work?

• That is, does it actually return a MST?

•Yes

2. How do we actually implement this?

• the pseudocode above says “slowKruskal”…

• Using a union-find data structure!

119

Page 118: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

What have we learned?

• Kruskal’s algorithm greedily grows a forest

• It finds a Minimum Spanning Tree in time O(mlog(n))

• if we implement it with a Union-Find data structure• if the edge weights are reasonably-sized integers and we ignore the inverse

Ackerman function, basically O(m) in practice.

• To prove it worked, we followed the same recipe for

greedy algorithms we saw last time.

• Show that, at every step, we don’t rule out success.

120

Page 119: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Compare and contrast

• Prim:

• Grows a tree.

• Time O(mlog(n)) with a red-black tree

• Time O(m + nlog(n)) with a Fibonacci heap

• Kruskal:

• Grows a forest.

• Time O(mlog(n)) with a union-find data structure

• If you can do radixSort on the edge weights, morally O(m)

Prim might be a better idea

on dense graphs if you can’t

radixSort edge weights

Kruskal might be a better idea

on sparse graphs if you can

radixSort edge weights

121

Page 120: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Both Prim and Kruskal

• Greedy algorithms for MST.

• Similar reasoning:

• Optimal substructure: subgraphs generated by cuts.

• The way to make safe choices is to choose light edges

crossing the cut.

DCB

A

H G F

I E

79

10

144

2

2

1

7 68

11

8

4

S is the set of thick orange edges

This edge is light

122

Page 121: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Can we do better?State-of-the-art MST on connected undirected graphs

• Karger-Klein-Tarjan 1995:

• O(m) time randomized algorithm

• Chazelle 2000:

• O(m⋅ "($)) time deterministic algorithm

• Pettie-Ramachandran 2002:

• O time deterministic algorithm

The optimal number of comparisons

you need to solve the problem,

whatever that is…

What is this number?

Do we need that silly " $ ?

Open questions!

123

Page 122: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Recap

• Two algorithms for Minimum Spanning Tree

• Prim’s algorithm

• Kruskal’s algorithm

• Both are (more) examples of greedy algorithms!

• Make a series of choices.

• Show that at each step, your choice does not rule out

success.

• At the end of the day, you haven’t ruled out success, so you must be successful.

124

Page 123: Lecture 15 - Stanford Universityweb.stanford.edu/class/cs161/Lectures/Lecture15/Lecture15-compre… · Following your pre-lecture exercise ... This is the cut “{B,C,E,G,H} and {A,D,I,F}”

Next time

• Back to randomized algorithms

• Minimum cuts!

• Pre-lecture exercise: a probability thought experiment.

125

Before next time