# introduction to algorithms - iit-computer wan/cs330/shortest-paths.pdf · introduction to...     Post on 27-Jun-2018

212 views

Category:

## Documents

Embed Size (px)

TRANSCRIPT

• Introduction to Algorithms6.046J/18.401J

Prof. Charles E. Leiserson

LECTURE 14Shortest Paths I Properties of shortest paths Dijkstras algorithm Correctness Analysis Breadth-first search

• Introduction to Algorithms November 1, 2004 L14.2 20014 by Charles E. Leiserson

Paths in graphsConsider a digraph G = (V, E) with edge-weight function w : E R. The weight of path p = v1 v2 L vk is defined to be

=+=

1

11),()(

k

iii vvwpw .

• Introduction to Algorithms November 1, 2004 L14.3 20014 by Charles E. Leiserson

Paths in graphsConsider a digraph G = (V, E) with edge-weight function w : E R. The weight of path p = v1 v2 L vk is defined to be

=+=

1

11),()(

k

iii vvwpw .

v1v1v2v2

v3v3v4v4

v5v54 2 5 1

Example:

w(p) = 2

• Introduction to Algorithms November 1, 2004 L14.4 20014 by Charles E. Leiserson

Shortest paths

A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as(u, v) = min{w(p) : p is a path from u to v}.

Note: (u, v) = if no path from u to v exists.

• Introduction to Algorithms November 1, 2004 L14.5 20014 by Charles E. Leiserson

Optimal substructure

Theorem. A subpath of a shortest path is a shortest path.

• Introduction to Algorithms November 1, 2004 L14.6 20014 by Charles E. Leiserson

Optimal substructure

Theorem. A subpath of a shortest path is a shortest path.

Proof. Cut and paste:

• Introduction to Algorithms November 1, 2004 L14.7 20014 by Charles E. Leiserson

Optimal substructure

Theorem. A subpath of a shortest path is a shortest path.

Proof. Cut and paste:

• Introduction to Algorithms November 1, 2004 L14.8 20014 by Charles E. Leiserson

Triangle inequality

Theorem. For all u, v, x V, we have(u, v) (u, x) + (x, v).

• Introduction to Algorithms November 1, 2004 L14.9 20014 by Charles E. Leiserson

Triangle inequality

Theorem. For all u, v, x V, we have(u, v) (u, x) + (x, v).

uu

Proof.

xx

vv(u, v)

(u, x) (x, v)

• Introduction to Algorithms November 1, 2004 L14.10 20014 by Charles E. Leiserson

Well-definedness of shortest paths

If a graph G contains a negative-weight cycle, then some shortest paths may not exist.

• Introduction to Algorithms November 1, 2004 L14.11 20014 by Charles E. Leiserson

Well-definedness of shortest paths

If a graph G contains a negative-weight cycle, then some shortest paths may not exist.

Example:

uu vv

< 0

• Introduction to Algorithms November 1, 2004 L14.12 20014 by Charles E. Leiserson

Single-source shortest pathsProblem. From a given source vertex s V, find the shortest-path weights (s, v) for all v V.If all edge weights w(u, v) are nonnegative, all shortest-path weights must exist. IDEA: Greedy.1. Maintain a set S of vertices whose shortest-

path distances from s are known.2. At each step add to S the vertex v V S

whose distance estimate from s is minimal.3. Update the distance estimates of vertices

• Introduction to Algorithms November 1, 2004 L14.13 20014 by Charles E. Leiserson

Dijkstras algorithmd[s] 0for each v V {s}

do d[v] S Q V Q is a priority queue maintaining V S

• Introduction to Algorithms November 1, 2004 L14.14 20014 by Charles E. Leiserson

Dijkstras algorithmd[s] 0for each v V {s}

do d[v] S Q V Q is a priority queue maintaining V Swhile Q

do u EXTRACT-MIN(Q)S S {u}for each v Adj[u]

do if d[v] > d[u] + w(u, v)then d[v] d[u] + w(u, v)

• Introduction to Algorithms November 1, 2004 L14.15 20014 by Charles E. Leiserson

Dijkstras algorithmd[s] 0for each v V {s}

do d[v] S Q V Q is a priority queue maintaining V Swhile Q

do u EXTRACT-MIN(Q)S S {u}for each v Adj[u]

do if d[v] > d[u] + w(u, v)then d[v] d[u] + w(u, v)

relaxation step

Implicit DECREASE-KEY

• Introduction to Algorithms November 1, 2004 L14.16 20014 by Charles E. Leiserson

Example of Dijkstras algorithm

AA

BB DD

CC EE

10

3

1 4 7 98

2

2

Graph with nonnegative edge weights:

• Introduction to Algorithms November 1, 2004 L14.17 20014 by Charles E. Leiserson

Example of Dijkstras algorithm

AA

BB DD

CC EE

10

3

1 4 7 98

2

2

Initialize:

A B C D EQ:0

S: {}

0

• Introduction to Algorithms November 1, 2004 L14.18 20014 by Charles E. Leiserson

Example of Dijkstras algorithm

AA

BB DD

CC EE

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A }

0

A EXTRACT-MIN(Q):

• Introduction to Algorithms November 1, 2004 L14.19 20014 by Charles E. Leiserson

Example of Dijkstras algorithm

AA

BB DD

CC EE

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A }

0

10

3

10 3

Relax all edges leaving A:

• Introduction to Algorithms November 1, 2004 L14.20 20014 by Charles E. Leiserson

Example of Dijkstras algorithm

AA

BB DD

CC EE

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C }

0

10

3

10 3

C EXTRACT-MIN(Q):

• Introduction to Algorithms November 1, 2004 L14.21 20014 by Charles E. Leiserson

Example of Dijkstras algorithm

AA

BB DD

CC EE

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C }

0

7

3 5

11

10 37 11 5

Relax all edges leaving C:

• Introduction to Algorithms November 1, 2004 L14.22 20014 by Charles E. Leiserson

Example of Dijkstras algorithm

AA

BB DD

CC EE

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E }

0

7

3 5

11

10 37 11 5

E EXTRACT-MIN(Q):

• Introduction to Algorithms November 1, 2004 L14.23 20014 by Charles E. Leiserson

Example of Dijkstras algorithm

AA

BB DD

CC EE

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E }

0

7

3 5

11

10 3 7 11 57 11

Relax all edges leaving E:

• Introduction to Algorithms November 1, 2004 L14.24 20014 by Charles E. Leiserson

Example of Dijkstras algorithm

AA

BB DD

CC EE

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E, B }

0

7

3 5

11

10 3 7 11 57 11

B EXTRACT-MIN(Q):

• Introduction to Algorithms November 1, 2004 L14.25 20014 by Charles E. Leiserson

Example of Dijkstras algorithm

AA

BB DD

CC EE

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E, B }

0

7

3 5

9

10 3 7 11 57 11

Relax all edges leaving B:

9

• Introduction to Algorithms November 1, 2004 L14.26 20014 by Charles E. Leiserson

Example of Dijkstras algorithm

AA

BB DD

CC EE

10

3

1 4 7 98

2

2A B C D EQ:0

S: { A, C, E, B, D }

0

7

3 5

9

10 3 7 11 57 11

9

D EXTRACT-MIN(Q):

• Introduction to Algorithms November 1, 2004 L14.27 20014 by Charles E. Leiserson

Correctness Part ILemma. Initializing d[s] 0 and d[v] for all v V {s} establishes d[v] (s, v) for all v V, and this invariant is maintained over any sequence of relaxation steps.

• Introduction to Algorithms November 1, 2004 L14.28 20014 by Charles E. Leiserson

Correctness Part ILemma. Initializing d[s] 0 and d[v] for all v V {s} establishes d[v] (s, v) for all v V, and this invariant is maintained over any sequence of relaxation steps.Proof. Suppose not. Let v be the first vertex for which d[v] < (s, v), and let u be the vertex that caused d[v] to change: d[v] = d[u] + w(u, v). Then,

d[v] < (s, v) supposition (s, u) + (u, v) triangle inequality (s,u) + w(u, v) sh. path specific path d[u] + w(u, v) v is first violation

• Introduction to Algorithms November 1, 2004 L14.29 20014 by Charles E. Leiserson

Correctness Part IILemma. Let u be vs predecessor on a shortest path from s to v. Then, if d[u] = (s, u) and edge (u, v) is relaxed, we have d[v] = (s, v) after the relaxation.

• Introduction to Algorithms November 1, 2004 L14.30 20014 by Charles E. Leiserson

Correctness Part IILemma. Let u be vs predecessor on a shortest path from s to v. Then, if d[u] = (s, u) and edge (u, v) is relaxed, we have d[v] = (s, v) after the relaxation.Proof. Observe that (s, v) = (s, u) + w(u, v). Suppose that d[v] > (s, v) before the relaxation. (Otherwise, were done.) Then, the test d[v] > d[u] + w(u, v) succeeds, because d[v] > (s, v) = (s, u) + w(u, v) = d[u] + w(u, v), and the algorithm sets d[v] = d[u] + w(u, v) = (s, v).

• Introduction to Algorithms November 1, 2004 L14.31 20014 by Charles E. Leiserson

Correctness Part IIITheorem. Dijkstras algorithm terminates with d[v] = (s, v) for all v V.

• Introduction to Algorithms November 1, 2004 L14.32 20014 by Charles E. Leiserson

Correctness Part IIITheorem. Dijkstras algorithm terminates with d[v] = (s, v) for all v V.Proof. It suffices to show that d[v] = (s, v) for every v V when v is added to S. Suppose u is the first vertex added t

Recommended