Lcs
Lcs
ANALYSIS OF
ALGORITHMS
Prepared By:
Jahanzaib Rauf
Waleed Ahmed Khan
Arslan Tariq
Iqra Afzal
Difference between Greedy method and
Dynamic Programming
Greedy method:
• We try to find predefined procedures to get the optimal result
• We follow that procedure to get the best result
• E.g Dijkstra Algorithms
• Decision is taken one time
Dynamic programming:
• We try to find all possible solutions then pick the best solution
• It follows principle of optimality
it states that a problem can be solved by sequence of statements
• E.g Longest Common Sequence (LCS)
LONGEST COMMON SUBSEQUENCE
A subsequence is a sequence that appears in the same relative
order, but not necessarily contiguous.
In LSC , we have to find Longest common Subsequence that is in
same relative order.
String of length n has 2^n different possible subsequences.
E.g.--
Subsequences of “ABCDEFG”.
“ABC”, “ABG”, “BDF”, “AEG”, ‘”ACEFG”, …
EXAMPLE
Now we will see table from which we can find LCS of Above
sequences.
CONDITIONS FOR ARROWS
Algorithm
LGS(X,Y,m,n)
for(i=0;i<=m;i++)
for(j=0;j<=n;j++)
if(i==0||j==0)
L[i][j]=0;
else if (X[i-1] == Y[j-1])
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = max(L[i-1][j], L[i][j-1]);
return L[m][n];
CONSTRUCTING AN LCS
COMPLEXITY
Complexity of Longest Common Subsequence is O(mn).
Where m and n are lengths of the two Strings.