SlideShare a Scribd company logo
2
Most read
4
Most read
9
Most read
Longest common subsequence
&
Matrices chain multiplication
Course Title : Algorithm Design And Analysis
2 LONGESTCOMMONSUBSEQUENCE
The longest common subsequence (LCS) problem is the
problem of finding the longest subsequence common
to all sequences in a set of sequences (often just two
sequences).
abc a b c ab bc ac
cab c a b ca ab cb
LCS=ab
3
Condition Table Value Sign
If i=j=0 0 No sign
If (i,j>0) && Xi=Yj C[i-1,j-1]
Else if (i,j>0 && Xi=Yj)
If [C(i-1,j)>=C(i,j-1)]
Else [C(i-1,j)<=C(i,j-1)]
C[i-1,j]
C[I,j-1]
LONGESTCOMMONSUBSEQUENCE
Example: Find the length of the longest subsequence present in both of them…….
Seq1: ABCDEFG , Seq2: CDBAEF
FEDC
Seq1 A B C D E F G
Seq2 0 0 0 0 0 0 0 0
C 0 0 0 1 1 1 1 1
D 0 0 0 1 2 2 2 2
B 0 0 1 1 2 2 2 2
A 0 1 1 1 2 2 2 2
E 0 1 1 1 2 3 3 3
F 0 1 1 1 2 3 4 4
1. m:=length(X)
2. n:=length(Y)
3. Let B=[1……m,1…..n]& C=[0……m,0…….n]
4. For i=1 to n
5. C[i,0]:=0
6. For j=1 to n
7. C[0,j]:=0
8. For i=1 to n
9. for j=1 to n
10. If Xi=Yj
11. C[I,j]:=C[i-1,j-1]
12. B[i,j]=“ “
13. Else
14. if C[i-1,j]>=C[i,j-1]
Algorithm
15. C[i,j]:=C[i-1,j]
16. B[i,j]:=“ “
17. Else
C[i,j]:=C[i,j-1]
18.B[i,j]:=“ “
19.Return
Algorithm
Definition: if we are given a sequence/chain of n Matrices (A1,A2………Ai) where i=1,2…..n &
Ai has dimension Pi-1*Pi to be multiplied and we wish to compute the product or
parenthesis In a way that the total number of scalar multiplication is minimized.
This process is known as Matrix Chain Multiplication.
Goal of MCM: parenthesizing a chain of matrices so that it can have a dramatic impact on
the cast evaluating multiplication of matrices or the product.
Where it is being used:
 Compiler design for cost optimization.
 Database or query optimization.
Rules to remember :
-Multiplication is associative not commulative.
Matrix chain multiplication
A1 A2
(5*4) (4*6)
Dimension: A1 A2 = 5*6
=30
Cost : A1 A2 = 5*4*6
= 120
How does cost can be calculated?
If the matrices are
A1 A2 A3
(5*4) (4*6) (6*2)
 (A1 A2 )A3
 A1( A2 A3)
 (A1 A2 )A3
A1 A2 = 5*4*6
C1 = 120
(A1 A2 )A3 = 5*6*2
C2 = 60
Total Cost = C1 + C2
=120 + 60
= 180
Law :
How many parenthesizing could be possible?
2ncn
P(n-1) =
n+1
Note : if the matrices are A1 ,A2,A3 ,A4 then we take n=3
2*3c3
P(3-1) =
3+1
= 5
A1 A2 A3 A4
(5*4) (4*6) (6*2) (2*7)
P0 P1 P1 P2 P2 P3 P3 P4
2*3c3
P(3-1) =
3+1
= 5
How many parenthesizing could be possible?
m(i,i)= 0
m(j,j)= 0
m(i,j)= min(i<=k<j) {m(i,k) + m(k+1,j) + pi-1 *pk * pj}
m(1,2)= min(1<=k<2) {m(1,1) + m(1+1,2) + p0 *p1 * p2}
= min(1<=k<2) {0 + 0+ 5 *4 *6}
= 120 (for k=1)
m(2,3)= min(2<=k<3) {m(2,2) + m(3,3) + p1 *p2 * p3}
= min(1<=k<3) {0 + 0+ 4 *6 *2}
= 48 (for k= 2)
m(3,4)= min(3<=k<4) {m(3,3) + m(4,4) + p2 *p3 * p4}
= min(1<=k<4) {0 + 0+ 6 *2 *7}
= 84 (for k= 3)
m(1,3)= min(1<=k<3) {m(1,1) + m(2,3) + p0 *p1 * p3} + {m(1,2) + m(3,3) + p0 *p2 * p3}
= min(1<=k<3) {0 + 48+ 5 *4 *2} + {120 + 0 + 5 *6 *2}
= min(1<=k<3) {88,180}
= 88 (for k= 1)
m(2,4)= min(2<=k<4) {m(2,2) + m(3,4) + p1 *p2 * p4} + {m(2,3) + m(4,4) + p1 *p3 * p4}
= min(1<=k<4) {0 + 84+ 4 *6 *7} + {48 + 0 + 4 *2 *7}
= min(1<=k<4) {272,104}
= 104 (for k= 3)
m(1,4)= min(1<=k<4) {m(1,1) + m(2,4) + p0 *p1 * p4} + {m(1,2) + m(3,4) + p0 *p2 * p4} + {m(1,3) + m(4,4) + p0 *p3 * p4}
= min(1<=k<4) {0 + 104+ 5 *4 *7} + {120 + 84 + 5 *6 *7} + {88 + 0 + 5 *2 *7}
= min(1<=k<4) {244,414,158}
= 158 (for k= 3)
Short :
m(1,2) = 120
m(1,3) = 88
m(1,4) = 158
m(2,3) = 48
m(2,4) = 104
m(3,4) = 84
M[1………….m,1………..n] s[1……..n-1,2…………n]
0 120 88 158
0 48 104
0 84
0
1 1 3
2 3
3
For cost value
For the value of k
J=1 2 3 4 J = 2 3 4
i=1
2
3
4
i=1
2
3
For cost value For the value of k
158, k=3
m(i,j)
m[1,4]
m(i,k) , m(k+1,j)
m(1,3) , m(4,4)
88, k=1
m(i,j)
m[1,3]
m(i,k) , m(k+1,j)
m(1,1) , m(2,3)
A4
A1
48, k=2
m(i,j)
m[2,3]
m(i,k) , m(k+1,j)
m(2,2) , m(3,3)
A2 A3
A1 A2 A3 A4
Tree Diagram ????
A1 A2 A3 A4
((A1(A2A3))A4)
A1 A2 A3 A4
A1 A2 A3
A2 A3
Tree Diagram
Thank You

More Related Content

PDF
Pertemuan 9 Array (Larik)
PPTX
Dijkstra s algorithm
PPTX
Vertex cover Problem
PDF
DBMS 6 | MySQL Practice List - Rank Related Queries
PPTX
PPTX
Dfs presentation
PDF
Support Vector Machine
PPTX
Daa unit 3
Pertemuan 9 Array (Larik)
Dijkstra s algorithm
Vertex cover Problem
DBMS 6 | MySQL Practice List - Rank Related Queries
Dfs presentation
Support Vector Machine
Daa unit 3

What's hot (20)

PPTX
PPT Functional dan OOP Programming.pptx
PPT
Selection sort
PPTX
Kruskal & Prim's Algorithm
PPTX
sum of subset problem using Backtracking
PPTX
Optimization of dfa
PDF
Pertemuan 4 alert dan toast
DOCX
Laporan Fungsi Select Pada DML (IBNU SINA BATAM)
PPTX
Longest Common Subsequence
PPTX
PPTX
Bellman ford Algorithm
PDF
Penerapan text mining menggunakan python
PPTX
T2 - Desain Basis Data
PDF
Solutions manual for guide to sql 9th edition by pratt
PPTX
Digital Search Tree
PPTX
State space search and Problem Solving techniques
PPTX
Rabin Carp String Matching algorithm
PPTX
Regular expressions
PDF
Optimal binary search tree
PPT
Depth First Search, Breadth First Search and Best First Search
PDF
PPT Functional dan OOP Programming.pptx
Selection sort
Kruskal & Prim's Algorithm
sum of subset problem using Backtracking
Optimization of dfa
Pertemuan 4 alert dan toast
Laporan Fungsi Select Pada DML (IBNU SINA BATAM)
Longest Common Subsequence
Bellman ford Algorithm
Penerapan text mining menggunakan python
T2 - Desain Basis Data
Solutions manual for guide to sql 9th edition by pratt
Digital Search Tree
State space search and Problem Solving techniques
Rabin Carp String Matching algorithm
Regular expressions
Optimal binary search tree
Depth First Search, Breadth First Search and Best First Search
Ad

Similar to Longest Common Subsequence & Matrix Chain Multiplication (20)

PPTX
Matrix chain multiplication in design analysis of algorithm
PPTX
Dynamic Programming Matrix Chain Multiplication
PPT
Dynamic_methods_Greedy_algorithms_11.ppt
PPT
Dynamic1
PPT
Learn about dynamic programming and how to design algorith
PDF
Matrix chain multiplication
PPTX
Dynamic Programming - Matrix Chain Multiplication
PPT
Chapter 16
PPT
PPTX
Chapter 5.pptx
PPTX
AAC ch 3 Advance strategies (Dynamic Programming).pptx
PDF
Dynamic programming
PDF
Computer algorithm(Dynamic Programming).pdf
PPTX
dynamic programming complete by Mumtaz Ali (03154103173)
PDF
Unit 2_final DESIGN AND ANALYSIS OF ALGORITHMS.pdf
PDF
Daa chapter 3
PDF
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
PPTX
unit3_Dynamic_Progrghaiajawzjabagamming1.pptx
PPT
Lecture11
PPT
Matrix chain multiplication in design analysis of algorithm
Dynamic Programming Matrix Chain Multiplication
Dynamic_methods_Greedy_algorithms_11.ppt
Dynamic1
Learn about dynamic programming and how to design algorith
Matrix chain multiplication
Dynamic Programming - Matrix Chain Multiplication
Chapter 16
Chapter 5.pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptx
Dynamic programming
Computer algorithm(Dynamic Programming).pdf
dynamic programming complete by Mumtaz Ali (03154103173)
Unit 2_final DESIGN AND ANALYSIS OF ALGORITHMS.pdf
Daa chapter 3
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
unit3_Dynamic_Progrghaiajawzjabagamming1.pptx
Lecture11
Ad

Recently uploaded (20)

PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT
Project quality management in manufacturing
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Current and future trends in Computer Vision.pptx
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Geodesy 1.pptx...............................................
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
DOCX
573137875-Attendance-Management-System-original
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
composite construction of structures.pdf
PPTX
Artificial Intelligence
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Project quality management in manufacturing
UNIT 4 Total Quality Management .pptx
Current and future trends in Computer Vision.pptx
Safety Seminar civil to be ensured for safe working.
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
R24 SURVEYING LAB MANUAL for civil enggi
Geodesy 1.pptx...............................................
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
573137875-Attendance-Management-System-original
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Lecture Notes Electrical Wiring System Components
OOP with Java - Java Introduction (Basics)
Internet of Things (IOT) - A guide to understanding
composite construction of structures.pdf
Artificial Intelligence
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Model Code of Practice - Construction Work - 21102022 .pdf

Longest Common Subsequence & Matrix Chain Multiplication

  • 1. Longest common subsequence & Matrices chain multiplication Course Title : Algorithm Design And Analysis
  • 2. 2 LONGESTCOMMONSUBSEQUENCE The longest common subsequence (LCS) problem is the problem of finding the longest subsequence common to all sequences in a set of sequences (often just two sequences).
  • 3. abc a b c ab bc ac cab c a b ca ab cb LCS=ab 3
  • 4. Condition Table Value Sign If i=j=0 0 No sign If (i,j>0) && Xi=Yj C[i-1,j-1] Else if (i,j>0 && Xi=Yj) If [C(i-1,j)>=C(i,j-1)] Else [C(i-1,j)<=C(i,j-1)] C[i-1,j] C[I,j-1] LONGESTCOMMONSUBSEQUENCE
  • 5. Example: Find the length of the longest subsequence present in both of them……. Seq1: ABCDEFG , Seq2: CDBAEF FEDC Seq1 A B C D E F G Seq2 0 0 0 0 0 0 0 0 C 0 0 0 1 1 1 1 1 D 0 0 0 1 2 2 2 2 B 0 0 1 1 2 2 2 2 A 0 1 1 1 2 2 2 2 E 0 1 1 1 2 3 3 3 F 0 1 1 1 2 3 4 4
  • 6. 1. m:=length(X) 2. n:=length(Y) 3. Let B=[1……m,1…..n]& C=[0……m,0…….n] 4. For i=1 to n 5. C[i,0]:=0 6. For j=1 to n 7. C[0,j]:=0 8. For i=1 to n 9. for j=1 to n 10. If Xi=Yj 11. C[I,j]:=C[i-1,j-1] 12. B[i,j]=“ “ 13. Else 14. if C[i-1,j]>=C[i,j-1] Algorithm
  • 7. 15. C[i,j]:=C[i-1,j] 16. B[i,j]:=“ “ 17. Else C[i,j]:=C[i,j-1] 18.B[i,j]:=“ “ 19.Return Algorithm
  • 8. Definition: if we are given a sequence/chain of n Matrices (A1,A2………Ai) where i=1,2…..n & Ai has dimension Pi-1*Pi to be multiplied and we wish to compute the product or parenthesis In a way that the total number of scalar multiplication is minimized. This process is known as Matrix Chain Multiplication. Goal of MCM: parenthesizing a chain of matrices so that it can have a dramatic impact on the cast evaluating multiplication of matrices or the product. Where it is being used:  Compiler design for cost optimization.  Database or query optimization. Rules to remember : -Multiplication is associative not commulative. Matrix chain multiplication
  • 9. A1 A2 (5*4) (4*6) Dimension: A1 A2 = 5*6 =30 Cost : A1 A2 = 5*4*6 = 120 How does cost can be calculated?
  • 10. If the matrices are A1 A2 A3 (5*4) (4*6) (6*2)  (A1 A2 )A3  A1( A2 A3)  (A1 A2 )A3 A1 A2 = 5*4*6 C1 = 120 (A1 A2 )A3 = 5*6*2 C2 = 60 Total Cost = C1 + C2 =120 + 60 = 180
  • 11. Law : How many parenthesizing could be possible? 2ncn P(n-1) = n+1 Note : if the matrices are A1 ,A2,A3 ,A4 then we take n=3 2*3c3 P(3-1) = 3+1 = 5
  • 12. A1 A2 A3 A4 (5*4) (4*6) (6*2) (2*7) P0 P1 P1 P2 P2 P3 P3 P4 2*3c3 P(3-1) = 3+1 = 5 How many parenthesizing could be possible?
  • 13. m(i,i)= 0 m(j,j)= 0 m(i,j)= min(i<=k<j) {m(i,k) + m(k+1,j) + pi-1 *pk * pj} m(1,2)= min(1<=k<2) {m(1,1) + m(1+1,2) + p0 *p1 * p2} = min(1<=k<2) {0 + 0+ 5 *4 *6} = 120 (for k=1)
  • 14. m(2,3)= min(2<=k<3) {m(2,2) + m(3,3) + p1 *p2 * p3} = min(1<=k<3) {0 + 0+ 4 *6 *2} = 48 (for k= 2) m(3,4)= min(3<=k<4) {m(3,3) + m(4,4) + p2 *p3 * p4} = min(1<=k<4) {0 + 0+ 6 *2 *7} = 84 (for k= 3)
  • 15. m(1,3)= min(1<=k<3) {m(1,1) + m(2,3) + p0 *p1 * p3} + {m(1,2) + m(3,3) + p0 *p2 * p3} = min(1<=k<3) {0 + 48+ 5 *4 *2} + {120 + 0 + 5 *6 *2} = min(1<=k<3) {88,180} = 88 (for k= 1) m(2,4)= min(2<=k<4) {m(2,2) + m(3,4) + p1 *p2 * p4} + {m(2,3) + m(4,4) + p1 *p3 * p4} = min(1<=k<4) {0 + 84+ 4 *6 *7} + {48 + 0 + 4 *2 *7} = min(1<=k<4) {272,104} = 104 (for k= 3)
  • 16. m(1,4)= min(1<=k<4) {m(1,1) + m(2,4) + p0 *p1 * p4} + {m(1,2) + m(3,4) + p0 *p2 * p4} + {m(1,3) + m(4,4) + p0 *p3 * p4} = min(1<=k<4) {0 + 104+ 5 *4 *7} + {120 + 84 + 5 *6 *7} + {88 + 0 + 5 *2 *7} = min(1<=k<4) {244,414,158} = 158 (for k= 3) Short : m(1,2) = 120 m(1,3) = 88 m(1,4) = 158 m(2,3) = 48 m(2,4) = 104 m(3,4) = 84
  • 17. M[1………….m,1………..n] s[1……..n-1,2…………n] 0 120 88 158 0 48 104 0 84 0 1 1 3 2 3 3 For cost value For the value of k J=1 2 3 4 J = 2 3 4 i=1 2 3 4 i=1 2 3
  • 18. For cost value For the value of k
  • 19. 158, k=3 m(i,j) m[1,4] m(i,k) , m(k+1,j) m(1,3) , m(4,4) 88, k=1 m(i,j) m[1,3] m(i,k) , m(k+1,j) m(1,1) , m(2,3) A4 A1 48, k=2 m(i,j) m[2,3] m(i,k) , m(k+1,j) m(2,2) , m(3,3) A2 A3 A1 A2 A3 A4
  • 21. A1 A2 A3 A4 ((A1(A2A3))A4) A1 A2 A3 A4 A1 A2 A3 A2 A3 Tree Diagram