DAA LAB
DAA LAB
Output:-
Output:-
3. WRITE A PROGRAM FOR MERGE SORT.
#include <iostream>
int main()
{
system("cls");
int n, i;
cout << "\nEnter the number of data element to be sorted: ";
cin >> n;
int arr[n];
for (i = 0; i < n; i++)
{
cout << "Enter element " << i + 1 << ": ";
cin >> arr[i];
}
MergeSort(arr, 0, n - 1);
return 0;
}
OUTPUT
ASSIGNMENT 5
GREEDY ALGORITHM IMPLEMENTATION
• Knapsack Problem:
CODE:
#include <iostream>
using namespace std;
int max(int a, int b)
{
return (a > b) ? a : b;
}
return K[n][W];
}
int main()
{
system("cls");
cout << "Enter the number of items in a Knapsack:";
int n, W;
cin >> n;
int val[n], wt[n];
for (int i = 0; i < n; i++)
{
cout << "Enter value and weight for item " << i << ":";
cin >> val[i];
cin >> wt[i];
}
OUTPUT:
• JOB SEQUENCING:
CODE:
#include <bits/stdc++.h>
#include <iostream>
job arr[n];
return 0;
}
OUTPUT:
• HUFFMAN CODING:
CODE:
#include <iostream>
#include <vector>
#include <queue>
#include <string>
class Huffman_Codes
{
struct New_Node
{
char data;
size_t freq;
New_Node *left;
New_Node *right;
New_Node(char data, size_t freq) : data(data),
freq(freq),
left(NULL),
right(NULL)
{
}
~New_Node()
{
delete left;
delete right;
}
};
struct compare
{
bool operator()(New_Node *l, New_Node *r)
{
return (l->freq > r->freq);
}
};
New_Node *top;
if (root->data == '$')
{
print_Code(root->left, str + "0");
print_Code(root->right, str + "1");
}
if (root->data != '$')
{
cout << root->data << " : " << str << "\n";
print_Code(root->left, str + "0");
print_Code(root->right, str + "1");
}
}
public:
Huffman_Codes(){};
~Huffman_Codes()
{
delete top;
}
void Generate_Huffman_tree(vector<char> &data, vector<size_t> &freq,
size_t size)
{
New_Node *left;
New_Node *right;
priority_queue<New_Node *, vector<New_Node *>, compare> minHeap;
while (minHeap.size() != 1)
{
left = minHeap.top();
minHeap.pop();
right = minHeap.top();
minHeap.pop();
return 0;
}
OUTPUT:
• KRUSKAL ALGORITHM:
CODE:
#include <iostream>
#include <algorithm>
#include <vector>
class edge
{
public:
int s;
int d;
int w;
edge()
{
}
s = src;
d = des;
w = wei;
}
};
if (parent[i] == i)
return i;
class graph
{
public:
int e, n;
edge *v;
graph(int n, int e)
{
this->n = n;
this->e = e;
v = new edge[e];
int x, y, w;
cout << "ENTER VERTICES AND WEIGHT OF EDGE " << i + 1 << " : ";
v[i] = e;
}
}
edge *unionfind()
{
parent[i] = i;
}
sort(v, v + e, compare);
edge *output;
int count = 0, i = 0;
while (count != n - 1)
{
edge c = v[i];
if (sourceparent != desparent)
{
output[count] = c;
parent[sourceparent] = desparent;
count++;
}
i++;
}
int sum = 0;
cout << output[i].s << " " << output[i].d << " " << output[i].w
<< endl;
sum += output[i].w;
}
return output;
}
};
int main()
{
system("cls");
int n, e;
cin >> n;
OUTPUT:
• PRIM’S ALGORITHM:
CODE:
#include <iostream>
// main function
int main()
{
system("cls");
int cost[V][V];
cout << "Enter the vertices for a graph with 6 vetices";
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
cin >> cost[i][j];
}
}
find_MST(cost);
return 0;
}
OUTPUT:
Assignment-6
IMPLEMENT THE DYNAMIC PROGRAMMING ALGORITHM:
• MATRIX CHAIN MULTIPLICATION ALGO
#include <bits/stdc++.h>
using namespace std;
return mini;
}
int main()
{
int arr[] = {3, 4, 7, 5, 2, 8};
int N = sizeof(arr) / sizeof(arr[0]);
int main()
{
char X[] = "MANISH Kumar";
char Y[] = "PRAJA PATI";
int m = strlen(X);
int n = strlen(Y);
return 0;
}
OUTPUT :-
if (n == 0 || W == 0)
return 0;
if (wt[n - 1] > W)
return knapSack(W, wt, val, n - 1);
else
return max(
val[n - 1] + knapSack(W - wt[n - 1],
wt, val, n - 1),
knapSack(W, wt, val, n - 1));
}
int main()
{
int val[] = {80, 200, 320};
int wt[] = {102, 206, 56};
int W = 500;
int n = sizeof(val) / sizeof(val[0]);
cout << knapSack(W, wt, val, n);
return 0;
}
OUTPUT :-
#include <bits/stdc++.h>
using namespace std;
struct Edge
{
int src, dest, weight;
};
struct Graph
{
int V, E;
printArr(dist, V);
return;
}
int main()
{
int V = 5;
int E = 8;
struct Graph *graph = createGraph(V, E);
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[0].weight = -6;
graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 3;
graph->edge[2].src = 1;
graph->edge[2].dest = 2;
graph->edge[2].weight = 5;
graph->edge[3].src = 1;
graph->edge[3].dest = 3;
graph->edge[3].weight = 3;
graph->edge[4].src = 1;
graph->edge[4].dest = 4;
graph->edge[4].weight = 8;
graph->edge[5].src = 2;
graph->edge[5].dest = 2;
graph->edge[5].weight = 3;
graph->edge[6].src = 3;
graph->edge[6].dest = 1;
graph->edge[6].weight = 1;
graph->edge[7].src = 4;
graph->edge[7].dest = 3;
graph->edge[7].weight = -3;
BellmanFord(graph, 0);
return 0;
}
OUTPUT :-
#include <stdio.h>
#define V 4
#define INF 99999
printSolution(dist);
}
int main()
{
floydWarshall(graph);
return 0;
}
OUTPUT:-
ASSIGNMENT 7
BACKTRACKING ALGORITHM IMPLEMENTATION
1. QUEEN PROBLEMS
#include <bits/stdc++.h>
#define N 4
using namespace std;
void printSolution(int board[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
cout << " " << board[i][j] << " ";
printf("\n");
}
}
bool isSafe(int board[N][N], int row, int col)
{
int i, j;
for (i = 0; i < col; i++)
if (board[row][i])
return false;
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;
for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j])
return false;
return true;
}
bool solveNQUtil(int board[N][N], int col)
{
if (col >= N)
return true;
for (int i = 0; i < N; i++)
{
if (isSafe(board, i, col))
{
board[i][col] = 1;
if (solveNQUtil(board, col + 1))
return true;
board[i][col] = 0;
}
}
return false;
}
bool solveNQ()
{
int board[N][N] = {{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}};
if (solveNQUtil(board, 0) == false)
{
cout << "Solution does not exist";
return false;
}
printSolution(board);
return true;
}
int main()
{
solveNQ();
return 0;
}
Output:-
color[i] = 0;
}
return false;
}
void printSolution(int color[])
{
cout << "Solution Exists:"
" Following are the assigned colors \n";
for (int i = 0; i < V; i++)
cout << " " << color[i];
cout << "\n";
}
int main()
{
bool graph[V][V] = {
{0, 1, 0, 1},
{1, 0, 1, 0},
{1, 0, 0, 1},
{1, 0, 1, 0},
};
int m = 3;
int color[V];
for (int i = 0; i < V; i++)
color[i] = 0;
if (!graphColoring(graph, m, 0, color))
cout << "Solution does not exist";
return 0;
}
Output:-
3.HAMILTONIAN CYCLES
#include <bits/stdc++.h>
using namespace std;
#define V 5
void printSolution(int path[]);
bool isSafe(int v, bool graph[V][V],
int path[], int pos)
{
if (graph[path[pos - 1]][v] == 0)
return false;
for (int i = 0; i < pos; i++)
if (path[i] == v)
return false;
return true;
}
bool hamCycleUtil(bool graph[V][V],
int path[], int pos)
{
if (pos == V)
{
if (graph[path[pos - 1]][path[0]] == 1)
return true;
else
return false;
}
for (int v = 1; v < V; v++)
{
if (isSafe(v, graph, path, pos))
{
path[pos] = v;
if (hamCycleUtil(graph, path, pos + 1) == true)
return true;
path[pos] = -1;
}
}
return false;
}
bool hamCycle(bool graph[V][V])
{
int *path = new int[V];
for (int i = 0; i < V; i++)
path[i] = -1;
path[0] = 0;
if (hamCycleUtil(graph, path, 1) == false)
{
cout << "\nSolution does not exist";
return false;
}
printSolution(path);
return true;
}
void printSolution(int path[])
{
cout << "Solution Exists:"
" Following is one Hamiltonian Cycle \n";
for (int i = 0; i < V; i++)
cout << path[i] << " ";
cout << path[0] << " ";
cout << endl;
}
int main()
{
bool graph1[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0}};
hamCycle(graph1);
bool graph2[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0}};
hamCycle(graph2);
return 0;
}
Output:-
node->parent = parent;
node->workerID = x;
node->jobID = y;
return node;
}
int calculateCost(int costMatrix[N][N], int x,
int y, bool assigned[])
{
int cost = 0;
bool available[N] = {true};
for (int i = x + 1; i < N; i++)
{
int min = INT_MAX, minIndex = -1;
for (int j = 0; j < N; j++)
{
if (!assigned[j] && available[j] &&
costMatrix[i][j] < min)
{
minIndex = j;
min = costMatrix[i][j];
}
}
cost += min;
available[minIndex] = false;
}
return cost;
}
struct comp
{
bool operator()(const Node *lhs,
const Node *rhs) const
{
return lhs->cost > rhs->cost;
}
};
void printAssignments(Node *min)
{
if (min->parent == NULL)
return;
printAssignments(min->parent);
cout << "Assign Worker " << char(min->workerID + 'A')
<< " to Job " << min->jobID << endl;
}
int findMinCost(int costMatrix[N][N])
{
priority_queue<Node *, std::vector<Node *>, comp> pq;
bool assigned[N] = {false};
Node *root = newNode(-1, -1, assigned, NULL);
root->pathCost = root->cost = 0;
root->workerID = -1;
pq.push(root);
while (!pq.empty())
{
Node *min = pq.top();
pq.pop();
int i = min->workerID + 1;
if (i == N)
{
printAssignments(min);
return min->cost;
}
for (int j = 0; j < N; j++)
{
if (!min->assigned[j])
{
Node *child = newNode(i, j, min->assigned, min);
child->pathCost = min->pathCost + costMatrix[i][j];
child->cost = child->pathCost +
calculateCost(costMatrix, i, j, child-
>assigned);
pq.push(child);
}
}
}
}
int main()
{
int costMatrix[N][N] =
{
{9, 2, 7, 8},
{6, 4, 3, 7},
{5, 8, 1, 8},
{7, 6, 9, 4}};
cout << "\nOptimal Cost is "
<< findMinCost(costMatrix);
return 0;
}
// job assignment using branch and bound technique
Output:-
5. 0/1 Knapsack problem.
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int max(int a, int b) { return (a > b) ? a : b; }
void printknapSack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n + 1][W + 1];
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w] = max(val[i - 1] +
K[i - 1][w - wt[i - 1]],
K[i - 1][w]);
else
K[i][w] = K[i - 1][w];
}
}
int res = K[n][W];
cout << res << endl;
w = W;
for (i = n; i > 0 && res > 0; i--)
{
if (res == K[i - 1][w])
continue;
else
{
cout << " " << wt[i - 1];
res = res - val[i - 1];
w = w - wt[i - 1];
}
}
}
int main()
{
int val[] = {80, 110, 120};
int wt[] = {20, 30, 40};
int W = 80;
int n = sizeof(val) / sizeof(val[0]);
printknapSack(W, wt, val, n);
return 0;
}
Output:-
int main()
{
int n = 4;
int graph[][V] = {
{0, 20, 15, 20},
{10, 0, 15, 25},
{15, 35, 0, 10},
{25, 25, 30, 0}};
vector<bool> v(n);
for (int i = 0; i < n; i++)
v[i] = false;
v[0] = true;
int ans = INT_MAX;
tsp(graph, v, 0, n, 1, 0, ans);
cout << ans;
return 0;
}
Output:-