Top Banner
Shortest paths What is the shortest path from a to d? A B C E D
136

UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Feb 19, 2016

Download

Documents

Viney Gupta
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Shortest paths

What is the shortest path from a to d?

A

B

C E

D

Page 2: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Shortest paths

BFS

A

B

C E

D

Page 3: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Shortest paths

What is the shortest path from a to d?

A

B

C E

D

1

1

3

2

2 3

4

Page 4: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Shortest paths

We can still use BFS

A

B

C E

D

1

1

3

2

2 3

4

Page 5: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Shortest paths

We can still use BFS

A

B

C E

D

1

1

3

2

2 3

4

A

B

C E

D

Page 6: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Shortest paths

We can still use BFS

A

B

C E

D

Page 7: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Shortest paths

What is the problem?

A

B

C E

D

Page 8: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Shortest paths

Running time is dependent on the weights

A

B

C 4

1

2

A

B

C 200

50

100

Page 9: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Shortest paths

A

B

C 200

50

100

A

B

C

Page 10: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Shortest paths

A

B

C

Page 11: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Shortest paths

A

B

C

Page 12: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Shortest paths

A

B

C

Nothing will change as we expand the frontier

until we’ve gone out 100 levels

Page 13: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Dijkstra’s algorithm

Page 14: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Dijkstra’s algorithm

Page 15: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Dijkstra’s algorithm

prev keeps track of

the shortest path

Page 16: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Dijkstra’s algorithm

Page 17: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Dijkstra’s algorithm

Page 18: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Dijkstra’s algorithm

Page 19: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Single source shortest paths

All of the shortest path algorithms we’ll look

at today are call “single source shortest

paths” algorithms

Why?

Page 20: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B

C E

D

1

1

3

3

2 1

4

Page 21: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B

C E

D

1

1

3

3

2 1

4

Page 22: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B

C E

D

1

1

3

3

2 1

4

0

Heap

A 0

B

C

D

E

Page 23: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B

C E

D

1

1

3

3

2 1

4

0

Heap

B

C

D

E

Page 24: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B

C E

D

1

1

3

3

2 1

4

0

Heap

B

C

D

E

Page 25: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B

C E

D

1

1

3

3

2 1

4

1

0

Heap

C 1

B

D

E

Page 26: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B

C E

D

1

1

3

3

2 1

4

1

0

Heap

C 1

B

D

E

Page 27: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B

C E

D

1

1

3

3

2 1

4

3

1

0

Heap

C 1

B 3

D

E

Page 28: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B

C E

D

1

1

3

3

2 1

4

3

1

0

Heap

C 1

B 3

D

E

Page 29: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

3

A

B

C E

D

1

1

3

2 1

4

3

1

0

Heap

B 3

D

E

Page 30: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

3

A

B

C E

D

1

1

3

2 1

4

3

1

0

Heap

B 3

D

E

Page 31: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

3

A

B

C E

D

1

1

3

2 1

4

3

1

0

Heap

B 3

D

E

Page 32: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

3

A

B

C E

D

1

1

3

2 1

4

2

1

0

Heap

B 2

D

E

Page 33: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

3

A

B

C E

D

1

1

3

2 1

4

2

1

0

Heap

B 2

D

E

Page 34: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

3

A

B

C E

D

1

1

3

2 1

4

2

5 1

0

Heap

B 2

E 5

D

Page 35: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

3

A

B

C E

D

1

1

3

2 1

4

2

5 1

0

Heap

B 2

E 5

D

Frontier?

Page 36: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

3

A

B

C E

D

1

1

3

2 1

4

2

5 1

0

Heap

B 2

E 5

D

All nodes reachable

from starting node

within a given distance

Page 37: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

3

A

B

C E

D

1

1

3

2 1

4

2 5

3 1

0

Heap

E 3

D 5

Page 38: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

3

A

B

C E

D

1

1

3

2 1

4

2 5

3 1

0

Heap

D 5

Page 39: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

3

A

B

C E

D

1

1

3

2 1

4

2 5

3 1

0

Heap

Page 40: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B

C E

D

1

1 1

2 5

3 1

0

Heap

3

Page 41: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Is Dijkstra’s algorithm correct?

Invariant:

Page 42: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 43: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 44: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Running time?

Page 45: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Running time?

1 call to MakeHeap

Page 46: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Running time?

|V| iterations

Page 47: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Running time?

|V| calls

Page 48: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Running time?

O(|E|) calls

Page 49: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 50: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 51: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 52: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

What about Dijkstra’s on…?

A

B

C E

D 1

1

-10

5

10

Page 53: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

What about Dijkstra’s on…?

A

B

C E

D 1

1

-10

5

10

Page 54: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

What about Dijkstra’s on…?

A

B

C E

D 1

1

5

10

Dijkstra’s algorithm only works

for positive edge weights

Page 55: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 56: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 57: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 v p2 p3 pk

Page 58: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 v p2 p3 pk

Page 59: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 v p2 p3 pk

correct

Page 60: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 v p2 p3 pk

correct correct

Page 61: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 v p2 p3 pk

correct correct

Page 62: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 v p2 p3 pk

Page 63: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 v p2 p3 pk

correct correct

Page 64: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 v p2 p3 pk

correct correct correct

Page 65: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 v p2 p3 pk

correct correct correct correct

Page 66: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 v p2 p3 pk

correct correct correct correct …

Page 67: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 v p2 p3 pk

correct correct correct correct …

Page 68: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Bellman-Ford algorithm

Page 69: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Bellman-Ford algorithm

Initialize all the

distances

Page 70: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Bellman-Ford algorithm

iterate over all

edges/vertices

and apply update

rule

Page 71: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Bellman-Ford algorithm

Page 72: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Bellman-Ford algorithm

check for negative

cycles

Page 73: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Negative cycles

A

B

C E

D 1

1

-10

5

10

3

What is the shortest path

from a to e?

Page 74: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Bellman-Ford algorithm

Page 75: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 76: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 77: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 78: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 79: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 80: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 81: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 82: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 83: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 84: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 85: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 86: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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

14 7

9

8

Iteration: 5

B has the correct

distance and path

Page 87: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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

10 7

9

8

Iteration: 6

Page 88: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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

9 7

9

8

Iteration: 7

D (and all other

nodes) have the

correct distance

and path

Page 89: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Correctness of Bellman-Ford

Loop invariant:

Page 90: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 91: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Runtime of Bellman-Ford

O(|V| |E|)

Page 92: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Runtime of Bellman-Ford

Can you modify the algorithm to run

faster (in some circumstances)?

Page 93: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 94: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 95: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

MST example

A

B D

C

4

1

2 3

4 F

E

5 4

6

4

A

B D

C

4

1

2

F

E

5 4

Page 96: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

MSTs

Can an MST have a cycle?

A

B D

C

4

1

2

F

E

5 4

4

Page 97: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

MSTs

Can an MST have a cycle?

A

B D

C

4

1

2

F

E

5 4

Page 98: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Applications?

Connectivity

Networks (e.g. communications)

Circuit desing/wiring

hub/spoke models (e.g. flights,

transportation)

Traveling salesman problem?

Page 99: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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

2 3

4 F

E

5 4

6

4

Page 100: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 101: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 102: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 103: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Kruskal’s algorithm

A

B D

C

4

1

2 3

4 F

E

5 4

6

4

G

MST

A

B D

C

F

E

Add smallest edge that connects

two sets not already connected

Page 104: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B D

C

4

1

2 3

4 F

E

5 4

6

4

G

MST

A

B D

C 1

F

E

Add smallest edge that connects

two sets not already connected

Kruskal’s algorithm

Page 105: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B D

C

4

1

2 3

4 F

E

5 4

6

4

G

MST

A

B D

C 1

2

F

E

Add smallest edge that connects

two sets not already connected

Kruskal’s algorithm

Page 106: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B D

C

4

1

2 3

4 F

E

5 4

6

4

G

MST

A

B D

C

4

1

2

F

E

Kruskal’s algorithm

Add smallest edge that connects

two sets not already connected

Page 107: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B D

C

4

1

2 3

4 F

E

5 4

6

4

G

MST

A

B D

C

4

1

2

F

E

4

Kruskal’s algorithm

Add smallest edge that connects

two sets not already connected

Page 108: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

A

B D

C

4

1

2 3

4 F

E

5 4

6

4

G

MST

A

B D

C

4

1

2

F

E

5 4

Kruskal’s algorithm

Add smallest edge that connects

two sets not already connected

Page 109: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 110: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Running time of Kruskal’s

|V| calls to MakeSet

Page 111: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Running time of Kruskal’s

O(|E| log |E|)

Page 112: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Running time of Kruskal’s

2 |E| calls to FindSet

Page 113: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Running time of Kruskal’s

|V| calls to Union

Page 114: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 115: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s algorithm

Page 116: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s algorithm

Page 117: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s algorithm

Page 118: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s algorithm

Start at some root node and build out the MST by adding the lowest weighted edge at the frontier

Page 119: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s

A

B D

C

4

1

2 3

4 F

E

5 4

6

4 MST

A

B D

C

F

E

Page 120: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s

A

B D

C

4

1

2 3

4 F

E

5 4

6

4 MST

A

B D

C

F

E

0

Page 121: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s

A

B D

C

4

1

2 3

4 F

E

5 4

6

4 MST

A

B D

C

F

E

4 5

6 0

Page 122: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s

A

B D

C

4

1

2 3

4 F

E

5 4

6

4 MST

A

B D

C

F

E

4 5

6 0

Page 123: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s

A

B D

C

4

1

2 3

4 F

E

5 4

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 124: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s

A

B D

C

4

1

2 3

4 F

E

5 4

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 125: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s

A

B D

C

4

1

2 3

4 F

E

5 4

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 126: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s

A

B D

C

4

1

2 3

4 F

E

5 4

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 127: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s

A

B D

C

4

1

2 3

4 F

E

5 4

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 128: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s

A

B D

C

4

1

2 3

4 F

E

5 4

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 129: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s

A

B D

C

4

1

2 3

4 F

E

5 4

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 130: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Prim’s

A

B D

C

4

1

2 3

4 F

E

5 4

6

4 MST

A

B D

C

F

E

1 4 5

4 2 0

Page 131: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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 132: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Running time of Prim’s

Θ(|V|)

Page 133: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Running time of Prim’s

Θ(|V|)

Page 134: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Running time of Prim’s

|V| calls to Extract-Min

Page 135: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

Running time of Prim’s

|E| calls to Decrease-Key

Page 136: UCS 732 ppt on graphs MSTs and Shortest paths Dijkstra Bellman ford.pdf

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| )