DAA CAT2
DAA CAT2
FRACTIONAL KNAPSACK
#include <iostream>
#include <vector>
struct Item {
// Constructor
};
currentWeight += item.weight;
totalValue += item.value;
} else {
return totalValue;
int main() {
int n, W;
cin >> n;
cin >> W;
vector<Item> items;
cout << "Enter value and weight of item " << i + 1 << ": ";
items.push_back(Item(value, weight));
cout << "Maximum value in Knapsack: " << maxValue << endl;
return 0;
HUFFMAN CODING
#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
struct Node {
char ch;
int freq;
Node* left;
Node* right;
};
struct Compare {
};
// Function to print the Huffman codes from the root of the Huffman tree
if (!root) {
return;
}
// If this is a leaf node, it contains a character
cout << root->ch << ": " << str << "\n";
// Create a leaf node for each character and add it to the priority queue
minHeap.pop();
minHeap.pop();
// Create a new internal node with a frequency equal to the sum of the two nodes' frequencies
newNode->left = left;
newNode->right = right;
minHeap.push(newNode);
}
printCodes(root, "");
int main() {
int n;
cin >> n;
char ch;
int freq;
cout << "Enter character " << i + 1 << " and its frequency: ";
freqMap[ch] = freq;
huffmanCoding(freqMap);
return 0;
}
DYNAMIC PROGRAMMING:
ASSEMBLY LINE SCHD.
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
// Base cases
dp1[i] = min(dp1[i - 1] + a[i], dp2[i - 1] + t[i - 1] + a[i]); // Coming from line 1 or switching
from line 2
dp2[i] = min(dp2[i - 1] + b[i], dp1[i - 1] + t[i - 1] + b[i]); // Coming from line 2 or switching
from line 1
int main() {
int n;
cin >> n;
vector<int> a(n), b(n), t(n - 1);
cout << "Enter processing times on line 1 for each task: ";
cout << "Enter processing times on line 2 for each task: ";
cout << "Enter switch times between lines for each task (except the last task): ";
cout << "Minimum time to complete all tasks: " << minTime << endl;
return 0;
DYNAMIC PROGRAMMING:
MATRIX CHAIN MULTIPLICATION
#include <iostream>
#include <vector>
for (int len = 2; len < n; len++) { // len is the chain length
int j = i + len - 1;
// q = cost of splitting at k
if (q < m[i][j]) {
m[i][j] = q;
int main() {
int n = p.size();
cout << "Minimum number of scalar multiplications: " << minMultiplications << endl;
return 0;
#include <vector>
#include <string>
int m = X.length();
int n = Y.length();
} else {
}
}
return dp[m][n];
string getLCS(const string &X, const string &Y, const vector<vector<int>> &dp) {
int i = X.length();
int j = Y.length();
i--;
j--;
i--;
} else {
j--;
return lcs;
int main() {
string X, Y;
cin >> X;
cin >> Y;
int m = X.length();
int n = Y.length();
} else {
cout << "Length of the longest common subsequence: " << dp[m][n] << endl;
cout << "Longest common subsequence: " << lcs << endl;
return 0;
#include <vector>
// If the current item's weight is less than or equal to the current capacity
if (weights[i - 1] <= w) {
} else {
// If the current item's weight exceeds the current capacity, skip the item
return dp[n][W];
int main() {
int n, W;
cin >> n;
cin >> W;
cout << "Enter value and weight for item " << i + 1 << ": ";
cout << "Maximum value in the knapsack: " << maxValue << endl;
return 0;
#include <vector>
#include <climits>
#include <cmath>
// DP table: dp[mask][i] -> minimum distance to visit all cities in mask and end at city i
dp[1][0] = 0;
}
}
return result;
int main() {
int n;
cin >> n;
cout << "The minimum cost of the tour is: " << minCost << endl;
return 0;
}
BACKTRACKING:
N – QUEENS PROBLEM
#include <iostream>
#include <vector>
if (board[row][i]) {
return false;
if (board[i][j]) {
return false;
if (board[i][j]) {
return false;
return true;
if (col >= N) {
return true;
// Consider this column and try placing this queen in all rows one by one
board[i][col] = 1;
return true;
board[i][col] = 0; // Backtrack
return false;
if (solveNQueensUtil(board, 0, N)) {
return true;
} else {
cout << "No solution exists for " << N << " queens." << endl;
return false;
int main() {
int N;
cin >> N;
solveNQueens(N);
return 0;
SUBEST SUM
#include <iostream>
#include <vector>
void subsetSumUtil(const vector<int>& set, vector<int>& subset, int index, int currentSum, int
targetSum) {
// Base case: If the current sum matches the target sum, print the subset
if (currentSum == targetSum) {
printSubset(subset);
return;
// If the current sum exceeds the target sum or if we've processed all elements, return
return;
subset.push_back(set[index]);
// Exclude the current element from the subset (backtrack) and recurse
subset.pop_back();
// Function to initiate the backtracking algorithm for the subset sum problem
int main() {
int n, targetSum;
cin >> n;
vector<int> set(n);
cout << "Subsets with sum " << targetSum << " are:" << endl;
subsetSum(set, targetSum);
return 0;
GRAPH COLORING
#include <iostream>
#include <vector>
return false;
return true;
if (v == graph.size()) {
return true;
return true;
color[v] = 0;
return false;
}
cout << "No solution exists with " << m << " colors." << endl;
return false;
cout << "Solution exists with the following colors assigned:" << endl;
cout << "Vertex " << i + 1 << " -> Color " << color[i] << endl;
return true;
int main() {
int V, E, m;
cin >> V;
cin >> E;
cout << "Enter the edges (u v) where there is an edge between vertex u and vertex v:" << endl;
int u, v;
cin >> m;
graphColoring(graph, m);
return 0;
STRING MATCHING :
NAIVE STRING MATCHING ALG.
#include <iostream>
#include <string>
int j;
if (text[i + j] != pattern[j]) {
// If we found a match
if (j == m) {
cout << "Pattern found at index: " << i << endl;
int main() {
naiveStringMatching(text, pattern);
return 0;
KMP ALGORITHM
#include <iostream>
#include <vector>
#include <string>
// Function to preprocess the pattern and create the LPS (Longest Prefix Suffix) array
int i = 1;
if (pattern[i] == pattern[length]) {
length++;
lps[i] = length;
i++;
} else {
// Mismatch after length matches
if (length != 0) {
} else {
lps[i] = 0;
i++;
int n = text.length();
int m = pattern.length();
while (i < n) {
if (pattern[j] == text[i]) {
i++;
j++;
if (j == m) {
j = lps[j - 1];
} else {
i++;
int main() {
KMPsearch(text, pattern);
return 0;
#include <string>
int n = text.size();
int m = pattern.size();
int i, j;
// Calculate the value of h = pow(d, m-1) % q
h = (h * d) % q;
// Calculate the initial hash values for the pattern and first window of the text
p = (d * p + pattern[i]) % q;
t = (d * t + text[i]) % q;
// If the hash values of current window of text and pattern match, then check characters
if (p == t) {
if (text[i + j] != pattern[j]) {
match = false;
break;
// If match is found
if (match) {
if (i < n - m) {
if (t < 0) {
t = (t + q);
}
int main() {
getline(cin, text);
getline(cin, pattern);
return 0;