0% found this document useful (0 votes)
13 views7 pages

Exp 7 Daa

The document describes an experiment to implement the fractional knapsack problem algorithm. It includes: 1) The algorithm takes as input arrays containing weights and profits of items and the knapsack capacity. It calculates the profit/weight ratio of each item and sorts them in descending order. 2) It adds the items to the knapsack without exceeding the capacity. Fractional parts of items can be added if capacity remains. 3) The code provided implements this algorithm to find the maximum profit that can be obtained within the knapsack capacity.

Uploaded by

Viney Chhillar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Exp 7 Daa

The document describes an experiment to implement the fractional knapsack problem algorithm. It includes: 1) The algorithm takes as input arrays containing weights and profits of items and the knapsack capacity. It calculates the profit/weight ratio of each item and sorts them in descending order. 2) It adds the items to the knapsack without exceeding the capacity. Fractional parts of items can be added if capacity remains. 3) The code provided implements this algorithm to find the maximum profit that can be obtained within the knapsack capacity.

Uploaded by

Viney Chhillar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment-07

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>

void knapsack(int n, float weight[], float profit[], float capacity) {


float x[20], tp = 0;
int i, j, u;
u = capacity;

for (i = 0; i < n; i++)


x[i] = 0.0;

for (i = 0; i < n; i++) {


if (weight[i] > u)
break;
else {
x[i] = 1.0;
tp = tp + profit[i];
u = u - weight[i];
}
}

if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);

printf("\nThe result vector is:- ");


for (i = 0; i < n; i++)
printf("%f\t", x[i]);

printf("\nMaximum profit is:- %f", tp);

int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;

printf("\nEnter the no. of objects:- ");


scanf("%d", &num);

printf("\nEnter the wts and profits of each object:- ");


for (i = 0; i < num; i++) {
scanf("%f %f", &weight[i], &profit[i]);
}

printf("\nEnter the capacityacity of knapsack:- ");


scanf("%f", &capacity);

for (i = 0; i < num; i++) {


ratio[i] = profit[i] / weight[i];
}

for (i = 0; i < num; i++) {


for (j = i + 1; j < num; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;

temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;

temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}

knapsack(num, weight, profit, capacity);


return(0);
}

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

void dijkstra(int G[MAX][MAX],int n,int startnode);

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:

You might also like