IITP · Semester ISummaries · Quizzes · Assignments

Lecture summary

DAA 03 Oct

03 Oct 20261h 26m2 parts

Source: IITP-big-files\Sem-1\ECS5101-DAA\Lectures\2026-10-03 0000 DAA 03 Oct\summary\notes.md

Overview

This class first recaps the maximum subarray problem and its three solution mechanisms, then covers Strassen's matrix multiplication (reducing complexity from n³ to n^2.81) and the closest pair problem in 1D and 2D as the final divide-and-conquer problem. The second half motivates and introduces recurrence relations — factorial, Fibonacci, merge sort, binary search, and Tower of Hanoi — and presents the three methods for solving them: substitution (guessing), recurrence tree, and the master's theorem. It sits at the transition point where divide-and-conquer algorithms end and formal complexity analysis of recursive algorithms begins.

Learning outcomes

  • Explain the three mechanisms for the maximum subarray problem and the divide-and-conquer closest pair approach.
  • State Strassen's algorithm's complexity improvement and its 7 products P1–P7 for 2×2 matrices.
  • Solve the closest pair problem in 1D and 2D using median-based division (left, right, cross distances).
  • Write recurrence relations for factorial, Fibonacci, Tower of Hanoi, merge sort, and binary search.
  • Distinguish base case, recursive case, and well-formed recursion.
  • Apply the substitution method and recurrence tree method to sketch a solution.
  • State the master's theorem form and its conditions, and identify case 1.

Core concepts

Maximum subarray recap

Three mechanisms: (1) a heuristic variant of brute force where repetitive sets are not used, differing only in their order; (2) dynamic programming leading to a computational complexity of n log n, assuming the maximum sum could lie in the left, right, or cross section; (3) a divide-and-conquer algorithm that finds the maximum sum value in order of n time by preserving the last-stage output of maximum sum and current sum. It matters as the template for divide-and-conquer thinking (left/right/cross) reused by the closest pair problem.

Strassen's algorithm

A famous divide-and-conquer algorithm for matrix multiplication that reduces computational complexity from n³ to n^2.81. Though the reduction looks small, at higher sizes the number of calculations is reduced significantly. Its aim is to minimize multiplication operations, compensated by more addition operations, so both exact solution and reduction in multiplications are achieved. It works for 2, 4, 8, 16, 32, 64 order matrices; at the lowest stage a 4×4 matrix can be assumed as a combination of multiple 2×2 matrices (base condition is the 2×2 product).

Closest pair problem

In a space with n points, identify which two points are closest / at the least distance — the pair of points with the smallest distance between them within a given collection of n points. It finds relevance in applications across domains from computational geometry to pattern recognition and beyond.

Closest pair — divide and conquer approach

Similar to maximum subarray: divide points based on the median; find the distance among points on the left side, among points on the right side, and the cross-section distance; whichever is lower is the result. Iteratively divide down to the one-point (or two-point) level, then combine.

Closest pair — worst-case complexity

With an efficient initial sort, the closest pair problem runs in Θ(n log n) in the worst case. Sort the points by x once (Θ(n log n)); the strip around the median is then scanned in y-order, and the packing argument shows at most 7 neighbours need be compared per point, so the strip work is linear. The recursion is therefore T(n) = 2T(n/2) + Θ(n), which solves to Θ(n log n). The naive all-pairs approach compares every pair and is much worse.

Median rule

Irrespective of the number of dimensions, the median is always calculated based on the x coordinate. In all books (for 2D, 3D, including Cormen) the closest pair problem is referred with respect to the x coordinate only. (You could use the y coordinate if you change the algorithm accordingly; both would be correct.)

Base/stopping condition for 1D closest pair

Stop when just two points remain (at least 2 points are needed to estimate a distance). Compute left distance, right distance, and middle/cross distance; whichever is smaller propagates upwards.

2D closest pair idea

Compute dL (minimum distance on left half) and dR (minimum distance on right half); let d = minimum of dL and dR. Then only points within proximity of dimension/length d from the median need to be considered — make a square of d; distances among points in this region will be at most less than 2d. There is no need to calculate all point distances on the left and right.

Recurrence

A mathematical relationship or equation that describes the recursion of a function or a sequence in terms of the previous value. Along with the base condition, an initial condition may be specified to determine the value of the function for the smallest input size where the relation may or may not hold.

Well-formed recursion

All recursions that reduce their value in every iteration and tend towards the base case. If a recursion does not follow this property, the algorithm will never converge.

Base case and recursive case

Base case: the stopping point or terminating point of the recursive algorithm (e.g., single element level in merge sort; set of two points in closest pair). Recursive case: the iterative division of a problem — e.g., in merge sort, every time the same function is called to partition the array into subarrays. Seen in binary search, quick sort, closest pair, maximum subarray.

Substitution method

Also called the guessing method / method of guessing. Guess a solution (e.g., the computational complexity of T(n) = 2T(n/2) + n), then prove the guess correct via induction. The guess comes with experience; "we cannot directly apply."

Recurrence tree method

Draw the tree structure formed by the divide-and-conquer subproblems (big problem → subproblem → sub-subproblem), observe how the algorithm behaves, and declare its computational complexity.

Master's theorem

The "famous" and "best" mechanism; a specialized method with a specific set of cases. If the cases are valid, substitute values and get the complexity directly. For T(n) = aT(n/b) + f(n): n is the input size, a is the number of subproblems, n/b is each subproblem's size; require a ≥ 1, b > 1 (if b is not greater than 1 the algorithm will never converge — the problem must be reduced), and f(n) > 0 and asymptotically positive. Procedure: first determine a, b, f(n) from the recurrence, immediately compute n^(log_b a), and compare it with f(n).

Ignoring boundary/corner cases

In practice we neglect technical details (floor/ceiling, unequal halves) when stating and solving recurrences; otherwise estimating complexity becomes very difficult. There is a hidden assumption here.

Recurrence applicability

Recurrence relations apply only to problems where we make repetitive calls to the same function (iterative division of the problem). All divide-and-conquer problems are recurrence problems; in most cases the division follows a logarithmic order (n, n/2, n/4, n/8, n/16).

Base condition design

When designing an algorithm you must decide the base condition yourself (e.g., merge sort stops when one element remains; in the quick-sort/insertion hybrid, when the array element count is 5, apply insertion sort directly). If a recursion is given explicitly to solve, the base condition and initial conditions are given to you.

Deriving a recurrence for a complex problem

Consider the number of inputs, but also analyze how the problem can be solved — in one go or with multiple similar calls (e.g., factorial removes one element and recurses; like a for loop repeating again and again).

Formulas and maths

  • Strassen complexity: (n^3 \rightarrow n^{2.81}) — old vs. new complexity of matrix multiplication; used to show the gain from Strassen's algorithm.
  • Strassen recurrence structure: 7 half iterations (products P1–P7) with order (n^2) work per operation; using the master's theorem, (n^{2.81}) is established — used to derive Strassen's complexity.
  • Euclidean distance 1D: (\sqrt{(x_2 - x_1)^2}) — distance between two points on a line; used in the 1D closest pair.
  • Euclidean distance 2D: (\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}) — distance between two planar points; used in the 2D closest pair.
  • Euclidean distance 3D: (\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2}) — distance between two points in space; used in the 3D closest pair.
  • Generalized k-dimensional distance: sum over (i = 1) to (k) dimensions of ((x_i - y_i)^2) [unclear] — transcript garbled; intended as the squared-difference sum under the root.
  • 1D closest pair base cases: if (n = 1), output (s) is infinity; if (n = 2), the distance formula can be applied — used as the stopping condition.
  • Median position: for sorted points, (n/2) if even, or (n/2 + 1) if odd — used to split points into halves.
  • Closest pair recurrence: (T(n) = 2T(n/2) + \Theta(n)) — two halves plus linear strip scan (at most 7 neighbours per point), giving (\Theta(n \log n)) worst case after a one-time (\Theta(n \log n)) sort by x.
  • Merge sort recurrence: (T(n) = 2T(n/2) + n) (also written (2T(n/2) + \Theta(n))) — problem divided into 2 problems of (n/2), with order (n) for merging; used to derive (n \log n).
  • Merge sort split sizes: for (n = 13), the two subarrays are of sizes 6 and 7 — (\lfloor n/2 \rfloor) and (\lceil n/2 \rceil).
  • Merge sort recursion depth: for (n = 64), depth is 6 — (\log_2 64).
  • Merge sort merge count: sorting 8 elements performs 7 merge operations — (n - 1) merges in total.
  • Binary search recurrence: (T(n/2) + 1) — problem divided into (n/2), with merger taking order 1 only; used to derive its complexity.
  • Binary search worst case: for (n = 1024), at most 10 comparisons — (\log_2 1024).
  • Factorial: (f(0) = 1) (also (f(1) = 1) in some books); (f(n) = n \times f(n-1)) — base and recursive case of the factorial recurrence.
  • Fibonacci: (f(0) = 0), (f(1) = 1); (f(n) = f(n-1) + f(n-2)) — base and recursive case of the Fibonacci recurrence.
  • Tower of Hanoi: (T(n) = 2T(n-1) + 1) — one problem divided into two subproblems plus one order-of-one result; used to model the disk-moving recurrence.
  • Master's theorem form: (T(n) = aT(n/b) + f(n)), with (a \ge 1), (b > 1), (f(n) > 0) asymptotically positive — general recurrence the theorem applies to.
  • Comparison quantity: (n^{\log_b a}) compared with (f(n)) — the first step in applying the master's theorem.
  • Master's theorem case 1 (as stated): if (f(n) = n^{\log_b a}) and we have to reduce something from it — e.g., (n^2) versus (n^{2.26}), reduce by (\varepsilon = 0.26) — then we are in case 1 and the complexity is (n^{\log_b a}).
  • Substitution sketch for (T(n) = 2T(\lfloor n/2 \rfloor) + n), guess (n \log n): show (T(n) \le c\, n \log n); assume the bound holds for (n/2), giving (c \lfloor n/2 \rfloor \log(\lfloor n/2 \rfloor) + n), ultimately (c\, n \log(n/2) + n); using (\log(n/2) = \log n - \log 2), this is (\le c\, n \log n - (\text{something}) < c\, n \log n) — used to prove the guess by induction.
  • Recurrence tree for merge sort: levels show (n), (n/2), (n/2), (n/4), (n/4), … down to single elements; (n) appears (\log n) times, giving (n \log n) — used to derive merge sort's complexity.
  • Note on the "17" example: in (T(n)) with a term like 17, the 17 can be treated as an add-on and ignored (again ignoring boundary cases).

Worked examples

8×8 matrix example (Strassen context): Two matrices of order 8×8; one is taken as an identity matrix so the product is easy. An 8×8 matrix can be assumed as multiple 4×4 matrices: A11, A12, A21 are each 4×4 matrices, dividing the product accordingly. All 7 products are computed. Because the matrix is an identity matrix (maximum zeros, only diagonal elements), the multiplication can be performed and the result obtained in a single slot; going purely with Strassen's, the 4×4 blocks would be further divided into 2×2 and results for m1–m7 combined. (The instructor says he will share the PPT for this problem.)

1D closest pair example: Points plotted on a line: −4, −2, 1, 4, 5, 10 (six points, sorted). Median position: n by 2 = 3, so the median is 1. Subsets: {−4, −2, 1} and {4, 5, 10}. Further division: {−4, −2} and {1}; {4, 5} and {10}. Distances: |−4 − (−2)| = 2; distance between −2 and 1 = 3 (instructor first said 2, then corrected to 3); 4 and 5 distance = 1; 5 and 10 distance = 5. Left minimum: −4 and −2 with distance 2. Right minimum: 4 and 5 with distance 1. Cross-section: median was 1, and no point is less than distance 1 from the median, so the result is 4 and 5 with value 1 — the closest pair.

2D closest pair example: Points taken: (1, 2), (3, −4), (−1, 6), (5, 7), (2, 3), (−2, −4). Arranged by x coordinate: (−2, −4), (−1, 6), (1, 2), (3, −4), (5, 7). With 5 points, median position is n by 2 plus 1 = 3, so (1, 2) is the median point. Bifurcation: {(−2, −4), (−1, 6), (1, 2)} and {(3, −4), (5, 7)}; further division holds (1, 2) aside, leaving {(−2, −4), (−1, 6)} and the already-two-point set. Distances computed among these pairs; then DL, DR, and combined DC are obtained and the smaller is the resultant. Sample distance estimation shown: Euclidean with (−2 − (−1))² plus (−4 − 6)² [as spoken; the instructor's spoken formula is garbled — see Flagged for later].

Factorial of 6: 6 × 5 × 4 × 3 × 2 × 1 = 30 × 24 = 720. Via the recurrence: 6 × f(5) → 6 × 5 × f(4) → 6 × 5 × 4 × f(3) → … → 6 × 5 × 4 × 3 × 2 × f(1) → 6 × 5 × 4 × 3 × 2 × 1 (f(0) = 1), again reaching 720.

Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … (each term is the sum of the previous two). The transcript's enumeration is partly garbled (e.g., "28 and 13 is 34", "20, 3, 5, 8") — the intended series is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.

Tower of Hanoi with 3 disks: Move sequence described — shift the smaller disk, move the bigger one, then the small one goes here, the medium one goes here, and lastly this comes; original problem used 63 disks.

Substitution example: T(n) = 2T(floor(n/2)) + n, guess n log n, proved via induction as above.

Recurrence tree example: Merge sort tree; n appears at each of log n levels → n log n.

Master's theorem comparison example: n² versus n^2.26 → ε = 0.26, case 1, complexity n^(log_b a).

Merge sort size counts: On 13 elements the split gives subarrays of 6 and 7 (floor and ceil of n/2). On 64 elements the recursion depth is 6, since log₂64 = 6. On 8 elements the total number of merge operations is 7 (n − 1).

Binary search count: On 1024 elements the worst case takes 10 comparisons, since log₂1024 = 10.

Questions asked in class

Q (step 4 of 1D closest pair): What happens in step 4, and what are the "2 to 3 points" that continue? A: We stop when just two points remain. In each half there are two points; compute the distance among them — call it distance L on the left, distance r on the right, and in the intersection compute distance c; whichever is smaller, declare those two points as the closest and store the result, proceeding similarly upward until the two points with the minimum distance are identified. The base/stopping condition is the pair of two points.

Q (2D slide): Is it correct that after making the median we run the algorithm to identify which points are closest to the median on the left and right? A: No — it is the points having the least distance, not closest to the median. First identify the two points having dL (minimum distance) on the left, similarly the minimum distance on the right; then take the minimum of these and make a square box of dimension d. Very few points will have distance less than or equal to d; if some point is found, declare it the closest, otherwise d is the closest. The distance between two points in the left half and the distance between two points in the right half are independent of their distance from the median — they can be anywhere. Because it is recursive, the halves have already been further divided by different medians.

Q: Could something like a minimum-subarray algorithm be used to put all points of one left half into a single array? A: It is similar to maximum sum / minimum sum, but with the little difference of making this square; otherwise it is equivalent to that algorithm.

Q (median for two points): How is the median calculated for two points — it will be mean and median same, so why specifically say median? A: The instructor responds by giving the 1D example (see Worked examples) to clarify.

Q (median step): Does "check the values and change" mean the median can be any other value? A: You have to arrange the points in x-coordinate order; median requires sorting first, and only then can median be applied — that is the rule of the algorithm. In design and analysis you are free to make your own variant (e.g., use mean), but then the points would not