There are three useful distinctions between dynamic programming and a conventional divide-and-conquer algorithm.
- A divide-and-conquer algorithm typically splits one instance into mostly separate smaller instances; dynamic programming deliberately recognizes overlapping subproblems that recur along different decision histories.
- A dynamic programming state is solved once and its result reused through a table or memoization. A direct divide-and-conquer algorithm normally combines the results of its recursive children without retaining a table indexed by all repeated states.
- The dependency structure of a direct recursive decomposition is a recursion tree; dynamic programming evaluates a Directed acyclic graph of states in dependency order, either bottom-up or by memoization. Its cost is the number of distinct states times the work per state, rather than the number of occurrences in the expanded recursion tree.
These are paradigm-level distinctions, not mutually exclusive definitions: a recursive algorithm can use memoization, and dynamic programming need not be implemented iteratively. For optimization, the dynamic programming state and optimal substructure justify combining stored optimum values; storing repeated work alone does not prove the state is sufficient.
For the unbounded knapsack problem, let be the greatest total value obtainable with weight at most capacity . Assume positive integer item weights and nonnegative integer capacity . Set , and fill increasing capacities usingThe zero alternative permits unused capacity. If a nonempty optimum includes item , removing one copy leaves a feasible smaller-capacity instance; its remaining value cannot exceed . Conversely every candidate in the displayed maximum is feasible, proving equality. The smaller-capacity state allows the same type again, implementing repetition.
The answer is , computed in time and space. Record an attaining item at each state to reconstruct the multiset. This is pseudopolynomial time, because is a number whose binary representation may have only bits. A positive-value zero-weight type would make the optimum unbounded and must be excluded or detected separately.
Use a Boolean dynamic programming state for whether amount can be made exactly. Initialize and, for , computeAn empty disjunction is false. Any nonempty representation ends in some coin; removing it gives one of the displayed predecessor states, and adding it back proves the converse. Return . This takes time and space for positive integer denominations and nonnegative integer target , with unlimited coins of each type.
For the minimum coin change recurrence, initialize and usewhere the minimum over no feasible predecessor is and . Removing the last coin of an optimal representation proves the lower bound; appending the corresponding coin to an optimal predecessor proves attainability. is the minimum number of coins, obtained in time and space. Store one minimizing coin per amount if the actual coins are wanted.
For , use largest-denomination-first greedy algorithm steps: for , take coins and replace the residual amount by . A target is representable exactly when it is divisible by , because the smallest denomination is , not one.
To prove optimality whenever the target is representable, observe that an optimum can have fewer than coins of every denomination with : replacing such coins by one coin preserves value and reduces the count by . After all such carrying, the coefficients of are the unique base- digits of , while the coefficient of is the remaining quotient. Largest-first division produces exactly these coefficients. The greedy coin count is optimal, and can be computed with quotient/remainder operations without listing every coin.
For , distinct denominations permit only ; then all coins have value one and the answer is . This handles the printed positive-integer condition without silently assuming .
Each physical item may enter neither knapsack or exactly one of them. Let be the largest combined value using the first items with remaining capacity limits . Initialize and computeAn infeasible alternative is omitted. The three alternatives classify the placement of item exhaustively. Every reference uses row , ensuring that no item is placed in both knapsacks. Removing item from an optimum and applying optimal substructure proves the recurrence relation.
The optimum is , with time , conventionally for positive capacities. Two successive layers require space; keep predecessor decisions or recompute them if the actual allocations are wanted. The stipulated uniqueness of values and weights is unnecessary for correctness. The capacity-indexed algorithm is pseudopolynomial.
Articles by others on the same topic
There are currently no matching articles.