| Learn | Algorithm - Dynamic Programming - Knapsack problem |
|---|
| Knapsack Problem 0-1 Knapsack The Knapsack Problem is a problem when given a set of items, each with a weight, a value and exactly 1 copy, determine the which item(s) to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. C++ Example:Implementation: Test: Output: That means the maximum value can be achieved is 3, which is achieved by choosing (2,1) and (3,2). Unbounded Knapsack The Unbounded Knapsack Problem is a problem which given a set of items, each with a weight, a value and infinite copies, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. Python(2.7.11) Example:Implementation: The complexity of that implementation is Test: Output: That means the maximum value can be achieved is 20, which is achieved by choosing (5, 8), (5, 8) and (3, 4). RemarksDynamic Programming is an improvement on Brute Force, see this example (opens new window) to understand how one can obtain a Dynamic Programming solution from Brute Force. A Dynamic Programming Solution has 2 main requirements: Overlapping Subproblems means that results of smaller versions of the problem are reused multiple times in order to arrive at the solution to the original problem Optimal Substructure means that there is a method of calculating a problem from its subproblems. A Dynamic Programming Solution has 2 main components, the State and the Transition The State refers to a subproblem of the original problem. The Transition is the method to solve a problem based on its subproblems The time taken by a Dynamic Programming Solution can be calculated as |
Read Article
https://develop.horje.com/learn/1434/reference