liveBook · Manning

Exponential time complexity

As explained in Modern C, Third Edition

Exponential computation time is runtime whose growth as input size increases is bounded by an exponential function.

Exponential time complexity describes runtime that grows exponentially with input size. Naive recursive Fibonacci computation repeatedly solves the same subproblems, making execution time proportional to a constant times φⁿ. section 7.3

Naive recursive Fibonacci

The function fib(n) calls fib(n−1) and fib(n−2) for each n above two. Its call tree contains repeated work, such as computing fib(2) twice when evaluating fib(4), so execution time becomes impractical as n increases. section 7.3

size_t fib(size_t n) [[__unsequenced__]] {
  if (n < 3)
    return 1;
  else
    return fib(n-1) + fib(n-2);
}    

Inspect the compact C implementation whose two recursive calls produce exponential computation time. section 7.3

Avoid repeated subproblem computation by changing the algorithm:

  • Cache previously computed Fibonacci values, trading storage for linear execution time. section 7.3
  • Recognize that algorithmic improvement can matter more than platform-specific implementation cleverness. section 7.3

How Modern C, Third Edition covers it

How deep it goes
Derives a recurrence and φⁿ bound, then contrasts exponential recursion with cached linear computation.
Examples
Uses naive fib, the fib(4) call tree, fibCache, and a constant-length-buffer implementation.
What you can do after
Diagnose repeated recursive work and apply caching to reduce Fibonacci computation from exponential to linear time.
How it connects
Connects recursion, Fibonacci sequence, caching, and algorithmic performance.

Also explained in

Compare how 3 books explain exponential time complexity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.