贪心算法一:最优装载问题


  1.基本思想:

  贪心算法是通过一系列的选择来得到问题的解,它所做的选择都是当前情况下最优的选择,即贪心算法并不考虑整体最优,而考虑的是当前情况下的局部最优,即贪心选择。

  2.贪心算法的两个性质:

  1)贪心选择性质:所求解的问题的整体最优解可以通过一系列局部最优的选择来,即贪心选择达到。贪心选择所依赖的是以前所做过的选择,而对以后所做的选择没有关系。

  2)最优子结构性质:一个问题的最优解包含其子问题的最优解。

  3.贪心算法与动态规划的区别:

  动态规划是通过自底向上的方式解决子问题,贪心算法是通过自顶向下的迭代方式做出贪心选择,求解问题的最优解。两共同点是都具有最优子结构性质。

  4.最优装载问题:采用重量最轻者先装载的贪心选择策略。

  

 1 #include "stdafx.h"  
 2 #include <iostream>   
 3 using namespace std;
 4 const int N = 4;
 5 template <class type>
 6 void Swap(type &x, type &y){
 7     type temp = x;
 8     x = y;
 9     y = temp;
10 }
11 void BubleSort(int w[],int t[], int n){
12     for (int i = 1; i <= n; i++)
13     {
14         t[i] = i;
15     }
16 
17     for  (int i = 1;  i < n; i++)
18     {
19         int temp = i;
20         for (int j =i+1; j <= n; j++){
21             if (w[temp]>w[j])
22             {
23                 temp = j;
24                 Swap(w[i], w[j]);
25             }    
26         }
27         Swap(t[i], t[temp]);
28     }
29 }
30 void BestLoad(int w[], int x[],int c, int n){
31     
32     int t[N + 1] = {0};//记录原始索引
33     BubleSort(w, t, N);
34     for (int i = 1; i <= n; i++)
35     {
36         x[i] = 0;
37     }
38     for (int i = 1; i <= n&& w[t[i]] <= c; i++)
39     {
40         x[t[i]] = 1;
41         c -= w[t[i]];
42     }
43 }
44 int main(){
45     int c = 50;
46     int w[] = { 0, 20,10,25,15 };
47     int x[N + 1];
48     cout << "载重容量为:\n" << c << endl;
49     cout << "物品的重量分别为:" << endl;
50     for (int i = 1; i <= N; i++)
51     {
52         cout << w[i] << " ";
53     }
54     cout << endl;
55     BestLoad(w,x, c, N);
56     cout << "贪心选择结果为:" << endl;
57     for (int i = 1; i <= N; i++)
58     {
59         cout << x[i] << " ";
60     }
61     cout << endl;
62 }

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
贪心算法是一种常用的解决最优装载问题算法。在Java中,可以使用贪心算法来解决最优装载问题最优装载问题是指在给定一组物品和一个容量限制的背包时,如何选择物品放入背包中,使得背包的总价值最大化。贪心算法的思想是每次选择当前最优的物品放入背包中,直到背包无法再放入更多物品为止。 以下是Java中贪心算法解决最优装载问题的基本步骤: 1. 定义一个物品类,包含物品的重量和价值属性。 2. 根据物品的价值重量比进行排序,从大到小排序。 3. 初始化背包的容量和总价值为0。 4. 遍历排序后的物品列表,依次将物品放入背包中,直到背包无法再放入更多物品或者所有物品都已经放入背包为止。 5. 返回背包的总价值作为最优解。 下面是一个简单的Java代码示例: ```java import java.util.Arrays; class Item implements Comparable<Item> { int weight; int value; public Item(int weight, int value) { this.weight = weight; this.value = value; } @Override public int compareTo(Item other) { double ratio1 = (double) this.value / this.weight; double ratio2 = (double) other.value / other.weight; if (ratio1 > ratio2) { return -1; } else if (ratio1 < ratio2) { return 1; } else { return 0; } } } public class GreedyAlgorithm { public static int knapsack(Item[] items, int capacity) { Arrays.sort(items); int totalValue = 0; int currentWeight = 0; for (Item item : items) { if (currentWeight + item.weight <= capacity) { currentWeight += item.weight; totalValue += item.value; } else { int remainingCapacity = capacity - currentWeight; totalValue += item.value * ((double) remainingCapacity / item.weight); break; } } return totalValue; } public static void main(String[] args) { Item[] items = {new Item(10, 60), new Item(20, 100), new Item(30, 120)}; int capacity = 50; int maxValue = knapsack(items, capacity); System.out.println("最优装载问题的最大价值为:" + maxValue); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若♡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值