0% found this document useful (0 votes)
10 views9 pages

AOA Experiment 6

The document outlines an experiment on the Fractional Knapsack Algorithm, aimed at studying greedy algorithms for optimization problems. It details the theory behind the knapsack problem, provides an example with calculations, and includes a C program implementation that maximizes profit based on value-to-weight ratios. The conclusion emphasizes the algorithm's effectiveness in maximizing value within a specified capacity while providing a clear step-by-step output.

Uploaded by

ashcraftgamer8
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)
10 views9 pages

AOA Experiment 6

The document outlines an experiment on the Fractional Knapsack Algorithm, aimed at studying greedy algorithms for optimization problems. It details the theory behind the knapsack problem, provides an example with calculations, and includes a C program implementation that maximizes profit based on value-to-weight ratios. The conclusion emphasizes the algorithm's effectiveness in maximizing value within a specified capacity while providing a clear step-by-step output.

Uploaded by

ashcraftgamer8
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/ 9

Experiment No.

6
Fraction Knapsack
Date of Performance: 05/03/2025
Date of Submission: 12/03/2025
Experiment No. 6

Title: Fractional Knapsack

Aim: To study and implement Fractional Knapsack Algorithm

Objective: To introduce Greedy based algorithms

Theory:

Greedy method or technique is used to solve Optimization problems. A solution that


can be maximized or minimized is called Optimal Solution.

The knapsack problem states that − given a set of items, holding weights and profit
values, one must determine the subset of the items to be added in a knapsack such
that, the total weight of the items must not exceed the limit of the knapsack and its
total profit value is maximum.
The knapsack problem or rucksack problem is a problem in combinatorial
optimization: Given a set of items, each with a mass and a value, determine the
number of each item to include in a collection so that the total weight is less than or
equal to a given limit and the total value is as large as possible. It derives its name
from the problem faced by someone who is constrained by a fixed size knapsack and
must fill it with the most valuable items. The most common problem being solved is
the 0-1 knapsack problem, which restricts the number xi of copies of each kind of item
to zero or one.
In Knapsack problem we are given:
1. n objects
2. Knapsack with capacity m.
3. An object iis associated with profit Wi.
4. Object i is associated with profit Pi.
5. Object i is placed in knapsack we get profit Pi Xi .
Here objects can be broken into pieces (Xi Values) The Objective of Knapsack
problem is to maximize the profit.
Example:
Find an optimal solution for fractional Knapsack problem.
Where,
Number of objects = 7
Capacity of Knapsack = 15
P1,P2,P3,P4,P5,P6,P7 = (10,5,15,7,6,18,3)
W1,W2,W3,W4,W5,W6,W7 = (2,3,5,7,1,4,1)
Solution:

Arrange the objects in decreasing order of Pi/Wi ratio.

Object 1 2 3 4 5 6 7
Pi 10 5 15 7 6 18 3
Wi 2 3 5 7 1 4 1
Pi/Wi 5 1.67 3 1 6 4.5 3

Select the objects with maximum Pi/Wi ratio:

Object Profit (Pi) Weight (Wi) Remaining Weight


- - - 15
5 6 1 14
1 10 2 12
6 18 4 8
3 15 5 3
7 3 1 2
2 3.33 2 0
Total 55.33
So, the maximum profit is 55.33 units.

Algorithm:

Fractional Knapsack Problem:


Here,
N- Total No. of Objects
M- Capacity of Knapsack
P- Initial profit. P=0
Pi- Profit of ith object
Wi- Weight of ith Object
Step 1:
For i=1 to N
Calculate Profit / Weight Ratio (i.e. Pi/Wi) O(n)

Step 2: O(n.logn)
Sort objects in decreasing order of Profit / Weight Ratio

Step 3: // Add all the profit by considering the weight capacity of fractional knapsack.
For i=1 to N
O(n)
if M > 0 AND Wi <= M
M = M –Wi
P = P + Pi
else
break
if M > 0 Then
P = P + Pi * (M/Wi)
Step 4:
Display Total Profit

Time Complexity = O(n) + O(n.logn) + O(n)


= Max(O(n),O(n.logn),O(n))
= O(n.logn)
Code:
#include <stdio.h>
#include <stdlib.h>

void sortItems(char id[], int value[], int weight[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if ((float)value[j]/weight[j] < (float)value[j + 1]/weight[j + 1]) {
char tempId = id[j];
int tempValue = value[j];
int tempWeight = weight[j];

id[j] = id[j + 1];


value[j] = value[j + 1];
weight[j] = weight[j + 1];

id[j + 1] = tempId;
value[j + 1] = tempValue;
weight[j + 1] = tempWeight;
}
}
}
}

void knapsack(char id[], int value[], int weight[], int n, int capacity) {
int currentWeight = 0;
int totalValue = 0;
int selected[n];

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


selected[i] = 0;
}

printf("\nKnapsack Filling Process (Step-by-Step):\n");


printf("--------------------------------------\n");

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


printf("\nIteration %d: Considering Item %c (Value: %d, Weight: %d)\n",
i + 1, id[i], value[i], weight[i]);

if (currentWeight + weight[i] <= capacity) {


selected[i] = 1;
currentWeight += weight[i];
totalValue += value[i];

printf(" -> Included Item %c\n", id[i]);


printf(" -> Current Items: ");
for (int j = 0; j <= i; j++) {
if (selected[j] == 1) {
printf("%c ", id[j]);
}
}
printf("\n -> Current Weight: %d\n", currentWeight);
printf(" -> Current Total Value: %d\n", totalValue);
} else {
printf(" -> Item %c cannot be included (exceeds capacity)\n", id[i]);
}
}

printf("\nFinal Knapsack Contents:\n");


printf("-----------------------\n");
printf("Selected Items: ");
for (int i = 0; i < n; i++) {
if (selected[i] == 1) {
printf("%c ", id[i]);
}
}
printf("\nTotal Weight: %d\n", currentWeight);
printf("\nTotal Value: %d\n", totalValue);
}

int main() {
int n, capacity;
printf("Enter the number of items: ");
scanf("%d", &n);
printf("Enter the knapsack capacity: ");
scanf("%d", &capacity);

char id[n];
int value[n];
int weight[n];

printf("\nEnter item details (ID Value Weight):\n");


for (int i = 0; i < n; i++) {
printf("Item %d: ", i + 1);
scanf(" %c %d %d", &id[i], &value[i], &weight[i]);
}

sortItems(id, value, weight, n);

printf("\nItems sorted by value/weight ratio:\n");


printf("ID\tValue\tWeight\tRatio\n");
for (int i = 0; i < n; i++) {
printf("%c\t%d\t%d\t%.2f\n", id[i], value[i],
weight[i], (float)value[i]/weight[i]);
}

knapsack(id, value, weight, n, capacity);

return 0;
}

Output:
Conclusion:
The knapsack algorithm implemented in this C program effectively maximizes value within a
given capacity using a greedy approach based on value-to-weight ratios. It sorts items by their
efficiency and iteratively includes them if they fit, providing detailed output for each step. The
program's step-by-step display shows the decision process, current contents, and running totals
clearly. Ultimately, it produces an optimized solution with a final list of selected items and their
total value and weight.

You might also like