DAA File
DAA File
PROGRAM NO. –1
WRITE A PROGRAM TO IMPLEMENT 0/1 KNAPSACK PROBLEM USING DYNAMIC
PROGRAMMING.
#include<stdio.h>
#include<conio.h>
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
clrscr();
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
SIGNATURE
217008 DATE- / /2023
PROGRAM NO. –2
WRITE A PROGRAM TO IMPLEMENT 0/1 KNAPSACK PROBLEM USING DYNAMIC
PROGRAMMING.
#include<iostream.h>
#include<conio.h>
int max(int a,int b)
{
return (a>b)? a:b;
}
int knapsack (int c,int w[],int p[],int n)
{
if (n==0||c== 0)
return 0 ;
if (w[n-1]>c)
return knapsack(c,w,p,n-1);
else
return max(p[n-1]+ knapsack(c-w[n-1],w,p,n-1),
knapsack(c,w,p,n-1));
}
int main()
{
int profit[]={60,100,120};
int weight[]={10,20,30};
int c=50;
int n = sizeof(profit)/sizeof(profit[0]);
clrscr();
cout<<"The optimal solution to 0/1 Knapsack problem is:
"<<knapsack(c,weight,profit,n);
getch();
return 0;
}
SIGNATURE
217008 DATE- / /2023
PROGRAM NO. –3
WRITE A PROGRAM TO FIND OPTIMAL SOLUTION TO MATRIX CHAIN
MULTIPLICATION USING DYNAMIC PROGRAMMING.
// C code to implement the
// matrix chain multiplication using recursion
#include <limits.h>
#include <stdio.h>
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
printf("Minimum number of multiplications is %d ",
MatrixChainOrder(arr, 1, N - 1));
getchar();
return 0;
}
SIGNATURE
217008 DATE- / /2023
PROGRAM NO. –4
WRITE A PROGRAM TO IMPLEMENT TRAVELLING SALESMAN PROBLEM USING
DYNAMIC PROGRAMMING.
#include<stdio.h>
#include<conio.h>
SIGNATURE
217008 DATE- / /2023
PROGRAM NO. –5
WRITE A PROGRAM TO FIND SHORTEST PATHS IN A GRAPH WITH POSITIVE
EDGE WEIGHTS USING DIJKASTRA’S ALGORITHM.
#include <stdio.h>
#define INFINITY 9999
#define MAX 10
#include<conio.h>
distance[start] = 0;
visited[start] = 1;
count = 1;
while (count < n - 1) {
mindistance = INFINITY;
visited[nextnode] = 1;
for (i = 0; i < n; i++)
if (!visited[i])
if (mindistance + cost[nextnode][i] < distance[i]) {
distance[i] = mindistance + cost[nextnode][i];
pred[i] = nextnode;
}
count++;
}
Graph[0][0] = 0;
Graph[0][1] = 0;
Graph[0][2] = 1;
Graph[0][3] = 2;
Graph[0][4] = 0;
Graph[0][5] = 0;
Graph[0][6] = 0;
Graph[1][0] = 0;
Graph[1][1] = 0;
Graph[1][2] = 2;
Graph[1][3] = 0;
Graph[1][4] = 0;
Graph[1][5] = 3;
Graph[1][6] = 0;
Graph[2][0] = 1;
Graph[2][1] = 2;
Graph[2][2] = 0;
Graph[2][3] = 1;
Graph[2][4] = 3;
Graph[2][5] = 0;
Graph[2][6] = 0;
Graph[3][0] = 2;
Graph[3][1] = 0;
Graph[3][2] = 1;
Graph[3][3] = 0;
Graph[3][4] = 0;
Graph[3][5] = 0;
Graph[3][6] = 1;
Graph[4][0] = 0;
Graph[4][1] = 0;
Graph[4][2] = 3;
Graph[4][3] = 0;
Graph[4][4] = 0;
Graph[4][5] = 2;
Graph[4][6] = 0;
Graph[5][0] = 0;
Graph[5][1] = 3;
Graph[5][2] = 0;
Graph[5][3] = 0;
Graph[5][4] = 2;
Graph[5][5] = 0;
Graph[5][6] = 1;
Graph[6][0] = 0;
Graph[6][1] = 0;
Graph[6][2] = 0;
Graph[6][3] = 1;
Graph[6][4] = 0;
Graph[6][5] = 1;
Graph[6][6] = 0;
u = 0;
Dijkstra(Graph, n, u);
getch();
return 0;
}
SIGNATURE
217008 DATE- / /2023
PROGRAM NO. –6
WRITE A PROGRAM TO FIND SHORTEST PATHS IN A GRAPH WITH ARBITRARY
EDGE WEIGHTS USING FLOYD’S ALGORITHM.
#include <stdio.h>
#include<conio.h>
int main() {
int graph[nV][nV] = {{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}};
clrscr();
floydWarshall(graph);
getch();
}
SIGNATURE
217008 DATE- / /2023
PROGRAM NO. –7
WRITE A PROGRAM TO FIND MINIMAL SPANNING TREE IN A WEIGHTED,
UNDIRECTED GRAPH USING PRIM’S ALGORITHM.
#include <stdio.h>
#include <limits.h>
#include<conio.h>
#define vertices 5 /*Define the number of vertices in the graph*/
/* create minimum_key() method for finding the vertex that has minimum
key-value and that is not added in MST yet */
int minimum_key(int k[], int mst[])
{
int minimum = INT_MAX, min,i;
/*iterate over all vertices to find the vertex with minimum key-value*/
for (i = 0; i < vertices; i++)
if (mst[i] == 0 && k[i] < minimum )
minimum = k[i], min = i;
return min;
}
/* create prim() method for constructing and printing the MST.
The g[vertices][vertices] is an adjacency matrix that defines the graph for
MST.*/
void prim(int g[vertices][vertices])
{
/* create array of size equal to total number of vertices for storing the
MST*/
int parent[vertices];
/* create k[vertices] array for selecting an edge having minimum
weight*/
int k[vertices];
int mst[vertices];
int i, count,edge,v; /*Here 'v' is the vertex*/
for (i = 0; i < vertices; i++)
{
k[i] = INT_MAX;
mst[i] = 0;
}
k[0] = 0; /*It select as first vertex*/
parent[0] = -1; /* set first value of parent[] array to -1 to make it root of
MST*/
for (count = 0; count < vertices-1; count++)
{
/*select the vertex having minimum key and that is not added in the
MST yet from the set of vertices*/
edge = minimum_key(k, mst);
mst[edge] = 1;
for (v = 0; v < vertices; v++)
{
if (g[edge][v] && mst[v] == 0 && g[edge][v] < k[v])
{
parent[v] = edge, k[v] = g[edge][v];
}
}
}
/*Print the constructed Minimum spanning tree*/
printf("\n Edge \t Weight\n");
for (i = 1; i < vertices; i++)
printf(" %d <-> %d %d \n", parent[i], i, g[i][parent[i]]);
}
int main()
{
int g[vertices][vertices] = {{0, 0, 3, 0, 0},
{0, 0, 10, 4, 0},
{3, 10, 0, 2, 6},
{0, 4, 2, 0, 1},
{0, 0, 6, 1, 0},
};
clrscr();
prim(g);
getch();
return 0;
}
SIGNATURE
217008 DATE- / /2023
PROGRAM NO. –8
WRITE A PROGRAM TO FIND MINIMAL SPANNING TREE IN A WEIGHTED,
UNDIRECTED GRAPH USING KRUSKAL’S ALGORITHM.
#include <stdio.h>
#include<conio.h>
#define MAX 30
edge_list elist;
int Graph[MAX][MAX], n;
edge_list spanlist;
void kruskalAlgo();
int find(int belongs[], int vertexno);
void applyUnion(int belongs[], int c1, int c2);
void sort();
void print();
sort();
spanlist.n = 0;
if (cno1 != cno2) {
spanlist.data[spanlist.n] = elist.data[i];
spanlist.n = spanlist.n + 1;
applyUnion(belongs, cno1, cno2);
}
}
}
// Sorting algo
void sort() {
int i, j;
edge temp;
n = 6;
clrscr();
Graph[0][0] = 0;
Graph[0][1] = 4;
Graph[0][2] = 4;
Graph[0][3] = 0;
Graph[0][4] = 0;
Graph[0][5] = 0;
Graph[0][6] = 0;
Graph[1][0] = 4;
Graph[1][1] = 0;
Graph[1][2] = 2;
Graph[1][3] = 0;
Graph[1][4] = 0;
Graph[1][5] = 0;
Graph[1][6] = 0;
Graph[2][0] = 4;
Graph[2][1] = 2;
Graph[2][2] = 0;
Graph[2][3] = 3;
Graph[2][4] = 4;
Graph[2][5] = 0;
Graph[2][6] = 0;
Graph[3][0] = 0;
Graph[3][1] = 0;
Graph[3][2] = 3;
Graph[3][3] = 0;
Graph[3][4] = 3;
Graph[3][5] = 0;
Graph[3][6] = 0;
Graph[4][0] = 0;
Graph[4][1] = 0;
Graph[4][2] = 4;
Graph[4][3] = 3;
Graph[4][4] = 0;
Graph[4][5] = 0;
Graph[4][6] = 0;
Graph[5][0] = 0;
Graph[5][1] = 0;
Graph[5][2] = 2;
Graph[5][3] = 0;
Graph[5][4] = 3;
Graph[5][5] = 0;
Graph[5][6] = 0;
kruskalAlgo();
print();
getch();
}
SIGNATURE