Like what
A item value of 1,
b Item Value 2,
C Item Value 8,
D Item Value 10
I give a number 30
The possible combinations
Requires the least amount of items to be used, repeatable
Reply content:
Like what
A item value of 1,
b Item Value 2,
C Item Value 8,
D Item Value 10
I give a number 30
The possible combinations
Requires the least amount of items to be used, repeatable
@Ukyoi_D Classmate's analysis is right, but the following proposed BFS solution pruning method is not a common method of dynamic programming, of course, the method is correct.
First, standardize the original topic:
There are n items, the value of each item is v[i], and v[0]<=v[i]<=v[i+1]<=v[n-1]; given the total value of W, to find the smallest m items, so that the total value of M goods is W.
The algorithm is dynamic programming, proving the process ukyoi_d the classmate has proved. The state transfer equation is given here.
The state transition equation is:
f[i][w] = min{ f[i-1][w] if f[i-1][w] exist , f[i-1][w-v[i]] +1 if f[i-1][w-v[i]]exist }
The number of items that represent the value of the items f[i][w] in the first I, there are items that indicate that the w f[i][w] previous i item can be combined w , and there is no item that indicates that the value cannot be combined w .
For f[i][w] actually there are 3 possibilities, 1. Select i items, the number of items required equals f[i-1][w-v[i]]+1 . 2. Do not select I items, you need the item equals f[i-1][w] , 3. f[i-1][w-v[i]] and f[i-1][w] neither exist nor f[i][w] necessarily exist.
The smallest or absent in three cases.
Only one 2-dimensional array is required for the implementation, and the non-existent state is represented by infinity.
This should not involve the algorithm what, the requirements of the least used items, that is, as far as possible value of goods,
Use 30 from D "A to take the whole, take the remainder, the integer is the current number of how many, the remainder is the rest, continue to operate downward, until the allocation is complete
//===============================
First look at the question:
1, did not say 301 must be assigned to finish
2. Minimum required items
So I think I've got nothing wrong with the solution, barely counting the greedy algorithm.
Everybody says it's a backpack problem, I don't think so.
1, knapsack problem is two dimensions to consider, weight and value and here only value
2, knapsack problem requires the final value maximization, here only requires the least items
Problem Description:
有m个物品, 价值从小到大 为V1 - Vm f(i) 为 "价值i且物品最少的组合"的 物品数求解 f(n)
Initialization
f(0) = 0f(i) = +∞,1 <= i <= n
Solving:
从 1到 nf(i) = min(f(i - Vx) {1 <= x <= m && i >= Vx}) + 1 如果不满足 1 <= x <= m && i >= Vx, f(i) = +∞
Minimum items used, repeatable
That's three d, just 30.
Is there anything less than this?
In addition, this is not a knapsack problem ah, knapsack problem not only has the value limit, but also has the weight limit ah.
Upstairs said the problem "no algorithm" is obviously wrong ... This is a classic dynamic programming entry question ...
The greedy solution to 30 of this particular value is feasible, but for example, 16 is not feasible, 16 of the optimal solution is obviously to take two 8, rather than the greedy method of the first take 10 and then three 2.
The original topic to use the fewest items to gather enough 30, if I take a casually, for example, I took a 2, and then I have to take 28 to meet the "gather enough 30" requirements, so there is a sub-problem: How to use the fewest items to make up enough 28. Similarly, if I take a 10, the problem is that I need to gather up to 20.
The key to this topic is the so-called "optimal substructure": When and only if the solution of the sub-problem is optimal, the solution of the parent problem is optimal. For example, take the first thing I can have 1, 2, 8, 104 kinds of choices, so this question becomes four sub-problem, if I already know four sub-question one of the answer is that four sub-problem of the least, for example, need to take K, then plus the first one to take, The original problem, then, requires at least a k+1. Of course, we still do not know which of the 4 sub-problems is the optimal solution, so the recursive method, the four sub-problems of the sub-problem, to find all the two sub-problems. In all two sub-problems, if there is an optimal solution, such as the need to take M, then the same way, the solution of a sub-problem is at least to take m+1, and the solution of the mother problem is to take m+2. The optimal substructure is the foundation of the dynamic programming implementation.
The implementation of the specific algorithm please see Bluedream's answer, that is the standard method of dynamic planning. The answer given below is recursive, and the computer needs more resources to handle function calls, and is usually slower.
Breadth-First search solution:
Just now the reasoning based on "If you know the solution of sub-problem", of course, the solution of the two-level sub-problem is still not known, then continue to look down. It's like peeling onions. Peel off layers (slang called "breadth-first search"). (Here is a small trick: First Take 8 again take 2 and take 2 again take 8 is the same, optimized off can save a half resources)
After finding several layers, such as having a route with the newest sub-question asking for a full 8, obviously taking a 8 is the optimal solution to this sub-problem. Then the other route can not forget, because the other route at least have to take another to meet the requirements. Along the first layer of the best solution found, the optimal solution (one) of the problem was found. Of course, there may be a route finally found no solution (the topic is impossible, because can take 1, always use 1 to make up enough of any positive integer, but if the original subject requires 30 pieces of 5 Mao so obviously no solution), then abandon this route, continue to find other routes until the solution. or traverse all possible cases to find no solution.
Of course, the above is the "breadth first" approach, you can also use the "depth first" approach, that is, like a maze, followed by one of the sub-problem (and the sub-problem of the sub-problem) a road to the black, if it is a dead end and back, a road, this time the first solution is not necessarily the optimal But with the reference to this solution, all the alley deeper than this solution can be labeled as dead end, no more drilling, because the best solution of the steps than the first found that there must be less than. If we find a better solution later, we will use this better Shing reference ... After all the paths have been drilled, either an optimal solution has not been found, or the newly discovered solution is the optimal solution.
First of all, the answer to all hand calculations is obviously wrong.
This is just for example, the actual situation of course the value of goods and the value of the solution is the program input.
It is impossible to figure out a result by hand and say the question is meaningless.
Secondly, the greedy solution mentioned above @ekea0407 is wrong.
Assuming that the weight of the item is 1,2,8,10 according to the topic, the weight of the solution is 16, this time by greedy algorithm:
16=10+6=10+2+4=10+2+2+2, turned into 4 items.
In fact 16=8+8, only need 2 items.
Finally, this problem and knapsack problem is still a bit different, but all with dynamic programming, the solution process is similar.
@brayden solution is OK.
The method of searching the solution space by brute force search is very poor, because brute force search does not store the local solution that has been found and solves the same problem many times.
This problem is still suitable for dynamic programming.
Feel the landlord this problem almost does not involve the algorithm. First Use 3 10, direct 30. If there is No 10, the direct is more than 3, the problem is not carried out necessary.
Knapsack problem
Dynamic planning
This is the knapsack problem, the above one classmate said this does not involve the algorithm, actually is unscientific. The main topic can search the floor of the master's ' backpack Nine Talk ', the problem is solved.