Week 5
Week 5
valuable
artifacts. Inside the temple, he finds a collection of items, each with its own
value and weight.
However, John's backpack has limited capacity, and he wants to maximize the total
value of
items he can carry out of the temple.
Write a program to help John determine the maximum total value of items he can
carry out of the
temple using the fractional knapsack algorithm.
#include <stdio.h>
#include <stdlib.h>
// Loop through items and add them to the knapsack until capacity is reached
for (int i = 0; i < n; i++) {
if (currentWeight + items[i].weight <= capacity) {
// Add the whole item if it fits in the knapsack
currentWeight += items[i].weight;
totalValue += items[i].value;
} else {
// Add a fraction of the item to fill the knapsack
int remainingCapacity = capacity - currentWeight;
totalValue += (items[i].value / (double)items[i].weight) *
remainingCapacity;
break; // Knapsack is full
}
}
return totalValue;
}
int main() {
int capacity, numItems;
printf("Maximum value John can carry out of the temple: %.2f\n", maxValue);
return 0;
}
Raj is a keen entrepreneur who is venturing into the business of selling items. He
has a
limited capacity backpack and wants to maximize his profit by selecting items to
carry in his
backpack. Each item has a weight and a corresponding profit. However, Raj can carry
fractions
of items to maximize his profit within the capacity of his backpack.
Write a program to help Raj determine the maximum profit he can achieve by using
the
fractional knapsack algorithm.
#include <stdio.h>
#include <stdlib.h>
// Loop through items and add them to the knapsack until capacity is reached
for (int i = 0; i < n; i++) {
if (currentWeight + items[i].weight <= capacity) {
// Add the whole item if it fits in the knapsack
currentWeight += items[i].weight;
totalProfit += items[i].profit;
} else {
// Add a fraction of the item to fill the knapsack
double remainingCapacity = capacity - currentWeight;
totalProfit += (items[i].profit / items[i].weight) * remainingCapacity;
break; // Knapsack is full
}
}
return totalProfit;
}
int main() {
double capacity;
int numItems;
return 0;
}
Alice wants to solve the fractional knapsack problem using a program.
She has a fixed list of 4 items, each with a certain value (v) and weight (w).
Alice's backpack has
a limited weight capacity (W).
The objective is to implement a program that determines the maximum total value of
items Alice
can carry in her backpack.
#include <stdio.h>
#include <stdlib.h>
// Loop through items and add them to the knapsack until capacity is reached
for (int i = 0; i < n; i++) {
if (currentWeight + items[i].weight <= capacity) {
// Add the whole item if it fits in the knapsack
currentWeight += items[i].weight;
totalValue += items[i].value;
} else {
// Add a fraction of the item to fill the knapsack
double remainingCapacity = capacity - currentWeight;
totalValue += (items[i].value / items[i].weight) * remainingCapacity;
break; // Knapsack is full
}
}
return totalValue;
}
int main() {
double capacity;
struct Item items[4]; // Fixed list of 4 items
// Input items' values and weights
for (int i = 0; i < 4; i++) {
printf("Enter value and weight of item %d: ", i + 1);
scanf("%lf %lf", &items[i].value, &items[i].weight);
}
return 0;
}
You are given a set of items, each with a specific weight and value. Your task is
to determine
the maximum value that can be obtained by selecting a combination of these items,
considering a
knapsack with a limited capacity. You are allowed to take fractions of items into
the knapsack.
Write a program to implement the fractional knapsack problem using the provided
algorithm.
#include <stdio.h>
#include <stdlib.h>
// Loop through items and add them to the knapsack until capacity is reached
for (int i = 0; i < n; i++) {
if (currentWeight + items[i].weight <= capacity) {
// Add the whole item if it fits in the knapsack
currentWeight += items[i].weight;
totalValue += items[i].value;
} else {
// Add a fraction of the item to fill the knapsack
double remainingCapacity = capacity - currentWeight;
totalValue += (items[i].value / items[i].weight) * remainingCapacity;
break; // Knapsack is full
}
}
return totalValue;
}
int main() {
double capacity;
int numItems;
return 0;
}
Josh is learning about the fractional knapsack problem. Given a set of items, each
with a
value and weight, along with a knapsack with a maximum capacity, he wants to
maximize the
total value of items he can fit into the knapsack. Unlike 0/1 knapsack, in
fractional knapsack, he
can take fractions of items.
Help Josh by writing a program that takes the number of items, their values,
weights, and the
capacity of the knapsack as input, and outputs the fractions of each item to be
included in the
knapsack to maximize its value.
#include <stdio.h>
#include <stdlib.h>
// Loop through items and add them to the knapsack until capacity is reached
for (int i = 0; i < n; i++) {
if (capacity >= items[i].weight) {
// Add the whole item if it fits in the knapsack
capacity -= items[i].weight;
items[i].fraction = 1.0;
totalValue += items[i].value;
} else {
// Add a fraction of the item to fill the remaining capacity
items[i].fraction = capacity / items[i].weight;
totalValue += items[i].value * items[i].fraction;
break; // Knapsack is full
}
}
int main() {
int numItems;
double capacity;
printf("Enter the capacity of the knapsack: ");
scanf("%lf", &capacity);
return 0;
}
You are given a knapsack with a maximum weight capacity W and n objects. Each
object i
has a weight c[i] and a value v[i]. Your task is to fill the knapsack with objects
in such a way that
the total value of the objects in the knapsack is maximized without exceeding the
weight
capacity.
Implement a program to solve this problem using a simple greedy algorithm known as
the
Fractional Knapsack Problem.
#include <stdio.h>
#include <stdlib.h>
// Function to compare objects based on their value per unit weight (ratio)
int compareObjects(const void *a, const void *b) {
double ratioA = ((struct Object *)a)->value / ((struct Object *)a)->weight;
double ratioB = ((struct Object *)b)->value / ((struct Object *)b)->weight;
if (ratioA > ratioB)
return -1; // Sort in descending order of value/weight ratio
else if (ratioA < ratioB)
return 1;
else
return 0;
}
// Loop through objects and add them to the knapsack until capacity is reached
for (int i = 0; i < numObjects; i++) {
if (currentWeight + objects[i].weight <= capacity) {
// Add the whole object if it fits in the knapsack
currentWeight += objects[i].weight;
totalValue += objects[i].value;
printf("Object %d: Fraction 1\n", i + 1);
} else {
// Add a fraction of the object to fill the knapsack
double remainingCapacity = capacity - currentWeight;
totalValue += (objects[i].value / objects[i].weight) *
remainingCapacity;
printf("Object %d: Fraction %.2f\n", i + 1, remainingCapacity /
objects[i].weight);
break; // Knapsack is full
}
}
int main() {
int numObjects;
double capacity;
return 0;
}
Implement the Knapsack algorithm to solve the fractional knapsack problem. Given a
set of
items with their respective weights and profits, as well as a knapsack capacity,
maximize the
total profit by selecting fractions of items to fit within the capacity of the
knapsack. The
objective is to fill the knapsack with items in such a way that the total profit is
maximized,
allowing fractional portions of items to be included.
#include <stdio.h>
#include <stdlib.h>
// Function to compare items based on their profit per unit weight (ratio)
int compareItems(const void *a, const void *b) {
double ratioA = ((struct Item *)a)->profit / ((struct Item *)a)->weight;
double ratioB = ((struct Item *)b)->profit / ((struct Item *)b)->weight;
if (ratioA > ratioB)
return -1; // Sort in descending order of profit/weight ratio
else if (ratioA < ratioB)
return 1;
else
return 0;
}
// Loop through items and add fractions of them to the knapsack until capacity
is reached
for (int i = 0; i < numItems; i++) {
if (currentWeight + items[i].weight <= capacity) {
// Add the whole item if it fits in the knapsack
items[i].fraction = 1.0;
currentWeight += items[i].weight;
totalProfit += items[i].profit;
} else {
// Add a fraction of the item to fill the knapsack
double remainingCapacity = capacity - currentWeight;
items[i].fraction = remainingCapacity / items[i].weight;
totalProfit += items[i].profit * items[i].fraction;
break; // Knapsack is full
}
}
int main() {
int numItems;
double capacity;
return 0;
}