Design techniques formulas
Greedy, dynamic programming and divide-and-conquer. 13 formulas for GATE CS, DA Data Structures and Algorithms, each with when to use it. Each is explained in the topic notes.
IAE Academy: Greedy, dynamic programming and divide-and-conquer formulas (GATE CS, DA)
All 13 Design techniques formulas
FRACTIONAL KNAPSACK
Applies only when any fraction of an item may be taken. Solved by sorting items on in descending order and filling greedily until capacity is used up; only the last item taken can be partial. Time .
EXPECTED (AVERAGE) CODE LENGTH
is the probability of symbol . If frequencies are given, divide each by the total frequency. is its codeword length (the depth of its leaf). Huffman's construction minimises over all prefix-free codes for the given distribution. When weights tie, different trees are possible, but they all have the same minimum .
DIVIDE-AND-CONQUER RECURRENCE (GENERAL FORM)
a = number of subproblems, n/b = size of each subproblem, f(n) = cost of the divide and combine steps outside the recursive calls.
MERGE SORT RECURRENCE
The same bound holds in the best, average and worst cases, because the split does not depend on the input order. Merging needs extra space (standard merge sort is not in-place). Merge sort is stable.
QUICKSORT RECURRENCES
The worst case happens when the pivot is always the smallest or largest element, giving (n−1, 0) splits. Example: an already sorted or reverse-sorted array with the first or last element as pivot. The best case is a perfectly balanced split. Any split in a fixed proportion (e.g. 1:9) still gives . The average case (random pivot, or all input orders equally likely) is also . Partitioning needs no auxiliary array, but the recursion stack uses space on average and in the worst case. Quicksort is not stable.
BINARY SEARCH RECURRENCE
Requires a sorted array (or a search space that is monotonic in the property being tested). The iterative version uses extra space and the recursive one stack. Inside the base of the logarithm does not matter; when an exact count is needed, use base 2.
MAX-SUBARRAY D&C RECURRENCE
This is the divide-and-conquer bound. Kadane's algorithm solves the same problem in with a single linear scan (a DP-style running "best sum ending here"). That is asymptotically better, and optimal, since every element must be read.
KADANE'S RECURRENCE
The answer is . It runs in time with extra space. With non-empty subarrays, an all-negative array returns its largest element.
LCS RECURRENCE
Indices are 1-based here: is the i-th character of X and the j-th of Y. The table is , and time and space are both . If only the length is needed, not the subsequence itself, two rows are enough, giving space.
EDIT DISTANCE RECURRENCE
In the last case the three terms are delete , insert and substitute. means deleting all i characters of X's prefix; means inserting all j characters. The table is , filled in time. If only insertions and deletions are allowed, a match still copies , but a mismatch gives . That distance equals .
0/1 KNAPSACK RECURRENCE
= best value using the first i items with capacity c. The table is , filled in time. This is pseudo-polynomial: polynomial in the numeric value of W, but exponential in the number of bits needed to write W. The DP is therefore efficient for small integer W, while 0/1 knapsack in general is NP-hard. Space can be reduced to with one array, if c runs from W down to for each item.
SUBSET SUM RECURRENCE
is true if and only if some subset of the first i numbers sums exactly to t. Base cases: and for . The table has Boolean entries, filled in time, which is also pseudo-polynomial.
MATRIX CHAIN MULTIPLICATION RECURRENCE
is the minimum cost of multiplying ; a single matrix costs nothing. Fill by increasing chain length from 2 up to n. The answer is , the last cell filled. Time (indices i, j and split k), space .
Where this was asked: 2018 (2), 2023 (1), 2024 (3), 2026 (2). See the 8 previous-year questions on Design techniques. This shows what was asked, not what will be asked.