Professional Documents
Culture Documents
int i = m, j = n;
while (i > 0 && j > 0)
{
if (S1[i - 1] == S2[j - 1])
{
lcsAlgo[ind - 1] = S1[i - 1];
i--;
j--;
ind--;
}
int main()
{
string S1,S2;
cout<<"\n!!! Computing a Longest Common Subsequence for Two Sequences
!!!\n";
cout<<"\t Enter First Sequence : ";
cin>>S1;
cout<<"\t Enter Second Sequence : ";
cin>>S2;
int m = S1.length();
int n = S2.length();
lcsAlgo(S1, S2, m, n);
}
Output :
Time Complexity:
1. Best Case: O(n*m)
Since we are using two for loops for both the strings ,therefore the time complexity of
finding the longest common subsequence using dynamic programming approach
is O(n * m) where n and m are the lengths of the strings.
Time Complexity:
1. Best Case: O(n+k)
Time Complexity:
There are three loops. Each loop has constant complexities. So, the time complexity of
the Floyd-Warshall algorithm is O(n3).
SO,
1. Best Case: O(n3)
Time Complexity:
1. Best Case: Based On the Graph the time complexity for best case is calculated.
2. Worst Case: Time Complexity of Dijkstra's Algorithm is O(V2) but with min-
priority queue it drops down to O(V+ElogV).