Dynamic Programming (dynamic programming)
concept
A large complex problems can be broken down into simple little question, to arrive at the solution of big problems by solving small problems.
nature
No after-effect: after a certain stage of state determination, after this stage of development without state influence at this stage of previous phases. That future has nothing to do with the past
Optimal substructure: the optimal solution can be launched by the big problem the optimal solution of small problems
Overlapping sub-problems: when a top-down recursive algorithm Solutions, each sub-problem are not always produced new problems, some sub-problems are of iterations.
Scope
Problems with overlapping subproblems and optimal substructure Properties
Differences divide and conquer and dynamic programming
Thing in common: both require the original problem has a sub-optimal structural properties, are the original problem of divide and rule, broken down into a number of smaller-scale (small program to easily resolved) sub-problems and then the combined solutions to subproblems. to form a solution of the original problem.
Different points : divide and conquer method subproblems decomposed as independent of each other, by using recursive do.
The dynamic programming sub-problems decomposed understood to have contact with each other, overlapping part, need to remember, usually an iterative do.
step
- Abstract problems
- Modeling (what ultimately required)
- Constraints (what kind of conditions are met)
- Determined to have optimal substructure
- Defined state
- Big issues and small issues state transition equation
- Filling
- To seek solutions of the composition according to the results table
Question 1: 0-1 knapsack problem
There are n items, they have their own size and value, given the capacity to backpack cap, how to make the items into the backpack has a maximum value of the sum?
Analysis step
Abstract problem: For each item, or are likely to do, so the use of (X1, X2, ... Xi, ..., Xn) represents the i-th item to be or not. The value of each item is Vi, the size of each item as Wi.
Modeling: to maximize the value. max (sum (Xi * Vi)).
Constraints: sum (Xi * Wi) <cap
Optimal substructure: before each stage can be expressed as a combination of items if i can make a total maximum value of the current stage, the issue at different stages apply.
Status is defined: V (i, j) - i-th article before the best combination total value, i-- represents the i-th former article, j-- represents the remaining capacity
State transition equation: i-th article, there are two choices - to be or not. To - not necessarily the current total value of the maximum; do not - the maximum value is equal to the total value of the previous stage.
Analyzing conditions - can not exceed the remaining capacity of the backpack.
V (i, j) = V (i -1, j), Wi> j; - not the i-th item
V (i, j) = max {V (i -1, j - Wi) + Vi, V (i -1, j)}, Wi <= j; - to the i-th item
static int[][] findMax(int cap, int n, int[] value, int[] weight, int[] item){
int[][] v = new int[n + 1][cap + 1]; //建立路由表
//首先初始化第一列第一行
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 <= n; i++) {
for (int j = 1; j <= cap; j++) {
if(weight[i - 1] > j) { //大小和价值数组均要减1才能不越界
v[i][j] = v[i - 1][j];
}else {
if(v[i - 1][j] < (v[i - 1][j - weight[i - 1]] + value[i - 1])) {
v[i][j] = v[i - 1][j - weight[i - 1]] + value[i - 1];
}
else {
v[i][j] = v[i - 1][j];
}
}
}
}
return v;
}
public static void main(String[] args) {
int n = 4;
int cap = 8;
int[] value = new int[n];
int[] weight = new int[n];
System.out.println("请输入每个物品的价值:");
for(int i = 0; i < n; i++) {
Scanner in = new Scanner(System.in);
if(in.hasNextInt())
value[i] = in.nextInt();
}
System.out.println("请输入每个物品的大小:");
for(int i = 0; i < n; i++) {
Scanner in = new Scanner(System.in);
weight[i] = in.nextInt();
}
int[] item = new int[n];
int[][] res = findMax(cap, n, value, weight,item);
System.out.println(res[n][cap]);
}
reference:
https://www.cnblogs.com/raichen/p/5772056.html
https://www.zhihu.com/question/23995189/answer/613096905
https://www.cnblogs.com/Christal-R/p/Dynamic_programming.html