# 14.3 dynamic programming algorithm

## 14.3.1 introduction to dynamic programming algorithm

- The core idea of dynamic programming algorithm is to divide large problems into small problems to solve, so as to obtain the optimal solution step by step.
- Dynamic programming algorithm is similar to divide and conquer algorithm. Its basic idea is to decompose the problem to be solved into several subproblems. First solve the subproblems, and then get the solution of the original problem from the solutions of these subproblems.
- Different from the divide and conquer method, the sub problems obtained by decomposition are often not independent of each other. (that is, the solution of the next sub stage is based on the solution of the previous sub stage for further solution)
- Dynamic programming can be advanced step by step by filling in tables to obtain the optimal solution.

## 14.3.2 application scenario - Knapsack Problem

Backpack problem: there is a backpack with a capacity of 4 pounds. There are the following items:

1) The goal to be achieved is to maximize the total value of the loaded backpack and not exceed the weight

2) The items required to be loaded cannot be repeated.

3) Knapsack problem mainly refers to a given capacity knapsack and several items with certain value and weight. How to select items to put into the knapsack to maximize the value of items. It is divided into 01 backpack and complete backpack (complete backpack means that there are unlimited items available for each item)

4) The problem here belongs to 01 backpack, that is, put one item at most. The infinite backpack can be transformed into 01 backpack.

5) The main idea of the algorithm is solved by dynamic programming. For the i-th item traversed each time, determine whether to put the item into the backpack according to w[i] and v[i]. That is, for a given n items, let v[i] and w[i] be the value and weight of the ith item respectively, and C be the capacity of the backpack. Let v[i][j] represent the maximum value that can be loaded into the backpack with capacity j in the first I items. Then we have the following results:

(1) v[i][0]=v[0][j]=0; // Indicates that the first row and the first column of the filled table are 0

(2) When w [i] > J: v[i][j]=v[i-1][j] / / when the capacity of the new item to be added is greater than the capacity of the current backpack, the previous item will be used directly

Cell loading policy

(3) When J > = w [i]: v[i][j]=max{v[i-1][j], v[i]+v[i-1][j-w[i]]}

//When the capacity of the new commodity to be added is less than or equal to the capacity of the current backpack, / / loading method:

v[i-1][j]: the maximum value of the previous cell

v[i]: indicates the value of the current commodity

v[i-1][j-w[i]]: load i-1 goods to the maximum value of the remaining space j-w[i]

When J > = w [i]: v[i][j]=max{v[i-1][j], v[i]+v[i-1][j-w[i]]};

- Graphic analysis

## 14.3.4 dynamic programming - code implementation of knapsack problem

/** * @author zk * @version 1.0.0 * @ClassName KnapsackProblem.java * @Description TODO Dynamic programming knapsack problem * @createTime 2021 09:41:00, September 30 */ public class KnapsackProblem { public static void main(String[] args) { // Weight of the item int[] w = {1, 4, 3}; // Value of the item int[] val = {1500, 3000, 2000}; int m = 4; //Backpack Capacity int n = val.length; // Number of items //v[i][j] represents the maximum price that can be loaded into a backpack with a capacity of j in the first I items int[][] v = new int[n + 1][m + 1]; //In order to record the placement of goods, we set up a two-dimensional array int[][] path = new int[n + 1][m + 1]; // Set v the first row and the first column to 0, but you can not, because the array defaults to 0 /*for (int i = 0; i < v.length; i++) { v[i][0] = 0; } for (int i = 0; i < v[0].length; i++) { v[0][i] = 0; }*/ for (int i = 1; i < v.length; i++) { for (int j = 1; j < v[0].length; j++) { if (w[i - 1] > j) { // The weight of the item is greater than the capacity of the backpack v[i][j] = v[i - 1][j]; } else { // Backpack capacity is greater than or equal to item weight // max{v[i-1][j],val[i-1] + v[i-1][j-w[i-1]]} if (v[i - 1][j] < val[i - 1] + v[i - 1][j - w[i - 1]]) { v[i][j] = val[i - 1] + v[i - 1][j - w[i - 1]]; //Record the current situation to path path[i][j] = 1; } else { v[i][j] = v[i - 1][j]; } } } } // Use your brain to print the types of items stored int i = path.length - 1; //Maximum subscript of row int j = path[0].length - 1; //Maximum subscript of column while(i > 0 && j > 0 ) { //Start at the end of the path if(path[i][j] == 1) { System.out.printf("The first%d Put items into the backpack\n", i); j -= w[i-1]; //w[i-1] } i--; } } }