Algorithms · UGC NET: Design Techniques Extras
Divide and Conquer Applications
Overview
Divide and conquer is an algorithm design paradigm that solves a problem by recursively breaking it into smaller independent subproblems of the same type, solving them, and combining their results.
It is a core topic in UGC NET Computer Science because its recurrence relations, Master theorem, and standard applications such as binary search, merge sort, Strassen matrix multiplication, closest pair of points, and MaxMin are asked directly.
The lesson also covers identifying when an algorithm is greedy or dynamic programming, and the disk-access cost of B-tree search.
Prerequisites are basic asymptotic notation, recurrence relations, and simple matrix and array operations.
Key concepts
- Divide and conquer consists of three stages: Divide the problem into a subproblems, each of size n/b; Conquer each subproblem recursively; Combine the partial solutions. Base cases are solved directly.
- The running time of a D&C algorithm satisfies a recurrence T(n) = a T(n/b) + f(n), where a ≥ 1 is the number of subproblems, b > 1 is the size-reduction factor, and f(n) is the cost of dividing and combining.
- The Master theorem compares f(n) with the watershed function . The quantity equals the number of leaves in the recursion tree (if each leaf has cost), so it represents the total cost at the leaves.
- Binary search on a sorted array uses recurrence T(n) = T(n/2) + and runs in worst case.
- Merge sort and inversion counting use T(n) = 2T(n/2) + and run in .
The expected number of inversions in a random permutation of n distinct elements is n(n−1)/4 = ;
this is a property of the permutation, not the algorithm's time. - Strassen's matrix multiplication multiplies two n×n matrices using 7 recursive multiplications of (n/2)×(n/2) blocks instead of 8.
Its recurrence is T(n) = 7T(n/2) + , giving ≈ , often written . - The closest pair of points in the plane can be found in time by D&C. After solving left and right halves, let δ = min(δ_L, δ_R).
Only points within a strip of width 2δ around the dividing line are checked, and each point needs comparison with at most 7 subsequent points in y-sorted order. - The MaxMin D&C algorithm finds both maximum and minimum of n elements using T(n) = 2T(n/2) + 2 for n > 2, with T(2) = 1.
For n a power of 2, comparisons = 3n/2 − 2.
For n = 16, this is 22 comparisons. - Algorithm paradigms: Dijkstra's single-source shortest-path algorithm is greedy. Floyd–Warshall all-pairs shortest-path algorithm is dynamic programming. Strassen, binary search, merge sort, and closest pair are divide and conquer.
- B-tree search is a balanced multiway tree search, not a D&C algorithm. Each node is one disk page, so the number of disk-page accesses equals the height.
For n keys and minimum degree t, height is ; hence disk accesses are .
How it works
-
General D&C procedure.
(a) Divide the input of size n into a subproblems of size n/b (often equal).
(b) Solve each subproblem recursively.
(c) Combine the a sub-results to produce the answer.
The recurrence is T(n) = aT(n/b) + f(n), with base T(1) = .
The recursion tree shows work per level: level k has subproblems of size n/ and cost f(n/). The leaves occur at depth n and number = . This leaf count is the watershed function. -
Master theorem (standard form).
For T(n) = aT(n/b) + f(n), let p = a. Compare f(n) with .- Case 1: if f(n) = O() for some ε > 0, then T(n) = .
- Case 2: if f(n) = for some k ≥ 0, then T(n) = . The common subcase k = 0 gives .
- Case 3: if f(n) = Ω() for some ε > 0 and the regularity condition a f(n/b) ≤ c f(n) holds for some c < 1 and all large n, then T(n) = .
-
Binary search.
On a sorted array, compare the key with the middle element. If equal, stop. If smaller, search the left half; if larger, search the right half. Exactly one subproblem is used, so T(n) = T(n/2) + . Master theorem gives worst case. The array must be sorted. -
Inversion counting via D&C.
An inversion is a pair (i, j) with i < j and A[i] > A[j]. Modify merge sort: recursively count inversions in the left half and right half; then during merging, when an element from the right half is smaller than an element from the left half, all remaining elements in the left half form inversions with it. Add (mid − i + 1) to the count. Recurrence T(n) = 2T(n/2) + = . For a random permutation of n distinct elements, each of the C(n,2) pairs is inverted with probability 1/2, so expected inversions = C(n,2)/2 = n(n−1)/4 = . Do not confuse the expected count with the algorithm's time. -
Strassen's matrix multiplication.
Split each n×n matrix into four (n/2)×(n/2) blocks. The standard block method uses 8 block multiplications. Strassen uses 7 multiplications and 18 additions/subtractions. Thus T(n) = 7T(n/2) + . By the Master theorem, p = 7 ≈ 2.807, and f(n) = is polynomially smaller than , so Case 1 gives T(n) = ≈ . The number of scalar multiplications for pure Strassen on is = . -
Closest pair of points in 2D.
(a) Sort all points by x-coordinate.
(b) Divide the set into left and right halves by a vertical line.
(c) Recursively find the minimum distance δ_L in the left half and δ_R in the right half. Let δ = min(δ_L, δ_R).
(d) Build the strip of points whose x-coordinate is within δ of the dividing line.
(e) Sort the strip by y-coordinate (or maintain a y-sorted list during recursion).
(f) For each point in the strip, compare its distance with the next at most 7 points in y-order. If a smaller distance is found, update δ.
The strip check is because any δ × 2δ rectangle contains at most 8 points, so each point needs at most 7 comparisons. The recurrence with presorted y is T(n) = 2T(n/2) + = . The worst-case time is . -
MaxMin by D&C.
(a) If the subarray has one element, max = min = that element.
(b) If it has two elements, compare once and return the larger as max and smaller as min.
(c) Otherwise, split into two halves, recursively find (max1, min1) and (max2, min2), then compare max1 with max2 and min1 with min2.
The recurrence is T(n) = 2T(n/2) + 2 for n > 2, T(2) = 1, T(1) = 0. For comparisons. This is optimal for finding both max and min. -
Recurrence with square root.
For T(n) = 2T(⌊√n⌋) + , substitute . Then T() = 2T() + m. Let . Then S(m) = 2S(m/2) + m. Master theorem: a = 2, b = 2, p = 1, f(m) = m = , Case 2 with k = 0 gives S(m) = . Back-substitute m = to get T(n) = . -
Recurrence with decreasing fraction.
For T(m) = T(3m/4) + 1, unroll: . Stop when = log_{4/3} m. Hence T(m) = . Any recurrence of the form T(n) = T(αn) + with 0 < α < 1 has solution. -
B-tree search disk accesses.
A B-tree of minimum degree t is a balanced multiway search tree. Each node occupies one disk page. A search reads the root, then follows one pointer per level to a leaf. The number of disk-page accesses equals the height plus one, which is , where n is the number of keys. The exact maximum height is about ((n+1)/2). Hence the disk-page access cost is .
Drawing the diagram…
Formulas
- General D&C recurrence: , with , .
- Watershed function: .
- Master theorem:
- Case 1: .
- Case 2: .
- Case 3: and for some .
- Binary search: .
- Merge sort and inversion count: .
- Expected inversions in a random permutation of distinct elements: .
- Strassen: .
- Closest pair: with presorted y; strip check compares each point with at most 7 others.
- MaxMin comparisons for : . For , .
- Square-root recurrence: .
- Fractional recurrence: .
- B-tree disk accesses: , where is the minimum degree and is the number of keys.
Worked examples
Example 1 (Master theorem with logarithmic factor).
Solve .
- , , so .
- .
- This matches Case 2 with . Therefore .
Answer: .
Example 2 (MaxMin comparisons).
Find the number of comparisons used by the standard D&C MaxMin algorithm on elements.
Base: . Recurrence: .
.
.
.
Formula: .
Answer: 22 comparisons.
Example 3 (Strassen scalar multiplications).
Using pure Strassen's algorithm recursively down to scalars, how many scalar multiplications are needed to multiply two matrices?
For , the number of multiplications is . Here , so .
Number .
For comparison, the standard algorithm needs multiplications.
Answer: 343 scalar multiplications.
Example 4 (Match the list).
Match each D&C algorithm in List-I with its recurrence and complexity in List-II.
List-I: A. Strassen's matrix multiplication; B. Binary search; C. Merge sort; D. MaxMin.
List-II: 1. , ; 2. , ; 3. , ; 4. , .
Solution: A-3, B-1, C-2, D-4.
Comparison
Table 1: Algorithm paradigms.
| Algorithm | Paradigm | Key idea |
|---|---|---|
| Dijkstra single-source shortest paths | Greedy | Repeatedly finalize the unvisited vertex with minimum tentative distance. |
| Floyd–Warshall all-pairs shortest paths | Dynamic programming | Build shortest paths using intermediate vertices one by one. |
| Strassen matrix multiplication | Divide and conquer | Reduce 8 block multiplications to 7. |
| Binary search | Divide and conquer | Discard half the search space each step. |
| Merge sort | Divide and conquer | Divide, sort halves recursively, merge. |
| Closest pair of points | Divide and conquer | Solve halves, then check strip. |
Table 2: Recurrences and tight complexities.
| Algorithm | Recurrence | Complexity |
|---|---|---|
| Binary search | ||
| Merge sort / inversion count | ||
| Standard matrix multiplication | ||
| Strassen matrix multiplication | ||
| MaxMin | , exactly comparisons for | |
| Closest pair (2D, presorted y) |
Table 3: Search structures.
| Structure | Disk accesses | Paradigm |
|---|---|---|
| Binary search on sorted array | Not disk-specific; comparisons | Divide and conquer |
| B-tree search | disk pages | Balanced multiway tree search (not D&C) |
Revision summary
- D&C recurrence: ; compare with .
- Watershed function is the leaf-level cost in the recursion tree.
- Strassen uses 7 multiplications, not 8; complexity .
- Expected inversions in a random permutation is , but counting them takes .
- MaxMin comparisons on : ; for it is 22.
- Closest pair strip check: at most 7 comparisons per point; total time .
- .
- .
- Dijkstra is greedy; Floyd–Warshall is dynamic programming.
- B-tree search disk accesses: .
- In Master theorem Case 2 with , multiply the log factor: .
- Binary search recurrence is , not .
Previous-year exam insights
- Direct questions ask for the solution of a recurrence using the Master theorem; always identify , , , and compare with first.
- Traps: forgetting the extra in Case 2 (e.g., gives ); misapplying Case 3 without the regularity condition.
- Numeric questions ask for MaxMin comparisons on a small such as 16; use or unroll the recurrence.
- Strassen questions ask for the number of multiplications or the final complexity; remember 7 and , not 8 or .
- Expected inversions is a classic trap: the expected number is , while the D&C counting algorithm is .
- Paradigm-identification questions mix greedy, dynamic programming, and divide-and-conquer; Dijkstra is greedy, Floyd–Warshall is DP, Strassen and closest pair are D&C.
- Recurrences with square root or fractional decrease require substitution ( or unrolling), not the standard Master theorem directly.
- B-tree disk-access questions expect ; recall that each node is one disk page and search follows one root-to-leaf path.
Practice at PARIKSHA
You are ready for the PARIKSHA tests of this sub-topic when you can:
- Solve using all three Master theorem cases, including Case 2 with .
- Explain what the watershed function represents in the recursion tree.
- Compute the number of comparisons for MaxMin on by both unrolling and the closed formula.
- Write the recurrence and tight complexity for Strassen's matrix multiplication and compute scalar multiplications for a given power of 2.
- Solve by the substitution .
- Solve by unrolling.
- Derive the expected number of inversions in a random permutation and distinguish it from the inversion-counting algorithm's time.
- Describe the closest pair strip check and state why at most 7 comparisons per point are needed.
- Classify Dijkstra, Floyd–Warshall, Strassen, and binary search by their algorithm paradigm.
- Compute the number of disk-page accesses in a B-tree search given and .
Attempt the sub-topic test first, then the subject test.