0% found this document useful (0 votes)
9 views8 pages

Daa Lab-Da2

The document discusses solutions to various algorithm problems including longest common subsequence, matrix chain multiplication, and subset sum using backtracking. Code snippets are provided for each problem to find if one string is embedded in another, calculate the minimum number of multiplications for matrix chain multiplication, and use backtracking to find a solution to the subset sum problem.

Uploaded by

Apoorva Saluja
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)
9 views8 pages

Daa Lab-Da2

The document discusses solutions to various algorithm problems including longest common subsequence, matrix chain multiplication, and subset sum using backtracking. Code snippets are provided for each problem to find if one string is embedded in another, calculate the minimum number of multiplications for matrix chain multiplication, and use backtracking to find a solution to the subset sum problem.

Uploaded by

Apoorva Saluja
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/ 8

APOORVA SALUJA

22BCE3169
DATA ALGORITHMS AND ANALYSIS
PROF: SUNIL KUMAR PV
LAB SLOT: L55/56
ASSIGNMENT-2
QUESTION: [LCS]
Develop a program to check whether one string (string1) is embedded in another
(string2)

Input Format

Read string1

Read string2

Output Format

Print YES if string1 is embedded in string2, print NO otherwise

Sample Input-1

CAS

CLASS

Sample Output-1

YES

Sample Input-2

ALS

CLASS

Sample Output-2

NO

CODE:

#include <iostream>

#include <string>

using namespace std;


bool isSubsequence(string s1, string s2) {

int index = -1;

for (char c : s1) {

index = s2.find(c, index + 1);

if (index == string::npos) {

return false;

return true;

int main() {

string s1 ;

string s2 ;

cin>>s1;

cin>>s2;

bool check = isSubsequence(s1, s2) ;

if(check){

cout<<"YES";

else{

cout<<"NO";

return 0;

}
QUESTION: [Matrix Chain multiplication]
Determine the minimum number of multiplications needed to multiply a chain of matrices.
Receive the dimensions as space-separated integers and output the minimum number of
multiplications needed.

Example:- If the problem has 4 matrices with dimensions 2x3, 3x6, 6x4 and 4x5, the input
will be the number and the dimensions array given as space-separated dimensions:

23645

and the output must be 124

CODE:
#include <bits/stdc++.h>

using namespace std;

int MatrixChainOrder(int p[], int i, int j)

if (i == j)

return 0;

int k;

int mini = INT_MAX;

int count;

for (k = i; k < j; k++)

count = MatrixChainOrder(p, i, k)

+ MatrixChainOrder(p, k + 1, j)

+ p[i - 1] * p[k] * p[j];

mini = min(count, mini);

return mini;

}
int main()

int N=0;

cin>>N;

int arr[N];

for(int i=0;i<N;i++)

cin>>arr[i];

cout << MatrixChainOrder(arr, 1, N - 1);

return 0;

}
QUESTION: [SUBSET SUM]
Consider the subset problem, and find a solution to the problem using backtracking.

Input Format

The first line should read the number of total elements in the set (n)

Next n lines should read the elements of the set

The following line should read the expected sum

Output format

A fixed-length solution output 1 if the element is chosen, and 0 if not.

Sample Input

13

Sample output

1101

CODE:

#include <iostream>

#include <vector>

using namespace std;


void backtrack(vector<int>& set, vector<int>& solution, int targetSum, int currentSum, int index)
{

if (currentSum == targetSum) {

for (int i = 0; i < solution.size(); i++) {

cout << solution[i] << " ";

cout << endl;

return;

if (currentSum > targetSum || index >= set.size()) {

return;

solution[index] = 1;

backtrack(set, solution, targetSum, currentSum + set[index], index + 1);

solution[index] = 0;

backtrack(set, solution, targetSum, currentSum, index + 1);

void subsetSum(vector<int>& set, int targetSum) {

vector<int> solution(set.size(), 0);

int currentSum = 0;

int index = 0;

backtrack(set, solution, targetSum, currentSum, index);

int main() {
int n;

cin >> n;

vector<int> set(n);

for (int i = 0; i < n; i++) {

cin >> set[i];

int targetSum;

cin >> targetSum;

subsetSum(set, targetSum);

return 0;

You might also like