You are on page 1of 14

Assembly Line Scheduling

Optimal Substructure
For assembly-line scheduling, an optimal solution to a problem (finding the fastest way
through station Si, j ) contains within it an optimal solution to subproblems (finding the
fastest way through either S1, j−1 or S2, j−1).

The fastest way through station S1, j is either

• the fastest way through station S1, j−1 and then directly through station S1, j, or

• the fastest way through station S2, j−1, a transfer from line 2 to line 1, and then through
station S1, j .

Using symmetric reasoning, the fastest way through station S2, j is either

• the fastest way through station S2, j−1 and then directly through station S2, j, or

• the fastest way through station S1, j−1, a transfer from line 1 to line 2, and then through
station S2, j .

From a Recursive solution

f*=min(f 1[n]+x1, f 2[n]+x2)


f 1[1]= e1 + a (1,1) f 2[1]= e2 + a (2,1)

L (i,j)= 1 or 2, based on the choices made

Compute the optimal answer

i 1 2 3 4 5 6 F*
F 1,j 9 18 20 24 32 35 38
F 2,j 12 16 22 25 30 37
i 2 3 4 5 6 L*
L 1,j 1 2 1 1 2 1
L 2,j 1 2 1 2 2

Optimal Path

Matrix Chain Multiplication

A (10,100), B (100,50), C (50,20) Final product dimensions 10x20

How many computations required to multiply two matrices ??


How many ways for three matrices ??

((A B) C) and (A(B C))


Cost ?? C1= 10x100x50 + 10x50x20= 60,000 C2=100x50x20+10x100x20 = 1,20,000

A, B, C, D Four matrices, how many ways ??

(((A B) C) D) ((A B) (C D)) ( (A (B C)) D) (A ((B C) D)) (A (B (C D)))

(A1, A2 A3) (A4 A5) k=3

Optimal sub-structure
Optimal Substructure Property: If final “optimal” solution of A (i..j)
involves splitting into A (i..k) and A(k+1…j), at final step then parenthesization
of A (i..k) and A(k+1…j) in final optimal solution must also be optimal for the
subproblems.
Recursive Solution

Computing optimal Solution

O(n^3) Space O(n^2)

P = 4, 10, 3, 12, 20, 7 0-n

A1= 4x10 A2=10x3 A3=3x12 A4=12x20 A5=20x7


m[1,2] = m[1,1]+ m[2,2] + 4x10x3 = 120 k=1 s[1,2]=1
m[2,3]= m[2,2]+m[3,3] + 10x3x12 = 360 k=2 s[2,3]=2
m[3,4] = m[3,3]+m[4,4] + 3x12x20 = 720 k=3 s[3,4]=3
m[4,5]=m[4,4]+m[5,5] + 12x20x7 = 1680 k=4 s[4,5]=4

m[1,3]  k=1 m[1,1]+m[2,3] + 4x10x12 = 0+360+480= 840


k=2 m[1,2]+m[3,3] + 4x3x12 = 120 + 0+ 144 = 264 s[1,3]=2

m[2,4]  k=2 m[2,2]+m[3,4] + 10x3x20 = 0+ 720+ 600 = 1320 s[2,4]=2


k=3 m[2,3]+m[4,4] + 10x12x20 = 360 + 0 + 2400= 2760

m[3,5]  k=3 m[3,3]+m[4,5] + 3x12x7 = 1932


k=4 m[3,4]+m[5,5] + 3x20x7 = 1140 s[3,5]=4

m[1,4]  k=1 m[1,1]+m[2,4] + 4x10x20= 2120


k=2 m[1,2] + m[3,4] + 4x3x20= 1080 s[1,4]=2
k=3 m[1,3] + m[[4,4] + 4x12x20= 1224
m[2,5]  k=2 m[2,2]+m[3,5]+ 10x3x7=1350 s[2,5]=2
k=3 m[2,3]+ m[4,5]+10x12x7=
k=4 m[2,4]+ m[5,5]+ 10x20x7=
m[1,5]  k=1 m[1,1]+m[2,5]+4x10x7=0+1350+280=1630
k=2 m[1,2]+ m[3,5]+ 4x3x7 =120+1140+84= 1344 s[1,5]=2
k=3 m[1,3]+ m[4,5]+4x12x7=264+1680
k=4 m[1,4]+ m[5,5] + 4x20x7=1080+0+560= 1640
m 1 2 3 4 5
1 0 120 264 1080 1344
2 0 360 1320 1350
3 0 720 1140
4 0 1680
5 0

s 1 2 3 4 5
1 1 2 2 2
2 2 2 2
3 3 4
4 4
5

(A1 A2) ((A3 A4) A5) ((A1 A2) ((A3 A4) A5))

Longest Common Subsequence (LCS)


X=(A, B, C, B, D, A, B) Z =(B,C,D,B) is a subsequence of X
Why ?? (2,3,5,7)
Y=(B, D, C, A, B, A) From X(1..m), Y(1..n), find Z, the LCS
Z=(B,C,B,A) (B,D,A,B) etc..
How ?? m 1 2 3 …..m power set Cost ?? 2^m --- O(n*2^m)
X=(…..A B )Y=(….. C ) |Z|=
Optimal Substructure
Recursive solution

Optimal solution

j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0 U0 U0 U0 D1 L1 D1
2 B 0 D1 L1 L1 U1 D2 L2
3 C 0 U1 U1 D2 L2 U2 U2
4 BB 0 D1 U1 U2 U2 D3 L3
5 D 0 U1 D2 U2 U2 U3 U3
6 AA 0 U1 U2 U2 D3 U3 D4
7 B 0 D1 U2 U2 U3 D4 U4
Optimal Path

O1 Knapsack example

W=8 wi=2,3,4,5 pi=1,2,5,6 p/w= 0.5, .66, 1.25. 1.2 P=5 W-w=4

W=60 wi=10, 40, 20 pi=100, 280, 120 Greedy ?? Optimal answer ??

pi wi -k -1 0 1 2 3 4 5 6 7 8
0 -inf -inf 0 0 0 0 0 0 0 0 0
1 2 1 -inf -inf 0 0 1 1 1 1 1 1 1
2 3 2 -inf -inf 0 0 1 2 2 3 3 3 3
5 4 3 -inf -inf 0 0 1 2 5 5 6 7 7
6 5 4 -inf -inf 0 0 1 2 5 6 6 7 8
I4 1 ; I3 0 ; I2 1 ; I1 0 A={ 0, 1, 0, 1} O(n+1*W+1) O(nW)

Recursive

T[I,j]= max( T[i-1,j], T[i-1,j-wi] + pi ) if j is < 0 then T[I,j]= -inf

If I or j are =0, then T[I,j]=0

Backtracking is an algorithmic paradigm that tries different solutions until finds a solution
that “works”. Problems which are typically solved using backtracking technique have the
following property in common. These problems can only be solved by trying every possible
configuration and each configuration is tried only once. A Naive solution for these problems
is to try all configurations and output a configuration that follows given problem constraints.
Backtracking works in an incremental way and is an optimization over the Naive solution
where all possible configurations are generated and tried.
n queens problem

4 Queens problem
Sum of Subsets
Boundary conditions

Algorithm
Example: n=6, m=30 w={5, 10, 12, 13, 15, 18} Complexity 2^n
Answers: { 1 1 0 0 1 0}, {1,0,1,1} {0,0,1,0,0,1}
X={1,0,1,0,1}
(0,1,73)

(5,2,68) (0,2,68)

(15,3,58) (5,3,58)

(15,4,46) (17,4,46) (5,4,46)

(15,5,33) Ans. (5,5,33)

Ans
Graph Colouring

m number of colours , n nodes Number of nodal states mn

nextvalue fn. has to go through n nodes  T.C.=O(nmn)

You might also like