DAA Record
DAA Record
1. Aim: To implement Knapsack problem to find max. profit (Using Greedy Method
approach)
Source Code:
#include<stdio.h>
int main(){
int n, m, i, j, t;
int p[100],w[100];
printf("Enter size: ");
scanf("%d",&n);
printf("Enter maximum capacity: ");
scanf("%d",&m);
printf("Enter the profits: ");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter the weights: ");
for(i=0;i<n;i++)
scanf("%d",&w[i]);
for(i=0;i<n;i++) {
for(j=0;j<n-i-1;j++) {
float t1 = (float) p[j]/w[j];
float t2 = (float) p[j+1]/w[j+1];
if(t1<t2) {
t=w[j];
w[j]=w[j+1];
w[j+1]=t;
t=p[j];
p[j]=p[j+1];
p[j+1]=t;
}
}
}
float result[100];
for(i=0;i<n;i++)
result[i]=0;
float profit = 0;
for (i = 0; i < n; i++) {
if (w[i] > m)
break;
result[i] = 1;
m = m - w[i];
profit += p[i];
}
if (i <= n) {
result[i] = (float) m / w[i];
profit += p[i] * result[i];
}
printf("\nTotal profit = %.2f",profit);
}
Output 1:
Enter size: 3
Enter capacity: 20
Enter profits: 25 24 15
Enter weights: 18 15 10
Total profit: 31.50
Output 2:
Enter size: 7
Enter capacity: 15
Enter profits: 10 5 15 7 6 18 3
Enter weights: 2 3 5 7 1 4 1
Total profit: 55.33
Vertex Cost
1 0
2 9
3 3
4 5
5 6
Output 1:
Enter number of vertices: 4
Enter the cost matrix:
0506
5 0 10 0
0 10 0 2
6020
The following matrix shows the shortest distances between every pair of vertices
0 5 8 6
5 0 10 11
8 10 0 2
6 11 2 0
Output 2:
Enter number of vertices: 5
Enter the cost matrix:
00300
0 0 10 4 0
3 10 0 2 6
04201
00610
The following matrix shows the shortest distances between every pair of vertices
0 9 3 5 6
9 0 6 4 5
3 6 0 2 3
5 4 2 0 1
6 5 3 1 0
10. Aim: To implement Knapsack problem to find max. profit (Using Dynamic
Programming approach)
Source Code:
#include <stdio.h>
#include<string.h>
int findMax(int a,int b){
return (a>b)?a:b;
}
void knapsack(int profit[],int W,int n,int weight[]){
int c[n+1][W+1];
memset(c,0,n*n*sizeof(int));
int i,w,j;
for(i=1;i<=n;i++){
for(j=0;j<=W;j++){
if(weight[i-1]>j){
c[i][j]=c[i-1][j];
}
else{
c[i][j]=findMax(profit[i-1]+c[i-1][j- weight[i-1]],c[i-1][j]);
}
}
}
for(i=0;i<=n;i++){
for(j=0;j<=W;j++){
printf("%d ",c[i][j]);
}
printf("\n");
}
int arr[n];
memset(arr,0,n*sizeof(int));
i=n;
j=W;
while(i>0 && j>0){
if(c[i][j]!=c[i-1][j]){
arr[i]=1;
j=j-weight[i-1];
i--;
}
else{
i--;
}
}
printf("Included Weights & their respective Profits are: ");
for(i=n;i>0;i--){
if(arr[i]==1)
printf("(%d, %d) ",weight[i-1],profit[i-1]);
}
printf("\nMaximum Profit Obtained: %d\n",c[n][W]);
}
int main()
{
int n,m,i;
printf("Enter the size of n: ");
scanf("%d",&n);
int profits[n],weights[n];
printf("Enter the size of knapsack: ");
scanf("%d",&m);
printf("Enter the profits: ");
for(i=0;i<n;i++)
scanf("%d",&profits[i]);
printf("Enter the weights: ");
for(i=0;i<n;i++)
scanf("%d",&weights[i]);
knapsack(profits,m,n,weights);
return 0;
}
Output 1:
Enter the size of n: 3
Enter the size of knapsack: 6
Enter the profits: 10 15 40
Enter the weights: 1 2 3
0000000
0 10 10 10 10 10 10
0 10 15 25 25 25 25
0 10 15 40 50 55 65
Included Weights & their respective Profits are: (3, 40), (2, 15), (1, 10)
Maximum Profit Obtained: 65
Output 2:
Enter the size of n: 4
Enter the size of knapsack: 8
Enter the profits: 2 3 1 4
Enter the weights: 3 4 6 5
000000000
000222222
000233355
000233355
000234456
Included Weights & their respective Profits are: (5, 4) (3, 2)
Maximum Profit Obtained: 6
11. Aim: To implement Single Source Shortest Path – General Weights – Bellman Ford
Algorithm (Using Dynamic Programming approach)
Source Code:
#include<stdio.h>
#define INF 9999999
void GeneralWeights(int V,int G[][3],int E){
int dist[V],i,j;
for(int i=0;i<V;i++)
dist[i]=INF;
dist[0]=0;
for(i=0;i<V-1;i++){
for(j=0;j<E;j++){
if(dist[G[j][0]]!=INF && dist[G[j][0]]+G[j][2]<dist[G[j][1]])
dist[G[j][1]]=dist[G[j][0]]+G[j][2];
}
}
for(i=0;i<E;i++){
int x=G[i][0],y=G[i][1],weight=G[i][2];
if(dist[x]!=INF && dist[x]+weight<dist[y])
printf("\nGraph Contains Negative Cyceles.");
}
printf("Vertex Distance\n");
for(i=0;i<V;i++)
printf("%d %d\n",i,dist[i]);
}
int main() {
int V,E,i,j;
printf("Enter number of vertices: ");
scanf("%d",&V);
printf("Enter number of edges: ");
scanf("%d",&E);
int G[E][3];
printf("Enter the adjacency matrix in the form(Vertex 1 - Vertex 2 - Cost of V(1,2):\n");
for(i=0;i<E;i++)
for(j=0;j<3;j++)
scanf("%d",&G[i][j]);
GeneralWeights(V,G,E);
}
Output 1:
Enter number of vertices: 5
Enter number of edges: 8
Enter the adjacency matrix in the form(Vertex 1 - Vertex 2 - Cost of V(1,2):
0 1 -1
0 2 4
1 2 3
1 3 2
1 4 2
3 2 5
3 1 1
4 3 -3
Vertex Distance
0 0
1 -1
2 2
3 -2
4 1
Output 2:
Enter number of vertices: 4
Enter number of edges: 5
Enter the adjacency matrix in the form(Vertex 1 - Vetrex 2 - Cost of V(1,2):
0 1 5
0 2 4
1 3 3
2 1 6
3 2 2
Vertex Distance
0 0
1 5
2 4
3 8