session: "5101-design-analysis-of-algo/2026-09-27 0000 DAA 27 Sep"
title: "DAA 27 Sep"
date: "2026-09-27 00:00"
duration: "1h 19m"
language: "en"
model: "deepseek-ai/DeepSeek-V4.1-Flash-1017"
parts: 2
transcript_chars: 51312
generated: "2026-11-23T23:47:36+05:30"
DAA 27 Sep
Overview
This class completes the maximum sub-array problem by moving from brute force and divide-and-conquer to dynamic programming, culminating in Kadane's algorithm and its index-tracking extension. It then introduces Strassen's matrix multiplication as a second divide-and-conquer case study, showing how reducing multiplications from 8 to 7 lowers complexity from n³ to n^2.81. The class also compares divide-and-conquer, DP and brute force, and discusses when each paradigm applies. It sits at the transition point in the course from classical divide-and-conquer recurrences to dynamic programming.
Learning outcomes
- Solve the maximum sub-array problem by brute force, divide and conquer, and Kadane's algorithm, and state each complexity.
- Trace the divide-and-conquer algorithm (base case, left/right recursion, cross sum) on a small array.
- Trace Kadane's algorithm step by step and recover the start/end indices of the maximum sub-array.
- Explain why Kadane's is a bottom-up (DP-style) approach and how DP differs from divide and conquer in speed and memory.
- Apply Strassen's 7-product scheme to a 2×2 matrix and verify the result against direct multiplication.
- Write the Strassen recurrence and state its solution n^2.81 via the master theorem.
- Decide whether a problem calls for divide and conquer, DP, or brute force.
Core concepts
Maximum sub-array problem
A set of values stored in an array containing both negative and positive signs; we must identify a subset whose sum is maximum. It matters because it is the one problem for which brute force, divide and conquer and DP all exist simultaneously, making it the ideal comparison case. Special case: if there is one positive number and all others are negative, the positive number itself is the maximum sum (including any negative number would reduce it).
Brute force approach
Used a heuristic to minimize the search space — removing reoccurrence of elements / duplicate entities — reaching computational complexity of order n square. It is the fallback when neither divide and conquer nor DP applies.
Divide and conquer approach
Divides the subarray into smaller components until a single element is reached (base condition of the recurrence). Then identifies left sum, right sum and cross sum. The subarray can lie in the left sub-half, the right sub-half, or on the intersection. It is top-down: split the big problem into smaller and smaller subproblems, solve, go ahead. Complexity n log n.
Base case
When high is equivalent to low (one number), return low, high and A of low.
Cross sum
From the median, go to one side and to the other side; take the maximum possible sum value on the left side and the maximum possible value on the right side; Ls + Rs gives the cross sum. Its cost is order n (from the median we go at most n by 2 on each side), which drives the overall n log n.
Dynamic programming (DP)
Primarily a bottom-up approach — solve smaller variants of problems, aggregate them to get the final result. At each stage it preserves/stores the outcome of the previous state, and at each step gets the best value possible. DP algorithms are faster but memory-inefficient because they store the result of all previous/optimized outcomes at each step. On the cost of memory we get a faster algorithm; since memory is now sufficiently large, DP is more viable in the current scenario.
Divide and conquer vs. DP
Divide and conquer: solve one half, store the result in temporary storage, shift to the other half, then aggregate upward; results of a solved part are discarded once aggregated ("these results are gone"). DP: solve all sub-parts at a level simultaneously (e.g., O1, O2, O3, O4, then O5, O6, then upward), storing all outputs at each level; among these, the best case is propagated upward. DP stores previous results, which creates memory usage; divide and conquer need not store results because a solution is obtained at each step. Faster: divide and conquer is n log n, DP is n.
Kadane's algorithm
The DP variant for solving the maximum subarray problem; imposes only order n computational complexity (a single for loop running n − 1 times). Kadane's moves from one index to the next, solving subproblems incrementally: for the first two indices the max value is 1, for the first three it is 1, for the first four it is 4, and so on. Each step reuses the previously stored result, which is why it is described as working like dynamic programming from a different angle. The hierarchical (tree-like) arrangement is only possible in tree-like problems; here the max sum is obtained iteratively, step by step.
Recovering the subarray
Kadane's algorithm as stated gives only the maximum sum value, not the indices. To get the subarray, additional steps must be added: whenever there is a change in the value of the max sum, store that index (e.g., in an additional array); the two stored indices are treated as the start and end of the subarray. This is an "assumptive" extension, not part of standard Kadane's — any mechanism you devise is acceptable ("whatever logic you write that is up to you"). Example approach: maintain an index array and an output array simultaneously (e.g., indices 1, 2, 3, 4 with outputs 1, 1, 4, 4); after evaluating the sum 6, check where 6 first appeared (e.g., index 6) and go back to where the increment started (4).
Strassen's algorithm
Given by the mathematician Strassen. Instead of 8 products for a 2×2 multiplication, it performs the operation in 7 products by increasing the number of additions and subtractions. It is a divide-and-conquer algorithm: any large matrix is ultimately brought down to 2×2. The seven steps were given by Strassen (hence the name); the derivation/proof can be looked up in Cormen.
Square-matrix requirement
In Strassen's multiplication the matrices must be square and of order 2, 4, 8, 16, 32, 64, and so on. This is mandatory. A 4×4 matrix is treated as 2×2 blocks of 2×2 matrices; a 16×16 is reduced to 8×8, then 4×4, then 2×2, at which point the 7-step procedure is applied.
Choosing a paradigm
Dynamic programming cannot be applied everywhere, nor can divide and conquer. If a problem can be broken into subproblems and a recurrence/recursive variant can be formed, divide and conquer or DP may apply. If storing previous results helps make the next decision, DP applies; if storing results is not possible or not fruitful, DP does not apply. If neither applies, use brute force. Example: for the closest pair problem, DP cannot be applied, so divide and conquer is used.
Formulas and maths
- Brute force complexity: order of n square.
- Symbols: n is the input size.
- Used when: stating the cost of the exhaustive/heuristic search approach.
- Divide and conquer complexity: n of log of n.
- Symbols: n is the input size.
- Used when: stating the cost of the recursive max-subarray algorithm.
- Maximum crossing subarray complexity: order of n.
- Symbols: n is the input size; from the median we go at most n by 2 on each side.
- Used when: costing the cross-sum step of the divide-and-conquer algorithm.
- Recursive division imposes log of n; the one or two comparisons ("2 or something") can be ignored; ultimately n of log of n.
- Symbols: log n counts the levels of recursion.
- Used when: combining cross-sum cost with recursion depth.
- Mid: floor of (low + high) by 2 (int can be used; ceiling is also acceptable — "totally up to you").
- Symbols: low and high are the current subarray bounds.
- Used when: splitting the subarray in the divide-and-conquer algorithm.
- Median: n by 2 for even n, n by 2 plus 1 for odd n.
- Symbols: n is the number of elements.
- Used when: locating the median position.
- Kadane's initialization: cs = ms = value at the first index of the array.
- Symbols: cs = current sum, ms = max sum.
- Used when: starting Kadane's algorithm.
- Kadane's loop (for i = 2 to n):
- current sum = max of (current element, current element + current sum)
- max sum = max of (current sum, max sum)
- Symbols: i is the array index; with zero-based indexing the loop can be 1 to n − 1.
- Used when: updating the running and best sums at each step.
- Divide-and-conquer result selection: if left sum is greater than right sum and left sum is greater than cross sum, result is left sum; otherwise right sum; otherwise cross sum (whichever is highest).
- Symbols: left sum, right sum, cross sum are the three candidates.
- Used when: returning the answer from a recursive call.
- Simple 2×2 matrix multiplication (A × B = C):
- C11 = a11·b11 + a12·b21
- C12 = a11·b12 + a12·b22
- C21 = a21·b11 + a22·b21
- C22 = a21·b12 + a22·b22
- Symbols: a_ij, b_ij are entries of A and B.
- Used when: computing the product directly.
- Count of multiplications: n·m·k; for 2×2 by 2×2 this is 2·2·2 = 8, giving complexity order of n³ (2³ = 8). For 4×4 it is 4·4·4 = 4³, and so on.
- Symbols: n, m, k are matrix dimensions.
- Used when: costing naive matrix multiplication.
- Strassen's 7 products:
- 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)
- Symbols: A_ij, B_ij are blocks of the partitioned matrices.
- Used when: performing the 7-multiplication step on 2×2 blocks.
- Combining the products:
- C11 = P5 + P4 − P2 + P6
- C12 = P1 + P2
- C21 = P3 + P4
- C22 = P5 + P1 − P3 − P7
- Symbols: C_ij are the blocks of the result matrix.
- Used when: assembling the product from the seven P values.
- Strassen recurrence: T(n) = 7·T(n/2) + order of n².
- Symbols: 7 subproblems of size n/2, with n² coming from the multiplication aspect.
- Used when: analysing Strassen's complexity; solved via the master theorem, it gives n^2.81.
- Complexity comparison: simple matrix multiplication is order n³; Strassen reduces it to n^2.81.
- Symbols: n is the matrix order.
- Used when: arguing why the reduction matters in longer/bigger computations.
Worked examples
Divide-and-conquer example
Array: 13, −3, −25, 20, −3, 16, −23, 18 with indices 1, 2, 3, 4, 5, 6, 7, 8 (a bigger problem of 16 elements was mentioned; students may instead take a 5–6 element sub-array).
- Left sum is 13, recorded as (low, high, sum); from the other side (2, 2, −3); the highest is 13.
- Cross sum computed as (1, 2, 10) — initial index, longest index, and sum.
- From the side with 25, 20: (4, 4, 20), so the right sum is 20.
- For cross sum, take the lowest index from the left and the highest index from the right: (1, 4, 10).
- Going from there: 10 on one side and −5 on the other; taking 25 as the median value gives 20 on one side and −28 and 13, i.e., −15 — the instructor says "I guess this may be wrong."
- Result: 10 on one side, −5 on the other, giving 5; among the three candidates, 20 is highest, so (4, 4, 20) proceeds upward.
- The instructor notes "maybe some mistake is there" and asks students to check it out.
Kadane's example
Array: −2, 1, −3, 4, −1, 2, 1, 1, −5, 4:
- Initialization: current sum = max sum = −2.
- i = 2 (element 1): cs = max(1, 1 + (−2) = −1) = 1; ms = max(1, −2) = 1.
- i = 3 (element −3): cs = max(−3, 1 − 3) = −2; ms = max(−2, 1) = 1.
- i = 4 (element 4): cs = max(4, 4 + (−2)) = 4; ms = max(4, 1) = 4.
- i = 5 (element −1): cs = max(−1, −1 + 4) = 3; ms = max(3, 4) = 4.
- i = 6 (element 2): cs = max(2, 2 + 3) = 5; ms = max(5, 4) = 5.
- i = 7 (element 1): cs = max(1, 1 + 5) = 6; ms = max(6, 5) = 6.
- i = 8 (element −5): cs = max(−5, −5 + 6) = 1; ms = max(1, 6) = 6.
- i = 9 (element 4): cs = max(4, 4 + 1) = 5; ms = max(5, 6) = 6.
- Final maximum sum = 6, from the subarray 4, −1, 2, 1. Complexity: order of n.
Partial re-explanation with index tracking
Array −2, 1, −3, 4, −1, 2, 1, only up to index 7:
- cs = ms = −2; i = 2: cs = max(1, 1 − 2 = −1) = 1, ms = 1; index stored: i = 2.
- i = 3: cs = max(−3, 1) = 1, ms = 1; index stored: 3.
- i = 4: cs = max(4, 4 + current sum) = 5, ms = 5; index stored: 4 (marked as a flag where the value changed).
- The instructor notes "I guess I am skipping something" during this re-explanation.
Kadane's index-storing illustration
At i = 2, 3, 4, 5 the value is not changing, so the stored index stays 2; when 4 is reached, the index becomes 4; when 6 is reached for the first time, that value is also stored — giving the two points where the value changed (4 and 6).
Strassen on a 2×2 example
A = [[1, 2], [3, 5]], B = [[3, 2], [1, 3]].
Direct multiplication (A · B):
- C11 = 1·3 + 2·1 = 5
- C12 = 1·2 + 2·3 = 8
- C21 = 3·3 + 5·1 = 14
- C22 = 3·2 + 5·3 = 21
- Result: [[5, 8], [14, 21]]
Via Strassen:
- P1 = 1 · (2 − 3) = −1
- P2 = (1 + 2) · 3 = 9
- P3 = (3 + 5) · 3 = 24
- P4 = 5 · (1 − 3) = −10
- P5 = (1 + 5) · (3 + 3) = 36
- P6 = (2 − 5) · (1 + 3) = −12
- P7 = (1 − 3) · (3 + 2) = −10
Combining:
- C11 = 36 + (−10) − 9 + (−12) = 5
- C12 = −1 + 9 = 8
- C21 = 24 + (−10) = 14
- C22 = 36 + (−1) − 24 − (−10) = 21
Result: [[5, 8], [14, 21]] — identical to direct multiplication. The number of computations appears larger, but the dominating operation (multiplication) is reduced to only 7.
Questions asked in class
- Anuja: Why does the loop run from 2 to n, and how does initialization work? — The algorithm starts from the second position; initialization declares current sum and max sum equal to the value stored at index one (or zero, whatever the first index is). The max sum will not change at the first step because it is the max of current sum and max sum; e.g., if the first value is 200 and the rest are negative, the max sum remains the first value. With zero-based indexing, the loop can be 1 to n − 1.
- Rohit: How was the subarray (starting at 4, ending at 1) identified? — The algorithm gives only the maximum sum value. To get indices, add steps: whenever there is a higher positive value, store that index,