Short answer: Weighted interval scheduling picks a compatible set of intervals whose weights sum to the maximum. Sort by finishing time. For each interval j, let p(j) be the rightmost interval that finishes before j starts. Then the optimum for the first j intervals is the larger of weight(j)…
Category: Dynamic Programming
How unbounded knapsack reuses a capacity row
Short answer: Unbounded knapsack, and the classic coin-change combination count, let each item be used any number of times. The same capacity array as 0-1 knapsack is scanned upward. When the loop reaches t, dp[t−w] already includes the choices just made for this item, so the item may be…
How the Viterbi algorithm decodes a hidden path
Short answer: The Viterbi algorithm finds the most probable sequence of hidden states for a sequence of observations in a hidden Markov model. The cell v[t][s] is the probability of the best path that emits the first t observations and ends in state s. It multiplies the best predecessor…
How the egg dropping recurrence limits the worst case
Short answer: The egg dropping problem asks for the minimum number of trials that suffice, in the worst case, to find a critical floor with e eggs and f floors. Dropping at floor x leaves either e−1 eggs and x−1 floors below, if the egg breaks, or e eggs…
How the CYK algorithm parses a Chomsky grammar
Short answer: The CYK algorithm decides whether a string belongs to a context-free grammar in Chomsky normal form, and it builds a parse table while doing so. Cell (length, start, variable) is true when that variable derives the substring. A production A → B C splits the substring into…
How matrix-chain order minimizes scalar multiplications
Short answer: Matrix-chain multiplication chooses where to parenthesize a product of matrices so the number of scalar multiplications is minimized. Matrix i has dimensions p[i−1] by p[i]. The cost of multiplying a chain from i through j is the minimum, over the last multiplication’s split k, of the two…
How Kadane resets a running sum
Short answer: Kadane’s algorithm finds the contiguous subarray of maximum sum in O(n) time and O(1) extra memory. It walks the array once, keeping the best sum that ends at the current index and the best sum seen anywhere. The sum that ends here is the current element plus…
How Held-Karp holds one subset per tour
Short answer: The Held-Karp algorithm solves the traveling salesman problem on n cities by dynamic programming over subsets. The cell dp[S][v] is the minimum cost of a path that starts at city 0, visits each city in the subset S exactly once, and ends at v. Each cell tries…
How an optimal BST weights search cost
Short answer: An optimal binary search tree puts keys in search-tree order and chooses the shape that minimizes the expected search cost. Key i is requested with probability p[i], and the gap between keys is requested with probability q[i]. The cost of a subtree is the sum of its…
How 0-1 knapsack fills a capacity row
Short answer: The 0-1 knapsack problem takes each item at most once. With capacity W and n items, the cell dp[i][w] is the best value using the first i items and capacity w. The item is skipped, or it is taken when its weight fits, and the cell is…
