0% found this document useful (0 votes)
13 views

Week 5

Uploaded by

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

Week 5

Uploaded by

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

John is a treasure hunter exploring an ancient temple rumoured to be filled with

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>

// Structure to represent an item


struct Item {
int value; // Value of the item
int weight; // Weight of the item
};

// Function to compare items based on their value per unit weight


int compareItems(const void *a, const void *b) {
double ratioA = ((struct Item *)a)->value / (double)((struct Item *)a)->weight;
double ratioB = ((struct Item *)b)->value / (double)((struct Item *)b)->weight;
if (ratioA > ratioB)
return -1; // Sort in descending order of value/weight ratio
else if (ratioA < ratioB)
return 1;
else
return 0;
}

// Function to calculate maximum value using fractional knapsack algorithm


double fractionalKnapsack(int capacity, struct Item items[], int n) {
// Sort items based on their value/weight ratio
qsort(items, n, sizeof(struct Item), compareItems);

double totalValue = 0.0; // Total value of items in the knapsack


int currentWeight = 0; // Current weight in the knapsack

// 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("Enter the capacity of the backpack: ");


scanf("%d", &capacity);

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


scanf("%d", &numItems);

struct Item items[numItems];

// Input items' values and weights


for (int i = 0; i < numItems; i++) {
printf("Enter value and weight of item %d: ", i + 1);
scanf("%d %d", &items[i].value, &items[i].weight);
}

double maxValue = fractionalKnapsack(capacity, items, 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>

// Structure to represent an item


struct Item {
double weight; // Weight of the item
double profit; // Profit of the item
};

// Function to compare items based on their profit per unit weight


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;
}

// Function to calculate maximum profit using fractional knapsack algorithm


double fractionalKnapsack(double capacity, struct Item items[], int n) {
// Sort items based on their profit/weight ratio
qsort(items, n, sizeof(struct Item), compareItems);

double totalProfit = 0.0; // Total profit of items in the knapsack


double currentWeight = 0.0; // Current weight in the knapsack

// 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;

printf("Enter the capacity of Raj's backpack: ");


scanf("%lf", &capacity);

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


scanf("%d", &numItems);

struct Item items[numItems];

// Input items' weights and profits


for (int i = 0; i < numItems; i++) {
printf("Enter weight and profit of item %d: ", i + 1);
scanf("%lf %lf", &items[i].weight, &items[i].profit);
}

double maxProfit = fractionalKnapsack(capacity, items, numItems);

printf("Maximum profit Raj can achieve: %.2f\n", maxProfit);

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>

// Structure to represent an item


struct Item {
double value; // Value of the item
double weight; // Weight of the item
};

// Function to compare items based on their value per unit weight


int compareItems(const void *a, const void *b) {
double ratioA = ((struct Item *)a)->value / ((struct Item *)a)->weight;
double ratioB = ((struct Item *)b)->value / ((struct Item *)b)->weight;
if (ratioA > ratioB)
return -1; // Sort in descending order of value/weight ratio
else if (ratioA < ratioB)
return 1;
else
return 0;
}

// Function to calculate maximum total value using fractional knapsack algorithm


double fractionalKnapsack(double capacity, struct Item items[], int n) {
// Sort items based on their value/weight ratio
qsort(items, n, sizeof(struct Item), compareItems);

double totalValue = 0.0; // Total value of items in the knapsack


double currentWeight = 0.0; // Current weight in the knapsack

// 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);
}

printf("Enter the capacity of Alice's backpack: ");


scanf("%lf", &capacity);

double maxTotalValue = fractionalKnapsack(capacity, items, 4);

printf("Maximum total value Alice can carry: %.2f\n", maxTotalValue);

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>

// Structure to represent an item


struct Item {
double value; // Value of the item
double weight; // Weight of the item
};

// Function to compare items based on their value per unit weight


int compareItems(const void *a, const void *b) {
double ratioA = ((struct Item *)a)->value / ((struct Item *)a)->weight;
double ratioB = ((struct Item *)b)->value / ((struct Item *)b)->weight;
if (ratioA > ratioB)
return -1; // Sort in descending order of value/weight ratio
else if (ratioA < ratioB)
return 1;
else
return 0;
}

// Function to calculate maximum total value using fractional knapsack algorithm


double fractionalKnapsack(double capacity, struct Item items[], int n) {
// Sort items based on their value/weight ratio
qsort(items, n, sizeof(struct Item), compareItems);

double totalValue = 0.0; // Total value of items in the knapsack


double currentWeight = 0.0; // Current weight in the knapsack

// 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;

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


scanf("%lf", &capacity);

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


scanf("%d", &numItems);

struct Item items[numItems];

// Input items' values and weights


for (int i = 0; i < numItems; i++) {
printf("Enter value and weight of item %d: ", i + 1);
scanf("%lf %lf", &items[i].value, &items[i].weight);
}

double maxTotalValue = fractionalKnapsack(capacity, items, numItems);

printf("Maximum total value that can be obtained: %.2f\n", maxTotalValue);

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>

// Structure to represent an item


struct Item {
double value; // Value of the item
double weight; // Weight of the item
double fraction; // Fraction of item to be included in the knapsack
};

// Function to compare items based on their value per unit weight


int compareItems(const void *a, const void *b) {
double ratioA = ((struct Item *)a)->value / ((struct Item *)a)->weight;
double ratioB = ((struct Item *)b)->value / ((struct Item *)b)->weight;
if (ratioA > ratioB)
return -1; // Sort in descending order of value/weight ratio
else if (ratioA < ratioB)
return 1;
else
return 0;
}

// Function to calculate fractions of items using fractional knapsack algorithm


void fractionalKnapsack(double capacity, struct Item items[], int n) {
// Sort items based on their value/weight ratio
qsort(items, n, sizeof(struct Item), compareItems);

double totalValue = 0.0; // Total value of items in the knapsack

// 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
}
}

// Print the fractions of items included in the knapsack


printf("Fractions of items to be included in the knapsack:\n");
for (int i = 0; i < n; i++) {
printf("Item %d: %.2f\n", i + 1, items[i].fraction);
}

printf("Maximum total value in the knapsack: %.2f\n", totalValue);


}

int main() {
int numItems;

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


scanf("%d", &numItems);

struct Item items[numItems];

// Input items' values and weights


for (int i = 0; i < numItems; i++) {
printf("Enter value and weight of item %d: ", i + 1);
scanf("%lf %lf", &items[i].value, &items[i].weight);
items[i].fraction = 0.0; // Initialize fraction to 0
}

double capacity;
printf("Enter the capacity of the knapsack: ");
scanf("%lf", &capacity);

fractionalKnapsack(capacity, items, numItems);

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>

// Structure to represent an object


struct Object {
double weight; // Weight of the object
double value; // Value of the object
};

// 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;
}

// Function to solve Fractional Knapsack Problem using greedy algorithm


void fractionalKnapsack(double capacity, struct Object objects[], int numObjects) {
// Sort objects based on their value/weight ratio
qsort(objects, numObjects, sizeof(struct Object), compareObjects);

double totalValue = 0.0; // Total value of objects in the knapsack


double currentWeight = 0.0; // Current weight in the knapsack

printf("Objects to be included in the knapsack (fractional):\n");

// 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
}
}

printf("Maximum total value that can be obtained: %.2f\n", totalValue);


}

int main() {
int numObjects;
double capacity;

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


scanf("%lf", &capacity);

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


scanf("%d", &numObjects);

struct Object objects[numObjects];

// Input objects' weights and values


for (int i = 0; i < numObjects; i++) {
printf("Enter weight and value of object %d: ", i + 1);
scanf("%lf %lf", &objects[i].weight, &objects[i].value);
}

fractionalKnapsack(capacity, objects, numObjects);

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>

// Structure to represent an item


struct Item {
double weight; // Weight of the item
double profit; // Profit of the item
double fraction; // Fraction of the item included in the knapsack
};

// 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;
}

// Function to solve fractional knapsack problem using Knapsack algorithm


void fractionalKnapsack(double capacity, struct Item items[], int numItems) {
// Sort items based on their profit/weight ratio
qsort(items, numItems, sizeof(struct Item), compareItems);

double totalProfit = 0.0; // Total profit of items in the knapsack


double currentWeight = 0.0; // Current weight in the knapsack

// 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
}
}

printf("Items to be included in the knapsack (fractional):\n");


for (int i = 0; i < numItems; i++) {
if (items[i].fraction > 0) {
printf("Item %d: Fraction %.2f\n", i + 1, items[i].fraction);
}
}

printf("Maximum total profit that can be obtained: %.2f\n", totalProfit);


}

int main() {
int numItems;
double capacity;

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


scanf("%lf", &capacity);

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


scanf("%d", &numItems);

struct Item items[numItems];

// Input items' weights and profits


for (int i = 0; i < numItems; i++) {
printf("Enter weight and profit of item %d: ", i + 1);
scanf("%lf %lf", &items[i].weight, &items[i].profit);
items[i].fraction = 0.0; // Initialize fraction to 0
}

fractionalKnapsack(capacity, items, numItems);

return 0;
}

You might also like