Practical 4
Practical 4
Practical 4
Practical-4
1) Aim: 0 1 knapsack
Program:
import java.io.*;
import java.util.*;
ID No.:22IT109 Page 1
IT265– Design & Analysis of Algorithm 4IT (2023-24)
Output:
return dp[0][n-1];
}
ID No.:22IT109 Page 2
IT265– Design & Analysis of Algorithm 4IT (2023-24)
}
Output:
Matrix chain multiplication is a problem where we can find minimum number of multiplication
for doing n number of matrices.
Program:
#include <cmath>
#include <cstdio>
#include <vector>
ID No.:22IT109 Page 3
IT265– Design & Analysis of Algorithm 4IT (2023-24)
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int m, n;
cin >> m >> n;
vector<int> lcs;
int i = m, j = n;
while (i > 0 && j > 0) {
if (A[i - 1] == B[j - 1]) {
lcs.push_back(A[i - 1]);
i--;
j--;
} else if (dp[i - 1][j] > dp[i][j - 1]) {
i--;
} else {
j--;
}
}
reverse(lcs.begin(), lcs.end());
ID No.:22IT109 Page 4
IT265– Design & Analysis of Algorithm 4IT (2023-24)
return 0;
}
Output:
/*
* Complete the 'getWays' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts following parameters:
* 1. INTEGER n
* 2. LONG_INTEGER_ARRAY c
*/
ID No.:22IT109 Page 5
IT265– Design & Analysis of Algorithm 4IT (2023-24)
if (i == 0) {
return n % c[0] == 0;
}
if (dp[i][n] != 0) {
return dp[i][n];
}
long long take = 0;
long long nottake = fun(n, i - 1, c, dp);
if (c[i] <= n) {
take = fun(n - c[i], i, c, dp);
}
return dp[i][n] = take + nottake;
}
long getWays(int n, vector<long> c) {
int l = c.size();
vector<vector<long long>> dp(l, vector<long long>(n + 1, 0));
return fun(n, l - 1, c, dp);
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string first_multiple_input_temp;
getline(cin, first_multiple_input_temp);
int n = stoi(first_multiple_input[0]);
int m = stoi(first_multiple_input[1]);
string c_temp_temp;
getline(cin, c_temp_temp);
vector<long> c(m);
ID No.:22IT109 Page 6
IT265– Design & Analysis of Algorithm 4IT (2023-24)
c[i] = c_item;
}
// Print the number of ways of making change for 'n' units usin
g coins having the values given by 'c'
fout.close();
return 0;
}
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)
))
);
return s;
}
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspac
e))).base(),
s.end()
);
return s;
}
string::size_type start = 0;
ID No.:22IT109 Page 7
IT265– Design & Analysis of Algorithm 4IT (2023-24)
string::size_type end = 0;
start = end + 1;
}
tokens.push_back(str.substr(start));
return tokens;
}
Output:
Conclusion: The problem involves determining the number of ways to make change for a
given amount using available coin denominations. Dynamic Programming can efficiently solve it
by considering overlapping subproblems. By storing and referencing previously computed
solutions, it optimizes the process. The goal is to find the number of unique combinations for
making change.
ID No.:22IT109 Page 8
IT265– Design & Analysis of Algorithm 4IT (2023-24)
import java.util.*;
public class CoinOnTheTable {
return -1;
}
costs[i][j] = cost;
if (board[i][j] == '*') {
return;
}
if (time == k) {
return;
}
ID No.:22IT109 Page 9
IT265– Design & Analysis of Algorithm 4IT (2023-24)
int n = scanner.nextInt();
int m = scanner.nextInt();
k = scanner.nextInt();
scanner.nextLine();
board = new char[n][];
System.out.println(solve());
scanner.close();
}
}
Output:
ID No.:22IT109 Page 10
IT265– Design & Analysis of Algorithm 4IT (2023-24)
Conclusion: The problem requires optimizing the path of a coin on a rectangular board,
reaching a specific cell before a given time limit. By strategically altering cell directions, the
minimum number of operations can be found. It emphasizes efficient pathfinding and dynamic
decision-making, crucial for puzzle-solving and algorithmic challenges.
ID No.:22IT109 Page 11