0% found this document useful (0 votes)
61 views

Algorithm Report PDF

The document discusses the difference between greedy and dynamic programming approaches. It focuses on the longest common subsequence (LCS) problem as an example of dynamic programming. LCS finds the longest subsequence common to two strings. The document provides examples, describes using a table to solve LCS, provides the LCS algorithm, and explains how to construct the LCS and that the time complexity is O(mn) where m and n are the string lengths.

Uploaded by

Waleed Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Algorithm Report PDF

The document discusses the difference between greedy and dynamic programming approaches. It focuses on the longest common subsequence (LCS) problem as an example of dynamic programming. LCS finds the longest subsequence common to two strings. The document provides examples, describes using a table to solve LCS, provides the LCS algorithm, and explains how to construct the LCS and that the time complexity is O(mn) where m and n are the string lengths.

Uploaded by

Waleed Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PRESENTATION REPORT

ANALYSIS OF ALGORITHM

PRESENTATION TOPIC :
Dynamic Programming And How It Differ From Greedy Method
(Longest common subsequence)

Submitted By:
Waleed Ahmed Khan 11476
Jahanzaib Rauf 11657
Arslan Tariq 11656
Iqra Afzal 11663
Submitted To:
Sir Waris

1
Table of Contents
Difference between Greedy method and Dynamic Programming ............................ 3
Dynamic programming: ....................................................................................... 3
About Longest Common Subsequence: ................................................................... 3
Example 1:........................................................................................................... 3
Example 2:........................................................................................................... 3
Example 3:........................................................................................................... 4
Use of table and arrows: ...................................................................................... 5
Algorithm: ........................................................................................................... 5
Constructing an LCS: .......................................................................................... 6
Complexity : ........................................................................................................ 6

2
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)

About 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 1:
LCS for input Sequences “ABCDGH” and “AEDFHR” is “ADH” of length 3.
LCS for input Sequences “AGGTAB” and “GXTXAYB” is “GTAB” of length 4.

Example 2:
Let the input sequences be X[0..m-1] and Y[0..n-1] of lengths m and n respectively.
let L(X[0..m-1], Y[0..n-1]) be the length of LCS of the two sequences X and Y. If
last characters of both sequences match (or X[m-1] == Y[n-1]) then àL(X[0..m-1],
Y[0..n-1]) = 1 + L(X[0..m-2], Y[0..n-2])

3
If last characters of both sequences do not match (or X[m-1] != Y[n-1] then àL(X[0..m-1],
Y[0..n-1]) = MAX ( L(X[0..m-2], Y[0..n-1]), L(X[0..m-
1], Y[0..n-2])

Example 3:
Consider the input strings “ABCDGH” and “AEDFHR”.
Last characters do not match for the strings. So length of LCS can
be written as:

L(“ABCDGH”, “AEDFHR”) = MAX ( L(“ABCDG”, “AEDFHR”), L(“ABCDGH”,


“AEDFH”) )
X=M,Z,J,A,W,X,U
Y=X,M,J,Y,A,U,Z
And Longest Common Subsequence is, LCS(X,Y)=M,J,A,U
Now we will see table from which we can find LCS of Above
sequences.

4
Use of table and arrows:

Algorithm:
LCS(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] == Y[j])
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];

5
Constructing an LCS:

Complexity :
Complexity of Longest Common Subsequence is O(mn). Where m and n are lengths of the two
Strings.

You might also like