Shortest-Path Problems ! A weighted graph G=(V,E) is a graph in which - - PDF document

shortest path problems
SMART_READER_LITE
LIVE PREVIEW

Shortest-Path Problems ! A weighted graph G=(V,E) is a graph in which - - PDF document

Shortest-Path Problems ! A weighted graph G=(V,E) is a graph in which each edge e E has a weight (cost) w(e) associated with it. Graph Algorithms ! Path cost: the cost of a path p= v 0 ,,v k is


slide-1
SLIDE 1

1

Graph Algorithms לע םימת ירוגלא םיפ רג

2

Shortest-Path Problems

! A weighted graph G=(V,E) is a

graph in which each edge e∈E has a weight (cost) w(e) associated with it.

! Path cost: the cost of a path

p=〈v0,…,vk〉 is the sum of the weights

  • f its edges:

! Shortest path problems:

– Single source – All pairs

( ) ( )

= −

=

k i i i

v v w p w

1 1,

3

Shortest-Path Problems

! Single-source shortest paths

problem: given a directed graph G=(V,E), in which each edge has non- negative cost, and a source s∈V, compute the cheapest path from s to every other vertex in V.

! All-pairs shortest paths problem:

given a directed graph G=(V,E), in which each edge has non-negative cost, compute for every ordered pair of vertices (u,v) the cheapest path from u to v.

4

Dijkstra’s Algorithm

! Maintain a set of vertices S whose

shortest path from the source has already been discovered (initially S contains only the source vertex).

! At each iteration add to S a vertex

from V-S with the minimum shortest- path estimate

! Update shortest-path estimates for V-S

1 2 10 5 2 3 4 6 9 7

slide-2
SLIDE 2

5

Dijkstra’s Algorithm (A)

Dijkstra(G=(V,E), w, s ∈ V) Q = V; S = ∅; for each v ∈ Q v.dist = ∞, v.from = null; s.dist = 0, s.from = null; while (Q is not empty) { u = vertex with min dist in Q; Q = Q – {u}; S = S ∪ {u}; for each v ∈ Q adjacent to u if (u.dist + w(u,v) < v.dist) { v.from = u; v.dist = u.dist + w(u,v); } }

6

Dijkstra’s Algorithm (B)

Dijkstra(G=(V,E), w, s ∈ V) PriorityQueue Q = V; S = ∅; for each v ∈ Q v.dist = ∞, v.from = null; s.dist = 0, s.from = null; while (Q is not empty) { u = Q.deleteMin(); S = S ∪ {u}; for each v ∈ Q adjacent to u if (u.dist + w(u,v) < v.dist) { v.from = u; v.dist = u.dist + w(u,v); Q.decreaseKey(v, v.dist); } }

7

Correctness Proof

!

Theorem: Upon termination of Dijkstra’s algorithm, v.dist is the length of the shortest path from s to v, for each v∈V.

!

Definition: we shall call a path from s to v “special” if it is the shortest path from s to v passing only through vertices in S.

!

Lemma: At each stage of Dijkstra’s alg. The following two properties hold:

1. For each v∈S, v.dist is the length of the shortest path from s to v. 2. For each v∈V-S, v.dist is the length of the shortest special path from s to v. Proof: by induction on the size of S.

!

The theorem follows from property 1 when S contains all of the vertices in the graph.

8

All-Pairs Shortest Paths

! Can be solved by calling Dijkstra once

  • n each vertex as a source.

! A more direct algorithm (Floyd-

Warshall).

! Assume vertices are {1,…,n} ! Maintain a cost matrix containing in

each entry (i,j) the cost of a path from vertex i to vertex j.

! Initially, the matrix is defined as

follows:

[ ]

     ∈ = ∞ =

  • therwise

) , ( if if ) , ( , E j i j i j i w j i C

slide-3
SLIDE 3

9

Example

1 2 3

8 3 5 2 2

8 5 3 ∞ ∞ 2

10

Floyd-Warshall Alg.

Floyd-Warshall(G=(V,E), w) Initialize cost matrix C; for (k = 1; k <= n; k++) { for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (C[i,k] + C[k,j] < C[i,j]) C[i,j] = C[i,k] + C[k,j]; } } } 1 2 3

8 3 5 2 2

11

What About the Paths?

Floyd-Warshall(G=(V,E), w) Initialize cost matrix C; Initialize path matrix P; for (k = 1; k <= n; k++) { for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (C[i,k] + C[k,j] < C[i,j]) C[i,j] = C[i,k] + C[k,j]; P[i,j] = k; } } } PrintShortestPath(P, i, j) k = P[i, j]; if (k != 0) { PrintShortestPath(P,i,k); print(k); PrintShortestPath(P,k,j); }

12

Transitive Closure

! The transitive closure of a graph

G=(V,E) is the graph G*=(V,E*), in which (u,v)∈E* iff a path from u to v exists in G. Transitive-Closure(G=(V,E)) // Assumes a boolean adjacency // matrix A[i,j] == true iff (i,j) ∈ E for (k = 1; k <= n; k++) { for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (A[i,j] == false) A[I,j] = A[i,k] && A[k,j]; } } }