2/28/2005Tucker, Sec Applied Combinatorics, 4th ed. Alan Tucker Section 4.1 Shortest Paths Prepared by Sarah Walker
2/28/2005Tucker, Sec Definition A network is a graph with a positive integer k(e) assigned to each edge e. This integer will typically represent the “length” of an edge, in units such as miles, or represent “capacity” of an edge, in units such as megwatts or gallons per minute. Note: Edge (a,b) has a “length” of 5, so k(a,b)=5. ab cd e
2/28/2005Tucker, Sec Shortest Path It can be helpful to find a shortest path in a network from point a to point z. We do not say the shortest path, because, in general, there may be more than one shortest path from a to z. We will assume that all networks are undirected and connected.
2/28/2005Tucker, Sec Shortest Path Cont. We can eliminate one shortest path algorithm: Determine the lengths of all paths from a to z, and choose the shortest one. Such enumeration is already infeasible for most networks with 20 vertices. We must be able to prove that a path is the shortest path without comparing it to all other possible paths.
2/28/2005Tucker, Sec Dijkstra’s Algorithm Gives shortest paths from a given vertex a to all other vertices. Let k(e) denote the length of edge e. Let the variable m be a “distance counter”, for increasing values of m, the algorithm labels vertices whose minimal distance from a is m.
2/28/2005Tucker, Sec Dijkstra’s Algorithm Cont. Step 1 – set m=1 and label vertex a with (-,0) (the “-” represents a blank a (-,0) b c de f g h
2/28/2005Tucker, Sec Notation A vertex in Dijkstra’s algorithm is labeled as follows: (r, d(p)) Where r is the preceding vertex in the path and d(p) is a distance counter from the initial vertex to the vertex p.
2/28/2005Tucker, Sec Dijkstra’s Algorithm Cont. Step 2 – check each edge e=(p,q) from some labeled vertex p to some unlabeled vertex q. Suppose p’s label are (r, d(p)). If d(p)+k(e)=m, label q with (p,m). Try m=1 d(a) =0 k(a,b)=1 d(a)+k(a,b)= 1 = m so we can label b with (a,1) a (-,0) b c de f g h (a,1) Note: if all sums d(p)+k(e) in Step 2 have values of at least m’>m, then the distance counter m should be increased immediately to m’
2/28/2005Tucker, Sec Dijkstra’s Algorithm Cont. Step 3 – If all vertices are not yet labeled, increment m by 1 and go to Step 2. Otherwise, go to Step 4. If we are only interested in the shortest path to z, then we go to Step 4 when z is labeled.
2/28/2005Tucker, Sec Dijkstra’s Algorithm Cont. For m=2 and m=3, no new labeling can be done. Try m=4 d(b) =1 k(b,c)=3 d(b)+k(b,c)= 4 = m so we can label c with (b,4) a (-,0) b c de f g h (a,1) (b,4)
2/28/2005Tucker, Sec Dijkstra’s Algorithm Cont. m=5 no new labeling can be done m=7 no new labeling can be done m=6 d(c) =4 k(c,d)=2 d(b)+k(b,c)= 6 = m so we can label d with (c,6) m=8 d(d) =6 k(d,h)=2 d(d)+k(d,h)= 8 = m so we can label h with (d,8) m=9 no new labeling can be done m=10 d(a) =0 k(a,f)=10 d(b)+k(b,c)= 10 = m so we can label f with (a,10) a (-,0) b c de f g h (a,1) (b,4) (c,6) (d,8) (a,10)
2/28/2005Tucker, Sec Dijkstra’s Algorithm Cont. m=10 d(d) =6 k(d,e)=10 d(d)+k(d,e)= 10 = m so we can label e with (d,10) m=11 no new labeling can be done m=12 d(h) =8 k(h,g)=4 d(h)+k(h,g)= 12 = m so we can label g with (h,12) a (-,0) b c de f g h (a,1) (b,4) (c,6) (d,8) (a,10) (d,10) (h,12)
2/28/2005Tucker, Sec Dijkstra’s Algorithm Cont. Step 4 – For any vertex y, a shortest path from a to y has length d(y), the second label of y. Such a path may be found by backtracking from y (using the first labels).
2/28/2005Tucker, Sec a (-,0) b c de f g h (a,1) Dijkstra’s Algorithm Cont. A shortest path from a to g is a-b-c-d-h-g and its length is 12 (b,4) (c,6) (d,8) (a,10) (d,10) (h,12)
2/28/2005Tucker, Sec Example for the Class Find the shortest path from a to any other point in this graph: ab cd e
2/28/2005Tucker, Sec Solution ab c d e (-,0) (a,1) (a,5)(d,4) (d,5) a to b = 5 (a-b) a to c = 5 (a-d-c) a to d = 1 (a-d) a to e = 4 (a-d-e)