Greedy Algorithm 0/1 knapsack problem + read data structure Algorithm and Application-C ++ language description

Source: Internet
Author: User

Given a group of items, each item has its own weight and value. within the defined total weight, how can we choose to make the total price of the item the highest.

 

Solution: value density PI/wi (PI indicates the value of the item I, and WI indicates the weight of the item I) greedy algorithm. This selection criterion is: select the item with the highest PI/wi value from the remaining item.

 

0/1 the knapsack problem is an NP complex problem (NP complete problem, one of the seven major mathematical difficulties in the world. NP is a non-deterministic polynomial problem, that is, the non-deterministic problem of polynomial complexity ). For such problems, it is impossible to find an algorithm with polynomial time. Although loading an item in the order of PI/wi non-delivery (increment) subtraction cannot guarantee the optimal solution, it is an intuitive approximate solution. We hope it is a good heuristic algorithm, and most of the time it can be very close to the final algorithm.

 

Code Implementation (from http://www.cnblogs.com/gentleming/archive/2010/07/17/1779515.html ):

# Include <stdio. h>
# Include <iostream>
# Include <stdlib. h>
# Define maxsize 100 // assume the total number of objects
# Define M 15 // carrying capacity of the backpack
Using namespace STD;

// Algorithm core, Greedy Algorithm
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 ++) // prepare the output result.
{
X [I] = 0;
}
For (I = 0; I <n; I ++)
{
For (Int J = 0; j <n; j ++)
If (sortresult [J] = I + 1)
{
Temp = J; // obtain the object order.
Break;
}
If (W [temp]> C)
{
Break;
}
X [temp] = 1; // retrieve if appropriate
C-= W [temp]; // change the capacity accordingly.
}
If (I <= N) // fill the backpack
{
X [temp] = C/W [temp]; // retrieve part of an item
}
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 ++) // assign an initial value 0 to the ing Array
{
Sortresult [I] = 0;
}
For (I = 0; I <n; I ++)
{
Float temp = 0;
Index = I;
// Find the most cost-effective item and save the subscript
For (j = 0; j <n; j ++)
{
If (temp <X [J]) & (sortresult [J] = 0 ))
{
Temp = x [J];
Index = J;
}
}
// Mark W [I]
If (sortresult [Index] = 0)
{
Sortresult [Index] = ++ K;
}
}
Cout <" ing array sortresult:" <Endl;
For (I = 0; I <n; I ++)
Cout <sortresult [I] <"";
Return;
}

// Obtain all input information of this algorithm
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); // get data
For (I = 0; I <n; I ++)
{
X [I] = P [I]/W [I]; // obtain the unit weight value of each item.
}
Sort (x, sortresult, n); // get the ing array. The values in the array are marked in the order from large to small based on the unit weight of the item, so that the item can be retrieved easily.
Greedy (w, x, sortresult, n); // compare the items and total weights in the order marked by the ing Array
Output (x, N );

System ("pause ");
Return 0;
}

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.