Dynammic Programming, ALS Problem
Dynammic Programming, ALS Problem
So, the first few numbers in this series will be 1, 1, 2, 3, 5, 8, 13, 21 and so on!
Recursion Using DP with Memorization
In the recursive code, a lot of values are being recalculated multiple times.
.We could do good with calculating each unique quantity only once
Assembly Line Scheduling
Assembly Line Scheduling
Automobile factory with two assembly lines •
Each line has n stations: S1,1, . . . , S1,n and S2,1, . . . , S2,n –
Corresponding stations S1, j and S2, j perform the same function –
but can take different amounts of time a1, j and a2, j
Entry times are: e1 and e2; exit times are: x1 and x2 –
9
Structure of the Optimal Solution
Let’s consider all possible ways to get from •
the starting point through station S1,j
:We have two choices of how to get to S1, j –
Through S1, j - 1, then directly to S1, j •
Through S2, j - 1, then transfer over to S1, j •
S1,j-1 S1,j
a
Line 1 a
1,j
-1 t 1,j
2,
j-
a
1
Line 2 2,
j-
S2,j-1
1
11
A Recursive Solution (cont.)
Base case: j = 1, i=1,2 (getting through station 1) •
f1[1] = e1 + a1,1
f2[1] = e2 + a2,1
12
A Recursive Solution (cont.) .2
General Case: j = 2, 3, …,n, and i = 1, 2 •
:Fastest way through S1, j is either •
the way through S1, j - 1 then directly through S1, j, or –
f1[j - 1] + a1,j
the way through S2, j - 1, transfer from line 2 to line 1, then through S1, j –
if j = 1 e2 + a2,1
= f2[j]
if j ≥ 2 min(f2[j - 1] + a2,j ,f1[j -1] + t1,j-1 + a2,j)
14
Example
1 2 3 4 5
f1[j] 9 [1]
18 [2]
20 [1]
24 [1]
32
f* = 35[1]
[1] [2] [1] [2]
f2[j] 12 16 22 25 30
17
FASTEST-WAY(a,
f [1] ← e + a
t, e, x, n)
1 1 1,
f2[1] ← e2 + a2,1 Compute initial values of f1 and f2
for j ← 2 to n
do if f1[j - 1] + a1,j ≤ f2[j - 1] + t2, j-1 + a1, j
then f1[j] ← f1[j - 1] + a1, j
l1[j] ← 1 Compute the values of
f1[j] and l1[j]
else f1[j] ← f2[j - 1] + t2, j-1 + a1, j
l1[j] ← 2
if f2[j - 1] + a2, j ≤ f1[j - 1] + t1, j-1 + a2, j
then f2[j] ← f2[j - 1] + a2, j
l2[j] ← 2 Compute the values of
f2[j] and l2[j]
else f2[j] ← f1[j - 1] + t1, j-1 + a2, j
l2[j] ← 1
18
FASTEST-WAY(a, t, e, x, n)
(cont.)
if f1[n] + x1 ≤ f2[n] + x2
then f* = f1[n] + x1
Compute the values of
l* = 1 the fastest time through the
else f* = f2[n] + x2 entire factory
l* = 2
19
Construct an Optimal Solution
Alg.: PRINT-STATIONS(l, n)
*i ← l
print “line ” i “, station ” n
for j ← n downto 2
do i ←li[j]
print “line ” i “, station ” j - 1
1 2 3 4 5
f1[j]/l1[j] 9 [1]
18 [2]
20 [1]
24 [1]
32
l* = 1
[1] [2] [1] [2]
f2[j]/l2[j] 12 16 22 25 30
20
☺ End of Lecture