MEPX
Chapter 11 of 12All chapters

Chapter 11 of 12

Recursion and recurrences

Working out the cost of a divide and conquer.

Reading a recurrence

Merge sort splits into two halves and does linear work to merge, written T(n) = 2T(n/2) + O(n). That solves to O(n log n): log n levels, each doing O(n) work.

  • Binary search is T(n) = T(n/2) + O(1), which gives O(log n).
  • Naive Fibonacci is T(n) = T(n-1) + T(n-2), which is exponential.

Memoisation

Storing results of subproblems turns exponential recursion into linear work. That is the whole idea behind dynamic programming, and it is a space for time trade.