Daa Assignment
Daa Assignment
CODE:COR08P/08: Write a C++ program to find the minimum number of scalar multiplication needed
for the chain of matrix?
#include <iostream>
#include <climits>
using namespace std;
int matrixChainMultiplication(int dims[], int n) {
int dp[n][n];
for (int i = 1; i < n; i++) {
dp[i][i] = 0;
}
for (int length = 2; length < n; length++) {
for (int i = 1; i < n - length + 1; i++) {
int j = i + length - 1;
dp[i][j] = INT_MAX;
for (int k = i; k <= j - 1; k++) {
int cost = dp[i][k] + dp[k + 1][j] + dims[i - 1] * dims[k] * dims[j];
if (cost < dp[i][j]) {
dp[i][j] = cost;
}
}
}
}
return dp[1][n - 1];
}
int main() {
int n;
cout << "Enter the number of matrices: ";
cin >> n;
int dims[n + 1];
cout << "Enter the dimensions of the matrices: ";
for (int i = 0; i <= n; i++) {
cin >> dims[i];
}
cout << "Minimum number of scalar multiplications is " << matrixChainMultiplication(dims, n + 1);
return 0;
}
OUTPUT:
CODE:COR08P/09: Write a C++ program to find the LCS?
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string X, Y;
cout << "Enter the first string: ";
cin >> X;
cout << "Enter the second string: ";
cin >> Y;
string lcs = findLCS(X, Y);
cout << "The Longest Common Subsequence is: " << lcs << endl;
return 0;
}
OUTPUT:
CODE:COR08P/10: Write a C++ program to find the profit on 0/1 Knapsack ?
#include <iostream>
#include <cstring>
using namespace std;
int knapsack(int W, int wt[], int val[], int n) {
int dp[n + 1][W + 1];
// Initialize the dp table
memset(dp, 0, sizeof(dp));
// Build the dp table
for (int i = 1; i <= n; i++) {
for (int w = 0; w <= W; w++) {
if (wt[i - 1] <= w) {
dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - wt[i - 1]] + val[i - 1]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}
int main() {
int n, W;
cout << "Enter the number of items: ";
cin >> n;
int val[n], wt[n];
cout << "Enter the values of the items: ";
for (int i = 0; i < n; i++) {
cin >> val[i];
}
cout << "Enter the weights of the items: ";
for (int i = 0; i < n; i++) {
cin >> wt[i];
}
cout << "Enter the capacity of the knapsack: ";
cin >> W;
cout << "The maximum profit is: " << knapsack(W, wt, val, n) << endl;
return 0;
}
OUTPUT:
CODE:COR08P/11: Write a C++ program to implement Factorial Knapsack problem?
#include <iostream>
#include <cstring>
using namespace std;
// Function to find the maximum profit for the 0/1 Knapsack problem
int knapsack(int W, int wt[], int val[], int n) {
int dp[n + 1][W + 1];
// Initialize the dp table
memset(dp, 0, sizeof(dp));
// Build the dp table
for (int i = 1; i <= n; i++) {
for (int w = 0; w <= W; w++) {
if (wt[i - 1] <= w) {
dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - wt[i - 1]] + val[i - 1]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}
int main() {
int n, W;
cout << "Enter the number of items: ";
cin >> n;
int wt[n], val[n];
cout << "Enter the weights of the items: ";
for (int i = 0; i < n; i++) {
cin >> wt[i];
val[i] = factorial(i + 1); // Using factorial of the item's index as its value
}
cout << "Enter the capacity of the knapsack: ";
cin >> W;
cout << "The maximum profit is: " << knapsack(W, wt, val, n) << endl;
return 0;
}
OUTPUT:
CODE:COR08P/12: Write a C++ program to implement Naïve Algorithm?
#include <iostream>
#include <string>
if (j == m) {
cout << "Pattern found at index " << i << endl;
}
}
}
int main() {
string text, pattern;
cout << "Enter the text: ";
getline(cin, text);
cout << "Enter the pattern: ";
getline(cin, pattern);
naiveSearch(text, pattern);
return 0;
}
OUTPUT:
CODE:COR08P/13: Write a C++ program to implement string matching algorithm using KMP (Knuth-
Morris-Pratt) Algorithm?
#include <iostream>
#include <string>
using namespace std;
int main() {
string text;
string pattern;
cout << "Enter the text: ";
getline(cin, text);
cout << "Enter the pattern: ";
getline(cin, pattern);
KMPSearch(text, pattern);
return 0;
}
OUTPUT:
CODE:COR08P/14: Write a C++ program to implement single source shortest path for a graph using
Dijkstra Algorithm?
#include <iostream>
#include <climits>
using namespace std;
OUTPUT:
CODE:COR08P/15: Write a C++ program to implement single source shortest path for a graph using
Bellman Ford Algorithm?
#include <iostream>
#include <limits>
#include <vector>
using namespace std;
// Driver code
int main() {
// Example graph representation
vector<Edge> edges = {{0, 1, -1}, {0, 2, 4}, {1, 2, 3}, {1, 3, 2},
{1, 4, 2}, {3, 2, 5}, {3, 1, 1}, {4, 3, -3}};
int V = 5; // Number of vertices in graph
int source = 0; // Source vertex
bellmanFord(edges, V, source);
return 0;
}
OUTPUT:
CODE:COR08P/16: Write a C++ program to implement single source shortest path for a graph using
Floyed Warshall Algorithm?
#include <iostream>
#include <limits>
#include <vector>
floydWarshall(graph, V);
return 0;
}
OUTPUT:
GROUP - A
Assignment No. Description Page No. Signature
GROUP – B
Assignment No. Description Page No. Signature