0% found this document useful (0 votes)
54 views16 pages

DAA Assignment 2: by B.Teja Sai (18VF1M3310)

The document contains code for 4 algorithms: 1) Knapsack problem - Finds optimal solution that maximizes profit within weight capacity. 2) Job sequencing with deadlines - Orders jobs by deadline while maximizing profit. 3) Minimum spanning tree - Finds minimum cost tree connecting all vertices using Prim's and Kruskal's algorithms. 4) Dijkstra's algorithm - Finds shortest paths from a single source vertex to all other vertices in a graph.

Uploaded by

Téjã Sâì
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views16 pages

DAA Assignment 2: by B.Teja Sai (18VF1M3310)

The document contains code for 4 algorithms: 1) Knapsack problem - Finds optimal solution that maximizes profit within weight capacity. 2) Job sequencing with deadlines - Orders jobs by deadline while maximizing profit. 3) Minimum spanning tree - Finds minimum cost tree connecting all vertices using Prim's and Kruskal's algorithms. 4) Dijkstra's algorithm - Finds shortest paths from a single source vertex to all other vertices in a graph.

Uploaded by

Téjã Sâì
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

DAA Assignment 2

By B.Teja Sai(18VF1M3310)
1)Knapsack
#include<stdio.h>
int Knapsack(int n, float w[], float p[], float m)
{
float x[20], tweight=0, tprofit=0;
int i, j, U;
U =m;
for (i =1; i <= n; i++)
x[i] = 0.0;
for (i =1; i <= n; i++)
{
if (w[i] > U)
break;
else
{
x[i] = 1.0;
tprofit = tprofit + p[i];
tweight = tweight + w[i];
U = U - w[i];
}
}
if (i <= n)
x[i] = U / w[i];
tweight = tweight + (x[i] * w[i]);
tprofit = tprofit + (x[i] * p[i]);

printf("The optimal solution is: ");


for (i =1; i <= n; i++)
printf("%.2f\t", x[i]);
printf("Total weight = %.2f", tweight);
printf("Total profit = %.2f", tprofit);
}

int main()
{
float w[20], p[20],m;
int n, i, j;
float r[20], temp;
printf("Enter the number of objects: ");
scanf("%d", &n);
printf("Enter weights and profits of each object
respectively:\n");
for (i=1;i<=n;i++)
{
scanf("%f %f", &w[i],&p[i]);
}
printf("Enter the capacity of knapsack: ");
scanf("%f", &m);
for (i=1;i<=n;i++)
{
r[i] = p[i] / w[i];
}
for (i=1;i<=n;i++)
{
for (j=i+1;j<=n;j++)
{
if (r[i] < r[j])
{
temp = r[j];
r[j] = r[i];
r[i] = temp;

temp = w[j];
w[j] = w[i];
w[i] = temp;

temp = p[j];
p[j] = p[i];
p[i] = temp;
}
}
}
Knapsack(n,w,p,m);
return(0);
}
2)Job sequencing with Deadlines
#include<stdio.h>

int Js(int d[6],int j[6],int n)


{
int q,i,r,k;
d[0]=0;
j[0]=0;
j[1]=1;
k=1;
for(i=2;i<=n;i++)
{
r=k;
while((d[j[r]]>d[i]) &&(d[j[r]]!=r))
r=r-1;
if((d[j[r]]<=d[i]) && (d[i]>r))
{
for(q=k;q>=r+1;q--)
{
j[q+1]=j[q];
}
j[r+1]=i;
k=k+1;
}
}
return k;
}

void main( )
{
int d[6],j[6],p[6],k,i;

printf("Enter the deadlines :");


for(i=1;i<=5;i++)
scanf("%d",&d[i]);
printf("Enter the profits :");
for(i=1;i<=5;i++)
scanf("%d",&p[i]);
for(i=1;i<=5;i++)
j[i]=i;
k=Js(d,j,5);
printf("\nThe solution job sequence is ");
for(i=1;i<=k;i++)
printf("\n%d",j[i]);
}

3)Minimum Cost Spanning Tree


a)Prim’s Algorithm
#include<stdio.h>
int a,b,u,v,n,i,j,new=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
printf("\nEnter the number of nodes:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
while(new < n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]< min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n Edge %d:(%d %d) cost:%d",new++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
}

b)Kruskal’s Algorithm
#include<stdio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,new=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int Union(int,int);
void main()
{
printf("\n Kruskal's algorithm\n");
printf("\nEnter the no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("The edges of Minimum Cost Spanning Tree
are\n");
while(new<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j] < min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(Union(u,v))
{
printf("%d edge (%d,%d) =%d\n",new++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %d\n",mincost);
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int Union(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}

4)Dijkstra (Single source path problem)


#include <stdio.h>
#define infinity 9999
int dij(int n,int v,int cost[10][10],int dist[])
{
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
{
flag[i]=0;

dist[i]=cost[v][i];
}
count=2;
while(count<=n)
{
min=9999;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
{
min=dist[w];
u=w;
}
flag[u]=1;

for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])

dist[w]=dist[u]+cost[u][w];

count++;
}
}

int main()
{
int n,v,i,j,cost[10][10],dist[10];
printf("Enter the number of nodes: ");
scanf("%d",&n);
printf("Enter the cost matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)

cost[i][j]=infinity;
}
printf("Enter the source vertex:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("Shortest path:\n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d -> %d = %d\n",v,i,dist[i]);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy