Top Banner
Lecture 20: Shortest Paths Shang-Hua Teng
22

Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Dec 19, 2015

Download

Documents

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: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Lecture 20:Shortest Paths

Shang-Hua Teng

Page 2: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Weighted Directed Graphs• Weight on edges for distance

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

400

2500

1000

1800800900

Page 3: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Shortest Paths• Given a weighted, directed graph G=(V, E) with

weight function w: ER. The weight of path p=<v0, v1,..., vk> is the sum of the weights of its edges:

• We define the shortest-path weight from u to v by

• A shortest path from vertex u to vertex v is any path with w(p)=(u, v)

k

iii vvwpw

11 ),()(

}:)(min{

),(

vupw

vup

If there is a path from u to v,

Otherwise.

Page 4: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Variants of Shortest Path Problem• Single-source shortest paths problem

– Finds all the shortest path of vertices reachable from a single source vertex s

• Single-destination shortest-path problem– By reversing the direction of each edge in the graph, we can

reduce this problem to a single-source problem

• Single-pair shortest-path problem– No algorithm for this problem are known that run

asymptotically faster than the best single-source algorithm in the worst case

• All-pairs shortest-path problem

Page 5: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Relaxation• For each vertex vV, we maintain an

attribute d[v], which is an upper bound on the weight of a shortest path from source s to v. We call d[v] a shortest-path estimate.

Possible Predecessor of v in the shortest path

Page 6: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Relaxation

• Relaxing an edge (u, v) consists of testing whether we can improve the shortest path found so far by going through u and, if so, update d[v] and [v]

By Triangle Inequality

Page 7: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Dijkstra’s Algorithm

Page 8: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Coping with Negative Weights

• Deciding whether there is a negative circle

• If the graph does not have a negative circle reachable from the source s, for the shortest path tree.

Page 9: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Bellman-Ford Algorithm

BellmanFord() for each v V d[v] = ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v) E Relax(u,v, w(u,v)); for each edge (u,v) E if (d[v] > d[u] + w(u,v)) return “no solution”;

Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w

Initialize d[], whichwill converge to shortest-path value

Relaxation: Make |V|-1 passes, relaxing each edge

Test for solution Under what conditiondo we get a solution?

Page 10: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Bellman-Ford Algorithm

BellmanFord() for each v V d[v] = ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v) E Relax(u,v, w(u,v)); for each edge (u,v) E if (d[v] > d[u] + w(u,v)) return “no solution”;

Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w

What will be the running time?

Page 11: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Bellman-Ford Algorithm

BellmanFord() for each v V d[v] = ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v) E Relax(u,v, w(u,v)); for each edge (u,v) E if (d[v] > d[u] + w(u,v)) return “no solution”;

Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w

What will be the running time?

A: O(VE)

Page 12: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Bellman-Ford Algorithm

BellmanFord() for each v V d[v] = ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v) E Relax(u,v, w(u,v)); for each edge (u,v) E if (d[v] > d[u] + w(u,v)) return “no solution”;

Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w

B

E

DC

A

-1 2

2

1-3

5

3

4

Ex: work on board

s

Page 13: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Bellman-Ford• Note that order in which edges are processed affects

how quickly it converges• Correctness: show d[v] = (s,v) after |V|-1 passes

– Lemma: d[v] (s,v) always• Initially true• Let v be first vertex for which d[v] < (s,v)• Let u be the vertex that caused d[v] to change:

d[v] = d[u] + w(u,v)• Then d[v] < (s,v)

(s,v) (s,u) + w(u,v) (Why?) (s,u) + w(u,v) d[u] + w(u,v) (Why?)

• So d[v] < d[u] + w(u,v). Contradiction.

Page 14: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Bellman-Ford

• Prove: after |V|-1 passes, all d values correct– Consider shortest path from s to v:

s v1 v2 v3 v4 v• Initially, d[s] = 0 is correct, and doesn’t change (Why?)

• After 1 pass through edges, d[v1] is correct (Why?) and doesn’t change

• After 2 passes, d[v2] is correct and doesn’t change

• …

• Terminates in |V| - 1 passes: (Why?)

• What if it doesn’t?

Page 15: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Properties of Shortest Paths

• Triangle inequality– For any edge (u,v) in E, (s,v) <= (s,u) + w(u,v)

• Upper bound property– d[v] >= (s,v)

• Monotonic property – d[v] never increase

• No-path property– If v is not reachable then d[v] = (s,v) = infty

Page 16: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Properties of Shortest Paths• Convergence property

– If (u,v) is on the shortest path from s to v and if d[u] = (s,u) at any time prior to relaxing (u,v), then d[v] = (s,v) at all time afterward

• Path-relaxation property– If p=< s=v0,v1, v2,..., vk> is the shortest path from s to vk

and edges of p are relaxed in order in the index, then d[vk] = (s, vk). This property holds regardless of any other relaxation steps that occur, even if they are intermixed with relaxations of the edges of p

Page 17: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Properties of Shortest Paths

• Predecessor-subgraph property– Once d[v] = (s,v), the predecessor subgraph is

a shortest-paths tree rooted at s

Page 18: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Matrix Basic

• Vector: array of numbers; unit vector

• Inner product, outer product, norm

• Matrix: rectangular table of numbers, square matrix; Matrix transpose

• All zero matrix and all one matrix

• Identity matrix

• 0-1 matrix, Boolean matrix, matrix of graphs

Page 19: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

Matrix Operations

• Matrix-vector operation– System of linear equations– Eigenvalues and Eigenvectors

• Matrix matrix operations

Page 20: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

1. Matrix Addition:

mnmnmm

nn

nn

mnmm

n

n

mnmm

n

n

baba

bababa

bababa

BA

bbb

bbb

bbb

B

a aa

a aa

a aa

A

,

,

11

2222222121

1112121111

21

22221

11211

21

22221

11211

Page 21: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

2. Scalar Multiplication:

mnmm

n

n

mnmm

n

n

aaa

aaa

aaa

aaa

aaa

aaa

A

21

2 2221

11211

21

22221

11211

Page 22: Lecture 20: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance 400 2500 1000 1800 800 900.

3. Matrix Multiplication

n

i ipmiimi

n

i ipi

n

i ii

n

i ii

n

i ipi

n

i ii

n

i ii

npnn

p

p

mnmm

n

n

baba

bababa

bababa

BA

bbb

bbb

bbb

B

aaa

aaa

aaa

A

1

n

1=i 1

1 21 221 12

1 11 211 11

21

22221

11211

21

22221

11211

,

,