0% found this document useful (0 votes)
11 views

Dynammic Programming, ALS Problem

This document discusses dynamic programming and how it can be used to solve the assembly line scheduling problem. It provides an example of calculating the optimal schedule through a multi-station assembly line using dynamic programming by storing previously calculated subproblem solutions in arrays to avoid recomputing them. The key aspects are using recursion with memorization to efficiently solve the problem in polynomial time rather than exponential time with a naive approach.

Uploaded by

Zoha Munawar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Dynammic Programming, ALS Problem

This document discusses dynamic programming and how it can be used to solve the assembly line scheduling problem. It provides an example of calculating the optimal schedule through a multi-station assembly line using dynamic programming by storing previously calculated subproblem solutions in arrays to avoid recomputing them. The key aspects are using recursion with memorization to efficiently solve the problem in polynomial time rather than exponential time with a naive approach.

Uploaded by

Zoha Munawar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Dynamic Programming

(Assembly Line Scheduling Problem)


The image above says a lot about Dynamic
.Programming
So, is repeating the things for which you
? already have the answer, a good thing
A programmer would disagree. That's what
Dynamic Programming is about i.e.
To always remember answers to the sub-problems
.you've already solved
In programming, Dynamic Programming is a
powerful technique that allows one to solve
different types of problems in time O(n2) or
O(n3) for which a naive approach would take
exponential time
Let's try to understand this by taking an
.example of Fibonacci numbers
Fibonacci (n) = 1; iff n = 0

Fibonacci (n) = 1; iff n = 1

Fibonacci (n) = Fibonacci(n-1) + Fibonacci(n-2)

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 –

f2[j -1] + t2,j-1 + a1,j


S1,j-1 S1,j
Line 1 a
a
1,j
-1 t 1,j
f1[j] = min(f1[j - 1] + a1,j ,f2[j -1] + t2,j-1 + a1,j)
2,
j-
a
1
2,
Line 2 j-
S2,j-1
1
13
A Recursive Solution (cont.)
if j = 1 e1 + a1,1
= f1[j]
if j ≥ 2 min(f1[j - 1] + a1,j ,f2[j -1] + t2,j-1 + a1,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

You might also like