Complexity Analysis
This chapter covers the fundamentals of complexity analysis. A complexity claim about an algorithm has to say which case it assumes (best, worst, or average), which resource it measures (time or space), and whether it covers one operation or a sequence (amortized cost). Along the way, we cover a few other related topics. For example, we work out how much memory recursion uses on the call stack, and why a dynamic array doubles its capacity when it grows.
After reading this chapter, you should be able to:
- Determine an algorithm’s best-, worst-, and average-case time complexity, and apply the convention that an unqualified Big-Oh names the worst case
- Determine an algorithm’s space complexity, separating the input space from the auxiliary space it allocates while running
- Determine a recursive method’s space from its maximum call-stack depth, and explain why its time and space can differ, as they do for recursive Fibonacci
- Define the amortized cost of a sequence of operations, distinguish it from average-case cost, and explain why doubling the capacity makes a dynamic array’s
addamortized - State a complete complexity claim, naming which resource it measures, which case it assumes, and whether it covers a single operation or a sequence
You can download the accompanying code for this chapter.
Sections
- Time Complexity
- Best Case and Worst Case
- Average Case Analysis
- Space Complexity
- Space Complexity of Recursion
- Time and Space of Recursive Fibonacci
- A Faster Fibonacci
- A Leaner Fibonacci
- Complexity of
growandadd - Amortized Analysis
- Growing One Slot at a Time
- Why Doubling Works
- Computational Complexity