DAADA
DAADA
DA-1
Name: Atharva Mahesh Bhangale
Reg No: 22BCE3274
Question 1: Multiplying 2 numbers using Karatsuba Algorithm.
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
cout << "Atharva Bhangale 22BCE3274" <<endl;
int a, b;
cout << "Enter first number: " << endl;
cin >> a;
cout << "Enter second number: " << endl;
cin >> b;
cout << "Result: " << karatsuba(a, b);
return 0;
}
Sample input: 456 x 567
Sample output:258552
Output:
int main(){
cout << "Atharva Bhangale 22BCE3274" <<endl;
int cap, n;
cout << "Enter capacity: " << endl;
cin >> cap;
cout << "Enter number of items: " << endl;
cin >> n;
item arr[n];
for (int i = 0; i < n; i++){
cout << "Enter value and weight for item " << i + 1 << endl;
cin >> arr[i].val >> arr[i].wt;
}
float values[n];
if (weights[index] <= j) {
include = values[index] + dp[i - 1][j - weights[index]];
}
dp[i][j] = max(include, not_include);
}
}
cout << "Maximum value in the knapsack: " << dp[n][capacity] <<
endl;
int row = n, col = capacity;
while (row != 0 && col != 0) {
if (dp[row][col] != dp[row - 1][col]) {
int index = row - 1;
cout << "Item " << index + 1 << " with weight: " <<
weights[index] << endl;
col -= weights[index];
}
row--;
}
return 0;
}
Sample Input: N = 3, W = 4, profit[] = {1, 2, 3}, weight[] = {4, 5, 1}
Output: 3
Output:
int main(){
cout << "Atharva Bhangale 22BCE3274" <<endl;
string s1,s2;
cout<<"Enter 2 strings to find LCS: "<<endl;
cin>>s1>>s2;
int len1 = s1.size();
int len2 = s2.size();
int dp[len1+1][len2+1] = {0};
int seq[len1+1][len2+1] = {0};
for (int i=1;i<=len1;i++){
for (int j=1;j<=len2;j++){
if (s1[i-1]==s2[j-1]){
dp[i][j] = 1 + dp[i-1][j-1];
seq[i][j] = 3;
}
else{
int a = dp[i-1][j];
int b = dp[i][j-1];
dp[i][j] = a > b ? a : b;
seq[i][j] = a > b ? 2 : 1;
}
}
}
cout<<dp[len1][len2]<<endl;
int i = len1, j = len2;
string result;
while ((i>0)&&(j>0)){
if (seq[i][j]==3){
result = s1[i-1] + result;
i--,j--;
}
else if(seq[i][j]==2){
i--;
}
else{
j--;
}
}
cout<<"Longest Common Subsequence: " << result;
return 0;
}
Sample Input: S1 = “AGGTAB”, S2 = “GXTXAYB”
Output: 4
Output:
Question 6: Matrix Chain Multiplication using Dynamic Programming
Approach.
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
cout << "Atharva Bhangale 22BCE3274" <<endl;
int n;
cout<<"Enter no. of matrices: "<<endl;
cin>>n;
int r[n+1];
cout<<"Enter dimensions of the matrices: "<<endl;
for (int i=0;i<=n;i++){
cin>>r[i];
}
int c[n][n] = {0};
for (int d=2;d<=n;d++){
for (int i=0;i<n-d+1;i++){
int j = i+d-1;
int min = 9999999;
for (int k=i;k<=j-1;k++){
int q = c[i][k] + c[k+1][j] + r[i]*r[k+1]*r[j+1];
if (q < min){
min = q;
}
}
c[i][j] = min;
}
}
cout<<endl;
cout<<"Result: "<<c[0][n-1];
return 0;
}
Sample Input: arr[] = {40, 20, 30, 10, 30}
Output: 26000
Output:
Question 7: Implementing Assembly Line Scheduling using Dynamic
Programming Approach.
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
cout << "Atharva Bhangale 22BCE3274" <<endl;
int n;
cout<<"Enter the number of work stations in each of the 2 lines:
"<<endl;
cin>>n;
int entry[2], exit[2];
int a[2][n] = {0};
int t[2][n] = {0};
cout<<"Enter time taken in each work station in line 0: "<<endl;
for (int j=0;j<n;j++){
cin>>a[0][j];
}
cout<<"Enter time taken in each work station in line 1: "<<endl;
for (int j=0;j<n;j++){
cin>>a[1][j];
}
cout<<"Enter time taken for transfers from line 0 to 1"<<endl;
for (int j=0;j<n;j++){
cin>>t[0][j];
}
cout<<"Enter time taken for transfers from line 1 to 0"<<endl;
for (int j=0;j<n;j++){
cin>>t[1][j];
}
cout<<"Enter entry values for lines 0 and 1"<<endl;
cin>>entry[0]>>entry[1];
cout<<"Enter exit values for lines 0 and 1"<<endl;
cin>>exit[0]>>exit[1];
int dp[2][n], path[2][n];
dp[0][0] = entry[0] + a[0][0];
dp[1][0] = entry[1] + a[1][1];
for (int j=1;j<n;j++){
int p = dp[0][j-1] + a[0][j];
int q = dp[1][j-1] + a[0][j] + t[1][j];
dp[0][j] = p > q ? q : p;
path[0][j] = p > q ? 1 : 0;
int c = dp[1][j-1] + a[1][j];
int d = dp[0][j-1] + a[1][j] + t[0][j];
dp[1][j] = c > d ? d : c;
path[1][j] = c > d ? 0 : 1;
}
int x1 = dp[0][n-1] + exit[0];
int x2 = dp[1][n-1] + exit[1];
int mintime = x1 > x2 ? x2 : x1;
int line = x1 > x2 ? 1 : 0;
cout<<"Total Time Taken : "<< mintime <<endl;
for (int j=n-1;j>0;j--){
line = path[line][j];
cout<<"Station "<<j<<" reached from "<<line<<endl;
}
return 0;
}
Sample Input:
Sample Output:35
Output:
Question 8: Placing N Queens on a NxN chessboard such that no 2
queens can capture each other using Backtracking Approach.
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void print(){
for (int i=0; i<n; i++){
for (int j=0; j<n; j++){
if (chess[i][j] == 1){
cout << "Q";
}
else{
cout << "-";
}
}
cout << endl;
}
}
int main(){
cout << "Atharva Bhangale 22BCE3274" <<endl;
solve(0);
if (found == false){
cout << "No Solution" << endl;
}
Return 0;
}
Output:
Question 9: Solving Subset-Sum problem using Backtracking
Approach.
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void print(){
stack<int> temp;
while(!sum.empty()){
temp.push(sum.top());
sum.pop();
}
while(!temp.empty()){
cout<<temp.top()<<", ";
sum.push(temp.top());
temp.pop();
}
}
int main(){
cout << "Atharva Bhangale 22BCE3274" <<endl;
cout<<"Target = "<<target<<endl;
solve(0,0);
if (found==false){
cout<<"No solution"<<endl;
}
return 0;
}
Sample Input: set[] = {3, 34, 4, 12, 5, 2},
Output: sum = 9
Output:
Question 10:
Huffman Coding
Code:
#include <iostream>
#include <bits/stdc++.h>
#include <queue>
using namespace std;
struct Node{
char letter;
int frequency;
Node *left, *right;
Node(char l, int f){
letter = l;
frequency = f;
}
};
struct Compare{
bool operator()(Node *a, Node *b){
return (a->frequency > b->frequency);
}
};
int main(){
cout << "Atharva Bhangale 22BCE3274" <<endl;
int n;
cout << "Enter the number of characters: " << endl;
cin >> n;
char letters[n];
int frequencies[n];
for (int i = 0; i < n; i++){
cout << "Enter character and corresponding frequency: " <<
endl;
cin >> letters[i] >> frequencies[i];
}
huffman(letters, frequencies, n);
return 0;
}
Sample Input:
character Frequency
a 5
b 9
c 12
d 13
e 16
f 45
Sample Output:
character code-word
f 0
c 100
d 101
a 1100
b 1101
e 111
Output: