0% found this document useful (0 votes)
45 views2 pages

knapsack 0/1 program in c

The document contains two implementations of the knapsack problem: the fractional knapsack and the 0-1 knapsack. The fractional knapsack uses a greedy approach to maximize value based on the value-to-weight ratio, while the 0-1 knapsack uses recursion to determine the maximum profit without exceeding weight capacity. Both implementations are provided in C programming language with example inputs and outputs.

Uploaded by

Atul Shintre
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)
45 views2 pages

knapsack 0/1 program in c

The document contains two implementations of the knapsack problem: the fractional knapsack and the 0-1 knapsack. The fractional knapsack uses a greedy approach to maximize value based on the value-to-weight ratio, while the 0-1 knapsack uses recursion to determine the maximum profit without exceeding weight capacity. Both implementations are provided in C programming language with example inputs and outputs.

Uploaded by

Atul Shintre
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/ 2

Practical No 6

Fractional knapsack

#include <stdio.h>
#include <stdlib.h>
// Structure to store item details
typedef struct {
float value;
float weight;
float ratio;
} Item;
// Comparison function for sorting items by valueto-weight ratio
int compare(const void *a, const void *b) {
float r1 = ((Item *)a)->ratio;
float r2 = ((Item *)b)->ratio;
return (r1 < r2) ? 1 : (r1 > r2) ? -1 : 0;
}
// Function to solve the fractional knapsack problem
float fractionalKnapsack(Item items[], int n, float capacity) {
qsort(items, n, sizeof(Item), compare);
float totalValue = 0.0;
float remainingCapacity = capacity;
for (int i = 0; i < n; i++) {
if (items[i].weight <= remainingCapacity) {
totalValue += items[i].value;
remainingCapacity -= items[i].weight;
} else {
totalValue += items[i].ratio * remainingCapacity;
break;
}
}
return totalValue;
}
int main() {
int n;
float capacity;
printf("Enter the number of items: ");
scanf("%d", &n);
Item items[n];
printf("Enter the value and weight of each item:\n");
for (int i = 0; i < n; i++) {
scanf("%f %f", &items[i].value, &items[i].weight);
items[i].ratio = items[i].value / items[i].weight;
}
printf("Enter the capacity of the knapsack: ");
scanf("%f", &capacity);
float maxValue = fractionalKnapsack(items, n, capacity);
printf("Maximum value in knapsack = %.2f\n", maxValue);
return 0;
}

Output:

0-1 Knapsack Problem

#include <stdio.h>

int max(int a, int b) { return (a > b) ? a : b; }

int knapSack(int W, int wt[], int val[], int n)


{
// Base Case
if (n == 0 || W == 0)
return 0;

if (wt[n - 1] > W)
return knapSack(W, wt, val, n - 1);
else
return max(
val[n - 1]
+ knapSack(W - wt[n - 1], wt, val, n - 1),
knapSack(W, wt, val, n - 1));
}

int main()
{
int profit[] = { 60, 100, 120 };
int weight[] = { 10, 20, 30 };
int W = 50;
int n = sizeof(profit) / sizeof(profit[0]);
printf("%d", knapSack(W, weight, profit, n));
return 0;
}

Output:

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