0-1 knapsack problem of algorithm design

Source: Internet
Author: User

1. Topic Analysis:

Considering that there are only 2 options for each item, that is, loading the backpack or not loading the backpack, and the number of items and the backpack capacity is given, to calculate the maximum value of the loaded backpack items and the optimal loading scheme, the backtracking method can be used to search the subset tree algorithm to solve.

2. Algorithm design:
A. There are n kinds of goods, backpack capacity C, respectively, using p[i] and W[i] storage of the value and weight of the article I, with
X[i] Mark the item I is loaded into the backpack, with Bestx[i] storage of the first item of the optimal loading scheme;
B. Using recursive function backtrack (I,CP,CW) to implement backtracking search subset tree (formal parameter i means recursive depth
Degrees, n is used to control the recursion depth, the form parameters CP and CW represent the current total value and the total weight, BESTP represents the current
Best total value):
① If I >n, the algorithm searches for a leaf node to determine whether the current total value is optimal:
1> if CP>BESTP, update the current optimal total value for the current total value (i.e. BESTP=CP), update
Loading scheme (i.e. Bestx[i]=x[i] (1≤i≤n));
The ② uses a for loop to discuss item I and no two cases (0≤j≤1):
1> x[i]=j;
2> If the total weight is not greater than the backpack capacity (i.e. cw+x[i]*w[i]<=c), update the current total price br= "" > Value and total weight (i.e. cw+=w[i]*x[i],cp+=p[i]*x[i]), the item i+1 call recursive letter
The number of backtrack (I+1,CP,CW) continues to load;
3> function Backtrack (I+1,CP,CW) returns the current total value and total weight after the call is completed
(i.e. cw-=w[i]*x[i],cp-=p[i]*x[i]);
4> when j>1, the For loop ends;
③ when the I=1, if all the loading scheme has been tested, the outer call will all end;
C. The main function calls once Backtrack (1,0,0) can complete the whole backtracking search process, the resulting BESTP and bestx[i] is the maximum total value and the optimal load scheme.
Finally, the code is given.

#include <iostream>
#include <stdlib.h>
#include <stdio.h
int N,C,BESTP;
int p[10000],w[10000],x[10000],bestx[10000];

void Backtrack (int i,int cp,int CW)
{
Int J;
if (i>n)
{
if (CP>BESTP)
{
BESTP=CP;
for (i=0;i<=n;i++) bestx[i]=x[i];
}
}
Else
for (j=0;j<=1;j++)
{
X[i]=j;
if (cw+x[i]*w[i]<=c)
{
Cw+=w[i]*x[i];
Cp+=p[i]*x[i];
Backtrack (I+1,CP,CW);
Cw-=w[i]*x[i];
Cp-=p[i]*x[i];
}
}
}

int main ()
{
int i;
Bestp=0;
printf ("Please enter backpack maximum capacity: \ n");
scanf ("%d", &c);
printf ("Please enter the number of items: \ n");
scanf ("%d", &n);
printf ("Please enter the weight of the item in turn: \ n");
for (i=1;i<=n;i++)
scanf ("%d", &w[i]);
printf ("Please enter the value of the item in turn: \ n");
for (i=1;i<=n;i++)
scanf ("%d", &p[i]);
Backtrack (1,0,0);
printf ("Maximum value: \ n");
printf ("%d\n", BESTP);
printf ("The selected items are in order (0 means unchecked, 1 is selected) \ n");
for (i=1;i<=n;i++)
printf ("%d", bestx[i]);
printf ("\ n");
return 0;
}

0-1 knapsack problem of algorithm design

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.