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

Lec 9 DynamicProgramming-1

Compiler construction

Uploaded by

shizamalik783
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lec 9 DynamicProgramming-1

Compiler construction

Uploaded by

shizamalik783
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 72

Advanced Algorithm Analysis

Lecture# 09

Dynamic Programming

1
Optimization Problems
• Some problems can have many possible/ feasible solutions
with each solution having a specific cost. We wish to find the
best solution with the optimal cost.
• Maximization problem finds a solution with maximum cost
• Minimization problem finds a solution with minimum cost

• A set of choices must be made in order to arrive at an optimal


(min/max) solution, subject to some constraints.

• Is “Sorting a sequence of numbers” optimization problem?


2
Optimization Problems
• Two common techniques:
• Greedy Algorithms (local)
• Make the greedy choice and THEN
• Solve sub-problem arising after the choice is made
• The choice we make may depend on previous choices, but
not on solutions to sub-problems
• Top down solution, problems decrease in size
• Dynamic Programming (global)
• We make a choice at each step
• The choice depends on solutions to sub-problems 3
• Bottom up solution, smaller to larger sub-problems
Dynamic Programming
• Dynamic Programming was introduced by Richard Bellman in 1955.

• A strategy for designing algorithms


• meta technique, not an algorithm

• Developed back in days when programming meant tabular method


(like linear programming). Doesn't really refer to computer
programming.

• Dynamic programming solves every sub problem just once.


• Saves its answer in a table (array)

• More efficient than “brute-force methods” as it avoids the work of re-


computing the answer every time the sub-problem is encountered.
4
• Dynamic programming is typically applied to optimization problems.
Dynamic Programming
• How to know if an optimization problem can be solved by
applying dynamic programming?
• Two key ingredients
• Optimal substructure
• Overlapping sub-problems

• Optimal Substructure: A problem exhibits optimal substructure


if an optimal solution to the problem contains within its optimal
solutions to the sub-problems

• Overlapping sub problems: When a recursive algorithm revisits


the same problem over and over again, then the optimization
5
problem has overlapping sub-problems
Dynamic Programming
Following steps are required in development of dynamic algorithms
1.Characterize the structure of an optimal solution
2.Recursively define the value of an optimal solution
3.Compute the value of an optimal solution in a bottom-up fashion
4.Construct an optimal solution from computed information

Note: Steps 1-3 form the basis of a dynamic programming


solution to a problem. Step 4 can be omitted only if the value of an
optimal solution is required.

6
Dynamic Programming
• Dynamic programming, like divide and conquer method, solves
problems by combining the solutions to sub-problems.

• Divide and conquer algorithms:


• partition the problem into independent sub-problem
• Solve the sub-problem recursively and
• Combine their solutions to solve the original problem

• In contrast, dynamic programming is applicable when the sub-


problems are not independent.

7
Divide-and-Conquer

Top-down
Prob

Subprob Subprob


Independent sub-problems (no overlapping work).


8
Dynamic Programming

Prob

Subprob1 Subprob2

Subproblem
Subprob3 sharing
Subprob4
Subprob7

Subprob5
9
Subprob6
Bottom-up
Knapsack Problem

10
Knapsack Problem
Given n items, each having a specific value vi and weight wi,
and a knapsack of fixed capacity W, pack the knapsack with
given items to maximize total value without exceeding the
total capacity W of the knapsack.

Item # Weight Value


1 1 8
2 3 6
3 5 5

11
Knapsack Problem
There are two versions of the problem:
1. “0-1 knapsack problem”
• Items are indivisible; you either take an item or not. Some
special instances can be solved with dynamic
programming

2. “Fractional knapsack problem”


• Items are divisible: you can take any fraction of an item

12
0-1 Knapsack Problem
• Given a knapsack with maximum capacity W, and a set S consisting
of n items
• Each item i has some weight wi and benefit value bi (all wi and W
are integer values)

• Problem: How to pack the knapsack to achieve maximum total


value of packed items?

max  bi subject to  wi W
iT iT

13
0-1 Knapsack Problem: Brute Force
Approach
Subset Total weight Total value
1.  0 0 # W
V
2. {1} 2 20 1 2
20
3. {2} 5 30 2 5
30
4. {3} 10 50 3 10
50
5. {4} 5 10 4 5
10 Go through all combinations and
6. {1,2} 7 50 find the one with maximum value
7. {1,3} 12 70 and with total weight
knapsack ≤W W
capacity
= 16
8. {1,4} 7 30
9. {2,3} 15 80 14
10. {2,4} 10 40
11. {3,4} 15 60 Running time = O(2n)
12. {1,2,3} 17 not feasible
0-1 Knapsack Problem: Brute Force
Approach
Knapsack-BF (n, V, W, C)
Compute all subsets, s, of S = {1, 2, 3, 4}
forall s  S
weight = Compute sum of weights of these items
if weight > C, not feasible
new solution = Compute sum of values of these items
solution = solution  {new solution}
Return maximum of solution

Approach: In brute force algorithm, we go through all combinations and find the
one with maximum value and with total weight less or equal to W = 16

Complexity
• Cost of computing subsets O(2n) for n elements
• Cost of computing weight = O(2n) 15
• Cost of computing values = O(2n)
• Total cost in worst case: O(2n)
0-1 Knapsack problem:
Dynamic Programming Approach
We can do better with an algorithm based on dynamic programming.
We need to carefully identify/define the sub-problems

Let’s try this:


•If items are labeled 1..n, then a sub-problem would be to find an
optimal solution for Sk = {items labeled 1, 2, .. k}

This is a reasonable sub-problem definition. The question is can we


describe the final solution (Sn ) in terms of sub-problems (Sk)?
Unfortunately, we can’t do that.

16
Defining a Sub-problem
w1 =2 w2 =4 w3 =5 w4 =3
Weight Benefit
b1 =3 b2 =5 b3 =8 b4 =4
Item wi bi
#
?
Max weight: W = 20
1 2 3
S4 2 4 5
For S4:
S5
Total weight: 14 3 5 8
Maximum benefit: 20 4 3 4
5 9 10
w1 =2 w2 =4 w3 =5 w5 =9
b1 =3 b2 =5 b3 =8 b5 =10
Solution for S4 is
For S5: 17
Total weight: 20 not part of the
Maximum benefit: 26 solution for S5!!!
Defining a Sub-problem
As we have seen, the solution for S4 is not part of the solution for S5 so
our definition of a sub-problem is flawed and we need another one!

Let’s add another parameter: w, which will represent the maximum


weight for each subset of items.

The sub-problem then will be to compute V[k,w], i.e., to find an optimal


solution for Sk = {items labeled 1, 2, .. k} in a knapsack of size w

Assuming knowing V[i, j], where i = 0, 1, 2, … k-1, and j = 0, 1, 2, …w,


how to derive V[k,w]?
18
Recursive Formula for sub-
problems
Recursive formula for sub-problems:
 V [k  1, w] if wk  w
V [ k , w] 
max{V [k  1, w],V [k  1, w  wk ]  bk } else

The best subset of Sk that has the total weight  w, either


contains item k or not.
•First case: wk>w. Item k can’t be part of the solution, since if it
was, the total weight would be > w, which is unacceptable.

•Second case: wk  w. Then the item k can be in the solution,


and we choose the case with greater value. 19
0-1 Knapsack Algorithm
for w = 0 to W O(W)
V[0,w] = 0
for i = 1 to n
O(n)
V[i,0] = 0
for i = 1 to n
Repeat n times
for w = 0 to W
if wi <= w // item i can be part of the O(W)
solution
if bi + V[i-1,w-wi] > V[i-1,w] Remember that the
V[i,w] = bi + V[i-1,w- wi] brute-force algorithm
else takes O(2n)
20
V[i,w] = V[i-1,w]
What is the running
else V[i,w] =time of this
V[i-1,w] // wi > w O(n*W)
Example
Let’s run our algorithm on the following data:

n = 4 (# of elements)
W = 5 (max weight)

Elements (weight, benefit):


(2,3), (3,4), (4,5), (5,6)

21
Example (2)
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1
2
3
4

for w = 0 to W
V[0,w] = 0
22
Example (3)
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0

for i = 1 to n
V[i,0] = 0
23
Items:
1: (2,3)
Example (4) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=1
0 0 0 0 0 0 0 bi=3
1 0 0 wi=2
2 0 w=1
3 0 w-wi =-1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 24
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (5) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=1
0 0 0 0 0 0 0 bi=3
1 0 0 3 wi=2
2 0 w=2
3 0 w-wi =0
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 25
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (6) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=1
0 0 0 0 0 0 0 bi=3
1 0 0 3 3 wi=2
2 0 w=3
3 0 w-wi =1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 26
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (7) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=1
0 0 0 0 0 0 0 bi=3
1 0 0 3 3 3 wi=2
2 0 w=4
3 0 w-wi =2
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 27
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (8) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=1
0 0 0 0 0 0 0 bi=3
1 0 0 3 3 3 3 wi=2
2 0 w=5
3 0 w-wi =3
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 28
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (9) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=2
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3 wi=3
2 0 0 w=1
3 0 w-wi =-2
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 29
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (10) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=2
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3 wi=3
2 0 0 3 w=2
3 0 w-wi =-1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 30
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (11) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=2
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3 wi=3
2 0 0 3 4 w=3
3 0 w-wi =0
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 31
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (12) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=2
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3 wi=3
2 0 0 3 4 4 w=4
3 0 w-wi =1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 32
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (13) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=2
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3 wi=3
2 0 0 3 4 4 7 w=5
3 0 w-wi =2
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 33
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (14) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=3
0 0 0 0 0 0 0 bi=5
1 0 0 3 3 3 3 wi=4
2 0 0 3 4 4 7 w= 1..3
3 0 0 3 4
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 34
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (15) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=3
0 0 0 0 0 0 0 bi=5
1 0 0 3 3 3 3 wi=4
2 0 0 3 4 4 7 w= 4
3 0 0 3 4 5 w- wi=0
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 35
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (16) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=3
0 0 0 0 0 0 0 bi=5
1 0 0 3 3 3 3 wi=4
2 0 0 3 4 4 7 w= 5
3 0 0 3 4 5 7 w- wi=1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 36
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (17) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=4
0 0 0 0 0 0 0 bi=6
1 0 0 3 3 3 3 wi=5
2 0 0 3 4 4 7 w= 1..4
3 0 0 3 4 5 7
4 0 0 3 4 5
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 37
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Items:
1: (2,3)
Example (18) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=4
0 0 0 0 0 0 0 bi=6
1 0 0 3 3 3 3 wi=5
2 0 0 3 4 4 7 w= 5
3 0 0 3 4 5 7 w- wi=0
4 0 0 3 4 5 7
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else 38
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Comments
This algorithm only finds the max possible value that can be
carried in the knapsack
• i.e., the value in V[n,W]

To know the items that make this maximum value, an


addition to this algorithm is necessary

39
How to find actual Knapsack Items
All of the information we need is in the table.

V[n,W] is the maximal value of items that can be placed in the


Knapsack.

Let i=n and k=W


if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1 // Assume the ith item is not in the knapsack
// Could it be in the optimally packed knapsack? 40
Items:
1: (2,3)
Finding the Items 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=4
0 0 0 0 0 0 0 k= 5
1 0 0 3 3 3 3 bi=6
2 0 0 3 4 4 7 wi=5
3 0 0 3 4 5 7 V[i,k] = 7
4 0 0 3 4 5 7 V[i1,k] =7
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
41
i = i1, k = k-wi
else
i = i 1
Items:
1: (2,3)
Finding the Items (2) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=4
0 0 0 0 0 0 0 k= 5
1 0 0 3 3 3 3 bi=6
2 0 0 3 4 4 7 wi=5
3 0 0 3 4 5 7 V[i,k] = 7
4 0 0 3 4 5 7 V[i1,k] =7
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
42
i = i1, k = k-wi
else
i = i 1
Items:
1: (2,3)
Finding the Items (3) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=3
0 0 0 0 0 0 0 k= 5
1 0 0 3 3 3 3 bi=5
2 0 0 3 4 4 7 wi=4
3 0 0 3 4 5 7 V[i,k] = 7
4 0 0 3 4 5 7 V[i1,k] =7
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
43
i = i1, k = k-wi
else
i = i 1
Items:
1: (2,3)
Finding the Items (4) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=2
0 0 0 0 0 0 0 k= 5
1 0 0 3 3 3 3 bi=4
2 0 0 3 4 4 7 wi=3
3 0 0 3 4 5 7 V[i,k] = 7
4 0 0 3 4 5 7 V[i1,k] =3
k  wi=2
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
44
i = i1, k = k-wi
else
i = i 1
Items:
1: (2,3)
Finding the Items (5) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=1
0 0 0 0 0 0 0 k= 2
1 0 0 3 3 3 3 bi=3
2 0 0 3 4 4 7 wi=2
3 0 0 3 4 5 7 V[i,k] = 3
4 0 0 3 4 5 7 V[i1,k] =0
k  wi=0
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
45
i = i1, k = k-wi
else
i = i 1
Items:
1: (2,3)
Finding the Items (6) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5 i=0
0 0 0 0 0 0 0 k= 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
The optimal
i=n, k=W
knapsack
while i,k > 0
should contain
if V[i,k]  V[i1,k] then
mark the nth item as in the knapsack
{1, 2}
46
i = i1, k = k-wi
else
i = i 1
Items:
1: (2,3)
Finding the Items (7) 2: (3,4)
3: (4,5)
i\W 4: (5,6)
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
The optimal
i=n, k=W
knapsack
while i,k > 0
should contain
if V[i,k]  V[i1,k] then
mark the nth item as in the knapsack
{1, 2}
47
i = i1, k = k-wi
else
i = i 1
0-1 Knapsack
Example
Input
• Given n items each
• weight wi
• value vi
• Knapsack of capacity W
Output: Find most valuable items that fit into the knapsack

Example:
item weight value knapsack capacity W = 16
1 2 20
2 5 30
3 10 50 48
4 5 10
Exercise

49
Assembly-Line Scheduling
Problem

50
Assembly-Line Scheduling Problem

• Given two “parallel” assembly lines each with n stations.


• Each assembly line can perform any job
• An auto enters factory, goes through an assembly line, and exits
• The auto is served at n stations, each performing individual tasks
• Problem is to determine which stations to choose from lines 1 & 2 to
minimize total time through the factory (i.e. the fastest route).

• After going through the jth station on a line i, the auto goes on to the
(j+1)st station on either line.There is no transfer cost if it stays on the
same line
• An optimal solution to the entire problem depends on optimal
solutions to sub-problems
• We formulate a solution for the assembly line problem stage-by-stage 51
Notations: Assembly-Line Scheduling Problem

e1 x1
2 3 1 3 4
In 2
Out
2 1 2 1
e2 x2

2 assembly lines, i = 1,2;


n stations, j = 1,...,n.
Stations Si,j;

ai,j = assembly time at Si,j;


ti,j = transfer time from Si,j (to Si-1,j+1 OR Si+1,j+1);
52
ei = entry time from line i;
xi = exit time from line i .
Brute Force Solution

e1 x1
2 3 1 3 4
In 2
Out
2 1 2 1
e2 x2

Total Computational Time


= possible ways to enter in stations at level n x one way Cost
Possible ways to enter in stations at level 1 = 21
Possible ways to enter in stations at level 2 = 22 . . .
Possible ways to enter in stations at level n = 2n
53
Total Computational Time = (2n)
Dynamic Programming Solution
Notations: Finding Objective Functions
•Let fi[j] = fastest time from starting point to station Si, j
•f1[n] = fastest time from starting point to station S1 n
•f2[n] = fastest time from starting point to station S2 n
•li[j] = The line number, 1 or 2, whose station j-1 is used in a fastest way
through station Si, j .
•It is to be noted that li[1] is not required to be defined because there is
no station before 1
•ti[j-1] = transfer time from line i to station Si-1, j or Si+1, j
•Objective function = f* = min(f1[n] + x1, f2[n] + x2)
•l* = to be the line no. whose nth station is used in a fastest way.
54
•Our improved strategy is to build a table to save previous results
so that they don’t have to be recomputed each time
Notations: Finding Objective Function

2 S1 j-1 3

In Out
4 2
8 5 6 Si,j 5 7

ai,j

fi(j)
t1,j-1
l2(j) =1
55
Mathematical Model: Finding Objective Function

2 3

In Out
4 2

f1[1] = e1 + a1,1;
f2[1] = e2 + a2,1.
f1[j] = min (f1[j-1] + a1,j, f2[j-1] + t2,j-1 + a1,j) for j ≥ 2;
f2[j] = min (f2[j-1] + a2,j, f1[j-1] + t1,j-1 + a2,j) for j ≥ 2;

Where fi[j] is the fastest possible time to Si,j


ti,j is the transfer time from Si,j 56
ai,j is the time at Si,j
ei is the entry time for assembly line i
Complete Model: Finding Objective Function
Base Cases
• f1[1] = e1 + a1,1
• f2[1] = e2 + a2,1
Two possible ways of computing f1[j]
• f1[j] = f2[j-1] + t2, j-1 + a1, j OR f1[j] = f1[j-1] + a1, j
For j = 2, 3, . . ., n
f1[j] = min (f1[j-1] + a1, j, f2[j-1] + t2, j-1 + a1, j)
Symmetrically
For j = 2, 3, . . ., n
f2[j] = min (f2[j-1] + a2, j, f1[j-1] + t1, j-1 + a2, j) 57

Objective function = f* = min(f1[n] + x1, f2[n] + x2)


Example: Computation of f1[2]

2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7
• f1[1] = e1 + a1,1 = 2 + 7 = 9
• f2[1] = e2 + a2,1 = 4 + 8 = 12
f1[j] = min (f1[j-1] + a1, j, f2[j-1] + t2, j-1 + a1, j)
f2[j] = min (f2[j-1] + a2, j, f1[j-1] + t1, j-1 + a2, j)
j=2
f1[2] = min (f1[1] + a1, 2, f2[1] + t2, 1 + a1, 2)
58
= min (9 + 9, 12 + 2 + 9) = min (18, 23) = 18, l1[2] = 1
Computation of f2[2]

2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7
• f1[1] = e1 + a1,1 = 2 + 7 = 9
• f2[1] = e2 + a2,1 = 4 + 8 = 12
f1[j] = min (f1[j-1] + a1, j, f2[j-1] + t2, j-1 + a1, j)
f2[j] = min (f2[j-1] + a2, j, f1[j-1] + t1, j-1 + a2, j)
j=2
f2[2] = min (f2[1] + a2, 2, f1[1] + t1, 1 + a2, 2)
59
= min (12 + 5, 9 + 2 + 5) = min (17, 16) = 16, l2[2] = 1
Computation of f1[3]

2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7

f1[j] = min (f1[j-1] + a1, j, f2[j-1] + t2, j-1 + a1, j)


f2[j] = min (f2[j-1] + a2, j, f1[j-1] + t1, j-1 + a2, j)
j=3
f1[3] = min (f1[2] + a1, 3, f2[2] + t2, 2 + a1, 3)
= min (18 + 3, 16 + 1 + 3)
60
= min (21, 20) = 20, l1[3] = 2
Computation of f2[3]

2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7

f1[j] = min (f1[j-1] + a1, j, f2[j-1] + t2, j-1 + a1, j)


f2[j] = min (f2[j-1] + a2, j, f1[j-1] + t1, j-1 + a2, j)
j=3
f2[3] = min (f2[2] + a2, 3, f1[2] + t1, 2 + a2, 3)
= min (16 + 6, 18 + 3 + 6)
61
= min (22, 27) = 22, l2[3] = 2
Computation of f1[4]

2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7

f1[j] = min (f1[j-1] + a1, j, f2[j-1] + t2, j-1 + a1, j)


f2[j] = min (f2[j-1] + a2, j, f1[j-1] + t1, j-1 + a2, j)
j=4
f1[4] = min (f1[3] + a1, 4, f2[3] + t2, 3 + a1, 4)
= min (20 + 4, 22 + 1 + 4)
62
= min (24, 27) = 24, l1[4] = 1
Computation of f2[4]

2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7

f1[j] = min (f1[j-1] + a1, j, f2[j-1] + t2, j-1 + a1, j)


f2[j] = min (f2[j-1] + a2, j, f1[j-1] + t1, j-1 + a2, j)
j=4
f2[4] = min (f2[3] + a2, 4, f1[3] + t1, 3 + a2, 4)
= min (22 + 4, 20 + 1 + 4)
63
= min (26, 25) = 25, l2[4] = 1
Computation of f1[5]

2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7

f1[j] = min (f1[j-1] + a1, j, f2[j-1] + t2, j-1 + a1, j)


f2[j] = min (f2[j-1] + a2, j, f1[j-1] + t1, j-1 + a2, j)
j=5
f1[5] = min (f1[4] + a1, 5, f2[4] + t2, 4 + a1, 5)
= min (24 + 8, 25 + 2 + 8)
64
= min (32, 35) = 32, l1[5] = 1
Computation of f2[5]

2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7

f1[j] = min (f1[j-1] + a1, j, f2[j-1] + t2, j-1 + a1, j)


f2[j] = min (f2[j-1] + a2, j, f1[j-1] + t1, j-1 + a2, j)
j=5
f2[5] = min (f2[4] + a2, 5, f1[4] + t1, 4 + a2, 5)
= min (25 + 5, 24 + 3 + 5)
65
= min (30, 32) = 30, l2[5] = 2
Computation of f1[6]

2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7

f1[j] = min (f1[j-1] + a1, j, f2[j-1] + t2, j-1 + a1, j)


f2[j] = min (f2[j-1] + a2, j, f1[j-1] + t1, j-1 + a2, j)
j=6
f1[6] = min (f1[5] + a1, 6, f2[5] + t2, 5 + a1, 6)
= min (32 + 4, 30 + 1 + 4)
66
= min (36, 35) = 35, l1[6] = 2
Computation of f2[6]

2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7

f1[j] = min (f1[j-1] + a1, j, f2[j-1] + t2, j-1 + a1, j)


f2[j] = min (f2[j-1] + a2, j, f1[j-1] + t1, j-1 + a2, j)
j=6
f2[6] = min (f2[5] + a2, 6, f1[5] + t1, 5 + a2, 6)
= min (30 + 7, 32 + 4 + 7)
67
= min (37, 43) = 37, l2[6] = 2
Keeping Track Constructing Optimal Solution
f* = min (f1[6] + x1, f2[6] + x2)
= min (35 + 3, 37 + 2)
= min (38, 39) = 38
l* = 1
l* = 1 => Station S1, 6
l1[6] = 2 => Station S2, 5
l2[5] = 2 => Station S2, 4
l2[4] = 1 => Station S1, 3
l1[3] = 2 => Station S2, 2
68
l2[2] = 1 => Station S1, 1
Entire Solution Set: Assembly-Line Scheduling

2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7

j 1 2 3 4 5 6 j 2 3 4 5 6
fi(j) li(j)
1 9 18 20 24 32 35 1 1 2 1 1 2
2 12 16 22 25 30 37 2 1 2 1 2 2
69
f* = 38 l* = 1
Fastest Way: Assembly-Line Scheduling

2 7 9 3 4 8 4 3
2 3 1 3 4
In 2
Out
2 1 2 1
4 2
8 5 6 4 5 7

l* = 1 => Station S1, 6


l1[6] = 2 => Station S2, 5
l2[5] = 2 => Station S2, 4
l2[4] = 1 => Station S1, 3
l1[3] = 2 => Station S2, 2 70

l2[2] = 1 => Station S1, 1


Dynamic Algorithm
FASTEST-WAY(a, t, e, x, n)
1 ƒ1[1]  e1 + a1,1
2 ƒ2[1]  e2 + a2,1
3 for j  2 to n
4 do if ƒ1[ j - 1] + a1, j ≤ ƒ2[ j - 1] + t2, j - 1 + a1, j
5 then ƒ1[ j]  ƒ1[ j - 1] + a1, j
6 l1[ j]  1
7 else ƒ1[ j]  ƒ2[ j - 1] + t2, j - 1 + a1, j
8 l1[ j]  2
9 if ƒ2[ j - 1] + a2, j ≤ ƒ1[ j - 1] + t1, j - 1 + a 2, j
• then ƒ2[ j]  ƒ2[ j - 1] + a2, j
• l2[ j]  2
• else ƒ2[ j]  ƒ1[ j - 1] + t1, j - 1 + a2, j
• l2[ j]  1
• if ƒ1[n] + x1 ≤ ƒ2[n] + x2 71
• then ƒ* = ƒ1[n] + x1 Total Computational Time = Ɵ(n)
• l =1
*

*
Optimal Solution: Constructing The Fastest Way

1. Print-Stations (l, n)
2. i  l*
3. print “line” i “, station” n
4. for j  n downto 2
5. do i  li[j]
6. print “line” i “, station” j - 1

This printout is “in reverse”;


How can the print routine be
changed to get a printout from
Station 1 to Station 6 ? 72

You might also like