dynamic prog.example RK Sir

13
5.2 Multistage Graphs Definition: multistage graph G(V,E) A directed graph in which the vertices are partitioned into k≥2 disjoint sets V i , 1≤i≤k If <u,v> ∈ E, then u ∈ V i and v ∈ V i+1 for some I, 1≤i<k |V 1 |= |V k |=1, and s(source) ∈ V 1 and t(sink) ∈ V k c(i,j)=cost of edge <i,j> Definition: multistage graph problem Find a minimum-cost path from s to t e.g., Figure 5.2 (5-stage graph)

Transcript of dynamic prog.example RK Sir

5.2 MultistageGraphsDefinition: multistage graph G(V,E) A directed graph in which the vertices are partitioned into k2 disjoint sets Vi, 1ik If E, then u Vi and v Vi+1 for some I, 1i = 1;j--){// Compute cost[j]. letrbe a vertex such that is an edge of Gandc[j][r] + cost[r] is minimum; cost[j] = c[j][r] + cost[r]; d[j] = r; }// Find a minimum-cost path. p[1] = 1; p[k] =n ; for ( j=2;j 2n Given a directed graph G(V,E) cij= edge cost A tour is a directed simple cycle that includes every vertex in V The TSP is to find a tour of minimum costMany applications 1. Routing a postal van to pick up mail from mail boxes located at n different sites 2. Planning robot arm movements to tighten the nuts on n different positions 3. Planning production in which n different commodities are manufactured on the same sets of machines 5.9 The Traveling Salesperson Problem DP formulation Assumption: A tour starts and ends at vertex 1 The principle of optimality holds (Why?) g(i,S) = length of a shortest path starting at vertex i, going through all vertices in S, and terminating at vertex 1g(1,V-{1}) = - (5.20)g(i,S) =- (5.21) Solving the recurrence relation g(i,) = ci1, 1in Then obtain g(i,S) for all S of size 1 Then obtain g(i,S) for all S of size 2 Then obtain g(i,S) for all S of size 3 Finally obtain g(1,V-{1})} k}) {1, V g(k, c { min1kn k 2 + } {j}) S g(j, c { minijS j +5.9 The Traveling Salesperson Problem Example 5.26 (Figure 5.21) For |S|=0 g(2,)=c21=5 g(3,)=c31=6 g(4,)=c41=8 For |S|=1 g(2,{3})=c23+g(3,)=15 g(2,{4})=c24+g(4,)=18 g(3,{2})=c32+g(2,)=18 g(3,{4})=c34+g(4,)=20 g(4,{2})=c42+g(2,)=13 g(4,{3})=c43+g(3,)=151 24 3(a)]]]]]]

0 9 8 812 0 13 610 9 0 520 15 10 0(b)5.9 The Traveling Salesperson ProblemExample 5.26 (Continued) For |S|=2 g(2,{3,4}) = min {c23+g(3,{4}), c24+g(4,{3})} = 25 g(3,{2,4}) = min {c32+g(2,{4}), c34+g(4,{2})} = 25 g(4,{2,3}) = min {c42+g(2,{3}), c43+g(3,{2})} = 23 For |S|=3 g(1,{2,3,4} = min {c12+g(2,{3,4}), c13+g(3,{2,4}), c14+g(4,{2,3})} = = min {35, 40, 43} = 35 Time complexity Let N be the number of g(i,S) that have to be computed before (5.20) can be used to compute g(1,V-{1}) N = Total time = O(n22n) This is better than enumerating all n! different tours2 n2 n0 k2 ) 1 n (k2 n) 1 n (

,`

.|