Vous êtes sur la page 1sur 7

30/11/13

Single Source Shortest Path


Search C455 Document last modified: 04/01/2013 23:08:06

Single Source Shortest Path


Overview - Shortest path on a DAG Given a weighted, directed graph G = (V, E) with a weight function mapping edges to a real-valued weight: w:ER Weight of a path p = <v0, v1, ..., vk>
k

w(p)=

i=1

w (vi-1, vi)

Shortest-Path weight | min{ w(p) : u (u, v)= | | Shortest path From vertex u to v is any path with weight w(p) = (u, v) Example Shortest path at right for (s, z) has shortest path weight (s, z) = 3, {(s, x), (x, y), (y, z)} min{ w(p) : s z } = 6-1-2 = 3 v} if there is a path from u to v otherwise

Greedy algorithms greedy algorithm - Shortest path problems are typically solved using a general method called a greedy algorithm. We'll examine greedy algorithms more fully later in the course but greed provides an important insight to shortest path problem that is useful to know now. greedy-choice property - Some problems have a structure such that a greedy approach is an optimal approach; problems such as these are said to have the greedy-choice property. When a "locally optimal" choice can lead to a "globally optimal" solution, the problem has the greedy-choice property. optimal global solution - Greedy algorithms exploit some property to determine what is the optimal of the possible local choices (such as finding the least path weight) which ultimately lead to the optimal global solution (e.g. the shortest path). Question 24.1 What currency (coins/bills) would you expect back for $78.96? Does the usual method of making change, giving out the smallest number of bills or coins by counting out the largest possible currency first possess a greedy-choice property?

Representing shortest paths - essentially same as with BFS. Note there is only a single source. predecessor subgraph G = (V, E) induced by where: v. is vs predecessor s is the source, s. = NIL set of vertices V = { v V : v. NIL } | {s}, means there is one source s set of edges E = { (v., v) : v V - {s} } forms a tree.

Initialization Distance estimate from source (circles) is Predecessor is NIL


homepages.ius.edu/rwisman/C455/html/notes/Chapter24/SingleSource.htm 1/7

30/11/13

Single Source Shortest Path

INITIALIZE-SINGLE-SOURCE(G, s) 1 for each vertex v G.V do 2 3 v.d v. NIL r d NIL s NIL 0 t NIL x NIL y NIL z NIL

4 s.d 0 Analysis Time = (V)

Relaxation v.d = shortest path estimate The upper-bound on the weight of a shortest path from source s to v for each v V. RELAX function lowers the weight upper-bound to a vertex if the new edge weight is lower than the current estimate. The current estimate, v.d, is shortest path explored so far from source to the vertex. Ensures v.d will always be the shortest path from source to vertex v of those paths computed so far.

Example (s, u) = 4 (s, v) = 10 w(u, v) = 3 Shorter path exists to v through u: (s, v) = (s, u) + w(u, v) = 4 + 3 = 7 Analysis

Example (s, u) = 4 (s, v) = 6 w(u, v) = 3 NO shorter path exists to v through u: (s, v) = 6

RELAX(u, v, w) 1 2 3 if v.d > u.d + w(u, v) then v.d u.d + w(u, v) v. u

Time = (1)

Example - w is the weight of the edge, e.g. w(r, s) = 5. Give the result of: RELAX( t, z, w ) RELAX( t, z, w) 1 2 3 if z.d > t.d + w(t, z) then z.d t.d + w(t, z) z. x d r NIL s NIL 0 t s 2 x s 6 y NIL z NIL t

Question 24.2 - w is the weight of the edge, e.g. w(r, s) = 5.


homepages.ius.edu/rwisman/C455/html/notes/Chapter24/SingleSource.htm 2/7

30/11/13

Single Source Shortest Path

Give the result of: RELAX( t, y, w ) RELAX( x, y, w )

r d NIL

s NIL 0

t s 2

x s 6

y NIL

z NIL

RELAX(u, v, w) 1 2 3 if v.d > u.d + w(u, v) then v.d u.d + w(u, v) v. u

24.2 Single-source shortest paths in directed acyclic graphs The following algorithm computes the shortest path from a source to all other connected vertices of a DAG. Topologically sorting the DAG arranges vertices in a linked list in the order reachable by following edges. Recall that topological sorting is accomplished by: Topological-Sort (G) 1 call DFS (G) to compute the v.f for each vertex v 2 as each vertex is finished, insert it onto the front of a linked list, last finish time is first. 3 return the linked list of vertices After line 2, starting at r and DFS visiting vertices alphabetically, topological sort yields: rs t xyz r d f NIL 1 12 s r 2 11 t r 3 10 x t 4 9 y t 5 8 z t 6 7

Example: Below, the topologically sorted linked list is: r, s, t, x, y, z. Topological sorting establishes an ordering of vertices in the graph. Starting with r ensures reaching all other vertices in the path, including s . Edge weights are accumulated from the source vertex to each vertex reachable from the source, following the order in the linked list. RELAX reduces the accumulated weight to a vertex when a lower weight path to the source is examined. DAG-SHORTEST-PATHS(G, w, s) 1 topologically sort the vertices of G, implies DFS 2 INITIALIZE-SINGLE-SOURCES(G, s) 3 for each vertex u taken in topologically sorted order 4 for each vertex v G.Adj[ u ] (V + E) (V) (V) (V+E)
3/7

homepages.ius.edu/rwisman/C455/html/notes/Chapter24/SingleSource.htm

30/11/13

Single Source Shortest Path

RELAX( u, v, w )

(V+E)

Source is vertex s Initial DAG G and weights w r s t x y z r s 5 t 3 2 x 6 7 y 4 -1 z 2 1 -2

Topological sort - r s t x y z

INITIALIZE-SINGLE-SOURCES(G, s ) r d NIL s NIL 0 t NIL x NIL y NIL z NIL

rs t xyz

rst xyz

3 4 5

for each vertex u taken in topologically sorted order for each vertex v G.Adj[ u ] RELAX( u, v, w ) RELAX(r, s, w) RELAX(s, t, w) 1 if t.d > s.d + w(s, t) 2 3 then t.d s.d + w(s, t) t. s RELAX(s, x, w) 1 if x.d > s.d + w(s, x) 2 z NIL 3 r d NIL s NIL 0 then x.d s.d + w(s, x) x. s t s 2 x s 6 y NIL z NIL

1 if s.d > r.d + w(r, s) 2 3 then s.d r.d + w(r, s) s. r RELAX(r, t, w) 1 if t.d > r.d + w(r, t) 2 3 r d NIL s NIL 0 then t.d r.d + w(r, t) t. r t NIL x NIL y NIL

Question 24.3 - No change from rt or rs. Why? rs txyz rs t xyz

for each vertex u taken in topologically sorted order

RELAX(x, y, w)
4/7

homepages.ius.edu/rwisman/C455/html/notes/Chapter24/SingleSource.htm

30/11/13

Single Source Shortest Path

4 5

for each vertex v G.Adj[ u ] RELAX( u, v, w ) RELAX(t, x, w)

1 if y.d > x.d + w(x, y) 2 3 then y.d x.d + w(x, y) y. x RELAX(x, z, w) 1 if z.d > x.d + w(x, z) r d NIL s NIL 0 t s 2 x s 6 y x 5 z t 4

1 if x.d > t.d + w(t, x) RELAX(t, y, w) 1 if y.d > t.d + w(t, y) 2 3 then y.d t.d + w(t, y) y. t RELAX(t, z, w) 1 if z.d > t.d + w(t, z) 2 3 r d NIL s NIL 0 then z.d t.d + w(t, z) z. t t s 2 x s 6 y t 6 z t 4

rs t xyz

rs t xyz

Shortest path weight: (u, v) = w(p) = 3 Shortest path (s, z) s xyz 3 4 5 for each vertex u taken in topologically sorted order for each vertex v G.Adj[ u ] RELAX( u, v, w ) RELAX(y, z, w) 1 if z.d > y.d + w(y, z) 2 3 r d NIL s NIL 0 then z.d y.d + w(y, z) z. y t s 2 x s 6 y x 5 z y 3 d r NIL s NIL 0 t s 2 x s 6 y x 5 z y 3

Analysis Similar to DFS analysis since DFS part of topological sort. Time = (V + E). Question 24.4 a. How many times is Line 4 executed using the graph at right for: t z
homepages.ius.edu/rwisman/C455/html/notes/Chapter24/SingleSource.htm 5/7

30/11/13

Single Source Shortest Path

total for all vertices. b. Explain the (V + E) cost in Line 5. 3 for each vertex u taken in topologically sorted order 4 5 for each vertex v Adj[ u ] RELAX( u, v, w ) (V) (V+E) (V+E)

c. Is the shortest path computed for all reachable vertices from source? Greedy? So where's the greedy nature of the algorithm? The RELAX procedure always chooses the shorter path from the current vertex back to the source over any others that have been examined. By repeatedly lowering the upper-bound weight v.d of all vertices, the minimum weight (s, v), from source s to vertex v is computed, the optimal solution.

Critical Path Analysis (PERT) Related to topological sorting, critical path analysis determines the longest path in the DAG, useful for determining time required to complete some task described by the DAG (e.g. commercial construction or other complex task). Industry makes considerable use of critical path analysis often under the acronym of PERT (Program Evaluation and Review Technique). The longest path for critical path analysis can be implemented by negating the edge weights and running DAGSHORTEST-PATHS. Example The following negates the edge weights of the previous example. The longest path is the critical path, since we negated the edge weights, d should also be negated; the greatest weight (longest path) from source s is then to z. We still get the shortest path of the negated weights. After line 2, topological sort (which does not examine weights) yields: r s t x y z r d f NIL 1 12 s r 10 11 t r 2 9 x t 7 8 y t 5 6 z t 3 4

Completing DAG-SHORTEST-PATHS on the negated weights produces the longest path. Longest path in blue, starting at s . Earlier shortest path analysis starting at s .

r r d NIL s NIL 0 t s -2 x t -9 y x -8 z x -10 d NIL

s NIL 0

t s 2

x s 6

y x 5

z y 3

homepages.ius.edu/rwisman/C455/html/notes/Chapter24/SingleSource.htm

6/7

30/11/13

Single Source Shortest Path

Question 24.5 Why are both y and z on the longest path? What is the longest path to s ? What is the purpose of Line 3, taking vertices in topologically sorted order? How would the longest path analysis be useful in construction project assuming weights are completion times? Compute the longest path for the graph at right, start at r. r d NIL 0 s NIL t NIL x NIL y NIL z NIL

DAG-SHORTEST-PATHS(G, w, s) 1 topologically sort the vertices of G, implies DFS 2 INITIALIZE-SINGLE-SOURCES(G, s) 3 for each vertex u taken in topologically sorted order 4 5 for each vertex v G.Adj[ u ] RELAX( u, v, w )

After INITIALIZE-SINGLE-SOURCES(G, s)

RELAX(u, v, w) 1 2 3 if v.d > u.d + w(u, v) then v.d u.d + w(u, v) v. u

homepages.ius.edu/rwisman/C455/html/notes/Chapter24/SingleSource.htm

7/7

Vous aimerez peut-être aussi