Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

138
Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009

Transcript of Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Page 1: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Graphs: MSTsand Shortest Paths

David Kauchak

cs161

Summer 2009

Page 2: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Administrative

Grading final grade extra credit

TA issues/concerns HW6

shorter will be due Wed. 8/12 before class

Errors in class slides and notes Anonymized scores posted

Page 3: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Shortest paths

What is the shortest path from a to d?

A

B

C E

D

Page 4: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Shortest paths

BFS

A

B

C E

D

Page 5: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Shortest paths

What is the shortest path from a to d?

A

B

C E

D

1

1

3

2

23

4

Page 6: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Shortest paths

We can still use BFS

A

B

C E

D

1

1

3

2

23

4

Page 7: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Shortest paths

We can still use BFS

A

B

C E

D

1

1

3

2

23

4

A

B

C E

D

Page 8: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Shortest paths

We can still use BFS

A

B

C E

D

Page 9: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Shortest paths

What is the problem?

A

B

C E

D

Page 10: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Shortest paths Running time is dependent on the weights

A

B

C4

1

2

A

B

C200

50

100

Page 11: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Shortest paths

A

B

C200

50

100

A

B

C

Page 12: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Shortest paths

A

B

C

Page 13: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Shortest paths

A

B

C

Page 14: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Shortest paths

A

B

C

Nothing will change as we expand the frontier until we’ve gone out 100 levels

Page 15: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Dijkstra’s algorithm

Page 16: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Dijkstra’s algorithm

Page 17: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Dijkstra’s algorithm

prev keeps track of the shortest path

Page 18: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Dijkstra’s algorithm

Page 19: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Dijkstra’s algorithm

Page 20: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Dijkstra’s algorithm

Page 21: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Single source shortest paths

All of the shortest path algorithms we’ll look at today are call “single source shortest paths” algorithms

Why?

Page 22: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B

C E

D

1

1

3

3

21

4

Page 23: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B

C E

D

1

1

3

3

21

4

Page 24: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B

C E

D

1

1

3

3

21

4

0

Heap

A 0B C D E

Page 25: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B

C E

D

1

1

3

3

21

4

0

Heap

B C D E

Page 26: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B

C E

D

1

1

3

3

21

4

0

Heap

B C D E

Page 27: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B

C E

D

1

1

3

3

21

4

1

0

Heap

C 1B D E

Page 28: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B

C E

D

1

1

3

3

21

4

1

0

Heap

C 1B D E

Page 29: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B

C E

D

1

1

3

3

21

4

3

1

0

Heap

C 1B 3D E

Page 30: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B

C E

D

1

1

3

3

21

4

3

1

0

Heap

C 1B 3D E

Page 31: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

3

A

B

C E

D

1

1

3

21

4

3

1

0

Heap

B 3D E

Page 32: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

3

A

B

C E

D

1

1

3

21

4

3

1

0

Heap

B 3D E

Page 33: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

3

A

B

C E

D

1

1

3

21

4

3

1

0

Heap

B 3D E

Page 34: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

3

A

B

C E

D

1

1

3

21

4

2

1

0

Heap

B 2D E

Page 35: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

3

A

B

C E

D

1

1

3

21

4

2

1

0

Heap

B 2D E

Page 36: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

3

A

B

C E

D

1

1

3

21

4

2

51

0

Heap

B 2E 5D

Page 37: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

3

A

B

C E

D

1

1

3

21

4

2

51

0

Heap

B 2E 5D

Frontier?

Page 38: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

3

A

B

C E

D

1

1

3

21

4

2

51

0

Heap

B 2E 5D

All nodes reachable from starting node within a given distance

Page 39: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

3

A

B

C E

D

1

1

3

21

4

2 5

31

0

Heap

E 3D 5

Page 40: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

3

A

B

C E

D

1

1

3

21

4

2 5

31

0

Heap

D 5

Page 41: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

3

A

B

C E

D

1

1

3

21

4

2 5

31

0

Heap

Page 42: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B

C E

D

1

11

2 5

31

0

Heap

3

Page 43: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Is Dijkstra’s algorithm correct?

Invariant:

Page 44: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Is Dijkstra’s algorithm correct?

Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v

Page 45: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Is Dijkstra’s algorithm correct?

Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v The only time a vertex gets visited is when the

distance from s to that vertex is smaller than the distance to any remaining vertex

Therefore, there cannot be any other path that hasn’t been visited already that would result in a shorter path

Page 46: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time?

Page 47: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time?

1 call to MakeHeap

Page 48: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time?

|V| iterations

Page 49: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time?

|V| calls

Page 50: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time?

O(|E|) calls

Page 51: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time?

Depends on the heap implementation

1 MakeHeap |V| ExtractMin |E| DecreaseKey Total

Array O(|V|) O(|V|2) O(|E|) O(|V|2)

Bin heap O(|V|) O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|)

O(|E| log |V|)

Page 52: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time?

Depends on the heap implementation

1 MakeHeap |V| ExtractMin |E| DecreaseKey Total

Array O(|V|) O(|V|2) O(|E|) O(|V|2)

Bin heap O(|V|) O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|)

O(|E| log |V|)

Is this an improvement? If |E| < |V|2 / log |V|

Page 53: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time?

Depends on the heap implementation

1 MakeHeap |V| ExtractMin |E| DecreaseKey Total

Array O(|V|) O(|V|2) O(|E|) O(|V|2)

Bin heap O(|V|) O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|)

Fib heap O(|V|) O(|V| log |V|) O(|E|) O(|V| log |V| + |E|)

O(|E| log |V|)

Page 54: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

What about Dijkstra’s on…?

A

B

C E

D11

-10

5

10

Page 55: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

What about Dijkstra’s on…?

A

B

C E

D11

-10

5

10

Page 56: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

What about Dijkstra’s on…?

A

B

C E

D11

5

10

Dijkstra’s algorithm only works for positive edge weights

Page 57: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bounding the distance

Another invariant: For each vertex v, dist[v] is an upper bound on the actual shortest distance start of at only update the value if we find a shorter distance

An update procedure

)},(][],[min{][ vuwudistvdistvdist

Page 58: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Can we ever go wrong applying this update rule? We can apply this rule as many times as we want

and will never underestimate dist[v] When will dist[v] be right?

If u is along the shortest path to v and dist[u] is correct

)},(][],[min{][ vuwudistvdistvdist

Page 59: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

dist[v] will be right if u is along the shortest path to v and dist[u] is correct

Consider the shortest path from s to v

)},(][],[min{][ vuwudistvdistvdist

s p1 vp2 p3 pk

Page 60: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

dist[v] will be right if u is along the shortest path to v and dist[u] is correct

What happens if we update all of the vertices with the above update?

)},(][],[min{][ vuwudistvdistvdist

s p1 vp2 p3 pk

Page 61: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

dist[v] will be right if u is along the shortest path to v and dist[u] is correct

What happens if we update all of the vertices with the above update?

)},(][],[min{][ vuwudistvdistvdist

s p1 vp2 p3 pk

correct

Page 62: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

dist[v] will be right if u is along the shortest path to v and dist[u] is correct

What happens if we update all of the vertices with the above update?

)},(][],[min{][ vuwudistvdistvdist

s p1 vp2 p3 pk

correct correct

Page 63: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

dist[v] will be right if u is along the shortest path to v and dist[u] is correct

Does the order that we update the vertices matter?

)},(][],[min{][ vuwudistvdistvdist

s p1 vp2 p3 pk

correct correct

Page 64: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

dist[v] will be right if u is along the shortest path to v and dist[u] is correct

How many times do we have to do this for vertex pi to have the correct shortest path from s? i times

)},(][],[min{][ vuwudistvdistvdist

s p1 vp2 p3 pk

Page 65: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

dist[v] will be right if u is along the shortest path to v and dist[u] is correct

How many times do we have to do this for vertex pi to have the correct shortest path from s? i times

)},(][],[min{][ vuwudistvdistvdist

s p1 vp2 p3 pk

correct correct

Page 66: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

dist[v] will be right if u is along the shortest path to v and dist[u] is correct

How many times do we have to do this for vertex pi to have the correct shortest path from s? i times

)},(][],[min{][ vuwudistvdistvdist

s p1 vp2 p3 pk

correct correct correct

Page 67: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

dist[v] will be right if u is along the shortest path to v and dist[u] is correct

How many times do we have to do this for vertex pi to have the correct shortest path from s? i times

)},(][],[min{][ vuwudistvdistvdist

s p1 vp2 p3 pk

correct correct correct correct

Page 68: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

dist[v] will be right if u is along the shortest path to v and dist[u] is correct

How many times do we have to do this for vertex pi to have the correct shortest path from s? i times

)},(][],[min{][ vuwudistvdistvdist

s p1 vp2 p3 pk

correct correct correct correct …

Page 69: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

dist[v] will be right if u is along the shortest path to v and dist[u] is correct

What is the longest (vetex-wise) the path from s to any node v can be? |V| - 1 edges/vertices

)},(][],[min{][ vuwudistvdistvdist

s p1 vp2 p3 pk

correct correct correct correct …

Page 70: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

Page 71: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

Initialize all the distances

Page 72: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

iterate over all edges/vertices and apply update rule

Page 73: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

Page 74: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

check for negative cycles

Page 75: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Negative cycles

A

B

C E

D11

-10

5

10

3

What is the shortest path from a to e?

Page 76: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

Page 77: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

G

S

F

E

A

D

B

C

10

8

1

-1

-1

3

1

1

2

-2

-4

How many edges is the shortest path from s to:

A:

Page 78: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

G

S

F

E

A

D

B

C

10

8

1

-1

-1

3

1

1

2

-2

-4

How many edges is the shortest path from s to:

A: 3

Page 79: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

G

S

F

E

A

D

B

C

10

8

1

-1

-1

3

1

1

2

-2

-4

How many edges is the shortest path from s to:

A: 3

B:

Page 80: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

G

S

F

E

A

D

B

C

10

8

1

-1

-1

3

1

1

2

-2

-4

How many edges is the shortest path from s to:

A: 3

B: 5

Page 81: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

G

S

F

E

A

D

B

C

10

8

1

-1

-1

3

1

1

2

-2

-4

How many edges is the shortest path from s to:

A: 3

B: 5

D:

Page 82: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

G

S

F

E

A

D

B

C

10

8

1

-1

-1

3

1

1

2

-2

-4

How many edges is the shortest path from s to:

A: 3

B: 5

D: 7

Page 83: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

G

S

F

E

A

D

B

C

10

8

1

-1

-1

3

1

1

2

-2

-4

0

Iteration: 0

Page 84: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

G

S

F

E

A

D

B

C

10

8

1

-1

-1

3

1

1

2

-2

-4

0 10

8

Iteration: 1

Page 85: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

G

S

F

E

A

D

B

C

10

8

1

-1

-1

3

1

1

2

-2

-4

0 10

12

9

8

Iteration: 2

Page 86: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

G

S

F

E

A

D

B

C

10

8

1

-1

-1

3

1

1

2

-2

-4

0 5

10

8

9

8

Iteration: 3

A has the correct distance and path

Page 87: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

G

S

F

E

A

D

B

C

10

8

1

-1

-1

3

1

1

2

-2

-4

0 5

6

11

7

9

8

Iteration: 4

Page 88: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

G

S

F

E

A

D

B

C

10

8

1

-1

-1

3

1

1

2

-2

-4

0 5

5

7

147

9

8

Iteration: 5

B has the correct distance and path

Page 89: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

G

S

F

E

A

D

B

C

10

8

1

-1

-1

3

1

1

2

-2

-4

0 5

5

6

107

9

8

Iteration: 6

Page 90: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Bellman-Ford algorithm

G

S

F

E

A

D

B

C

10

8

1

-1

-1

3

1

1

2

-2

-4

0 5

5

6

97

9

8

Iteration: 7

D (and all other nodes) have the correct distance and path

Page 91: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Correctness of Bellman-Ford

Loop invariant:

Page 92: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Correctness of Bellman-Ford

Loop invariant: After iteration i, all vertices with shortest paths from s of length i edges or less have correct distances

Page 93: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Runtime of Bellman-Ford

O(|V| |E|)

Page 94: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Runtime of Bellman-Ford

Can you modify the algorithm to run faster (in some circumstances)?

Page 95: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

All pairs shortest paths

Simple approach Call Bellman-Ford |V| times O(|V|2 |E|)

Floyd-Warshall – Θ(|V|3) Johnson’s algorithm – O(|V|2 log |V| + |V| |E|)

Page 96: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Minimum spanning trees

What is the lowest weight set of edges that connects all vertices of an undirected graph with positive weights

Input: An undirected, positive weight graph, G=(V,E)

Output: A tree T=(V,E’) where E’ E that minimizes

'

)(Ee

ewTweight

Page 97: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

MST example

A

B D

C

4

1

23

4F

E

54

6

4

A

B D

C

4

1

2

F

E

54

Page 98: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

MSTs

Can an MST have a cycle?

A

B D

C

4

1

2

F

E

54

4

Page 99: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

MSTs

Can an MST have a cycle?

A

B D

C

4

1

2

F

E

54

Page 100: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Applications?

Connectivity Networks (e.g. communications) Circuit desing/wiring

hub/spoke models (e.g. flights, transportation)

Traveling salesman problem?

Page 101: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Cuts

A cut is a partitioning of the vertices into two sets S and V-S

An edges “crosses” the cut if it connects a vertex uV and vV-S

A

B D

C

4

1

23

4F

E

54

6

4

Page 102: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Minimum cut property Given a partion S, let edge e be the minimum

cost edge that crosses the partition. Every minimum spanning tree contains edge e.

S V-S

e’

e

Consider an MST with edge e’ that is not the minimum edge

Page 103: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Minimum cut property Given a partion S, let edge e be the minimum

cost edge that crosses the partition. Every minimum spanning tree contains edge e.

S V-S

e’

e

Using e instead of e’, still connects the graph, but produces a tree with smaller weights

Page 104: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Algorithm ideas?

Given a partion S, let edge e be the minimum cost edge that crosses the partition. Every minimum spanning tree contains edge e.

Page 105: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Kruskal’s algorithm

A

B D

C

4

1

23

4F

E

54

6

4

G

MST

A

B D

C

F

E

Add smallest edge that connects two sets not already connected

Page 106: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B D

C

4

1

23

4F

E

54

6

4

G

MST

A

B D

C1

F

E

Add smallest edge that connects two sets not already connected

Kruskal’s algorithm

Page 107: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B D

C

4

1

23

4F

E

54

6

4

G

MST

A

B D

C1

2

F

E

Add smallest edge that connects two sets not already connected

Kruskal’s algorithm

Page 108: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B D

C

4

1

23

4F

E

54

6

4

G

MST

A

B D

C

4

1

2

F

E

Kruskal’s algorithmAdd smallest edge that connects two sets not already connected

Page 109: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B D

C

4

1

23

4F

E

54

6

4

G

MST

A

B D

C

4

1

2

F

E

4

Kruskal’s algorithmAdd smallest edge that connects two sets not already connected

Page 110: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

A

B D

C

4

1

23

4F

E

54

6

4

G

MST

A

B D

C

4

1

2

F

E

54

Kruskal’s algorithmAdd smallest edge that connects two sets not already connected

Page 111: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Correctness of Kruskal’s Never adds an edge that connects already

connected vertices Always adds lowest cost edge to connect two sets.

By min cut property, that edge must be part of the MST

Page 112: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time of Kruskal’s

|V| calls to MakeSet

Page 113: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time of Kruskal’s

O(|E| log |E|)

Page 114: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time of Kruskal’s

2 |E| calls to FindSet

Page 115: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time of Kruskal’s

|V| calls to Union

Page 116: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time of Kruskal’s

Disjoint set data structure

O(|E| log |E|) +

MakeSet FindSet|E| calls

Union|V| calls

Total

Linked lists |V| O(|V| |E|) |V| O(|V||E| + |E| log |E|)

O(|V| |E|)

Linked lists +heuristics

|V| O(|E| log |V|) |V| O(|E| log |V|+ |E| log |E|)

O(|E| log |E| )

Page 117: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s algorithm

Page 118: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s algorithm

Page 119: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s algorithm

Page 120: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s algorithm Start at some root node and build out the MST by

adding the lowest weighted edge at the frontier

Page 121: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s

A

B D

C

4

1

23

4F

E

54

6

4 MST

A

B D

C

F

E

Page 122: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s

A

B D

C

4

1

23

4F

E

54

6

4 MST

A

B D

C

F

E

0

Page 123: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s

A

B D

C

4

1

23

4F

E

54

6

4 MST

A

B D

C

F

E

4 5

6 0

Page 124: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s

A

B D

C

4

1

23

4F

E

54

6

4 MST

A

B D

C

F

E

4 5

6 0

Page 125: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s

A

B D

C

4

1

23

4F

E

54

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 126: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s

A

B D

C

4

1

23

4F

E

54

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 127: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s

A

B D

C

4

1

23

4F

E

54

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 128: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s

A

B D

C

4

1

23

4F

E

54

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 129: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s

A

B D

C

4

1

23

4F

E

54

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 130: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s

A

B D

C

4

1

23

4F

E

54

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 131: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s

A

B D

C

4

1

23

4F

E

54

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 132: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Prim’s

A

B D

C

4

1

23

4F

E

54

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 133: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Correctness of Prim’s?

Can we use the min-cut property? Given a partion S, let edge e be the minimum cost

edge that crosses the partition. Every minimum spanning tree contains edge e.

Let S be the set of vertices visited so far The only time we add a new edge is if it’s the

lowest weight edge from S to V-S

Page 134: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time of Prim’s

Θ(|V|)

Page 135: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time of Prim’s

Θ(|V|)

Page 136: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time of Prim’s

|V| calls to Extract-Min

Page 137: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time of Prim’s

|E| calls to Decrease-Key

Page 138: Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Running time of Prim’s

Same as Dijksta’s algorithm

1 MakeHeap |V| ExtractMin |E| DecreaseKey Total

Array O(|V|) O(|V|2) O(|E|) O(|V|2)

Bin heap O(|V|) O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|)

Fib heap O(|V|) O(|V| log |V|) O(|E|) O(|V| log |V| + |E|)

O(|E| log |V|)

Kruskal’s: O(|E| log |E| )