21. Algorithm Analysis#
A program can produce the right sample answer and still lack a correctness argument or a useful cost model. This chapter connects specifications and loop invariants to operation counts, asymptotic bounds, and recurrences. You will state the input and counting assumptions behind each claim and distinguish measured performance from general reasoning.
Correctness: A claim that an algorithm satisfies its specification for every valid input.
Complexity: A description of how time or memory use grows as input size grows.
Big-O: An upper-bound growth description that ignores constant factors and lower-order terms.
Invariant: A fact that remains true before and after each loop iteration.
Recurrence: An equation that describes the cost of a recursive algorithm in terms of smaller inputs.
Chapter Video
Data Structures and Algorithms In C#: Big-O Notation (14:53) by Teddy Smith.
Hosted on YouTube; property of its original creator, embedded here for educational reference only.
Learning Objectives
By the end of this chapter, you will be able to:
Define input sizes and a cost model; derive exact counts for sequential, rectangular, triangular, and halving loops.
Distinguish upper, lower, and tight bounds and best, worst, expected, and amortized costs.
State precise contracts and use initialization, maintenance, exit reasoning, and decreasing variants to establish total correctness.
Choose boundary tests and counterexamples that challenge an implementation.
Derive recurrences with base cases and rounding; separate modeled work, total calls, stack depth, and other auxiliary storage.
Chapter Flow
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Input size |
A measure of how large the input is, usually written as |
2 |
Growth rate |
A description of how work or memory changes as input size increases. |
3 |
Big-O |
An asymptotic upper bound on growth. |
4 |
Big-Theta |
An asymptotically tight growth description. |
5 |
Constant time |
A cost that does not grow with input size. |
6 |
Linear time |
A cost that grows proportionally with input size. |
7 |
Logarithmic time |
A cost that grows by repeatedly reducing the problem size by a factor. |
8 |
Precondition |
A condition that must be true before an algorithm runs. |
9 |
Postcondition |
A condition that should be true after an algorithm finishes. |
10 |
Loop invariant |
A fact that is true before and after every loop iteration. |
11 |
Recurrence relation |
An equation that defines a recursive algorithm cost using smaller inputs. |
12 |
Big-Omega |
An asymptotic lower bound. |
13 |
Cost model |
The operations and assumptions used to count work. |
14 |
Auxiliary space |
Storage used in addition to the input, under a stated output convention. |
15 |
Variant |
A nonnegative integer measure that strictly decreases on continuing loop iterations. |
16 |
Partial correctness |
The postcondition holds if execution terminates on a valid input. |
17 |
Total correctness |
Partial correctness plus guaranteed termination. |
18 |
Worst-case cost |
Maximum cost over valid inputs of a given size. |
19 |
Expected cost |
A cost average under a stated probability model. |
20 |
Amortized cost |
Cost per operation averaged over an operation sequence. |
21 |
Recursion depth |
Maximum simultaneously active calls, under a stated counting convention. |
22 |
Encoded input length |
The number of symbols or bits representing the actual input, distinct from a numeric value contained in it. |