Sunteți pe pagina 1din 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 Vi, 1≤i≤k
– If <u,v> ∈ E, then u ∈ Vi and v ∈ Vi+1 for some I, 1≤i<k
– |V1|= |Vk|=1, and s(source) ∈ V1 and t(sink) ∈ Vk
– 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)
5.2 Multistage Graphs
• Many problems can be formulated as multistage
graph problem
– An example: resource allocation problem
• n units of resource are to be allocated to r projects
• N(i,j) = net profit when j units of resource allocated
to project i
• V(i,j) = vertex representing the state in which a total
of j units have already been allocated to projects
1,2,..,i-1
5.2 Multistage Graphs
• e.g., Figure 5.3 (3 projects with n=4)
5.2 Multistage Graphs
• DP formulation
– Every s to t path is the result of a sequence of k-2
decisions
– The principle of optimality holds (Why?)
– p(i,j) = a minimum-cost path from vertex j in Vi to vertex
t
– cost(i,j) j) = min
cos t (=i, cost of path l) + cost (i + 1, l)}
{c( j,p(i,j)
l ∈ Vi+1
< j, l > ∈ E
• - (5.5)

– Solving (5.5)
• cost(k-1,j) = c(j,t) if <j,t> ∈ E, ∞ otherwise
• Then computing cost(k-2,j) for all j ∈ Vk-2
• Then computing cost(k-3,j) for all j ∈ Vk-3
• …
• Finally computing cost(1,s)
5.2 Multistage Graphs
• Example: Figure 5.2 (k=5)

– Stage 5
• cost(5,12) = 0.0
– Stage 4
• cost(4,9) = min {4+cost(5,12)} = 4
• cost(4,10) = min {2+cost(5,12)} = 2
• cost(4,11) = min {5+cost(5,12)} = 5
– Stage 3
• cost(3,6) = min {6+cost(4,9), 5+cost(4,10)} = 7
• cost(3,7) = min {4+cost(4,9), 3+cost(4,10)} = 5
• cost(3,8) = min {5+cost(4,10), 6+cost(4,11)} = 7
5.2 Multistage Graphs
• Example: Figure 5.2 (k=5) (Continued)

– Stage 2
• cost(2,2) = min {4+cost(3,6), 2+cost(3,7), 1+cost(3,8)} = 7
• cost(2,3) = min {2+cost(3,6), 7+cost(3,7)} = 9
• cost(2,4) = min {11+cost(3,8)} = 18
• cost(2,5) = min {11+cost(3,7), 8+cost(3,8)} = 15
– Stage 1
• cost(1,1) = min {9+cost(2,2), 7+cost(2,3), 3+cost(2,4),
2+cost(2,5)} = 16
– Important notes: avoiding the recomputation of cost(3,6),
cost(3,7), and cost(3,8) in computing cost(2,2)
5.2 Multistage Graphs
• Recording the path
– d(i,j) = value of l (l is a vertex) that minimizes c(j,l)
+cost(i+1,l) in equation (5.5)
– In Figure 5.2
• d(3,6)=10; d(3,7)=10; d(3,8)=10
• d(2,2)=7; d(2,3)=6; d(2,4)=8; d(2,5)=8
• d(1,1)=2
– When letting the minimum-cost path 1,v2,v3,…,vk-1,t,
• v2 = d(1,1) = 2
• v3 = d(2,d(1,1)) = 7
• v4 = d(3,d(2,d(1,1))) = d(3,7) = 10
• So the solution (minimum-cost path) is
1271012 and its cost is 16
5.2 Multistage Graphs
• Algorithm (Program 5.1)
– Vertices numbered in order of stages (like Figure 5.2)
void Fgraph (graph G, int k, int n, int p[] )
// The input is a k-stage graph G = (V,E) with n vertices indexed in order
// of stages. E is a set of edges and c[i][j] is the cost of <i, j>.
// p[1 : k] is a minimum-cost path.
{
float cost[MAXSIZE]; int d[MAXSIZE], r;
cost[n] = 0.0;
for (int j=n-1; j >= 1; j--) { // Compute cost[j].
let r be a vertex such that <j, r> is an edge
of G and c[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 <= k-1; j++) p[j] = d[ p[ j-1 ] ];
}
5.2 Multistage Graphs
• Backward approach
– bcost (i, j) = min {bcost (i − 1, l) + c(l, j)} -- (5.6)
l ∈ Vi−1
< j,l > ∈ E
5.9 The Traveling Salesperson Problem
• Problem
– TSP is a permutation problem (not subset problem)
• Usually the permutation problem is harder than the
subset one
• Because n! > 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 cost
• Many 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 1

= { c1k + g(k, V − {1, k}) }


g(1,V-{1}) min - (5.20)
2≤ k ≤ n

g(i,S) =min { c ij + g(j, S −{j}) } - (5.21)


j∈S

• Solving the recurrence relation


– g(i,Φ) = ci1 , 1≤i≤n
– 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})
5.9 The Traveling Salesperson Problem
• Example 5.26 (Figure 5.21)

1 2 0 10 15 20
5 0 9 10 
 
6 13 0 12 
4 3  
8 8 9 0 
(a) (b)
– 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,Φ)=15
5.9 The Traveling Salesperson Problem
• Example 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
2
 n −can
beforen −(5.20) 2  be used to compute g(1,V-{1})
∑ (n − 1)k
n −2
 = (n − 1)2
k =0  
• N=
– Total time = O(n22n)
– This is better than enumerating all n! different tours

S-ar putea să vă placă și