0% found this document useful (0 votes)
15 views31 pages

DAADA

The document contains a series of programming assignments focused on various algorithms, including the Karatsuba algorithm for multiplication, maximum sub-array sum, fractional and 0-1 knapsack problems, longest common subsequence, matrix chain multiplication, assembly line scheduling, N-Queens problem, and subset-sum problem. Each section includes code snippets in C++, sample inputs, and expected outputs. The author is Atharva Mahesh Bhangale, with a registration number of 22BCE3274.

Uploaded by

atharvabhangale3
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)
15 views31 pages

DAADA

The document contains a series of programming assignments focused on various algorithms, including the Karatsuba algorithm for multiplication, maximum sub-array sum, fractional and 0-1 knapsack problems, longest common subsequence, matrix chain multiplication, assembly line scheduling, N-Queens problem, and subset-sum problem. Each section includes code snippets in C++, sample inputs, and expected outputs. The author is Atharva Mahesh Bhangale, with a registration number of 22BCE3274.

Uploaded by

atharvabhangale3
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/ 31

Design and Analysis of Algorithms Lab

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 karatsuba(int a, int b){


string sa = to_string(a);
string sb = to_string(b);
int la = sa.length();
int lb = sb.length();
if ((la == 1) && (lb == 1)){
return a * b;
}
else{
int n = max(la, lb);
int m = ceil(n / 2.0);
while (la < n){
sa = "0" + sa;
la++;
}
while (lb < n){
sb = "0" + sb;
lb++;
}
int al = stoi(sa.substr(0, n / 2));
int ar = stoi(sa.substr(n / 2));
int bl = stoi(sb.substr(0, n / 2));
int br = stoi(sb.substr(n / 2));
int x1 = karatsuba(al, bl);
int x2 = karatsuba(al + ar, bl + br);
int x3 = karatsuba(ar, br);
return x1 * pow(10, 2 * m) + (x2 - x1 - x3) * pow(10, m) + x3;
}
}

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:

Question 2: Finding the maximum sub-array sum in a given array.


Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int cs(int arr[], int l, int m, int h) {


int s = 0, left = INT_MIN, right = INT_MIN;
for (int i = m; i >= l; i--) {
s += arr[i];
if (s > left) {
left = s;
}
}
s = 0;
for (int i = m + 1; i <= h; i++) {
s += arr[i];
if (s > right) {
right = s;
}
}
return (left + right);
}

int ms(int arr[], int l, int h) {


if (l == h) {
return arr[l];
} else {
int m = (l + h) / 2;
int ls = ms(arr, l, m);
int rs = ms(arr, m + 1, h);
int css = cs(arr, l, m, h);
return max({ls, rs, css});
}
}
int main() {
int arr[7];
cout << "Atharva Bhangale 22BCE3274" <<endl;
cout << "Enter elements in array: " << endl;
for (int i = 0; i < 7; i++) {
cin >> arr[i];
}
cout << "Maximum Sub-array Sum: " << ms(arr, 0, 6);
return 0;
}
Sample Input: -5 8 6 -1 4 -3 4
Sample Output: 18
Output:

Question 3: Solving Fractional Knapsack problem using Greedy


approach.
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

typedef struct Item{


float val, wt, ratio;
}item;

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;
}

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


arr[i].ratio = arr[i].val / arr[i].wt;
}
for (int i = 0; i < n - 1; i++){
int min = i;
for (int j = i + 1; j < 3; j++){
if (arr[j].ratio > arr[min].ratio){
min = j;
}
item temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
float amt = 0;
for (int i = 0; i < n; i++){
if (cap != 0){
if (arr[i].wt < cap){
cap -= arr[i].wt;
amt += arr[i].val;
}
else{
amt += arr[i].val * (cap / arr[i].wt);
cap = 0;
}
}
else{
break;
}
}
cout << "Amount: " << amt << endl;
return 0;
}
Sample Input: = {{60, 10}, {100, 20}, {120, 30}}, W = 50
Output: 240
Output:

Question 4: Solve 0-1 Knapsack Problem using Dynamic Programming


Approach.
Code:
#include <iostream>
#include <climits>
using namespace std;
int main() {
cout << "Atharva Bhangale 22BCE3274" <<endl;
int n;
cout << "Enter the number of items: ";
cin >> n;
int weights[n], capacity;
cout << "Enter the weights of items: ";
for (int i = 0; i < n; i++) {
cin >> weights[i];
}

float values[n];

cout << "Enter the values of items: ";


for (int i = 0; i < n; i++) {
cin >> values[i];
}

cout << "Enter the capacity of the knapsack: ";


cin >> capacity;

float dp[n + 1][capacity + 1];


for (int i = 0; i <= n; i++) {
dp[i][0] = 0;
}

for (int i = 1; i <= capacity; i++) {


dp[0][i] = 0;
}

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


for (int j = 1; j <= capacity; j++) {
int index = i - 1;
float include = INT_MIN;
float not_include = dp[i - 1][j];

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:

Question 5: Finding the Longest Common Subsequence between two


strings using Dynamic Programming Approach.
Code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

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;

int n=5, chess[5][5] = {0};


bool found = false;

bool isSafe(int r, int c){


for (int j=c; j>=0; j--){
if (chess[r][j] == 1){
return false;
}
}
for (int i=r, j=c; i>=0 && j>=0; i--,j--){
if (chess[i][j] == 1){
return false;
}
}
for (int i=r, j=c; i<n && j>=0; i++,j--){
if (chess[i][j] == 1){
return false;
}
}
return true;
}

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;
}
}

void solve(int c){


if (c == n){
found = true;
print();
cout << endl;
return;
}
else{
for (int i=0; i<n; i++){
if (isSafe(i, c)){
chess[i][c] = 1;
solve(c+1);
chess[i][c] = 0;
}
}
}
return;
}

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;

int a[] = {10,5,15,20,7,13,3};


int n = 7, target = 20;
stack<int> sum;
bool found = false;

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();
}
}

void solve(int Sum, int ind){


if (Sum>target){
return;
}
if(Sum==target){
found=true;
print();
cout<<endl;
return;
}
for (int i=ind;i<n;i++){
sum.push(a[i]);
solve(Sum+a[i],i+1);
sum.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);
}
};

void printCode(Node *root, string s){


if (root != NULL){
if (root->letter != '*'){
cout << root->letter << " = " << s << endl;
}
printCode(root->left, s + "0");
printCode(root->right, s + "1");
}
}

void huffman(char data[], int freq[], int n){


Node *leftChild, *rightChild;
priority_queue<Node *, vector<Node *>, Compare> pq;
for (int i = 0; i < n; i++){
pq.push(new Node(data[i], freq[i]));
}
while (pq.size() > 1){
leftChild = pq.top();
pq.pop();
rightChild = pq.top();
pq.pop();
Node *temp;
temp = new Node('*', leftChild->frequency + rightChild-
>frequency);
temp->left = leftChild;
temp->right = rightChild;
pq.push(temp);
}
printCode(pq.top(), " ");
}

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:

You might also like