背包问题 小灰
Prerequisites: Algorithm for fractional knapsack problem
先决条件: 分数背包问题算法
Here, we are discussing the practical implementation of the fractional knapsack problem. It can be solved using the greedy approach and in fractional knapsack problem, we can break items i.e we can take a fraction of an item. For examples, you can read this article first.
在这里,我们正在讨论小背包问题的实际实现 。 可以使用贪婪方法解决该问题,在小背包问题中,我们可以破坏物品,即可以取物品的一小部分。 例如,您可以先阅读本文 。
Problem statement: We have given items i1, i2, ..., in (item we want to put in our bag) with associated weights w1, w2, ..., wn and profit values P1 , P2 ,..., Pn. Now problem is how we can maximize the total benefit given capacity of bag is C?
问题陈述:我们给了项目i 1 , i 2 ,..., i n (我们要放入袋中的项目),其权重为w 1 , w 2 ,..., w n和利润值P 1 , P 2 ,..., P n 。 现在的问题是,在袋子容量为C的情况下, 如何才能使总收益最大化 ?
Algorithm:
算法:
Compute the profit per weight density for each item using the formula di = Pi / wi.
使用公式d i = P i / w i计算每个项目的每重量密度利润。
Sort each item by its profit per weight density.
按每重量密度的利润对每个项目进行排序。
Maximize the profit i.e Take as much as possible of the profit per weight density item not already in the bag.
最大化利润,即尽可能多地利用袋中尚未存在的每重量密度物品的利润。
分数背负问题的C ++实现 (C++ implementation of fractional knapsack problem)
#include <bits/stdc++.h>
using namespace std;
// Structure for an item
struct myItem
{
int itemNo;
int profit;
int weight;
float ppw; // profit per weight
};
// Comparison function to sort Item according to profit per weight ratio
bool cmp(struct myItem a, struct myItem b)
{
return a.ppw > b.ppw;
}
float fractionalKnapsack(int Capacity, struct myItem arr[], int n)
{
//calculating profit per weight ratio
for(int i=0;i<n;i++)
{
arr[i].ppw = ((float)arr[i].profit / arr[i].weight);
}
// sorting Item on basis of profit per weight ratio
sort(arr, arr + n, cmp);
cout<<"details of all items : \n";
cout<<"itemNo\t"<<"Profit\t"<<"Weight\t"<<"PPW\t"<<endl;
for (int i = 0; i < n; i++)
{
cout <<arr[i].itemNo<<"\t"<<arr[i].profit<<"\t"<<arr[i].weight<<"\t"<<((float)arr[i].ppw)<<endl;
}
cout<<endl;
float Max = 0.0; // Maximum profit
int i=0;
//take items until capacity becomes zero
while(Capacity > 0 && i<=n-1)
{
// if we can take all weights of item
if(Capacity >= arr[i].weight)
{
Max = Max + (float)arr[i].profit;
Capacity = Capacity - arr[i].weight;
}
// we can take only fraction of item
else
{
Max += (Capacity * arr[i].ppw);
Capacity = 0;
}
i++;
}
return Max;
}
// driver function
int main()
{
int C = 25; // Capacity of knapsack
myItem arr[] = { {1, 30, 10, 0}, {2, 20, 5, 0} , {3, 40, 15, 0}, {4, 36, 8, 0}};
int n = sizeof(arr) / sizeof(arr[0]);
cout<<"details of all items before sorting and without calculating PPW: \n";
cout<<"itemNo\t"<<"Profit\t"<<"Weight\t"<<"PPW\t"<<endl;
for (int i = 0; i < n; i++)
{
cout <<arr[i].itemNo<<"\t"<<arr[i].profit<<"\t"<<arr[i].weight<<"\t"<<((float)arr[i].ppw)<<endl;
}
cout<<endl;
cout << "Maximum profit we can obtain = "<< fractionalKnapsack(C, arr, n);
return 0;
}
Output
输出量
details of all items before sorting and without calculating PPW:
itemNo Profit Weight PPW
1 30 10 0
2 20 5 0
3 40 15 0
4 36 8 0
details of all items :
itemNo Profit Weight PPW
4 36 8 4.5
2 20 5 4
1 30 10 3
3 40 15 2.66667
Maximum profit we can obtain = 91.3333
翻译自: https://www.includehelp.com/icp/fractional-knapsack-problem.aspx
背包问题 小灰