Video summary
1.5.2 Time Complexity Example #2
Main summary
Key takeaways
Main Ideas
The video explains how to analyze loop time complexity by tracking how a loop variable changes and determining how many updates it takes to reach the stopping condition. The key distinction is whether the variable changes by a constant amount or by a constant factor.
Loop Patterns and Their Complexity
-
Incrementing or decrementing by a constant
- Examples include
i++,i += 2, or decreasing fromnby 1. - The loop runs a number of iterations proportional to
n, so its time complexity is Θ(n). - Changing the step from 1 to 2, 10, or another fixed constant changes the iteration count only by a constant factor, not the asymptotic complexity.
- Examples include
-
Multiplying the loop variable by a constant
- A loop that starts at 1 and doubles each time visits values such as
1, 2, 4, 8, .... - After
kupdates, the value is2^k. Setting this nearngivesk ≈ log₂ n. - The loop therefore takes Θ(log n) time. Multiplying by 3 instead gives Θ(log₃ n); the base does not affect the asymptotic class.
- For a loop with condition
i < n, the exact number of executions isceil(log₂ n)for positive integern > 1. For example, it runs 3 times whenn = 8and 4 times whenn = 10.
- A loop that starts at 1 and doubles each time visits values such as
-
Dividing the loop variable by a constant
- Starting at
nand halving repeatedly gives values such asn, n/2, n/4, .... - The number of divisions needed to reach a value below 1 is logarithmic, so the loop takes Θ(log n) time.
- Starting at
-
A loop bounded by a square
- If a loop continues while
i * i <= nandiincreases by 1, it stops wheniis around√n. - It therefore takes Θ(√n) time.
- If a loop continues while
Independent and Nested Loops
- Independent loops run one after another: add their costs. Two separate linear loops take
n + n = 2noperations, which simplifies to Θ(n), not Θ(n²). - Nested loops: multiply their iteration counts. An outer loop that runs
ntimes with an inner loop that doubles its counter and runs Θ(log n) times takes Θ(n log n) overall.
Using a Variable from an Earlier Loop
The video also considers a loop whose limit is a value p produced by a previous loop. If the first loop makes p proportional to log n, and a later loop doubles its counter until it reaches p, then the later loop takes Θ(log p) = Θ(log log n) time.
General Method
- Write down the loop variable’s values across iterations.
- Express its value after
kiterations:- Constant increments typically produce a linear relationship.
- Multiplication or division by a constant produces a power relationship.
- Use the stopping condition to solve for
k, the number of iterations. - For multiple loops, add costs for sequential independent loops and multiply costs for nested loops.
- Simplify the result using asymptotic notation, dropping constant factors and lower-order terms. Exact counts may require a floor or ceiling, but that distinction does not change the asymptotic class.
Speaker or Source Featured
- Abdul Bari — instructor/presenter, as attributed to the channel.
Rate this summary
Your feedback will help improve summaries.
Improve this summary
Reprocess with a stronger model when the summary feels incomplete or inaccurate.
Translate summary in another language
Ask questions to this video
Chat for follow-up questions, clarifications, and source-backed answers.