Professional Documents
Culture Documents
3-Module 2 Scribblings-24-01-2024
3-Module 2 Scribblings-24-01-2024
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−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 .
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
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
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))
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
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
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)
Ans
Graph Colouring