Exp 7 Daa
Exp 7 Daa
Aim: Given two arrays weight[] and profit[] the weights and profit of N items, we
need to put these items in a knapsack of capacity W to get the maximum
total value in the knapsack. Write a program to implement fractional
Knapsack problem.
Algorithm:
Consider all the items with their weights and profits mentioned respectively.
Calculate Pi/Wi of all the items and sort the items in descending order based on
their Pi/Wi values.
Without exceeding the limit, add the items into the knapsack.
If the knapsack can still store some weight, but the weights of other items
exceed the limit, the fractional part of the next time can be added.
Hence, giving it the name fractional knapsack problem.
Code:
# include<stdio.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;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
Output:
Experiment-08
Aim: From a given vertex in a weighted connected graph, find shortest paths to
other vertices using Dijkstra's algorithm.
Algorithm:
Create a set sptSet (shortest path tree set) that keeps track of vertices included
in the shortest path tree, i.e., whose minimum distance from the source is
calculated and finalized. Initially, this set is empty.
Assign a distance value to all vertices in the input graph. Initialize all distance
values as INFINITE. Assign the distance value as 0 for the source vertex so that
it is picked first.
While sptSet doesn’t include all vertices
Pick a vertex u that is not there in sptSet and has a minimum distance value.
Include u to sptSet.
Then update the distance value of all adjacent vertices of u.
To update the distance values, iterate through all adjacent vertices.
For every adjacent vertex v, if the sum of the distance value of u (from source)
and weight of edge u-v, is less than the distance value of v, then update the
distance value of v.
Code:
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
void dijkstra(int G[MAX][MAX],int n,int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
//pred[] stores the predecessor of each node
//count gives the number of nodes seen so far
//create the cost matrix
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
//initialize pred[],distance[] and visited[]
for(i=0;i<n;i++)
{distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{mindistance=INFINITY;
//nextnode gives the node at minimum distance
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{mindistance=distance[i];
nextnode=i;}
//check if a better path exists through nextnode
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++;
}
//print the path and distance of each node
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}
Output: