DAA FILE Taran
DAA FILE Taran
DAA FILE Taran
#include <bits/stdc++.h>
struct Item {
int value, weight;
int curWeight = 0;
return finalprofit;
}
int main()
{
int N = 60;
OUTPUT:
AIM – 2 :- To code and analyze Knapsack problem using Dynamic approach.
#include <iostream>
using namespace std;
int max(int x, int y) {
return (x > y) ? x : y;
}
int knapSack(int W, int w[], int v[], int n) {
int i, wt;
int K[n + 1][W + 1];
for (i = 0; i <= n; i++) {
for (wt = 0; wt <= W; wt++) {
if (i == 0 || wt == 0)
K[i][wt] = 0;
else if (w[i - 1] <= wt)
K[i][wt] = max(v[i - 1] + K[i - 1][wt - w[i - 1]], K[i - 1][wt]);
else
K[i][wt] = K[i - 1][wt];
}
}
return K[n][W];
}
int main() {
cout << "Enter the number of items in a Knapsack:";
int n, W;
cin >> n;
int v[n], w[n];
for (int i = 0; i < n; i++) {
cout << "Enter value and weight for item " << i << ":";
cin >> v[i];
cin >> w[i];
}
cout << "Enter the capacity of knapsack";
cin >> W;
cout << knapSack(W, w, v, n);
return 0;
}
OUTPUT:-
EXPERIMENT - 02
AIM:- Code and analyze to find an optimal solution to matrix chain
multiplication using dynamic programming.
#include <iostream>
using namespace std;
int dp[100][100];
vector<int> vertex;
for (int i = 0; i < V; i++)
if (i != s)
vertex.push_back(i);
int current_pathweight = 0;
int k = s;
for (int i = 0; i < vertex.size(); i++) {
current_pathweight += graph[k][vertex[i]];
k = vertex[i];
}
current_pathweight += graph[k][s];
min_path = min(min_path, current_pathweight);
} while (
next_permutation(vertex.begin(), vertex.end()));
return min_path;
}
int main()
{
// matrix representation of graph
int graph[][V] = { { 0, 10, 15, 20 },
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 },
{ 20, 25, 30, 0 } };
int s = 0;
cout << travllingSalesmanProblem(graph, s) << endl;
return 0;
}
OUTPUT:-
EXPERIMENT -04
AIM:- Implementing an application of DFS such as:
class Graph {
int V;
list<int>* adj;
void topologicalSortUtil(int v, bool visited[],
stack<int>& Stack);
public:
Graph(int V);
void addEdge(int v, int w);
void topologicalSort();
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
return 0;
}
OUTPUT:-
AIM.2:- To find a path from source to goal in a maze.
#include <cstring>
#include <iostream> #include <stack> using namespace std; #define
N4
#define M 5
node(int i, int j) {
x = i; y = j;
// Initially direction
// set to 0 dir
= 0;
}
};
temp.dir++; s.pop();
s.push(temp);
if (i == fx and j == fy) { return true;
}
// Maze matrix
int maze[N][M] = {
{ 1, 0, 1, 1, 0 },
{ 1, 1, 1, 0, 1 },
{ 0, 1, 0, 1, 1 },
{ 1, 1, 1, 1, 1 }
};
// Food coordinates fx = 2; fy = 3;
if (isReachable(maze)) {
cout << "Path Found!" << '\n';
}
else cout << "No Path Found!" << '\n';
return 0;
}
OUTPUT:-
EXPERIMENT -05
#include <iostream>
#include <queue>
#define V 4
while (!q.empty())
{
int u = q.front();
q.pop();
if (G[u][u] == 1)
return false;
return true;
}
int main()
{
int G[][V] = {{0, 1, 0, 1},
{1, 0, 1, 0},
{0, 1, 0, 1},
{1, 0, 1, 0}
};
OUTPUT:-
YES
EXPERIMENT .:-6
Aim: - Code and analyse to find shortest path in a graph with positive edge
weights using Dijkstra’s algorithm.
#include <iostream>
using namespace std;
#include <limits.h>
#define V 9
{ cout <<"Vertex \t Distance from Source" << endl; for (int i = 0; i < V; i++)
cout << i << " \t\t"<<dist[i]<< endl;
}
void dijkstra(int graph[V][V], int src)
{
int dist[V]; bool sptSet[V]; for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false; dist[src] = 0;
for (int count = 0; count < V - 1; count++)
{
int u = minDistance(dist, sptSet); sptSet[u] = true; for (int v = 0; v < V; v++)
printSolution(dist);
}
int main()
{ int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
return 0;
}
Output: -
EXPERIMENT 7:-
Aim: Code and analyse to find shortest path in a graph
with arbitrary edge weights using Bellman-Ford
algorithm.
#include<iostream>
struct Edge {
int src, dest, weight;
};
struct Graph {
int V, E;
};
int V = graph->V;
int E = graph->E;
int dist[V]; for (int i = 0; i < V; i++)
int u = graph>edge[j].src;
int v = graph>edge[j].dest;
int weight = graph>edge[j].weight;
if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) dist[v] = dist[u]
+ weight;
}
int u = graph>edge[i].src;
int v = graph>edge[i].dest;
int weight = graph>edge[i].weight;
if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) {
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 = -1;
graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 4;
graph->edge[2].src = 1;
graph->edge[2].dest = 2;
graph->edge[2].weight = 3;
graph->edge[3].src = 1;
graph->edge[3].dest = 3;
graph->edge[3].weight = 2;
graph->edge[4].src = 1;
graph->edge[4].dest = 4;
graph->edge[4].weight = 2;
graph->edge[5].src = 3;
graph->edge[5].dest = 2;
graph->edge[5].weight = 5;
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:
EXPERIMENT-8
Aim:- Code and analyse to find shortest path in a graph
with arbitrary weights Floyd’s algorithm.
int dist[V][V], i, j, k;
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
dist[i][j] = graph[i][j];
for (k = 0; k < V; k++) {
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
printSolution(dist);
if (dist[i][j] == INF)
}
}
int main()
{
int graph[V][V] = { { 0, 5, INF, 10 },
{ INF, 0, 3, INF };
{ INF, INF, 0, 1 };
Output:
EXPERIMENT: 9
Aim: - Code and analyze to find the minimum spanning tree
in a weighted, undirected graph using Prims’ algorithm
#include <iostream>
using namespace std;
#define V 5
bool isValidEdge(int u, int v, vector<bool> inMST)
{ if (u == v)
return false;
if (inMST[u] == false && inMST[v] == false)
return false;
else if (inMST[u] == true && inMST[v] == true)
return false;
return true;
}
void primMST(int cost[][V])
{ vector<bool> inMST(V, false);
inMST[0] = true; int edge_count = 0, mincost = 0; while (edge_count < V
- 1)
{
int min = INT_MAX, a = -1, b = -1;
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (cost[i][j] < min) {
if (isValidEdge(i, j, inMST))
{
min = cost[i][j]; a = i; b = j;
}
}
}
}
if (a != -1 && b != -1) { printf("Edge %d:(%d, %d) cost: %d \n",
edge_count++, a, b, min);
mincost = mincost + min; inMST[b] = inMST[a] = true;
}
}
printf("\n Minimum cost= %d \n", mincost);
}
int main()
{
int cost[][V] =
{
{ INT_MAX, 2, INT_MAX, 6, INT_MAX },
{ 2, INT_MAX, 3, 8, 5 },
{ INT_MAX, 3, INT_MAX, INT_MAX, 7 },
{ 6, 8, INT_MAX, INT_MAX, 9 },
{ INT_MAX, 5, 7, 9, INT_MAX },
};
primMST(cost); return 0;
}
Output: -
EXPERIMENT-10
Aim- Code and analyze to find the minimum spanning tree in a
weighted, undirected graph using Kruskals’ algorithm.
#include <iostream>
using namespace std;
#define V 5
int parent[V];
int find(int i)
{ while (parent[i] != i)
i = parent[i];
return i;
}
int edge_count = 0;
while (edge_count < V - 1)
{
int min = INT_MAX, a = -1, b = -1;
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (find(i) != find(j) && cost[i][j] < min) {
min = cost[i][j];
a = i; b = j;
}
}
}
union1(a, b);
printf("Edge %d:(%d, %d) cost:%d \n", edge_count++, a, b, min);
mincost += min;
}
printf("\n Minimum cost= %d \n", mincost);
}
return 0;
}
OUTPUT:
EXPERIMENT -11
#include <bits/stdc++.h>
using namespace std;
#define V 4
// update minimum
min_path = min(min_path, current_pathweight);
} while (
next_permutation(vertex.begin(), vertex.end()));
return min_path;
}
int main()
{
// matrix representation of graph
int graph[][V] = { { 0, 10, 15, 20 },
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 }, {
20, 25, 30, 0 } };
int s = 0;
cout << travllingSalesmanProblem(graph,s)<<endl; return 0 }
OUTPUT:-
80