CA Module 1
CA Module 1
1
TY IT 21-22
D B Kulkarni
Walchand College of Engineering, Sangli
Schedule
➢ Knapsack problem
○ Knapsack (Bag)
○ Weight handling capacity of Knapsack M
○ Items to be inserted
○ Item (wheat, jowar, rice…) Weight and profit factor given
➢ Optimization
○ Best the one which carries max profit!!!
○ Best the one which has least weight!!!
Item (M=100) 1 2 3 4
60 10 30 80
30 40 10 20
Optimal:
Greedy Approach 1: Less weight: Sort items w˄- 2,3,1-p80
Greedy Approach 2: More profit: Sort items p˅-2,1,3/8(4)-p77.5
Correct Approach: Less weight/profit: Sort items (w/p) ˄-2,1,3-p80
10/22/2020
0-1 Knapsack problem
➢ Brute force method: Consider all subsets of weight where sum is <M, select
the one with highest profit
➢ Dynamic programming approach
10/22/2020
Dynamic programming Approach
◆ Wasted space
▪ Unicode uses twice as much space as ASCII
• inefficient for plain-text messages containing only ASCII characters
◆ Same number of bits used to represent all characters
▪ ‘a’ and ‘e’ occur more frequently than ‘q’ and ‘z’
A = 00
001011011100111111111
B = 01
1
C = 10
D = 11 ACDBADDDDD
Prefix property
◆ A code has the prefix property if no character code is the prefix (start of the
code) for another character
◆ Example: Symbol Code
P 000
Q 11
0100110110001
R 01 0
S 001 RSTQPT
T 10
Symbol Code
P 0
Q 1
R 01
S 10
T 11
◆ The pattern 1110 can be decoded as QQQP, QTP, QQS, or TS
Problem
DEAACAAAAAB
A
Symbol Code
A 0
B 10
C 110
D 1110
E 11110
111011110001100000010 22
0 bits
Another possible code
DEAACAAAAAB
A
Symbol Code
A 0
B 100
C 101
D 1101
E 1111
110111110010100000100 22
0 bits
Better code
DEAACAAAAAB
A
Symbol Code
A 0
B 100
C 101
D 110
E 111
1101110010100000100 20
0 bits
What code to use?
◆ Question: Is there a variable-length code that makes the most efficient use
of space?
Answer:
Yes!
Huffman coding tree
◆ Binary tree
▪ each leaf contains symbol (character)
▪ label edge from node to left child with 0
▪ label edge from node to right child with 1
◆ Code for any symbol obtained by following path from root to the leaf
containing symbol
◆ Code has prefix property
▪ leaf node cannot appear on path to another leaf
▪ note: fixed-length codes are represented by a complete Huffman tree and clearly have the
prefix property
Building a Huffman tree
1 1 1 1 2 2 3 3 5
1 1 1 1 2 2 3 3 5
A G M T E H _ I S
Step 1
1 1 1 1 2 2 3 3 5
A G M T E H _ I S
Step 2
2 2
1 1 1 1 2 2 3 3 5
A G M T E H _ I S
Step 3
2 2 4
1 1 1 1 2 2 3 3 5
A G M T E H _ I S
Step 4
2 2 4
1 1 1 1 2 2 3 3 5
A G M T E H _ I S
Step 5
2 2 4 6
1 1 1 1 2 2 3 3 5
A G M T E H _ I S
Step 6
4 4
2 2 2 2 6
E H
1 1 1 1 3 3 5
A G M T _ I S
Step 7
8 1
1
4 4 6 5
S
2 2 2 2 3 3
E H _ I
1 1 1 1
A G M T
Step 8
1
9
8 1
1
4 4 6 5
S
2 2 2 2 3 3
E H _ I
1 1 1 1
A G M T
Label edges
1
0 9 1
8 1
1
0 1
0 1
4 4 6 5
0 1 0 1 0 1 S
2 2 2 2 3 3
0 1 0 1 E H _ I
1 1 1 1
A G M T
Huffman code & encoded message
S 11
E 010
H 011
_ 100
I 101
A 0000
G 0001
M 0010
T 0011
001101110111100101111000111011110000100101111000000010
10
Matrix Multiplication
• Suppose we have A1, A2 matrices to be
multiplied
– That is, we want to compute the product A1*A2
– Orders of matrix
• A1(pxq)
• A2(rxs)
• For multiplication q=r
• They must be compatible
Matrix-chain Multiplication
• Suppose we have a sequence or chain A1, A2, …, An of
n matrices to be multiplied
– That is, we want to compute the product A1A2…An
11-38
Matrix-chain Multiplication …contd
11-39
Matrix-chain Multiplication …contd
11-40
Algorithm to Multiply 2 Matrices
Input: Matrices Ap×q and Bq×r (with dimensions p×q and q×r)
Result: Matrix Cp×r resulting from the product A·B
MATRIX-MULTIPLY(Ap×q , Bq×r)
1. for i ← 1 to p
2. for j ← 1 to r
3. C[i, j] ← 0
4. for k ← 1 to q
5. C[i, j] ← C[i, j] + A[i, k] · B[k, j]
6. return C
Scalar multiplication in line 5 dominates time to compute CNumber of scalar
multiplications = pqr
11-41
Matrix-chain Multiplication …contd
0 if i=j
m[i, j ] =
min {m[i, k] + m[k+1, j ] + pi-1pk pj } if i<j
i ≤ k< j
Dynamic Programming Approach …contd
• To keep track of how to construct an optimal solution,
we use a table s
• s[i, j ] = value of k at which Ai Ai+1 … Aj is split for optimal
parenthesization
• Algorithm: next slide
– First computes costs for chains of length l=1
– Then for chains of length l=2,3, … and so on
– Computes the optimal cost bottom-up
Algorithm to Compute Optimal Cost
Input: Array p[0…n] containing matrix dimensions and n
Result: Minimum-cost table m and split table s
MATRIX-CHAIN-ORDER(p[ ], n)
for i ← 1 to n Takes O(n3) time
m[i, i] ← 0
Requires O(n 2) space
for l ← 2 to n
for i ← 1 to n-l+1
j ← i+l-1
m[i, j] ← ∞
for k ← i to j-1
q ← m[i, k] + m[k+1, j] + p[i-1] p[k] p[j]
if q < m[i, j]
m[i, j] ← q
s[i, j] ← k
return m and s
Constructing Optimal Solution
• Our algorithm computes the minimum-cost table m and
the split table s
• The optimal solution can be constructed from the split
table s
– Each entry s[i, j ]=k shows where to split the product Ai Ai+1 …
Aj for the minimum cost
MCM Example..contd…
● For m[i,j] let s[i,j]=k for optimal splitting (index for which m[i,j] is minimum)
● Lets split matrix chain AiAi+1…. Aj into (Ai..Ak)*( Ak+1..Aj) and compute m[i,k] and m[k+1,j]
● For m[i,k] let s[i,k]=l for optimal splitting and for m[k+1,j] let s[k+1,j]=m be the value for
optimal splitting
● split matrix chain AiAi+1…. Ak into (Ai..Al)*( Al+1..Ak) and Ak+1Ak+2…. Aj into (Ak+1..Am)*( Am+1..Aj)
● Recurse
● Example
● Minimum from which m[1,4] is calculated, will give value of corresponding k
● Record this value as s[1,4]
● m[1,s[1,4]] OR m[s[1,4]+1,4]
● Parentisize
MCM Example
● For four matrices with vector p=
{5,4,6,2,7} such that
{A1(5x4),A2(4x6),A3(6x2),A4(2x7)} find
anesthetization
● To find k such that m[1,4] is minimum
M[2,4] =min2<k<4{m[2,k]+m[k+1,4]+p1pkp4}
=min{(m[2,2]+m[3,4]+p1p2p4),
(m[2,3]+m[4,4]+p1p3p4)}
=min{(120+84+4*6*7), (48+0+4*2*7)}
=Min{(254),(104)}
Example
• Show how to multiply this matrix Matrix Dimension
chain optimally
A1 30×35
A2 35×15
• Solution ??
– Minimum cost 15,125 A3 15×5
– Optimal parenthesization
((A1(A2A3))((A4 A5)A6))
A4 5×10
A5 10×20
A6 20×25
Longest common sub-sequence (LCS)
● Given two sequences {X,Y}, finding the longest subsequence common to both. X={x1,x2…xn}
Y={y1,y2…ym}
● Sub-sequences are different from sub-string
● Complexity NP Hard problem with complexity 2^(lenth of sequence)
● Let Z={z1,z2…zk} be the LCS of X and Y
● If zk=xn= ym, then zk-1 is LCS of Xn -1 and Ym -1
● xn<> ym
● If zk <> xn then then Zk is LCS of Xn -1 and Ym
● To find
● Length of LCS
● LCS
LCS: Example
Xi=5={A,C,A,D,B}, Yj=4 ={C,B,D,A}
{C,A}{C,D}{C,B}
LCS:Homework Example
● X={A,B,C,B,D,A,B}, Y{B,D,C,A,B,A}