Chapter 1: Divide and Conquer

Chapter 1: Divide and Conquer · Study Questions
Divide · Conquer · CombineMaster theoremMerge sort and quicksortFast exponentiationQuickhullClosest pair of pointsKaratsuba and Strassen
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · Paradigm

Question 1: Where Is the Work Done?

In merge sort and quicksort, which step does the real work (almost all the comparisons)?

C: Merge sort splits the array in the middle (O(1)) and does its work while merging the sorted halves. In quicksort the work is in partitioning around the pivot; the parts are sorted in place, so there is no merge step.

Multiple Choice · Recurrences

Question 2: Which Recurrence?

Which of these algorithm and recurrence pairings is correct?

C: power makes a single recursive call (y is computed once) and adds a constant number of multiplications → Θ(log n). Merge sort is 2T(n/2) + Θ(n) and binary search is T(n/2) + Θ(1); fib makes two calls, so T(n) = T(n − 1) + T(n − 2) + Θ(1) → exponential.

Classic Question · Recurrences

Question 3: Solving with the Master Theorem

Solve the three recurrences below with the master theorem; for each, give a, b, nlogba, the case and the result.
(a) T(n) = 8T(n/2) + n²   (b) T(n) = 2T(n/2) + n²   (c) T(n) = 4T(n/2) + n²

Show answer
a, bnlogbaf(n)caseT(n)
(a)8, 2n³n²1 (leaves)Θ(n³)
(b)2, 2nn²3 (root)Θ(n²)
(c)4, 2n²n²2 (equal)Θ(n² log n)

(a) is the recurrence of naive block matrix multiplication: with 8 sub-products there is no gain. In (b) the regularity condition holds: 2·(n/2)² = n²/2 ≤ ½·n². In (c) every level sums to n², and there are log₂ n levels.

f(n) is compared with nlogba; the larger one wins, and if they are equal an extra log n factor appears.

Multiple Choice · Recurrences

Question 4: A Square-Root Recurrence

What is the solution of T(n) = 2T(n/4) + √n by the master theorem?

A: a = 2, b = 4 → nlog₄2 = n1/2 = √n. f(n) = √n is of the same order → case 2 → Θ(√n · log n). The Θ(√n) option forgets the log factor; Θ(n · log n) comes from assuming the critical exponent is 1.

Multiple Choice · Recurrences

Question 5: Limits of the Master Theorem

For which of these recurrences can the master theorem be applied directly?

D: a = 9, b = 3 → nlog₃9 = n² = f(n) → case 2 → Θ(n² log n). A is a decrease by a constant (there is no b), B is an unequal split, and in C f(n) = n/log n is not polynomially smaller than n (it falls between cases). These are solved with a recursion tree or substitution.

Output Question · Fast Exponentiation

Question 6: Multiplication Counter

The fast exponentiation code was changed to count the multiplications it makes. What does the program print? Compare with the naive method (n − 1 multiplications in a loop).

static int mults = 0;
static long power(long x, int n) {
  if (n == 0) return 1;
  long y = power(x, n / 2);
  if (n % 2 == 0) { mults += 1; return y * y; }
  mults += 2;
  return x * y * y;
}
// main:
long s = power(2, 20);
System.out.println(s + " " + mults);
Show answer

Output: 1048576 7

  • Sequence of n: 20 → 10 → 5 → 2 → 1 → 0.
  • Multiplications: n = 1 (odd): 2, n = 2 (even): 1, n = 5 (odd): 2, n = 10: 1, n = 20: 1 → 7.
  • The naive method does 19 multiplications. Fast exponentiation does at most 2·(⌊log₂ n⌋ + 1): T(n) = T(n/2) + O(1) = O(log n).

Multiple Choice · Fast Exponentiation

Question 7: Computing the Half Twice

power method: if the variable y is dropped and for even n the code return power(x, n / 2) * power(x, n / 2); is used instead (similarly for odd n), what is the running time?

B: Two calls plus constant extra work: T(n) = 2T(n/2) + Θ(1). In the master theorem a = 2, b = 2, log₂ 2 = 1 > 0 → case 1 → Θ(n). The recursion tree has log₂ n levels but n leaves. power(3, 11) makes 31 calls with this version and power(2, 16) makes 63; the single-call version makes 5 and 6.

Output Question · Convex Hull

Question 8: Orientation Test

A = (1, 1) is the leftmost and B = (9, 3) the rightmost point. Compute cross(A, B, X) for P, Q, R, S, T. In the upper set, which point does Quickhull choose as C, and which point inside triangle ACB is discarded?
cross = (B.x − A.x)(P.y − A.y) − (B.y − A.y)(P.x − A.x)

A(1,1)B(9,3)P(3,6)Q(6,0)R(5,7)S(8,5)T(5,4)
Show answer

cross = 8·(y − 1) − 2·(x − 1):

PQRST
cross36-18401816
sideleftrightleftleftleft
  • Upper (left) set: P, R, S, T. The largest cross is R (40) → C = R is on the hull. The lower set has only the point Q (−18).
  • For T, cross(A, R, T) = 4·3 − 6·4 = −12 and cross(R, B, T) = 4·(−3) − (−4)·0 = −12: it is to the right of both edges → inside the triangle, T is discarded.
  • cross(A, R, P) = 8 > 0 → P is outside AR; cross(R, B, S) = 4 > 0 → S is outside RB. The recursion continues with these two sets.

Multiple Choice · Convex Hull

Question 9: Quickhull's Worst Case

When does Quickhull take O(n²)?

B: Each call scans S once: O(|S|). If the split is always unbalanced, like 1 and |S| − 2, then T(n) = T(n − 1) + O(n) = O(n²) (e.g. the points lie on a convex curve and the farthest point is always next to an end). If most points are inside the first triangle, on the contrary, they are all discarded in one step; that is the fastest case.

Output Question · Closest Pair of Points

Question 10: Scanning the Middle Strip

In the closest pair algorithm the line L is at x = 10; the two halves returned dLeft = 5, dRight = 4. The list Y (sorted by y) is: (11,0), (8,1), (11,4), (7,6), (12,9), (7,11), (15,12). Which points form the strip, how many distances are computed, and which d does the code return?

double d = Math.min(dLeft, dRight);
List<Point> s = new ArrayList<>();  // strip
for (Point p : Y)                   // y order
  if (Math.abs(p.x - m.x) < d) s.add(p);
for (int i = 0; i < s.size(); i++) {
  Point p = s.get(i);
  for (int j = i + 1; j < s.size()
       && s.get(j).y - p.y < d; j++)
    d = Math.min(d, distance(p, s.get(j)));
}
return d;
Show answer
  • d = min(5, 4) = 4. Strip |x − 10| < 4: (15,12) is outside; 6 points remain (y order is kept).
  • Each point is compared only with the following points whose y difference is less than d; as d shrinks, the inner loop stops earlier: (11,0)–(11,4) (y difference 4) is never computed.
  • Computed distances: (11,0)–(8,1) = 3.16, (8,1)–(11,4) = 4.24, (11,4)–(7,6) = 4.47, (7,6)–(12,9) = 5.83, (12,9)–(7,11) = 5.39 → 5 distances.
  • Result: d = 3.16, pair (11,0)–(8,1). This pair crosses L (one is on the left, one on the right); it could not have been found within the two halves separately.

Multiple Choice · Closest Pair of Points

Question 11: Why At Most 7 Neighbors?

It is enough to compare a point p in the strip with at most 7 following points. What is the reason?

D: The δ × 2δ rectangle above p is divided into 8 squares of side δ/2. Two points in one square would be in the same half and at distance at most δ/√2 < δ, contradicting that the closest distance of that half is δ. The strip can be long; what is bounded is the number of neighbors of each point.

Step-by-Step Trace · Karatsuba

Question 12: Tracing Karatsuba

System.out.println(karatsuba(47, 85)) and the value each call returns.

47 · 85

main calls karatsuba(47, 85). The code makes three recursive calls: z0 = b·d, z1 = (a + b)(c + d), z2 = a·c.

47 · 85

Both numbers are ≥ 10 → not the base case. n = 2, p = 10¹ = 10: a = 4, b = 7, c = 8, d = 5 (47 = 4·10 + 7, 85 = 8·10 + 5).

47 · 857 · 5= 35

z0 = karatsuba(b, d) = karatsuba(7, 5): 7 < 10 → base case, directly 35.

47 · 857 · 5= 3511 · 13

z1 = karatsuba(a + b, c + d) = karatsuba(11, 13): both ≥ 10. n = 2, p = 10 → a = 1, b = 1, c = 1, d = 3.

47 · 857 · 5= 3511 · 131 · 3= 3

Inner z0 = karatsuba(1, 3) = 3 (base case).

47 · 857 · 5= 3511 · 131 · 3= 32 · 4= 8

Inner z1 = karatsuba(1 + 1, 1 + 3) = karatsuba(2, 4) = 8 (base case).

47 · 857 · 5= 3511 · 131 · 3= 32 · 4= 81 · 1= 1

Inner z2 = karatsuba(1, 1) = 1 (base case).

47 · 857 · 5= 3511 · 13= 1431 · 3= 32 · 4= 81 · 1= 1

11 · 13 = z2·p² + (z1 − z2 − z0)·p + z0 = 1·100 + (8 − 1 − 3)·10 + 3 = 143.

47 · 857 · 5= 3511 · 13= 1431 · 3= 32 · 4= 81 · 1= 14 · 8= 32

z2 = karatsuba(a, c) = karatsuba(4, 8) = 32 (base case).

47 · 85= 39957 · 5= 3511 · 13= 1431 · 3= 32 · 4= 81 · 1= 14 · 8= 32

47 · 85 = 32·100 + (143 − 32 − 35)·10 + 35 = 3200 + 760 + 35 = 3995. 7 calls, 5 base multiplications (the schoolbook method would do 4 digit products; the gain shows for large n).

long karatsuba(long x, long y) {
  if (x < 10 || y < 10) return x * y;
  int n = Math.max(digits(x), digits(y));
  long p = (long) Math.pow(10, n / 2);
  long a = x / p, b = x % p;
  long c = y / p, d = y % p;
  long z0 = karatsuba(b, d);
  long z1 = karatsuba(a + b, c + d);
  long z2 = karatsuba(a, c);
  return z2 * p * p
       + (z1 - z2 - z0) * p + z0;
}

Multiple Choice · Karatsuba

Question 13: Multiplications in Karatsuba

n = 2k-digit numbers are multiplied with Karatsuba; assume each split gives halves of exactly n/2 digits and the base case is a single-digit product. How many single-digit multiplications are made for n = 8?

C: The number of multiplications triples at each level: 3log₂8 = 3³ = 27. The schoolbook method does n² = 64 multiplications (naive divide and conquer with 4 sub-products also gives 4³ = 64). 24 = 3·8 comes from miscounting the levels. In general nlog₂3 ≈ n1.585.

Step-by-Step Trace · Strassen

Question 14: Strassen Step by Step

Compute the product of A = [[2, 1], [0, 3]] and B = [[1, 4], [5, 2]] with Strassen's seven multiplications.

P1?P2?P3?P4?P5?P6?P7?

A = [[2, 1], [0, 3]], B = [[1, 4], [5, 2]]. Since the blocks are 1×1, each block is a single number: A11 = 2, A12 = 1, A21 = 0, A22 = 3; B11 = 1, B12 = 4, B21 = 5, B22 = 2.

P14P2?P3?P4?P5?P6?P7?

P1 = A11·(B12 − B22) = 2·(4 − 2) = 4.

P14P26P3?P4?P5?P6?P7?

P2 = (A11 + A12)·B22 = (2 + 1)·2 = 6.

P14P26P33P4?P5?P6?P7?

P3 = (A21 + A22)·B11 = (0 + 3)·1 = 3.

P14P26P33P412P5?P6?P7?

P4 = A22·(B21 − B11) = 3·(5 − 1) = 12.

P14P26P33P412P515P6?P7?

P5 = (A11 + A22)·(B11 + B22) = (2 + 3)·(1 + 2) = 15.

P14P26P33P412P515P6-14P7?

P6 = (A12 − A22)·(B21 + B22) = (1 − 3)·(5 + 2) = -14.

P14P26P33P412P515P6-14P710

P7 = (A11 − A21)·(B11 + B12) = (2 − 0)·(1 + 4) = 10.

P14P26P33P412P515P6-14P710C =7101567 multiplications (classic 8)

C11 = P5 + P4 − P2 + P6 = 15 + 12 − 6 − 14 = 7 · C12 = P1 + P2 = 10 · C21 = P3 + P4 = 15 · C22 = P5 + P1 − P3 − P7 = 15 + 4 − 3 − 10 = 6. Check: 2·1 + 1·5 = 7 ✓.

P1 = A11 * (B12 - B22);
P2 = (A11 + A12) * B22;
P3 = (A21 + A22) * B11;
P4 = A22 * (B21 - B11);
P5 = (A11 + A22) * (B11 + B22);
P6 = (A12 - A22) * (B21 + B22);
P7 = (A11 - A21) * (B11 + B12);
C11 = P5 + P4 - P2 + P6;
C12 = P1 + P2;
C21 = P3 + P4;
C22 = P5 + P1 - P3 - P7;

Multiple Choice · Strassen

Question 15: Scalar Multiplications in Strassen

Two 16 × 16 matrices are multiplied with Strassen's algorithm with a 1 × 1 base case. How many scalar multiplications are made, and how many does the classic triple-loop method make?

A: n = 2⁴ → 7⁴ = 2401 scalar multiplications; the classic method does n³ = 16³ = 4096. 343 and 512 are for n = 8. The recurrence T(n) = 7T(n/2) + Θ(n²) → Θ(nlog₂7) ≈ Θ(n2.807); the gain comes entirely from reducing 8 to 7.

Classic Question · Review

Question 16: The Largest Element

Write the method int findMax(int[] a, int lo, int hi) that finds the largest element of an array by divide and conquer. What are the recurrence, its solution, and the number of comparisons for n elements?

Show answer
int findMax(int[] a, int lo, int hi) {
  if (lo == hi) return a[lo];
  int mid = (lo + hi) / 2;
  int left = findMax(a, lo, mid);
  int right = findMax(a, mid + 1, hi);
  return Math.max(left, right);
}

T(n) = 2T(n/2) + O(1) → a = 2, b = 2, nlog₂2 = n > 1 → case 1 → Θ(n).

Each merge makes 1 comparison; the recursion tree has n leaves and n − 1 internal nodes → n − 1 comparisons, the same as a simple loop. Divide and conquer improves here not the time but the parallelism: depth O(log n).

Multiple Choice · Review

Question 17: Choosing a Paradigm

Which of these pairings of problem structure and suitable paradigm is correct?

D: Divide and conquer suits independent subproblems, dynamic programming suits overlapping ones, and the greedy approach suits cases where the locally best choice leads to the global optimum. With overlapping subproblems, plain divide and conquer repeats the same work (Fibonacci: O(φⁿ)).

The End

Chapter 1: Divide and Conquer: Study Questions
Lecture slides: Chapter 1: Divide and Conquer · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.