A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Algorithm Analysis and Complexity Classes

College Depth 76 in the knowledge graph ☐ I know this ☆ Set as goal
435topics build on this
409prerequisites beneath it
See this on the map →
Big-O Notation and Asymptotic AnalysisSetting Up Recurrence Relations→→Algorithmic Composition TheoryComplexity Classes and the Complexity Hierarchy+12 more
algorithm-analysis time-complexity P-vs-NP NP-complete sorting-algorithms

Core Idea

Algorithm analysis applies Big-O notation to classify algorithms by their time and space requirements as functions of input size n. Linear search is O(n); binary search is O(log n); comparison-based sorting is Ω(n log n), achieved by merge sort and heap sort. The complexity classes P (problems solvable in polynomial time) and NP (problems whose solutions are verifiable in polynomial time) frame the central open question of theoretical computer science: whether P = NP. NP-complete problems — the hardest problems in NP — include SAT, graph coloring, and Hamiltonian circuits.

How It's Best Learned

Analyze familiar algorithms step by step, deriving their complexity by counting operations as a function of n. Understand binary search's O(log n) cost from the halving argument. Discuss P vs. NP conceptually: why verifying a solution is often easier than finding one.

Common Misconceptions

Explainer

You already know Big-O notation — the language for describing how an algorithm's resource use grows as input size grows. Algorithm complexity analysis applies that language systematically to compare algorithms and, at a deeper level, to classify the problems themselves. The key shift is from "how fast is this algorithm?" to "how fast *can* any algorithm for this problem be?"

Start with sorting. Algorithms like bubble sort and insertion sort run in O(n²) in the worst case: they compare each element against roughly every other element. Merge sort and heap sort achieve O(n log n) by exploiting divide-and-conquer structure — they split the problem, solve each half, and merge, doing O(n) merge work at O(log n) levels of recursion. The remarkable fact is that this is *optimal*: any comparison-based sorting algorithm must perform at least Ω(n log n) comparisons in the worst case. You can prove this with an information-theoretic argument — there are n! possible orderings of n elements, and each comparison eliminates at most half the possibilities, so you need at least log₂(n!) ≈ n log n comparisons to identify the correct one. No sorting algorithm that works only by comparison can do better.

Binary search achieves O(log n) by a different divide-and-conquer insight: at each step, it compares the target against the middle element and discards the half that cannot contain the target. Starting with n elements, each comparison halves the search space — after k comparisons, at most n/2k elements remain. You need k large enough that n/2k = 1, which gives k = log₂(n). Any algorithm that multiplicatively shrinks its remaining work at each step will have logarithmic complexity.

The classification of problems into complexity classes P and NP is one of the deepest open questions in mathematics. P is the class of problems solvable in polynomial time — O(nk) for some fixed k. NP is the class of problems whose *solutions can be verified* in polynomial time. Finding a Hamiltonian circuit (a path visiting every vertex exactly once) in a graph is hard — no polynomial algorithm is known. But if someone hands you a proposed circuit, you can verify in O(n) time that it visits every vertex exactly once. This asymmetry between finding and checking is what defines NP. Whether P = NP — whether every problem whose solution is easy to verify is also easy to solve — is unresolved. Most complexity theorists believe P ≠ NP, but no one has proved it.

NP-complete problems sit at the hardest end of NP: they are in NP, and every problem in NP can be reduced to them in polynomial time, meaning a polynomial algorithm for any NP-complete problem would imply P = NP. SAT (can a boolean formula be satisfied?), graph coloring, and the traveling salesman problem are all NP-complete. In practice, this means that if you encounter an NP-complete problem in an application, you should not expect to find an efficient exact algorithm — the field instead turns to approximation algorithms, heuristics, or exploiting special structure in real-world instances.

Practice Questions 3 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Dividing Integers → Unit Rates → Proportions → Percent Concept → Converting Between Fractions, Decimals, and Percents → Operations with Rational Numbers → Two-Step Equations → Solving Multi-Step Equations → Equations with Variables on Both Sides → Literal Equations → Slope-Intercept Form → Point-Slope Form → Writing Linear Equations → Parallel and Perpendicular Line Slopes → Graphing Linear Equations → Piecewise Functions → Step Functions → Composition of Functions → Inverse Functions → Radical Functions and Graphs → Rational Exponents → Exponential Functions and Graphs → Logarithms Introduction → Big-O Notation and Asymptotic Analysis → Algorithm Analysis and Complexity Classes

Longest path: 77 steps · 409 total prerequisite topics

Prerequisites (2)

Leads To (14)