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

Big-O Notation and Complexity Analysis

College Depth 83 in the knowledge graph I know this Set as goal
342topics build on this
399prerequisites beneath it
See this on the map →
Asymptotic Notation: Big-O, Big-Omega, Big-ThetaAmortized AnalysisExternal Memory Algorithms+14 more
complexity analysis big-o asymptotics

Core Idea

Big-O notation provides an upper bound on how an algorithm's runtime grows with input size, focusing on asymptotic behavior while ignoring constant factors and lower-order terms. It enables meaningful algorithm comparison independent of hardware. For example, linear search is O(n) while binary search is O(log n), making binary search vastly superior for large inputs despite higher constant factors.

How It's Best Learned

Start with concrete examples by counting operations in simple loops and recursive functions, identifying the dominant term. Practice deriving Big-O for nested loops, divide-and-conquer recurrences, and data structure operations before moving to general complexity classes.

Common Misconceptions

Explainer

You already understand from algorithm design basics that algorithms can be compared by how efficiently they solve a problem, and from asymptotic notation that Big-O provides an upper bound on growth rate. This topic shifts focus from the mathematical definition to the practical skill of analyzing code to determine its Big-O complexity. The goal is to look at an algorithm — its loops, recursive calls, and data structure operations — and derive how its runtime scales with input size n.

The fundamental technique is counting the dominant operation. For a single loop that iterates n times and does constant work per iteration, the complexity is O(n). For two nested loops that each iterate n times, the inner loop executes n times for each of the n outer iterations, giving O(n²). If the inner loop depends on the outer variable — say, iterating from i to n — the total work is n + (n-1) + ... + 1 = n(n+1)/2, which is still O(n²) because we drop the constant factor and lower-order term. The rule is always the same: identify the term that grows fastest and discard everything else.

Recursive algorithms require a different approach. Consider binary search: each recursive call halves the input and does constant work, giving the recurrence T(n) = T(n/2) + O(1). This solves to O(log n). Merge sort splits the input in half but does O(n) work to merge, giving T(n) = 2T(n/2) + O(n), which solves to O(n log n). You do not need to memorize these — the pattern is to write the recurrence, then either expand it, use the Master Theorem, or draw a recursion tree to see how the total work accumulates across levels. The key insight is that dividing the problem in half at each step is the signature of logarithmic depth, while doing linear work at each level multiplies that depth by n.

In practice, Big-O analysis is a starting point, not the final answer. Two O(n log n) sorting algorithms — merge sort and quicksort — have very different real-world performance because quicksort has better cache behavior and smaller constant factors. An O(n) algorithm with a constant factor of 1000 will lose to an O(n log n) algorithm for inputs under about 10,000. The power of Big-O is in ruling out bad choices at scale: if your dataset has a billion elements, no O(n²) algorithm is viable regardless of constant factors, while O(n log n) is comfortable. Use Big-O to narrow the field, then benchmark the survivors on your actual data and hardware.

Practice Questions 5 questions

Prerequisite Chain

Understanding ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersIntroduction to ExponentsOrder of OperationsInteger Order of OperationsVariable ExpressionsThe Distributive PropertyVariables and Expressions ReviewIntroduction to PolynomialsAdding and Subtracting PolynomialsMultiplying PolynomialsFactorialPermutationsCombinationsCounting Principles: Addition and Multiplication RulesIntroduction to Graph TheoryPropositional Logic FoundationsLogical EquivalencesBoolean AlgebraBoolean Type and Truth ValuesComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraConditional StatementsDefining and Calling FunctionsFunctions: Decomposing ProblemsFunction Parameters and Argument PassingReturn ValuesVariable ScopeIntroduction to ClassesObjects and InstancesMethods and AttributesAlgorithm Design BasicsAsymptotic Notation: Big-O, Big-Omega, Big-ThetaBig-O Notation and Complexity Analysis

Longest path: 84 steps · 399 total prerequisite topics

Prerequisites (1)

Leads To (16)