0% found this document useful (0 votes)
7 views7 pages

11339AoA - EX-7

The document outlines an experiment focused on implementing the Longest Common Subsequence (LCS) using a dynamic programming approach. It includes the aim, prerequisites, outcomes, theory, algorithm, and a sample implementation in Python. The experiment emphasizes the efficiency of dynamic programming in solving LCS problems compared to brute-force methods, and discusses its applications in various fields such as bioinformatics and text comparison.

Uploaded by

wogef67818
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

11339AoA - EX-7

The document outlines an experiment focused on implementing the Longest Common Subsequence (LCS) using a dynamic programming approach. It includes the aim, prerequisites, outcomes, theory, algorithm, and a sample implementation in Python. The experiment emphasizes the efficiency of dynamic programming in solving LCS problems compared to brute-force methods, and discusses its applications in various fields such as bioinformatics and text comparison.

Uploaded by

wogef67818
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PART A

(PART A: TO BE REFFERED BY STUDENTS)

Experiment No.07
A.1 Aim: Write a program to implement longest common subsequence using Dynamic
Programming Approach.

A.2 Prerequisite:

A.3 Outcome:

After successful completion of this experiment students will be able to solve a problem
by applying dynamic programming approach and analyze the complexity of the problem.

A.4 Theory:
Given two strings: string S of length n, and string T of length m. Goal is to produce their
longestcommon subsequence: the longest sequence of characters that appear left-to-right,
but notnecessarily in a contiguous block, in both strings.For example, consider: S =
ABAZDC and T = BACBAD.The LCS has length 4 and is the string ABAD. This type of
problem comes up in genomics: given two DNA fragments, the LCS gives information
about what they have in common and the best way to line them up.

Let A = <a1, a2, a3,…an> and B = <b1, b2, b3,…bn> be two strings over alphabets. Then B
is a subsequence of A if B can be generated by striking out some elements from A.

If A = {a, b, a, c, b, c, b} and B = {a, b, c, b, b, a, c}, then the sequence {a, c}, {a, b, c},
{a, c, c}, {a, b, c, b} etc are common subsequences of A and B, but {a, b, c, b, a} is not.
{a, b, c, b, a} is a subsequence of B but it is not a subsequence of A.

Let two strings Am and Bn of length m and n respectively. If thelast character of both the
strings is same i.e. am = bn, then the length of LCS is incremented by one and length of
both strings is reduced by one. The problem is to to find LCS of strings Am-1 and Bn-1.

If am ≠ bn, following two problems are considered.

- Apply LCS on strings Am-1 and B


- Apply LCS on strings A and Bn-1

Select the result which gives longest subsequence.

Thus optimal substructure of LCS problem is defined as,


Algorithm:

Example:
Let A be “XMJYAUZ” and B be “MZJAWXU”. The longest common subsequence
between A and B is “MJAU”. The tableshown below, which is generated by the
function LCSLength, shows the lengths of the longest common subsequences between
A and B. The ith row and jth column shows the length of the LCS between A and
B.The highlighted numbers show the path that would follow from the bottom right to the
top left corner, when reading out an LCS. If the current symbols in A and B are equal,
they are the part of LCS.

Time Complexity:
In dynamic programming, the only table of size m × n is filled by using two nested for
loops. So running time of this algorithm in O(mn).
PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll No.: C26 Name: Hrishikesh Sanap

Class: C Batch: C2

Date of Experiment: 15-03-2025 Date of Submission15-03-2025

Grade:

B.1 Software Code written by student:

def lcs(X, Y):

m = len(X)

n = len(Y)

L = [[0] * (n + 1) for i in range(m + 1)]

# Building the LCS table in bottom-up manner

for i in range(1, m + 1):

for j in range(1, n + 1):

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

# Constructing the LCS string

index = L[m][n]

lcs_string = [''] * index


i, j = m, n

while i > 0 and j > 0:

if X[i - 1] == Y[j - 1]:

lcs_string[index - 1] = X[i - 1]

i -= 1

j -= 1

index -= 1

elif L[i - 1][j] > L[i][j - 1]:

i -= 1

else:

j -= 1

return "".join(lcs_string)

# Example usage

X = "XMJYAUZ"

Y = "MZJAWXU"

print("Longest Common Subsequence:", lcs(X, Y))

B.2 Input and Output:


B.3 Observations and learning:

The implementation of the LCS problem using dynamic


programming efficiently finds the longest common subsequence
by storing intermediate results in a table. The backtracking step
successfully reconstructs the LCS by following the optimal path.
The approach ensures that repeated subproblems are not
recomputed, leading to improved performance. The table-based
computation ensures a time complexity of O(mn), making it
suitable for moderate-length strings.

B.4 Conclusion:

The experiment demonstrates that the dynamic programming


approach is an effective way to solve the LCS problem. By
breaking down the problem into smaller subproblems and storing
their results, the algorithm optimally finds the LCS. This method is
widely used in computational biology and text comparison
applications. The approach highlights the power of dynamic
programming in solving problems with overlapping subproblems
and optimal substructure.

B.5 Question of Curiosity


Q1: What are the characteristics of dynamic programming?

Dynamic Programming (DP) has the following key characteristics:

1. Optimal Substructure – The problem can be broken down into smaller


subproblems, and the solution to the main problem can be constructed from
the solutions to these subproblems.
2. Overlapping Subproblems – The same subproblems are solved multiple
times, making it efficient to store their results to avoid redundant
computations.
3. Bottom-Up Approach – DP usually builds a solution from the smallest
subproblems up to the larger ones using tables or memoization.
4. Memory Utilization – DP techniques often use a 2D table or an array to
store intermediate results, reducing redundant calculations.
5. Efficiency Improvement – DP significantly improves time complexity
compared to recursive or brute-force approaches.

Q2: Why is Brute Force approach not efficient to solve LCS?

The brute-force approach generates all possible subsequences of one string


and checks for their presence in the other string. This results in an
exponential time complexity O(2^n), making it highly inefficient for long
strings. Since it does not store intermediate results, it repeatedly solves the
same subproblems, leading to redundant computations. In contrast, dynamic
programming reduces the complexity to O(mn) by storing subproblem
solutions in a table, making it much faster.
Q3: Compare Brute Force approach and Dynamics Programming
approach.

Q4: Explain application areas of LCS.

The Longest Common Subsequence (LCS) problem has several real-world applications,
including:

1. Bioinformatics (DNA Sequence Analysis) – LCS is used to compare DNA sequences to


find similarities and evolutionary relationships.
2. Text and Document Comparison – LCS is used in plagiarism detection, version control
systems (like Git), and file comparison tools.
3. Data Compression – LCS helps in finding repeated patterns, which is useful in
compression algorithms.
4. Speech and Pattern Recognition – LCS is used in handwriting recognition, speech
analysis, and other AI applications.
5. Natural Language Processing (NLP) – LCS is used in text summarization and sentence
similarity analysis.

************************

You might also like