0% found this document useful (0 votes)
19 views5 pages

Practical - 8: Aim: Implementation of Making A Change Problem Using Dynamic Programming

The document discusses an implementation of the longest common subsequence problem using dynamic programming. It includes Python code to find the LCS of two input strings and outputs the LCS string.

Uploaded by

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

Practical - 8: Aim: Implementation of Making A Change Problem Using Dynamic Programming

The document discusses an implementation of the longest common subsequence problem using dynamic programming. It includes Python code to find the LCS of two input strings and outputs the LCS string.

Uploaded by

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

IU2241231336 DAA[CE0516] CSE 5 E-2

Practical - 8
Aim: Implementation of making a change problem using dynamic programming.

Code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m, n, i, j;
printf("Enter the amount of which you want change\n");
scanf("%d", &m);
printf("\nEnter the number of determinants\n");
scanf("%d", &n);
int c[n + 1][m + 1], dc[n + 1];
printf("\nEnter the determinants\n");
dc[0] = 0;
for (i = 1; i < n + 1; i++)
scanf("%d", &dc[i]);
for (i = 0; i < n + 1; i++)
{
for (j = 0; j <= m; j++)
c[i][j] = 0;
}
for (i = 0; i < n + 1; i++)
c[i][0] = 0;
for (i = 0; i < m + 1; i++)
{
c[0][i] = i;

24
IU2241231336 DAA[CE0516] CSE 5 E-2

}
for (i = 1; i < n + 1; i++)
{
for (j = 1; j < m + 1; j++)
{
if (dc[1] > c[0][j])
c[i][j] = 0;
else if (dc[i] <= j)
{
if ((c[i][j - dc[i]] + 1) < c[i - 1][j])
c[i][j] = (c[i][j - dc[i]] + 1);
else
c[i][j] = c[i - 1][j];
}
else
c[i][j] = c[i - 1][j];
}
}
printf("\n");
for (i = 0; i < n + 1; i++)
{
printf("%d ", dc[i]);
for (j = 0; j < m + 1; j++)
{
printf(" %d", c[i][j]);
}
printf("\n");
}

25
IU2241231336 DAA[CE0516] CSE 5 E-2

printf("\nThe change you get is \n");


i = n;
j = m;
while (i != 1)
{
if (c[i][j] == c[i - 1][j])
i = i - 1;
else
{
j = j - dc[i];
printf(" %d ", dc[i]);
}
}
printf("\n\n");
return 0;
}
OUTPUT :

Signature of Faculty:____________ Date:_________

26
IU2241231336 DAA[CE0516] CSE 5 E-2

Practical - 10
Aim: Implement LCS Problem.

Code :
def lcs_algo(S1, S2, m, n):
L = [[0 for x in range(n+1)] for x in range(m+1)]
for i in range(m+1):
for j in range(n+1):
if i == 0 or j == 0:
L[i][j] = 0
elif S1[i-1] == S2[j-1]:
L[i][j] = L[i-1][j-1] + 1
else:
L[i][j] = max(L[i-1][j], L[i][j-1])

index = L[m][n]

lcs_algo = [""] * (index+1)


lcs_algo[index] = ""
i=m
j=n
while i > 0 and j > 0:
if S1[i-1] == S2[j-1]:
lcs_algo[index-1] = S1[i-1]
i -= 1
j -= 1
index -= 1

27
IU2241231336 DAA[CE0516] CSE 5 E-2

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


i -= 1
else:
j -= 1

print("S1 : " + S1 + "\nS2 : " + S2)


print("LCS: " + "".join(lcs_algo))

S1=input(("Enter Dict"))
S2=input(("Enter Dict"))
m = len(S1)
n = len(S2)
lcs_algo(S1, S2, m, n)

OUTPUT:

Signature of Faculty:____________ Date:_________

28

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy