背包问题 小灰

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 1i 2 ,..., i n (我们要放入袋中的项目),其权重为w 1w 2 ,..., w n和利润值P 1P 2 ,..., P n 。 现在的问题是,在袋子容量为C的情况下, 如何才能使总收益最大化

Algorithm:

算法:

  1. Compute the profit per weight density for each item using the formula di = Pi / wi.

    使用公式d i = P i / w i计算每个项目的每重量密度利润。

  2. Sort each item by its profit per weight density.

    按每重量密度的利润对每个项目进行排序。

  3. 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

背包问题 小灰

背包问题 小灰_小背包问题相关推荐

  1. java01背包问题算法_经典动态规划--01背包问题

    背包问题具体例子:假设现有容量10kg的背包,另外有3个物品,分别为a1,a2,a3.物品a1重量为3kg,价值为4:物品a2重量为4kg,价值为5:物品a3重量为5kg,价值为6.将哪些物品放入背包 ...

  2. java实现背包问题例子_动态规划(背包问题)java实现

    背包问题(Knapsack problem)是一种组合优化的NP完全问题.问题可以描述为:给定一组物品,每种物品都有自己的重量和价格,在限定的总重量内,我们如何选择,才能使得物品的总价格最高.问题的名 ...

  3. java 完全背包问题算法_算法笔记(c++)--完全背包问题

    算法笔记(c++)--完全背包和多重背包问题 完全背包 完全背包不同于01背包-完全背包里面的东西数量无限 假设现在有5种物品重量为5,4,3,2,1 价值为1,2,3,4,5 背包容量为10 #in ...

  4. java背包问题程序_背包问题-java实现

    背包问题 背包问题是计算机科学里的经典问题.在最简单的形式中,包括试图将不同重量的数据项放到 背包中.以使背包最后达到指定的总重量.不需要把所有的选项都放入背包中. 举例来说,假设想要背包精确地承重2 ...

  5. o-1背包问题迭代_经典动态规划:01背包问题的变体

    点击上方蓝字设为星标 东哥带你手把手撕力扣~ 作者:labuladong   公众号:labuladong 若已授权白名单也必须保留以上来源信息 上篇文章 经典动态规划:0-1 背包问题 详解了通用的 ...

  6. 华为校招机试 - 攻城战(Java JS Python)

    目录 题目描述 输入描述 输出描述 用例 题目解析 JavaScript算法源码 Java算法源码 Python算法源码 题目描述 一支攻城部队, 有若干种大炮各座, 以及数量有限的火药,每种大炮的威 ...

  7. 变种 背包问题_动态规划入门——传说中的零一背包问题

    今天是周三算法与数据结构专题的第12篇文章,动态规划之零一背包问题.在之前的文章当中,我们一起探讨了二分.贪心.排序和搜索算法,今天我们来看另一个非常经典的算法--动态规划.在acm-icpc竞赛领域 ...

  8. 变种 背包问题_【朝夕的ACM笔记】动态规划-背包问题

    [朝夕的ACM笔记]目录与索引 背包问题 一.0/1背包 1.1 问题描述 有 件物品和一个大小为 的背包,以及若干物品,每种物品只有一件,大小分别为 ,其价值分别为 .问题:将哪些物品装入背包,可使 ...

  9. 用贪婪算法解决背包问题_解决主要算法问题的贪婪策略

    用贪婪算法解决背包问题 Introduction: 介绍: Let's start the discussion with an example that will help to understan ...

最新文章

  1. 一个WEB***的处理过程
  2. 蛋疼的mocha库-promise异步测试
  3. 数据结构--插入排序
  4. 2013 javaB3 振兴中华、从我做起
  5. python变量类型是集合_python基础-基本数据类型:集合
  6. linux-shell命令之chown(change owner)【更改拥有者】
  7. C语言 字符串转结构体,字符串指针转化为结构体指针!
  8. 连麦互动技术及其连麦调研
  9. SOLIDWORDS API修改零部件属性全部保存
  10. 考研:无穷小微积分的不适症
  11. 微信小程序实现图片懒加载
  12. 鸿蒙系统和安装包,鸿蒙系统安装包
  13. python宣传视频 抖音_python下载抖音无水印视频
  14. java qlv转mp4 代码_怎么将qlv格式转换成mp4?教你快速转换视频格式的技巧
  15. 广播电台常用51首背景音乐~甘醇永久
  16. 【LTE】Qualcomm LTE Packets log 分析(二)LTE Access Stratum Log Analysis 3_SRB1 4_UL_DATA_Tran
  17. [SSD核心技术:FTL 13] 不求同生,但求同死?固态硬盘闪存磨损均衡技术详解
  18. python 爬虫及数据可视化展示
  19. 从应用到平台,云服务架构的演进过程
  20. 【Css】使用float:left浮动后,导致后面div高度“塌陷”的解决办法(示例和图示)

热门文章

  1. Bash脚本教程之命令提示符
  2. uniapp /deep/设置uni-app组件样式时 h5生效 小程序失效问题解决
  3. angular 拼接html 事件无效
  4. 只需5步,轻松创建HTML5离线应用
  5. Android IPC系列(一):AIDL使用详解
  6. 教务管理及教材订购系统设计文档
  7. JavaWeb学习中的小问题
  8. node--更新数据库问题
  9. C# datetime 操作
  10. Linux中后台执行scp