貪婪演算法之0/1背包問題+讀資料結構演算法與應用-C++語言描述

來源:互聯網
上載者:User

給定一組物品,每種物品都有自己的重量和價值,在限定的總重量內,我們如何選擇,才能使得物品的總價格最高。

 

解決方案:價值密度Pi / Wi(Pi表示第i件物品的價值,Wi表示第i物品的重量)貪婪演算法,這種選擇準則為:從剩餘物品中選擇可裝入包的Pi / Wi值最大的物品。

 

0/1背包問題是一個NP複雜問題(NP完全問題,是世界七大數學難題之一。 NP的英文全稱是Non-deterministic Polynomial的問題,即多項式複雜程度的非確定性問題)。對於這類問題,也許根本就不可能找到具有多項式時間的演算法。雖然按Pi/Wi非遞(增)減的次序裝入物品不能保證得到最優解,但它是一個直覺上近似的解。我們希望它是一個好的啟發學習法演算法,且大多數時候能很好地接近最後演算法。

 

代碼實現(摘自http://www.cnblogs.com/gentleming/archive/2010/07/17/1779515.html):

#include <stdio.h>
#include <iostream>
#include<stdlib.h>
#define MAXSIZE 100 //假設物體總數
#define M 15 //背包的載荷能力
using namespace std;

//演算法核心,貪心演算法
void GREEDY(float w[], float x[], int sortResult[], int n)
{   
 float c = M;   
 int i = 0;   
 int temp = 0;   
 for (i = 0; i < n; i++)//準備輸出結果   
 {       
  x[i] = 0;   
 }   
 for (i = 0; i < n; i++)   
 {      
  for(int j=0;j<n;j++)           
   if(sortResult[j]==i+1)           
   {               
    temp = j;//得到取物體的順序               
    break;           
   }      
   if (w[temp] > c)      
   {           
    break;       
   }       
   x[temp] = 1;//若合適則取出       
   c -= w[temp];//將容量相應的改變   
 }   
 if (i <= n)//使背包充滿   
 {       
  x[temp] = c / w[temp];//取某件物品的一部分  
 }   
 return;
}

void sort(float x[], int sortResult[], int n)
{   
 int i = 0, j = 0;  
 int index = 0, k = 0;   
 for (i = 0; i < n; i++)//對映射數組賦初值0   
 {      
  sortResult[i] = 0;  
 }   
 for (i = 0; i < n; i++)   
 {      
  float temp = 0;       
  index = i;       
  //找到性價比最高的商品,並儲存下標       
  for (j = 0; j < n; j++)       
  {           
   if ((temp < x[j]) && (sortResult[j] == 0))           
   {               
    temp = x[j];               
    index = j;           
   }       
  }       
  //對w[i]作標記           
  if (sortResult[index] == 0)           
  {               
   sortResult[index] = ++k;           
  }  
 }  
 cout<<"映射數組sortResult:"<<endl;   
 for (i = 0; i < n; i++)      
  cout<<sortResult[i]<<"  ";
 return;
}

//得到本演算法的所有輸入資訊
void getData(float p[], float w[], int *n)
{   
 int i = 0;   
 printf("please input the total count of object: ");   
 scanf("%d", n);  
 printf("Please input array of p :\n"); 
 for (i = 0; i < (*n); i++)   
 {       
  scanf("%f", &p[i]);   
 }   
 printf("Now please input array of w :\n");   
 for (i = 0; i < (*n); i++)   
 {      
  scanf("%f", &w[i]);   
 }   
 return;
}

void output(float x[], int n)
{  
 int i;   
 printf("\n\nafter arithmetic data: advise method\n");   
 for (i = 0; i < n; i++)   
 {
  printf("x[%d]\t", i);   
 }   
 printf("\n");   
 for (i = 0; i < n; i++)   
 {      
  printf("%2.3f\t", x[i]);  
 }   
 return;
}

int main()
{   
 float p[MAXSIZE], w[MAXSIZE], x[MAXSIZE];   
 int i = 0, n = 0;   
 int sortResult[MAXSIZE];   
 getData(p, w, &n); //擷取資料   
 for (i = 0; i < n; i++)   
 {       
  x[i] = p[i] / w[i];//得到每件物品的單位重量的價值   
 }   
 sort(x, sortResult, n);//得到映射數組,數組中按照物品單位重量的價值從大到小的順序做了標記,方便取物品  
 GREEDY(w, x, sortResult, n);//按照映射數組標記的順序取物品,和總重量比較   
 output(x, n);   

 system("PAUSE");
 return 0;
}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.