CS 253: Algorithms

24
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis

description

CS 253: Algorithms. Chapter 24 Shortest Paths. Credit : Dr. George Bebis. Shortest Path Problems. How can we find the shortest route between two points on a road map? Model the problem as a graph problem: Road map is a weighted graph: vertices = cities - PowerPoint PPT Presentation

Transcript of CS 253: Algorithms

Page 1: CS  253:  Algorithms

CS 253: AlgorithmsChapter 24

Shortest Paths

Credit: Dr. George Bebis

Page 2: CS  253:  Algorithms

2

Shortest Path Problems

How can we find the shortest route between two points on a road map?

Model the problem as a graph problem:◦ Road map is a weighted graph:

vertices = cities

edges = road segments between cities

edge weights = road distances

◦ Goal: find a shortest path between two vertices (cities)

Page 3: CS  253:  Algorithms

Shortest Path Problem

Input:

◦ Directed graph G = (V, E)

◦ Weight function w : E → R

Weight of path p = v0, v1, . . . , vk

Shortest-path weight from u to v:

δ(u, v) = min w(p) : u v if there exists a path from u to v

∞ otherwise

Note: there might be multiple shortest paths from u to v

k

iii vvwpw

11 ),()(

p

0

3 9

5 11

36

57

6

s

t x

y z

22 1 4

3

Page 4: CS  253:  Algorithms

4

Negative-Weight Edges

Negative-weight edges may form negative-weight cycles If such cycles are reachable from the source,

then δ(s, v) is not properly defined!◦ Keep going around the cycle, and get

w(s, v) = - for all v on the cycle

Therefore, negative-weight edges will not be considered here

0

3-4

2

8

-6

s

a b

e f

-3y 3

56

4

7

c d g

Page 5: CS  253:  Algorithms

CyclesCan shortest paths contain cycles?

No!

Negative-weight cycles◦ Shortest path is not well defined (we will not consider

this case)

If there is a positive-weight cycle, we can get a shorter path by removing the cycle.

Zero-weight cycles?◦ No reason to use them◦ Can remove them and obtain a path with the same weight

Page 6: CS  253:  Algorithms

Shortest-Paths NotationFor each vertex v V: δ(s, v): shortest-path weight

d[v]: shortest-path weight estimate◦ Initially, d[v]=∞◦ d[v]δ(s,v) as algorithm progresses

[v] = predecessor of v on a shortest path from s◦ If no predecessor, [v] = NIL◦ induces a tree—shortest-path tree

0

3 9

5 11

36

57

6

s

t x

y z

22 1 4

3

Page 7: CS  253:  Algorithms

7

Initialization

Alg.: INITIALIZE-SINGLE-SOURCE(V, s)1. for each v V2. do d[v] ← 3. [v] ← NIL4. d[s] ← 0

All the shortest-paths algorithms start with INITIALIZE-SINGLE-SOURCE

Page 8: CS  253:  Algorithms

Relaxation StepRelaxing an edge (u, v) = testing whether we can

improve the shortest path to v found so far by going through u

If d[v] > d[u] + w(u, v) we can improve the shortest path to v d[v]=d[u]+w(u,v) [v] ← u

5 92u v

5 72u v

RELAX(u, v, w)

5 62u v

5 62u v

RELAX(u, v, w)

After relaxation: d[v] d[u] + w(u, v)s s

no change

Page 9: CS  253:  Algorithms

Dijkstra’s Algorithm Single-source shortest path problem:

◦ No negative-weight edges: w(u, v) > 0, (u, v) E Vertices in (V – S) reside in a min-priority queue

◦ Keys in Q are estimates of shortest-path weights d[u] Repeatedly select a vertex u (V – S), with the minimum

shortest-path estimate d[u] Relax all edges leaving u

STEPS1) Extract a vertex u from Q (i.e., u has the highest priority)2) Insert u to S3) Relax all edges leaving u4) Update Q

Page 10: CS  253:  Algorithms

10

Dijkstra (G, w, s)

0

10

1

5

2

s

t x

y z

2 39

7

4 60

10

1

5

2

s

t x

y z

2 39

7

4 6

10

5

S=<> Q=<s,t,x,z,y> S=<s> Q=<y,t,x,z>

Page 11: CS  253:  Algorithms

Example (cont.)

0

10

5

10

1

5

2

s

t x

y z

2 39

7

4 6

8 14

7

0

8 14

5 7

10

1

5

2

s

t x

y z

2 39

7

4 6

13

S=<s,y> Q=<z,t,x> S=<s,y,z> Q=<t,x>

Page 12: CS  253:  Algorithms

Example (cont.)

0

8 13

5 7

10

1

5

2

s

t x

y z

2 39

7

4 6

9

0

8 9

5 7

10

1

5

2

s

t x

y z

2 39

7

4 6

S=<s,y,z,t> Q=<x> S=<s,y,z,t,x> Q=<>

Page 13: CS  253:  Algorithms

Dijkstra (G, w, s)

1. INITIALIZE-SINGLE-SOURCE(V, s)2. S ← s3. Q ← V[G]4. while Q 5. do u ← EXTRACT-MIN(Q)6. S ← S {u} 7. for each vertex v Adj[u]8. do RELAX(u, v, w)9. Update Q (DECREASE_KEY)

(V)

(V) build min-heapExecuted (V) times

(lgV)

(E) times (max)

(lgV)

(VlgV)

(ElgV)

Running time: (VlgV + ElgV) = (ElgV)

Page 14: CS  253:  Algorithms

Dijkstra’s SSSP Algorithm (adjacency matrix)

b

a

d

c

f

e

1

1

1

5

5

3

2

42 a b c d e

f a 0 1 3

2b 1 0 5 1

c 3 5 0 2 1

d 1 2 0 4

e 1 4 0

5f 2 5

0

a b c d e f

L [.] = 1 0 5 1 S

new L[i] = Min{ L[i], L[k] + W[k, i] }where k is the newly-selected intermediate nodeand W[.] is the distance between k and i

Page 15: CS  253:  Algorithms

b

a

d

c

f

e

1

1

1

5

5

3

2

42 a b c d e

f a 0 1 3

2b 1 0 5 1

c 3 5 0 2 1

d 1 2 0 4

e 1 4 0

5f 2 5

0

a b c d e f

L [.] = 1 0 5 1 a b c d e

f new L [.] = 1 0 3 1 5

SSSP cont.

new L[i] = Min{ L[i], L[k] + W[k, i] }where k is the newly-selected intermediate nodeand W[.] is the distance between k and i

Page 16: CS  253:  Algorithms

b

a

d

c

f

e

1

1

1

5

5

3

2

42 a b c d e

f a 0 1 3

2b 1 0 5 1

c 3 5 0 2 1

d 1 2 0 4

e 1 4 0

5f 2 5

0

a b c d e f

L [.] = 1 0 3 1 5 a b c d e

f new L [.] = 1 0 3 1 5

3

new L[i] = Min{ L[i], L[k] + W[k, i] }where k is the newly-selected intermediate nodeand W[.] is the distance between k and i

Page 17: CS  253:  Algorithms

b

a

d

c

f

e

1

1

1

5

5

3

2

42 a b c d e

f a 0 1 3

2b 1 0 5 1

c 3 5 0 2 1

d 1 2 0 4

e 1 4 0

5f 2 5

0

a b c d e f

L [.] = 1 0 3 1 5 3 a b c d e

f new L [.] = 1 0 3 1 4

3

Page 18: CS  253:  Algorithms

b

a

d

c

f

e

1

1

1

5

5

3

2

42 a b c d e

f a 0 1 3

2b 1 0 5 1

c 3 5 0 2 1

d 1 2 0 4

e 1 4 0

5f 2 5

0

a b c d e f

L [.] = 1 0 3 1 4 3 a b c d e

f new L [.] = 1 0 3 1 4

3

Page 19: CS  253:  Algorithms

b

a

d

c

f

e

1

1

1

5

5

3

2

42 a b c d e

f a 0 1 3

2b 1 0 5 1

c 3 5 0 2 1

d 1 2 0 4

e 1 4 0

5f 2 5

0

a b c d e f

L [.] = 1 0 3 1 4 3 a b c d e

f new L [.] = 1 0 3 1 4

3

Running time: (V2) (array representation)

(ElgV) (Min-Heap+Adjacency List)Which one is better?

Page 20: CS  253:  Algorithms

All-Pairs Shortest PathsGiven:Directed graph G = (V, E)Weight function w : E → R

Compute: The shortest paths between all pairs of vertices in a

graphResult: an n × n matrix of shortest-path distances δ(u, v)

We can run Dijkstra’s algorithm once from each vertex:◦ O(VElgV) with binary heap and adjacency-list representation◦ if the graph is dense O(V3lgV)◦ We can achieve O(V3) by using an adjacency-matrix.

1

2

3

5 4

3

3 7

6

2

4

1 9

8

Page 21: CS  253:  Algorithms

21

Problem 1

We are given a directed graph G=(V, E) on which each edge (u,v) has an associated value r(u,v), which is a real number in the range 0 ≤ r(u,v) ≤ 1 that represents the reliability of a communication channel from vertex u to vertex v.

We interpret r(u,v) as the probability that the channel from u to v will not fail, and we assume that these probabilities are independent.

Design an efficient algorithm to find the most reliable path between two given vertices.

Page 22: CS  253:  Algorithms

22

Problem 1 (cont.)

◦r(u,v) = Pr(channel from u to v will not fail)◦Assuming that the probabilities are independent, the

reliability of a path p=<v1,v2,…,vk> is: r(v1,v2) r(v2,v3) … r(vk-1,vk)

Solution 1: modify Dijkstra’s algorithm

◦ Perform relaxation as follows: if d[v] < d[u] w(u,v) then

d[v] = d[u] w(u,v)

◦ Use “EXTRACT_MAX” instead of “EXTRACT_MIN”

Page 23: CS  253:  Algorithms

Problem 1 (cont.)Solution 2: use Dijkstra’s algorithm without any

modifications!

◦ We want to find the channel with the highest reliability, i.e.,

◦ But Dijkstra’s algorithm computes

◦ Take the log

( , )

max ( , )pu v p

r u v

( , )

min ( , )pu v p

w u v

( , )( , )

lg(max ( , )) max lg( ( , ))p pu v pu v p

r u v r u v

Page 24: CS  253:  Algorithms

Problem 1 (cont.)

Turn this into a minimization problem by taking the negative:

Run Dijkstra’s algorithm using

( , ) ( , )

min lg( ( , )) min lg( ( , ))p pu v p u v p

r u v r u v

( , ) lg( ( , ))w u v r u v