cs 8833 algorithms algorithms shortest path problems

69
CS 8833 Algorithms Algorithms Shortest Path Problems

Post on 19-Dec-2015

290 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Algorithms

Shortest Path Problems

Page 2: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

G = (V, E) weighted directed graphw: ER weight function

Weight of a path p = <v0, v1,. . ., vn>

Shortest path weight from u to v

Shortest path from u to v: Any path from u to v with w(p) = (u,v)

[v] predecessor of v on a path

n

ii vvwpw

11 )()(

otherwise

exitspath vu, a if}:)(min{),(p

vupwvu

Page 3: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Page 4: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Variants Single-source shortest paths:

find shortest paths from source vertex to every other vertex

Single-destination shortest paths:find shortest paths to a destination from every vertex

Single-pair shortest-pathfind shortest path from u to v

All pairs shortest paths

Page 5: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Lemma 25.1 Subpaths of shortest paths are shortest

paths.Given G=(G,E) w: E R

Let p = p = <v1, v2,. . ., vk> be a shortest path from v1 to vk

For any i,j such that 1 i j k, let pij be a subpath from vi to vj.. Then pij is a shortest path from vi to vj.

Page 6: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

1 i j k

p

Page 7: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Corollary 25.2Let G = (V,E) w: E R

Suppose shortest path p from a source s to vertex v can be decomposed into

p’

s u v

for vertex u and path p’.

Then weight of the shortest path from s to v is

(s,v) = (s,u) + w(u,v)

Page 8: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Lemma 25.3

Let G = (V,E) w: E R

Source vertex s

For all edges (u,v)E

(s,v) (s,u) + w(u,v)

Page 9: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

s v

u1

u2

u4

u3

un

Page 10: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Relaxation Shortest path estimate

d[v] is an attribute of each vertex which is an upper bound on the weight of the shortest path from s to v

Relaxation is the process of incrementally reducing d[v] until it is an exact weight of the shortest path from s to v

Page 11: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

INITIALIZE-SINGLE-SOURCE(G, s)

1. for each vertex v V(G)

2. do d[v]

3. [v] nil

4. d[s] 0

Page 12: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Page 13: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Relaxing an Edge (u,v) Question: Can we improve the shortest

path to v found so far by going through u?

If yes, update d[v] and [v]

Page 14: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

RELAX(u,v,w)

1. if d[v] > d[u] + w(u,v)

2. then d[v] d[u] + w(u,v)

3. [v] u

Page 15: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

EXAMPLE 1

s

u

v

s

u

v

Relax

Page 16: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

EXAMPLE 2

s

u

v

s

u

v

Relax

Page 17: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Dijkstra’s Algorithm Problem:

– Solve the single source shortest-path problem on a weighted, directed graph G(V,E) for the cases in which edge weights are non-negative

Page 18: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Dijkstra’s Algorithm Approach

– maintain a set S of vertices whose final shortest path weights from the source s have been determined.

– repeat» select vertex from V-S with the minimum

shortest path estimate» insert u in S» relax all edges leaving u

Page 19: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

DIJKSTRA(G,w,s)

1. INITIALIZE-SINGLE-SOURCE(G,s)

2. S

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

Page 20: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Page 21: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Analysis of Dijkstra’s Algorithm

Suppose priority Q is:– an ordered (by d) linked list

» Building the Q O(V lg V)» Each EXTRACT-MIN O(V)» This is done V times O(V2)» Each edge is relaxed one time O(E)» Total time O(V2 + E) = O(V2)

Page 22: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Analysis of Dijkstra’s Algorithm

Suppose priority Q is:– a binary heap

» BUILD-HEAP O(V)» Each EXTRACT-MIN O(lg V)» This is done V times O(V lg

V)» Each edge is relaxation O(lg V)» Each edge relaxed one time O(E lg

V)» Total time O(V lg V + E lg V))

Page 23: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Properties of Relaxation Lemma 25.4

G=(V,E) w: E R (u,v) E

After relaxing edge (u,v) by executing RELAX(u,v,w) we have

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

Page 24: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Lemma 25.5– Given:

G=(V,E) w: E R source s V

Graph initialized by

INITIALIZE-SINGLE-SOURCE(G,s)– then

d[v] (s,v) for all v V

and this invariant is maintained over all relaxation steps

Once d[v] achieves a lower bound (s,v), it never changes

Page 25: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Corollary 25.6– Given:

G=(V,E) w: E R source s V

No path connects s to given v– then

after initialization d[v] (s,v)

and this inequality is maintained over all relaxation steps.

Page 26: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Lemma 25.7– Given:

G=(V,E) w: E R source s V

Let s - - u v be the shortest path in G for all vertices u and v.

Suppose G initialized by INITIALIZE-SINGLE-SOURCE is followed by a sequence of relaxations including RELAX(u,v,w)

– Then d[u] = (s,u) prior to call implies that d[u] = (s,u) after the call

Page 27: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Bottom Line Therefore, relaxation causes the

shortest path estimates to descend monotonically toward the actual shortest-path weights.

Page 28: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Shortest-Paths Tree of G(V,E) The shortest-paths tree at S of G(V,E)

is a directed subgraph G’-(V’,E’), where V’ V, E’E, such that– V’ is the set of vertices reachable from S in

G– G’ forms a rooted tree with root s, and– for all v V’, the unique simple path from s

to v in G’ is a shortest path from s to v in G

Page 29: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Goal We want to show that successive

relaxations will yield a shortest-path tree

Page 30: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Lemma 25.8– Given:

G=(V,E) w: E R source s V

Assume that G contains no negative-weight cycles reachable from s.

– Then after the graph is initialized with INITIALIZE-SINGLE-SOURCE

• the predecessor subgraph G forms a rooted tree with root s, and

• any sequence of relaxation steps on edges in G maintains this property as an invariant.

Page 31: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Algorithms

Bellman-Ford Algorithm

Directed-Acyclic Graphs

All Pairs-Shortest Path Algorithm

Page 32: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Why does Dijkstra’s greedy algorithm work?

Because we know that when we add a node u to the set S, the value d is the length of the shortest path from s to u.

But, this only works if the edges of the graph are nonnegative.

Page 33: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

a

b c

7 10

-4

Would Dikjstra’s Algorithm work with this graph?

Page 34: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

a

b c

7 6

-14

What is the length of the shortest path from a to c in this graph?

Page 35: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Bellman-Ford Algorithm The Bellman-Ford algorithm can be

used to solve the general single source shortest path problem

Negative weights are allowed The algorithm detects negative cycles

and returns false if the graph contains one.

Page 36: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

BELLMAN-FORD(G,w,s)

1 INITIALIZE-SINGLE-SOURCE(G,s)

2 for i 1 to |V[G]| -1

3 do for each edge (u,v) E[G]

4 do RELAX(u,v,w)

5 for each edge (u,v) E[G]

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

7 then return false

8 return true

Page 37: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

When there are no cycles of negative length, what is maximum number of edges in a shortest path when the graph has |V[G]| vertices and |E[G]| edges?

Page 38: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Dynamic Programming Formulation

The following recurrence shows how the Bellman Ford algorithm computes the d values for paths of length k.

)}},(][{min ],[min{][ 1i

1 uiwidistudud kkk

Page 39: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

a

b c

7 10

-4

Processing order

1 2 3

(a,c)

(b,a)

(c,b)

(c,d)

(d,b)

d

-59

Page 40: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Complexity of the Bellman Ford Algorithm

Time complexity:

Performance can be improved by– adding a test to the loop to see if any d values

were updated on the previous iteration, or– maintain a queue of vertices whose d value

changed on the previous iteration-only process these on the next iteration

Page 41: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Single-source shortest paths in directed acyclic graphs

Topological sorting is the key to efficient algorithms for many DAG applications.

A topological sort of a DAG is a linear ordering of all of its vertices such that if G contains an edge (u,v), then u appears before v in the ordering.

Page 42: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

a d

c

c b e f g

Page 43: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

DAG-SHORTEST-PATHS(G,w,s)

1 topologically sort the vertices of G

2 INITIALIZE-SINGLE-SOURCE(G,s)

3 for each vertex u taken in topological order

4 do for each vertex v Adj[u]

5 do RELAX(u,v,w)

Page 44: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Topological Sorting

TOPOLOGICAL-SORT(G)

1 call DFS(G) to compute finishing times f[v] for each vertex v

2 as each vertex is finished, insert it onto the front of a linked list

3 return the linked list of vertices

Page 45: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Depth-first search Goal: search all edges in the graph one

time Strategy: Search deeper in the graph

whenever possible Edges are explored out of the most

recently discovered vertex v that still has unexplored edges leaving it.

Backtrack when a dead end is encountered

Page 46: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Predecessor subgraph The predecessor subgraph of a depth

first search– forms a depth-first forest– composed of depth-first trees

The edges in E are called tree edges

Page 47: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Vertex coloring scheme All vertices are initially white A vertex is colored gray when it is

discovered A vertex is colored black when it is

finished (all vertices adjacent to the vertex have been examined completely)

Page 48: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Time Stamps Each vertex v has two time-stamps

– d[v] records when v is first discovered (and grayed)

– f[v] records when the search finishes examining its adjacency list (and is blackened)

For every vertex u– d[u] < f[u]

Page 49: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Color and Time Stamp Summary

Vertex u is – white before d[u]– gray between d[u] and f[u]– black after f[u]

Time is a global variable in the pseudocode

Page 50: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

DFS(G)

1 for each vertex u V[G]

2 do color[u] white

3 [u] nil

4 time 0

5 do for each vertex u Adj[u]

6 do if color[u] = white

7 then DFS-VISIT(u)

Page 51: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

DFS-VISIT(u)

1 color[u] gray

2 d[u] time time time +1

3 for each vertex v Adj[u]

4 do if color[v] = white

5 then [v] u

6 DFS-VISIT(v)

7 color[u] black

8 f[u] time time time +1

Page 52: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c1/

Page 53: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c1/

2/

Page 54: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c1/

2/ 3/

Page 55: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c1/

2/ 3/

4/

Page 56: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c1/

2/ 3/

4/5

Page 57: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c1/

2/ 3/6

4/5

Page 58: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c1/

2/7 3/6

4/5

Page 59: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c1/

2/7 3/6

4/58/

Page 60: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c1/

2/7 3/6

4/58/9

Page 61: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c1/

2/7 3/6

4/58/9

10/

Page 62: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c1/

2/7 3/6

4/58/9

10/ 11/

Page 63: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c1/

2/7 3/6

4/58/9

10/ 11/12

Page 64: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c1/

2/7 3/6

4/58/9

10/13 11/12

Page 65: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c1/14

2/7 3/6

4/58/9

10/13 11/12

Page 66: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Running time of DFS lines 1-3 of DFS lines 5-7 of DFS lines 2-6 of DFS-VISIT

Page 67: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Running Time of Topological Sort

DFS Insertion in linked list

Page 68: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

b

a

d

e

g

f

c14

7 6

59

13 12

7

2

31

4

8

10

9

6

7

Page 69: CS 8833 Algorithms Algorithms Shortest Path Problems

CS 8833 Algorithms

Running Time for DAG-SHORTEST-PATHS

Topological sort Initialize-single source 3-5 each edge examined one time