Fool notes --- Dynamic Programming (three) - knapsack problem (a)

Description of the problem: There are a number of items, each item has two attributes, namely weight (w), value (v), now has a capacity of G backpack, the backpack selected items into items reaches the maximum value, and the corresponding output value of items or item by weight.

Example: w = {0,2,3,4,5}, v = {0,3,4,5,6}, G = 8 

The process items into the backpack, the process will be placed after each item into the stage of an impact, this is a phased process, and each item is placed in a smaller backpack capacity a solution, both sub-problems of the original problem, the solution to these records and integrate the optimal solution to the original problem.

So the problem can be solved by dynamic programming, dynamic programming since then, then you need to construct an array of table to record the status of each phase.

Here, let dp [i, j] represents the total value of the backpack, then the meaning represented by i, j need to consider. In this problem, there is a critical property: backpack capacity, the capacity can replace knapsack i, j where a variable. Another variable is undoubtedly a number of items as an array.

So now provides: dp [i, j] = backpack value where i = the i items, j = backpack remaining capacity, w [i] = weight of i-th items of, v [i] = the value of the i-th items of

The first problem now is that i is not installed in the end items into a bag, then the state transition equation can be obtained:

 

dp [i, j] = 1, dp [i-1] [j] w [i]> j (not into the backpack) 

    2, max (dp [i-1, j], dp [i-1, jw [i]] + w [i]) into the backpack

 

1, and charged into the backpack that the condition is not enough space charged

2, if space is available, you can choose a backpack loaded, then we should pay attention to these items into the backpack value in the end is not the most valuable thing that is, the goods are loaded but not the value of the item does not come loaded high size because you are not only a waste of space and maximum value, then we must compare the load and do not load the goods when the goods

 

The following is the full text of the code:

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 背包问题_1_
 8 {
 9     class Program
10     {
11         public static int G = 8;   // 容量
12         public static int []w = { 0, 2, 3, 4, 5 ,};     // item weight 
13 is          public  static  int [] V = { 0 , . 3 , . 4 , 5 , . 6 };     // Value 
14          public  static  int [,] DP = new new  int [w.Length, G + . 1 ];
 15          public  static  int [,] = FDP new new  int [w.Length, G + . 1 ];
 16          static  void the Main ( String [] args)
 . 17         {
18             DP();
19             for(int i=0;i<w.Length;++i)
20             {
21                 for(int j=0;j<=G;++j)
22                 {
23                     Console.Write(dp[i, j]);
24                 }
25                 Console.Write("\n");
26             }
27             findDP(4,8);
28             Console.ReadKey();
29         }
30 
31         public static void findDP(int i,int j)
32         {
33             if (i == 0 || j == 0) return;
34             else if (fdp[i,j] == 1)
35             {
36                 findDP(i - 1, j - w[i]);
37                 Console.WriteLine(v[i]);
38             }
39             else
40             {
41                 findDP(i - 1, j);
42             }
43         }
44 
45         public  static int [,] DP()
46         {
47             for (int i = 1;i<w.Length;++i)
48             {
49                 for(int j = 1;j<=G;j++)
50                 {
51                     if (i == 0 || j == 0)
52                         dp[i, j] = 0;
53                     else if (j<w[i])
54                     {
55                         dp[i, j] = dp[i - 1, j];
56                     }
57                     else
58                     {
59                         dp[i, j] = Math.Max(dp[i - 1, j], dp[i - 1, j - w[i]] + v[i]);
60                         if (dp[i - 1, j] <= dp[i - 1, j - w[i]] + v[i])
61                         {
62                             fdp[i, j] = 1;
63                         }
64                     }
65                 }
66             }
67             return dp;
68         }
69     }
70 }

 

 

Complete the filling process are:

 1  public  static int [,] DP()
 2         {
 3             for (int i = 1;i<w.Length;++i)
 4             {
 5                 for(int j = 1;j<=G;j++)
 6                 {
 7                     if (i == 0 || j == 0)
 8                         dp[i, j] = 0;
 9                     else if (j<w[i])
10                     {
11                         dp[i, j] = dp[i - 1, j];
12                     }
13                     else
14                     {
15                         dp[i, j] = Math.Max(dp[i - 1, j], dp[i - 1, j - w[i]] + v[i]);
16                         if (dp[i - 1, j] <= dp[i - 1, j - w[i]] + v[i])
17                         {
18                             fdp[i, j] = 1;
19                         }
20                     }
21                 }
22             }
23             return dp;
24         }

 

Where DP [] is responsible for recording, fdp [] is responsible for recording the items into the backpack

Output value of the charged article fragments:

 1 public static void findDP(int i,int j)
 2         {
 3             if (i == 0 || j == 0) return;
 4             else if (fdp[i,j] == 1)
 5             {
 6                 findDP(i - 1, j - w[i]);
 7                 Console.WriteLine(v[i]);
 8             }
 9             else
10             {
11                 findDP(i - 1, j);
12             }
13         }

Similarly, the longest and longest substring and subsequence as backtracking back on a state from the current state.

 

Guess you like

Origin www.cnblogs.com/CHANGKTITI/p/11233589.html