1 of 38

Announcements

Project 3 will be out this weekend.

  • Significant project so don’t put off starting until the last minute!
    • Try to get the starter code compiling by the end of the weekend!
  • Larger than project 1, but not as big as project 2.
  • This project will be the closest to what you might do at an internship.
    • Using datasets.
    • Working with and improving upon an existing code base.
    • Reading javadocs and documentation.
    • Use of many tools at once (webbrowser, Maven, IntelliJ).

Midterm 2 Friday. Lecture Friday will be review time. Bring questions. I’ll mostly focus on things that can be answered in ~5 minutes or so or less I think.

2 of 38

Warm-up Problem

Given a undirected graph, determine if it contains any cycles.

  • May use any data structure or algorithm from the course so far.

1

2

3

4

5

6

0

3 of 38

Warm-up Problem

Given a undirected graph, determine if it contains any cycles.

  • May use any data structure or algorithm from the course so far.

Approach 1: Do DFS from 0 (arbitrary vertex).

  • Keep going until you see a marked vertex.
  • Potential danger:
    • 1 looks back at 0 and sees marked.
    • Solution: Just don’t count the node you came from.

Worst case runtime: Θ(V + E) -- do study guide problems to reinforce this.

1

2

3

4

5

6

0

4 of 38

Warm-up Problem

Given a undirected graph, determine if it contains any cycles.

  • May use any data structure or algorithm from the course so far.

Approach 2: Use a WeightedQuickUnionUF object.

  • For each edge, check if the two vertices are connected.
    • If not, union them.
    • If so, there is a cycle.

Worst case runtime: O(V + E log* V) if we have path compression.

1

2

3

4

5

6

0

5 of 38

CS61B

Lecture 30: Graphs IV: Minimum Spanning Trees

  • MST, Cut Property, Generic MST Algorithm
  • Prim’s Algorithm
  • Kruskal’s Algorithm�

6 of 38

Spanning Trees

Given an undirected graph, a spanning tree T is a subgraph of G, where T:

  • Is connected.
  • Is acyclic.
  • Includes all of the vertices.

Example:

  • Spanning tree is the black edges and vertices.

A minimum spanning tree is a spanning tree of minimum total weight.

  • Example: Directly connecting buildings by power lines.

These two properties make it a tree.

This makes it spanning.

7 of 38

Spanning Trees

8 of 38

How Many are Spanning Trees? http://shoutkey.com/too

9 of 38

MST Applications

Network design (e.g. bicycle routes in Seattle (left))

Medical imaging (e.g. arrangement of nuclei in cancer cells (right))

For more, see: http://www.ics.uci.edu/~eppstein/gina/mst.html

10 of 38

MST

Find the MST for the graph.

B

C

A

s

2

3

2

2

D

11 of 38

MST

Find the MST for the graph.

B

C

A

s

2

3

2

2

D

12 of 38

MST vs. SPT, http://shoutkey.com/were

Is the MST for this graph also a shortest paths tree? If so, using which node as the starting node for this SPT?

  1. A
  2. B
  3. C
  4. D
  5. No SPT is an MST.

B

C

A

s

2

3

2

2

D

13 of 38

MST vs. SPT, http://shoutkey.com/were

Is the MST for this graph also a shortest paths tree? If so, using which node as the starting node for this SPT?

  • A
  • B
  • C
  • D
  • No SPT is an MST.

B

C

A

2

3

2

2

D

s = D

14 of 38

MST vs. SPT, http://shoutkey.com/were

Is the MST for this graph also a shortest paths tree? If so, using which node as the starting node for this SPT?

  • A
  • B
  • C
  • D
  • No SPT is an MST.

B

C

A

s

2

3

2

2

D

SPT from A

B

C

A

2

3

2

2

D

SPT from B,

MST

B

C

A

s

2

3

2

2

D

SPT from C

SPT from D

s

s

s

15 of 38

MST vs. SPT, http://shoutkey.com/were

A shortest paths tree depends on the start vertex:

  • Because it tells you how to get from a source to EVERYTHING.

There is no source for a MST.

Nonetheless, the MST sometimes happens to be an SPT for a specific vertrex.�

16 of 38

Spanning Tree, http://shoutkey.com/degree

Give a valid MST for the tree below.

  • Hard B level question: Is there a node for which this is also the SPT?
  • Rephrased: is there a node whose SPT is also the MST?

  1. Yes
  2. No

2

2

2

2

1

1

1

1

1

1

1

1

1

1

1

1

0

0

17 of 38

Spanning Tree

Give a valid MST for the tree below.

  • Is there a node for which this is also the SPT?
  • No! Minimum spanning tree must include only 2 of the 2 weight edges, but the SPT always includes at least 3 of the 2 weight edges.

2

2

2

2

1

1

1

1

1

1

1

1

1

1

1

1

0

0

2

2

2

2

1

1

1

1

1

1

1

1

1

1

1

1

0

0

Example SPT from bottom left vertex:

18 of 38

A Useful Tool for Finding the MST: Cut Property

  • A cut is an assignment of a graph’s nodes to two non-empty sets.
  • A crossing edge is an edge which connects a node from one set to a node from the other set.

Cut property: Given any cut, minimum weight crossing edge is in the MST.

  • For rest of today, we’ll assume edge weights are unique.

19 of 38

Cut Property in Action: http://shoutkey.com/went

Which edge is the minimum weight edge crossing the cut {2, 3, 5, 6}?

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

1-5 0.32

2-7 0.34

4-5 0.35

1-2 0.36

4-7 0.37

0-4 0.38

6-2 0.40

3-6 0.52

6-0 0.58

6-4 0.93

20 of 38

Cut Property in Action

Which edge is the minimum weight edge crossing the cut {2, 3, 5, 6}?

  • 0-2. Must be part of the MST!

0-7 0.16

2-3 0.17

1-7 0.19

0-2 0.26

5-7 0.28

1-3 0.29

1-5 0.32

2-7 0.34

4-5 0.35

1-2 0.36

4-7 0.37

0-4 0.38

6-2 0.40

3-6 0.52

6-0 0.58

6-4 0.93

21 of 38

Cut Property Proof

Suppose that the minimum crossing edge e were not in the MST.

  • Adding e to the MST creates a cycle.
  • Some other edge f must also be a crossing edge.
  • Removing f and adding e is a lower weight spanning tree.
  • Contradiction!

22 of 38

Generic MST Finding Algorithm

Start with no edges in the MST.

  • Find a cut that has no crossing edges in the MST.
  • Add smallest crossing edge to the MST.
  • Repeat until V-1 edges.

This should work, but we need some way of finding a cut with no crossing edges!

  • Random isn’t a very good idea.

23 of 38

Prim’s Algorithm

24 of 38

Prim’s Algorithm. Demo: Link or alternate demo: Link

Start from some arbitrary start node.

  • Repeatedly add shortest edge (mark black) that has one node inside the MST under construction.
  • Repeat until V-1 edges.

Why does Prim’s work? Special case of generic algorithm.

  • Suppose we add edge e = v->w.
  • Side 1 of cut is all vertices connected to start, side 2 is all the others.
  • No crossing edge is black (all connected edges on side 1).
  • No crossing edge has lower weight (consider in increasing order).

25 of 38

Prim’s vs. Dijkstra’s (visual)

Prim’s Algorithm

Dijkstra’s Algorithm

Demos courtesy of Kevin Wayne, Princeton University

26 of 38

Prim’s vs. Dijkstra’s

Prim’s and Dijkstra’s algorithms are exactly the same, except for the following:

Visit order:

  • Dijkstra’s algorithm visits vertices in order of distance from the source.
  • Prim’s algorithm visits vertices in order of distance from the MST under construction.

Visitation:

  • Visiting a vertex in Dijkstra’s algorithm means to relax all of its edges.
  • Visiting a vertex in Prim’s algorithm means relaxing all of its edges, but under the metric of distance from tree instead of distance from source.

27 of 38

Prim’s Implementation (Pseudocode, 1/2)

Fringe is ordered by distTo tree. Must be a specialPQ like Dijkstra’s.

Get vertex closest to tree that is unvisited.

Scan means to consider all of a vertices outgoing edges.

28 of 38

Prim’s Implementation (Pseudocode, 2/2)

Important invariant, fringe must be ordered by current best known distance from tree.

Already in MST, so go to next edge.

Better path to a particular vertex found, so update current best known for that vertex (not shown in demo).

Vertex is closest, so add to MST.

29 of 38

Prim’s Runtime

What is the runtime of Prim’s algorithm?

  • Assume all priority queue operations take log(V) time.

30 of 38

Prim’s Runtime

Exactly like Dijkstra’s runtime:

  • Insertions: V, each costing O(log V) time.
  • Delete-min: V, each costing O(log V) time.
  • DecreasePriority: E, each costing O(log V) time.
    • Data structure not discussed in class.

Overall runtime, assuming E > V, we have O(E log V) runtime.

Operation

Number of Times

Time per Operation

Total Time

Insert

V

O(log V)

O(V log V)

Delete minimum

V

O(log V)

O(V log V)

Decrease priority

E

O(log V)

O(E log V)

31 of 38

Kruskal’s Algorithm

32 of 38

Kruskal’s Algorithm. Demo: Link, or alternate demo Link

Initially mark all edges gray.

  • Consider edges in increasing order of weight.
  • Add edge to MST (mark black) unless doing so creates a cycle.
  • Repeat until V-1 edges.

�Why does Kruskal’s work? Special case of generic MST algorithm.

  • Suppose we add edge e = v->w.
  • Side 1 of cut is all vertices connected to v, side 2 is everything else.
  • No crossing edge is black (since we don’t allow cycles).
  • No crossing edge has lower weight (consider in increasing order).

33 of 38

Prim’s vs. Kruskal’s

Prim’s Algorithm

Demos courtesy of Kevin Wayne, Princeton University

Kruskal’s Algorithm

34 of 38

Kruskal’s Implementation (Pseudocode)

35 of 38

Kruskal’s Runtime

Kruskal’s algorithm is O(E log E).

Note: If edges are already sorted (e.g. stored in an ordered linked list), then we can skip Build-PQ and deletion is constant time, so overall runtime is O(E log* V).

Operation

Number of Times

Time per Operation

Total Time

Build-PQ of edges

1

O(E)

O(E)

Delete minimum

E

O(log E)

O(E log E)

connect

V

O(log* V)

O(V log* V)

isConnected

E

O(log* V)

O(E log*V)

E log E if we insert repeatedly, E if we use “bottom up heapification” see HeapSort lecture.

36 of 38

Shortest Paths and MST Algorithms Summary

Question: Can we do better than O(E log* V)?

Problem

Algorithm

Runtime (if E > V)

Notes

Shortest Paths

Dijkstra’s

O(E log V)

Fails for negative weight edges.

MST

Prim’s

O(E log V)

Analogous to Dijkstra’s

MST

Kruskal’s

O(E log E)

Uses WQUPC

MST

Kruskal’s with pre-sorted edges

O(E log* V)

Uses WQUPC

37 of 38

170 Spoiler: State of the Art Compare-Based MST Algorithms

year

worst case

discovered by

1975

E log log V

Yao

1984

E log* V

Fredman-Tarjan

1986

E log (log* V)

Gabow-Galil-Spencer-Tarjan

1997

E α(V) log α(V)

Chazelle

2000

E α(V)

Chazelle

2002

optimal

Pettie-Ramachandran

???

E ???

???

38 of 38

Citations