0% found this document useful (0 votes)
25 views4 pages

Program No 8

Uploaded by

Akul Kumar
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)
25 views4 pages

Program No 8

Uploaded by

Akul Kumar
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/ 4

Program No: - 8

Aim: Write a program to implement the Knapsack Problem using a greedy solution.
Knapsack Problem: The Knapsack Problem is a combinatorial optimization problem where the
goal is to maximize the value of items placed in a knapsack without exceeding its capacity. The
greedy approach works by selecting items based on the highest value-to-weight ratio, allowing
us to maximize the total value.

Complexity:

 Worst-case complexity: O(nlog⁡n)O(n \log n)O(nlogn) (for sorting items by their value-to-
weight ratio)

 Best-case complexity: O(n)O(n)O(n) (if items are already sorted)

Algorithm:

1. Sort the items based on their value-to-weight ratio in descending order.

2. Start adding items to the knapsack:

o If adding an item doesn’t exceed the knapsack’s capacity, add it completely.

o If adding an item exceeds the capacity, add a fractional portion of the item to fill
the knapsack to its limit.

3. Stop when the knapsack reaches its maximum capacity.

Process:

1. Store the weights and values of the items in arrays.

2. Sort items by their value-to-weight ratio.

3. Traverse the sorted items and add them to the knapsack, considering their weight.

4. If a full item can be added, add it; otherwise, add a fraction to maximize value without
exceeding capacity.

Source Code:

#include <stdio.h>

struct Item {

int weight;

int value;

};

void knapsack(struct Item items[], int n, int capacity) {


float totalValue = 0.0;

int currentWeight = 0;

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

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

currentWeight += items[i].weight;

totalValue += items[i].value;

} else {

int remainingWeight = capacity - currentWeight;

totalValue += items[i].value * ((float)remainingWeight / items[i].weight);

break;

printf("Maximum Value: %.2f\n", totalValue);

void sortItemsByRatio(struct Item items[], int n) {

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

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

float ratio1 = (float)items[j].value / items[j].weight;

float ratio2 = (float)items[j + 1].value / items[j + 1].weight;

if (ratio1 < ratio2) {

struct Item temp = items[j];

items[j] = items[j + 1];

items[j + 1] = temp;

}
int main() {

int n, capacity;

printf("Enter the number of items: ");

scanf("%d", &n);

struct Item items[n];

printf("Enter the weight and value of each item:\n");

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

printf("Item %d - Weight: ", i + 1);

scanf("%d", &items[i].weight);

printf("Item %d - Value: ", i + 1);

scanf("%d", &items[i].value);

printf("Enter the capacity of the knapsack: ");

scanf("%d", &capacity);

sortItemsByRatio(items, n);

knapsack(items, n, capacity);

return 0;

Output:-

Enter the number of items: 3

Enter the weight and value of each item:

Item 1 - Weight: 10

Item 1 - Value: 60
Item 2 - Weight: 20

Item 2 - Value: 100

Item 3 - Weight: 30

Item 3 - Value: 120

Enter the capacity of the knapsack: 50

Maximum Value: 240.00

You might also like