1. Gyanastra
  2. Algorithms
  3. UGC NET: Design Techniques Extras
  4. Divide and Conquer Applications
Free lesson, no account needed 13 min read 8 practice questions after it
Asked in past papers3 times
  • GATE CSE1994
  • ISRO2017
  • UGC NET2013

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 nlog⁡ban^{\log_b a}. The quantity nlog⁡ban^{\log_b a} equals the number of leaves in the recursion tree (if each leaf has Θ(1)\Theta(1) cost), so it represents the total cost at the leaves.
  • Binary search on a sorted array uses recurrence T(n) = T(n/2) + O(1)O(1) and runs in Θ(log⁡n)\Theta(\log n) worst case.
  • Merge sort and inversion counting use T(n) = 2T(n/2) + Θ(n)\Theta(n) and run in Θ(nlog⁡n)\Theta(n \log n).
    The expected number of inversions in a random permutation of n distinct elements is n(n−1)/4 = Θ(n2)\Theta(n^{2});
    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) + Θ(n2)\Theta(n^{2}), giving Θ(nlog⁡27)\Theta(n^{\log_{2} 7}) ≈ Θ(n2.807)\Theta(n^{2.807}), often written O(n2.81)O(n^{2.81}).
  • The closest pair of points in the plane can be found in O(nlog⁡n)O(n \log n) 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 O(log⁡tn)O(\log_{t} n); hence disk accesses are Θ(log⁡tn)\Theta(\log_{t} n).

How it works

  1. 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) = Θ(1)\Theta(1).
    The recursion tree shows work per level: level k has aka^{k} subproblems of size n/bkb^{k} and cost aka^{k} f(n/bkb^{k}). The leaves occur at depth log⁡b\log_b n and number alog⁡bna^{\log_b n} = nlog⁡ban^{\log_b a}. This leaf count is the watershed function.

  2. Master theorem (standard form).
    For T(n) = aT(n/b) + f(n), let p = log⁡b\log_b a. Compare f(n) with npn^{p}.

    • Case 1: if f(n) = O(np−εn^{p−ε}) for some ε > 0, then T(n) = Θ(np)\Theta(n^{p}).
    • Case 2: if f(n) = Θ(nplog⁡kn)\Theta(n^{p} \log^{k} n) for some k ≥ 0, then T(n) = Θ(nplog⁡k+1n)\Theta(n^{p} \log^{k+1} n). The common subcase k = 0 gives Θ(nplog⁡n)\Theta(n^{p} \log n).
    • Case 3: if f(n) = Ω(np+εn^{p+ε}) 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) = Θ(f(n))\Theta(f(n)).
  3. 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) + O(1)O(1). Master theorem gives Θ(log⁡n)\Theta(\log n) worst case. The array must be sorted.

  4. 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) + Θ(n)\Theta(n) = Θ(nlog⁡n)\Theta(n \log n). 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 = Θ(n2)\Theta(n^{2}). Do not confuse the expected count with the algorithm's time.

  5. 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) + Θ(n2)\Theta(n^{2}). By the Master theorem, p = log⁡2\log_2 7 ≈ 2.807, and f(n) = Θ(n2)\Theta(n^{2}) is polynomially smaller than n2.807n^{2.807}, so Case 1 gives T(n) = Θ(nlog⁡27)\Theta(n^{\log_{2} 7}) ≈ Θ(n2.807)\Theta(n^{2.807}). The number of scalar multiplications for pure Strassen on n=2kn = 2^{k} is 7k7^{k} = nlog⁡27n^{\log_2 7}.

  6. 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 O(n)O(n) 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) + O(n)O(n) = O(nlog⁡n)O(n \log n). The worst-case time is O(nlog⁡n)O(n \log n).

  7. 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 n=2k,T(n)=3n/2−2n = 2^{k}, T(n) = 3n/2 - 2 comparisons. This is optimal for finding both max and min.

  8. Recurrence with square root.
    For T(n) = 2T(⌊√n⌋) + log⁡n\log n, substitute n=2mn = 2^{m}. Then T(2m2^{m}) = 2T(2m/22^{m/2}) + m. Let S(m)=T(2m)S(m) = T(2^{m}). Then S(m) = 2S(m/2) + m. Master theorem: a = 2, b = 2, p = 1, f(m) = m = Θ(m1)\Theta(m^{1}), Case 2 with k = 0 gives S(m) = Θ(mlog⁡m)\Theta(m \log m). Back-substitute m = log⁡n\log n to get T(n) = Θ(log⁡n⋅log⁡log⁡n)\Theta(\log n \cdot \log\log n).

  9. Recurrence with decreasing fraction.
    For T(m) = T(3m/4) + 1, unroll: T(m)=T((3/4)km)+kT(m) = T((3/4)^{k} m) + k. Stop when (3/4)km=1,i.e.,k(3/4)^{k} m = 1, i.e., k = log_{4/3} m. Hence T(m) = Θ(log⁡m)\Theta(\log m). Any recurrence of the form T(n) = T(αn) + O(1)O(1) with 0 < α < 1 has Θ(log⁡n)\Theta(\log n) solution.

  10. 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 O(log⁡tn)O(\log_{t} n), where n is the number of keys. The exact maximum height is about log⁡t\log_t((n+1)/2). Hence the disk-page access cost is Θ(log⁡tn)\Theta(\log_{t} n).

Figure. The procedure divides a problem of size n into a subproblems of size n/b, solves them recursively, then combines the results. The recurrence is T(n) = aT(n/b) + f(n), with base T(1) = Θ(1).

Formulas

  • General D&C recurrence: T(n)=a T(n/b)+f(n)T(n) = a\,T(n/b) + f(n), with a≥1a \ge 1, b>1b > 1.
  • Watershed function: nlog⁡ban^{\log_b a}.
  • Master theorem:
    • Case 1: f(n)=O(nlog⁡ba−ϵ)  ⟹  T(n)=Θ(nlog⁡ba)f(n) = O(n^{\log_b a - \epsilon}) \implies T(n) = \Theta(n^{\log_b a}).
    • Case 2: f(n)=Θ(nlog⁡balog⁡kn)  ⟹  T(n)=Θ(nlog⁡balog⁡k+1n)f(n) = \Theta(n^{\log_b a} \log^k n) \implies T(n) = \Theta(n^{\log_b a} \log^{k+1} n).
    • Case 3: f(n)=Ω(nlog⁡ba+ϵ)f(n) = \Omega(n^{\log_b a + \epsilon}) and af(n/b)≤cf(n)a f(n/b) \le c f(n) for some c<1c < 1   ⟹  T(n)=Θ(f(n))\implies T(n) = \Theta(f(n)).
  • Binary search: T(n)=T(n/2)+Θ(1)=Θ(log⁡n)T(n) = T(n/2) + \Theta(1) = \Theta(\log n).
  • Merge sort and inversion count: T(n)=2T(n/2)+Θ(n)=Θ(nlog⁡n)T(n) = 2T(n/2) + \Theta(n) = \Theta(n \log n).
  • Expected inversions in a random permutation of nn distinct elements: n(n−1)4=Θ(n2)\frac{n(n-1)}{4} = \Theta(n^2).
  • Strassen: T(n)=7T(n/2)+Θ(n2)=Θ(nlog⁡27)≈Θ(n2.807)T(n) = 7T(n/2) + \Theta(n^2) = \Theta(n^{\log_2 7}) \approx \Theta(n^{2.807}).
  • Closest pair: T(n)=2T(n/2)+O(n)=O(nlog⁡n)T(n) = 2T(n/2) + O(n) = O(n \log n) with presorted y; strip check compares each point with at most 7 others.
  • MaxMin comparisons for n=2kn = 2^k: T(n)=3n2−2T(n) = \frac{3n}{2} - 2. For n=16n=16, T(16)=22T(16)=22.
  • Square-root recurrence: T(n)=2T(n)+log⁡n=Θ(log⁡nlog⁡log⁡n)T(n) = 2T(\sqrt{n}) + \log n = \Theta(\log n \log \log n).
  • Fractional recurrence: T(m)=T(3m/4)+1=Θ(log⁡m)T(m) = T(3m/4) + 1 = \Theta(\log m).
  • B-tree disk accesses: Θ(log⁡tn)\Theta(\log_t n), where tt is the minimum degree and nn is the number of keys.

Worked examples

Example 1 (Master theorem with logarithmic factor).
Solve T(n)=2T(n/2)+nlog⁡nT(n) = 2T(n/2) + n \log n.

  1. a=2a=2, b=2b=2, so log⁡ba=log⁡22=1\log_b a = \log_2 2 = 1.
  2. f(n)=nlog⁡n=Θ(n1log⁡1n)f(n) = n \log n = \Theta(n^1 \log^1 n).
  3. This matches Case 2 with k=1k=1. Therefore T(n)=Θ(n1log⁡1+1n)=Θ(nlog⁡2n)T(n) = \Theta(n^1 \log^{1+1} n) = \Theta(n \log^2 n).
    Answer: Θ(nlog⁡2n)\Theta(n \log^2 n).

Example 2 (MaxMin comparisons).
Find the number of comparisons used by the standard D&C MaxMin algorithm on n=16n=16 elements.
Base: T(2)=1T(2)=1. Recurrence: T(n)=2T(n/2)+2T(n)=2T(n/2)+2.
T(4)=2(1)+2=4T(4)=2(1)+2=4.
T(8)=2(4)+2=10T(8)=2(4)+2=10.
T(16)=2(10)+2=22T(16)=2(10)+2=22.
Formula: 3n/2−2=24−2=223n/2 - 2 = 24 - 2 = 22.
Answer: 22 comparisons.

Example 3 (Strassen scalar multiplications).
Using pure Strassen's algorithm recursively down to 1×11 \times 1 scalars, how many scalar multiplications are needed to multiply two 8×88 \times 8 matrices?
For n=2kn=2^k, the number of multiplications is 7k7^k. Here n=8=23n=8=2^3, so k=3k=3.
Number =73=343= 7^3 = 343.
For comparison, the standard algorithm needs 83=5128^3 = 512 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. T(n)=T(n/2)+O(1)T(n)=T(n/2)+O(1), Θ(log⁡n)\Theta(\log n); 2. T(n)=2T(n/2)+Θ(n)T(n)=2T(n/2)+\Theta(n), Θ(nlog⁡n)\Theta(n \log n); 3. T(n)=7T(n/2)+Θ(n2)T(n)=7T(n/2)+\Theta(n^2), Θ(n2.81)\Theta(n^{2.81}); 4. T(n)=2T(n/2)+2T(n)=2T(n/2)+2, Θ(n)\Theta(n).
Solution: A-3, B-1, C-2, D-4.

Comparison

Table 1: Algorithm paradigms.

AlgorithmParadigmKey idea
Dijkstra single-source shortest pathsGreedyRepeatedly finalize the unvisited vertex with minimum tentative distance.
Floyd–Warshall all-pairs shortest pathsDynamic programmingBuild shortest paths using intermediate vertices one by one.
Strassen matrix multiplicationDivide and conquerReduce 8 block multiplications to 7.
Binary searchDivide and conquerDiscard half the search space each step.
Merge sortDivide and conquerDivide, sort halves recursively, merge.
Closest pair of pointsDivide and conquerSolve halves, then check strip.

Table 2: Recurrences and tight complexities.

AlgorithmRecurrenceComplexity
Binary searchT(n)=T(n/2)+O(1)T(n)=T(n/2)+O(1)Θ(log⁡n)\Theta(\log n)
Merge sort / inversion countT(n)=2T(n/2)+Θ(n)T(n)=2T(n/2)+\Theta(n)Θ(nlog⁡n)\Theta(n \log n)
Standard matrix multiplicationT(n)=8T(n/2)+Θ(n2)T(n)=8T(n/2)+\Theta(n^2)Θ(n3)\Theta(n^3)
Strassen matrix multiplicationT(n)=7T(n/2)+Θ(n2)T(n)=7T(n/2)+\Theta(n^2)Θ(nlog⁡27)≈Θ(n2.81)\Theta(n^{\log_2 7}) \approx \Theta(n^{2.81})
MaxMinT(n)=2T(n/2)+2T(n)=2T(n/2)+2Θ(n)\Theta(n), exactly 3n/2−23n/2-2 comparisons for n=2kn=2^k
Closest pair (2D, presorted y)T(n)=2T(n/2)+O(n)T(n)=2T(n/2)+O(n)O(nlog⁡n)O(n \log n)

Table 3: Search structures.

StructureDisk accessesParadigm
Binary search on sorted arrayNot disk-specific; O(log⁡n)O(\log n) comparisonsDivide and conquer
B-tree searchΘ(log⁡tn)\Theta(\log_t n) disk pagesBalanced multiway tree search (not D&C)

Revision summary

  • D&C recurrence: T(n)=aT(n/b)+f(n)T(n)=aT(n/b)+f(n); compare f(n)f(n) with nlog⁡ban^{\log_b a}.
  • Watershed function nlog⁡ban^{\log_b a} is the leaf-level cost in the recursion tree.
  • Strassen uses 7 multiplications, not 8; complexity Θ(nlog⁡27)≈n2.81\Theta(n^{\log_2 7}) \approx n^{2.81}.
  • Expected inversions in a random permutation is n(n−1)/4=Θ(n2)n(n-1)/4 = \Theta(n^2), but counting them takes Θ(nlog⁡n)\Theta(n \log n).
  • MaxMin comparisons on n=2kn=2^k: 3n/2−23n/2 - 2; for n=16n=16 it is 22.
  • Closest pair strip check: at most 7 comparisons per point; total time O(nlog⁡n)O(n \log n).
  • T(n)=2T(n)+log⁡n=Θ(log⁡nlog⁡log⁡n)T(n)=2T(\sqrt{n})+\log n = \Theta(\log n \log \log n).
  • T(m)=T(3m/4)+1=Θ(log⁡m)T(m)=T(3m/4)+1 = \Theta(\log m).
  • Dijkstra is greedy; Floyd–Warshall is dynamic programming.
  • B-tree search disk accesses: Θ(log⁡tn)\Theta(\log_t n).
  • In Master theorem Case 2 with f(n)=Θ(nlog⁡balog⁡kn)f(n)=\Theta(n^{\log_b a}\log^k n), multiply the log factor: log⁡k+1n\log^{k+1} n.
  • Binary search recurrence is T(n)=T(n/2)+O(1)T(n)=T(n/2)+O(1), not 2T(n/2)2T(n/2).

Previous-year exam insights

  • Direct questions ask for the solution of a recurrence using the Master theorem; always identify aa, bb, f(n)f(n), and compare with nlog⁡ban^{\log_b a} first.
  • Traps: forgetting the extra log⁡n\log n in Case 2 (e.g., nlog⁡nn \log n gives nlog⁡2nn \log^2 n); misapplying Case 3 without the regularity condition.
  • Numeric questions ask for MaxMin comparisons on a small nn such as 16; use 3n/2−23n/2 - 2 or unroll the recurrence.
  • Strassen questions ask for the number of multiplications or the final complexity; remember 7 and n2.81n^{2.81}, not 8 or n2.67n^{2.67}.
  • Expected inversions is a classic trap: the expected number is Θ(n2)\Theta(n^2), while the D&C counting algorithm is Θ(nlog⁡n)\Theta(n \log n).
  • 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 (n=2mn=2^m or unrolling), not the standard Master theorem directly.
  • B-tree disk-access questions expect Θ(log⁡tn)\Theta(\log_t n); 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:

  1. Solve T(n)=aT(n/b)+f(n)T(n)=aT(n/b)+f(n) using all three Master theorem cases, including Case 2 with log⁡kn\log^k n.
  2. Explain what the watershed function nlog⁡ban^{\log_b a} represents in the recursion tree.
  3. Compute the number of comparisons for MaxMin on n=16n=16 by both unrolling and the closed formula.
  4. Write the recurrence and tight complexity for Strassen's matrix multiplication and compute scalar multiplications for a given power of 2.
  5. Solve T(n)=2T(n)+log⁡nT(n)=2T(\sqrt{n})+\log n by the substitution n=2mn=2^m.
  6. Solve T(m)=T(3m/4)+1T(m)=T(3m/4)+1 by unrolling.
  7. Derive the expected number of inversions in a random permutation and distinguish it from the inversion-counting algorithm's time.
  8. Describe the closest pair strip check and state why at most 7 comparisons per point are needed.
  9. Classify Dijkstra, Floyd–Warshall, Strassen, and binary search by their algorithm paradigm.
  10. Compute the number of disk-page accesses in a B-tree search given nn and tt.

Attempt the sub-topic test first, then the subject test.

Pages 1–2 of 2