androidengineers.Book a session

Divide and Conquer

Matrix Multiplication and Closest Pair Problem

article20 minMedium

Improving the combine step can change the total bound

Classical square matrix multiplication performs O(n³) arithmetic operations. Splitting into four blocks still requires eight recursive block multiplications, giving T(n)=8T(n/2)+O(n²) and retaining cubic growth. Strassen's method uses seven block products, leading to roughly O(n^2.807), with practical tradeoffs in constants, memory, and numerical behavior.

The closest-pair problem shows a different combine argument. Sort 2D points by x, solve left and right halves, then inspect a strip near the boundary whose width is determined by the best known distance. Maintaining y-order permits a linear combine step using a geometric packing bound; re-sorting each strip can add another logarithmic factor.

Worked example

Points (0,0), (2,2), (3,2), and (8,8) have closest pair (2,2) and (3,2), at distance one. A split can place this pair on opposite sides, showing why returning only the better half-result is incorrect.

Exercise

Implement a brute-force closest-pair baseline for small inputs and use it to verify a divide-and-conquer version. Include duplicate points and equal x coordinates.

Check: specify squared-distance arithmetic bounds and minimum input size. These are algorithm outlines, not permission to omit the cross-boundary proof.

Further reading: Closest-pair implementation

YOUR LEARNING JOURNEY

0 of 55 available lessons completed

Progress saved in this browser. No account needed.
Matrix Multiplication and Closest Pair Problem | Algorithms | Android Engineers