Context:FIT2004_MOC Β· a Week 1 Divide and Conquer algorithm Β· beats schoolbook O(n2) by trading one multiplication for cheap additions
NOT EXAMINABLE
A motivating hook for Divide & Conquer, not assessed content β stripped from FIT2004 Unit Cheatsheet. The transferable skill that is examinable is reading a and b off a recurrence and classifying by r=a/b β Solving Recurrences (Telescoping). Read for intuition; do not drill it in SWOTVAC.
Quick Revision
π― Objective: multiply two n-digit integers sub-quadratically by splitting each in half and doing 3 (not 4) half-size multiplications β Ξ(nlog2β3)βΞ(n1.585).
π¦ Core Components: split x=xLβBm+xRβ β naive needs 4 products β Gauss trick recovers the cross term as (xLβ+xRβ)(yLβ+yRβ)βxLβyLββxRβyRβ.
β‘ Key Constraint: the win is entirely a=3 vs a=4 β the exponent drops from log2β4=2 to log2β3; the combine (adds/shifts) stays Ξ(n). Input size is digit count, not value β Algorithmic Complexity.
β The split and the Gauss trick
With m=n/2 in base B:
x=xLβBm+xRβ,y=yLβBm+yRββxy=AxLβyLβββB2m+middle(xLβyRβ+xRβyLβ)ββBm+CxRβyRβββ
Naive β 4 half-size products βΉ T(n)=4T(n/2)+Ξ(n)=Ξ(n2) β no better than schoolbook.
The trick β (xLβ+xRβ)(yLβ+yRβ)=A+xLβyRβ+xRβyLβ+Cβmiddle=(xLβ+xRβ)(yLβ+yRβ)βAβC, so 3 products plus Ξ(n) additions/shifts. A and C must be computed first β the subtraction is what isolates the cross term.
βοΈ Core Implementation
recursive Karatsuba
def karatsuba(x, y, n): if n == 1: # base case: one-digit multiply return x * y m = n // 2 Bm = 10 ** m # base^(n/2) β a shift, not a multiply xl, xr = x // Bm, x % Bm yl, yr = y // Bm, y % Bm A = karatsuba(xl, yl, m) # 1st recursive mult C = karatsuba(xr, yr, m) # 2nd recursive mult mid = karatsuba(xl + xr, yl + yr, m) # 3rd recursive mult mid = mid - A - C # Gauss trick: recover cross term return A * (Bm * Bm) + mid * Bm + C # shifts + adds are Theta(n)
π‘ Common Mistake: the ΓBm / ΓB2m are digit shifts (Ξ(n)), not multiplications β only the three karatsuba(...) calls drive the recurrence. Miscounting them re-derives O(n2). Note (xLβ+xRβ) may carry an extra digit (46β 134), and the high term shifts by 2m, not m2.
When It Flips: Karatsuba's constants are larger, so schoolbook wins for small n; libraries switch below a threshold. The Ξ(n1.585) claim is about nββ β the same cut-over reasoning as merge sort's insertion-sort tail.
π Complexity β the level-sum written out
Level i holds 3i subproblems of size n/2i with Ξ(size) combine:
T(n)=βi=0log2βnβ3iβ c2inβ=cnβi=0log2βnβ(23β)iβr=23β>1βΞ(nβ nnlog2β3β)=Ξ(nlog2β3)
Leaf-dominated β branching (Γ3) outruns shrinking (Γ·2), so the nlog2β3 leaves, not the root combine, are the whole cost. Merge sortβs r=1 gives an extra logn factor instead of an exponent bump.
Auxiliary space Ξ(n), not Ξ(nlogn) β the three calls run sequentially, so one root-to-leaf chain of frames of sizes n,n/2,n/4,β¦ is live, summing to <2cn.
What it proves: steps 1β2 are reused by step 5, so only one extra product is needed; step 3 realises the carry pitfall live.
π§ Active Recall
Karatsuba and naive divide-and-conquer both split the numbers in half β why is only one sub-quadratic?
Answer
Short answer: naive D&C computes four half-size products βΉ 4T(n/2)+Ξ(n)=Ξ(n2); Karatsuba computes three βΉ 3T(n/2)+Ξ(n)=Ξ(nlog2β3).
Why:The exponent is log2βa β the level-sum has ratio a/2 and is dominated by its alog2βn=nlog2βa leaves, so dropping a from 4 to 3 moves the exponent from 2 to β1.585 β the entire speedup.