
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Solve the Fractional Knapsack Problem
In Fractional knapsack problem, a set of items are given, each with a weight and a value. We need to break items for maximizing the total value of knapsack and this can be done in greedy approach.
Algorithm
Begin Take an array of structure Item Declare value, weight, knapsack weight and density Calculate density=value/weight for each item Sorting the items array on the order of decreasing density We add values from the top of the array to total value until the bag is full, i.e; total value <= W End
Example Code
#include <iostream> #include <bits/stdc++.h> using namespace std; typedef struct { int v; int w; float d; } Item; void input(Item items[],int sizeOfItems) { cout << "Enter total "<< sizeOfItems <<" item's values and weight" << endl; for(int i = 0; i < sizeOfItems; i++) { cout << "Enter "<< i+1 << " V "; cin >> items[i].v; cout << "Enter "<< i+1 << " W "; cin >> items[i].w; } } void display(Item items[], int sizeOfItems) { int i; cout << "values: "; for(i = 0; i < sizeOfItems; i++) { cout << items[i].v << "\t"; } cout << endl << "weight: "; for (i = 0; i < sizeOfItems; i++) { cout << items[i].w << "\t"; } cout << endl; } bool compare(Item i1, Item i2) { return (i1.d > i2.d); } float knapsack(Item items[], int sizeOfItems, int W) { int i, j; float totalValue = 0, totalWeight = 0; for (i = 0; i < sizeOfItems; i++) { items[i].d = (float)items[i].v / items[i].w; //typecasting done (v is int and w is also int so we get final value of d as int) } sort(items, items+sizeOfItems, compare); /* uncomment if u need to check the data after sortingis done cout << "values : "; for(i = 0; i < sizeOfItems; i++) { cout << items[i].v << "\t"; } cout << endl << "weights: "; for (i = 0; i < sizeOfItems; i++) { cout << items[i].w << "\t"; } cout << endl << "ratio : "; for (i = 0; i < sizeOfItems; i++) { cout << items[i].d << "\t"; } cout << endl; */ for(i=0; i<sizeOfItems; i++) { if(totalWeight + items[i].w<= W) { totalValue += items[i].v ; totalWeight += items[i].w; } else { int wt = W-totalWeight; totalValue += (wt * items[i].d); totalWeight += wt; break; } } cout << "Total weight in bag " << totalWeight<<endl; return totalValue; } int main() { int W; Item items[4]; input(items, 4); cout << "Entered data \n"; display(items,4); cout<< "Enter Knapsack weight \n"; cin >> W; float mxVal = knapsack(items, 4, W); cout << "Max value for "<< W <<" weight is "<< mxVal; }
Output
Enter total 4 item's values and weight Enter 1 V Enter 1 W Enter 2 V Enter 2 W Enter 3 V Enter 3 W Enter 4 V Enter 4 W Entered data values: 2 0 0 0 weight: 0 490642064 0 4196544 Enter Knapsack weight Total weight in bag 0 Max value for 0 weight is 2
- Related Articles
- Fractional Knapsack Problem
- Program to implement the fractional knapsack problem in Python
- C++ Program to Solve the 0-1 Knapsack Problem
- C++ Program to Solve Knapsack Problem Using Dynamic Programming
- Python Program for 0-1 Knapsack Problem
- 0-1 Knapsack Problem in C?
- C++ Program to Solve the Dominating Set Problem
- C++ Program to Solve N-Queen Problem
- Write a C# program to solve FizzBuzz problem
- Program to find maximum value we can get in knapsack problem by taking multiple copies in Python
- C++ Program to Solve Travelling Salesman Problem for Unweighted Graph
- Python Program to solve Maximum Subarray Problem using Kadane’s Algorithm
- C++ program to Solve Tower of Hanoi Problem using Binary Value
- Python Program to solve Maximum Subarray Problem using Divide and Conquer
- C++ Program to Solve a Matching Problem for a Given Specific Case

Advertisements