Learning Objectives
By the end of this section, you will be able to:
- Understand what logarithms are and why they're fundamental to algorithm analysis
- Recognize logarithmic patterns in algorithms like binary search and divide-and-conquer
- Calculate and manipulate logarithms using key properties
- Explain why the logarithm base doesn't matter in Big-O notation
- Connect logarithms to real-world systems like databases, search engines, and tree structures
- Appreciate the dramatic efficiency that logarithmic algorithms provide at scale
Why This Matters: Logarithms are the mathematical foundation of efficient algorithms. They explain why binary search can find an item among a billion in just 30 steps, why balanced trees remain fast as they grow, and why divide-and-conquer algorithms are so powerful. Understanding logarithms transforms algorithm analysis from memorization to intuition.
The Story Behind Logarithms
Long before computers existed, mathematicians faced a practical problem: multiplication and division of large numbers were incredibly tedious and error-prone. In the early 17th century, Scottish mathematician John Napier invented a revolutionary tool—the logarithm.
The Original Purpose
Napier's insight was brilliant: multiplication can be converted to addition using logarithms. If you need to multiply two large numbers, say 3,456 × 7,891:
- Look up log(3,456) ≈ 3.539 and log(7,891) ≈ 3.897 in a table
- Add them: 3.539 + 3.897 = 7.436
- Look up the antilog: 107.436 ≈ 27,279,696
This technique powered centuries of scientific calculation and navigation before calculators. Slide rules—mechanical logarithm computers—helped send astronauts to the moon!
Logarithms in Computer Science
In computer science, logarithms gained a new and profound importance: they describe how many times you can divide a problem in half before reaching a single element. This is the essence of efficient algorithms.
Historical Connection
Definition: What is a Logarithm?
A logarithm answers a fundamental question: "What power do I need to raise a base to, in order to get a given number?"
Formal Definition
The logarithm base of , written , is the exponent such that:
In other words, asks: "To what power must I raise to get ?"
Examples with Different Bases
| Logarithm | Question | Answer | Verification |
|---|---|---|---|
| log₂(8) | 2 to what power = 8? | 3 | 2³ = 8 ✓ |
| log₂(1024) | 2 to what power = 1024? | 10 | 2¹⁰ = 1024 ✓ |
| log₁₀(1000) | 10 to what power = 1000? | 3 | 10³ = 1000 ✓ |
| log₂(1) | 2 to what power = 1? | 0 | 2⁰ = 1 ✓ |
| log₃(81) | 3 to what power = 81? | 4 | 3⁴ = 81 ✓ |
Intuitive Understanding
Think of logarithms as counting multiplications:
- log₂(n): How many times must I multiply 2 by itself to reach n?
- Equivalently: How many times can I divide n by 2 before reaching 1?
For example, because:
11024 → 512 → 256 → 128 → 64 → 32 → 16 → 8 → 4 → 2 → 1
2 1 2 3 4 5 6 7 8 9 10 divisionsMemory Aid
Quick Check
What is log₂(32)?
Interactive: Logarithm Explorer
Use this interactive tool to explore logarithms with different bases. Notice how all logarithm curves have the same fundamental shape—they differ only by a constant factor.
Key Logarithm Properties
These properties are essential for analyzing algorithms and simplifying expressions. Each property corresponds to a rule about exponents.
The Three Fundamental Properties
| Property | Formula | Intuition |
|---|---|---|
| Product Rule | log(ab) = log(a) + log(b) | Multiplying numbers = adding their exponents |
| Quotient Rule | log(a/b) = log(a) - log(b) | Dividing numbers = subtracting their exponents |
| Power Rule | log(aⁿ) = n × log(a) | Raising to a power = multiplying the exponent |
Additional Important Properties
| Property | Formula | Example |
|---|---|---|
| log of 1 | log_b(1) = 0 | Any number to power 0 is 1 |
| log of base | log_b(b) = 1 | log₂(2) = 1, log₁₀(10) = 1 |
| Change of base | log_a(x) = log_b(x) / log_b(a) | log₂(x) = ln(x) / ln(2) |
| Inverse | b^(log_b(x)) = x | 2^(log₂(8)) = 8 |
Change of Base Formula
The change of base formula is particularly important in computer science:
This means any logarithm can be converted to any other base by dividing by a constant. This constant factor is why the base doesn't matter in Big-O notation.
Quick Check
Using the product rule, what is log₂(8 × 16)?
Why Logarithms Appear in Algorithms
Logarithms appear in algorithms for a fundamental reason: they describe repeated halving (or more generally, repeated division). This pattern emerges in three main scenarios:
1. Searching in Sorted Data
When searching a sorted collection, you can eliminate half the remaining elements with each comparison. Starting with elements:
The number of steps to reach 1 element is .
2. Balanced Tree Heights
In a balanced binary tree with nodes, each level doubles the number of nodes. A tree of height can hold nodes. Inverting this:
3. Divide and Conquer Recursion
When a problem of size is divided into smaller subproblems (typically of size ), the recursion depth is . This is why algorithms like merge sort and quicksort have complexity.
The Common Thread
Case Study: Binary Search
Binary search is the canonical example of logarithmic complexity. It searches a sorted array by repeatedly dividing the search space in half.
The Algorithm
Why O(log n)?
Each iteration eliminates half the remaining elements. After iterations, we have:
We're done when only 1 element remains, so we solve for :
Interactive: Binary Search in Action
Watch how binary search eliminates half the search space at each step. Notice how even with 64 elements, it takes at most 6 comparisons.
Why Binary Search is O(log n)
Each step eliminates half of the remaining elements. Starting with 32 elements:
After k steps, we have n/2k elements. We need n/2k = 1, so k = log₂(n) = log₂(32) ≈ 5.
Quick Check
Binary search on an array of 1,000,000 elements requires at most how many comparisons?
Case Study: Tree Heights
The height of a balanced tree is logarithmic in the number of nodes. This is why tree-based data structures (BSTs, heaps, B-trees) provide efficient operations.
The Relationship
For a balanced binary tree with nodes:
- Level 0 (root): 1 node = 20
- Level 1: 2 nodes = 21
- Level 2: 4 nodes = 22
- Level k: 2k nodes
A complete binary tree of height has nodes. Solving for height:
Interactive: Tree Height Visualization
Why This Matters
Tree operations (search, insert, delete) traverse from root to leaf. In a balanced tree:
- 1,000 nodes: height ≈ 10, so operations take ~10 steps
- 1,000,000 nodes: height ≈ 20, so operations take ~20 steps
- 1,000,000,000 nodes: height ≈ 30, so operations take ~30 steps
A billion-node tree is only 50% taller than a million-node tree!
Unbalanced Trees
Case Study: Divide and Conquer
Divide and conquer algorithms split problems into smaller subproblems, solve them recursively, and combine the results. The recursion depth is logarithmic because each level reduces problem size by a constant factor.
The Pattern
A typical divide-and-conquer recurrence looks like:
Where:
- = number of subproblems
- = size of each subproblem
- = work to divide and combine
The recursion depth is because we divide by at each level.
Interactive: Divide and Conquer Decomposition
Merge Sort Example
Merge sort divides an array in half (), makes 2 recursive calls (), and does work to merge:
There are levels, each doing work total:
Common Logarithmic Complexities
Here are the most common complexity classes involving logarithms:
| Complexity | Name | Examples | 1 Million Items |
|---|---|---|---|
| O(log n) | Logarithmic | Binary search, balanced tree lookup | ~20 ops |
| O(√n) | Square root | Some number theory algorithms | 1,000 ops |
| O(n) | Linear | Linear search, array traversal | 1,000,000 ops |
| O(n log n) | Linearithmic | Merge sort, heap sort, FFT | ~20,000,000 ops |
| O(n²) | Quadratic | Bubble sort, naive matrix multiply | 10¹² ops |
The Log n Factor: What It Represents
The factor typically represents one of:
- Recursion depth: How many times you divide the problem (merge sort)
- Tree height: How many levels in a balanced tree structure (heap sort)
- Bits in the input: Number of binary digits needed to represent n (radix sort)
Pattern Recognition
Why the Base Doesn't Matter in Big-O
You'll often see written without specifying the base. This is intentional: in asymptotic analysis, the base is absorbed by the constant factor.
The Mathematical Reason
Using the change of base formula:
The factor is a constant that Big-O notation ignores. Therefore:
Concrete Example
| n | log₂(n) | log₁₀(n) | Ratio |
|---|---|---|---|
| 10 | 3.32 | 1.00 | 3.32 |
| 100 | 6.64 | 2.00 | 3.32 |
| 1,000 | 9.97 | 3.00 | 3.32 |
| 1,000,000 | 19.93 | 6.00 | 3.32 |
The ratio log₂(n)/log₁₀(n) is always 3.32 (which equals log₂(10)). This constant multiplier is exactly what Big-O ignores.
When Base Does Matter
Real-World Impact
The practical impact of logarithmic complexity is staggering. Let's examine some real-world scales:
Database Indexing
Modern databases use B-trees with branching factors around 100-1000. For a database with 10 billion records:
Just 5 disk accesses to find any record among 10 billion! Without indexing, you'd need to scan all 10 billion records.
Internet Search
Google indexes approximately 100 billion web pages. A tree-based index structure with branching factor 1000:
Four index lookups can narrow down from 100 billion pages to a manageable set!
Version Control (Git)
Git uses Merkle trees (hash trees) to efficiently compare repositories. Comparing two repositories with millions of files:
- Without tree structure: O(n) = millions of comparisons
- With Merkle tree: O(log n) ≈ 20 hash comparisons to find differences
Comparison: Linear vs Logarithmic at Scale
| Data Size | Linear O(n) | Logarithmic O(log n) | Speedup |
|---|---|---|---|
| 1,000 | 1,000 ops | 10 ops | 100× |
| 1,000,000 | 1,000,000 ops | 20 ops | 50,000× |
| 1,000,000,000 | 1 billion ops | 30 ops | 33 million× |
| 1 trillion | 1 trillion ops | 40 ops | 25 billion× |
The Takeaway: Logarithmic algorithms don't just run faster—they transform impossible problems into trivial ones. A billion-element search in 30 steps is the difference between instant results and hours of waiting.
Summary
In this section, we've explored logarithms—one of the most important mathematical concepts in algorithm analysis:
Key Concepts
| Concept | Key Insight |
|---|---|
| Definition | log_b(x) = y means b^y = x; it counts repeated multiplications or divisions |
| Properties | Product: log(ab) = log(a) + log(b); Power: log(a^n) = n×log(a) |
| Binary Search | Halving the search space gives O(log n) comparisons |
| Tree Height | Balanced tree with n nodes has height O(log n) |
| Divide & Conquer | Dividing by constant factor gives log n recursion depth |
| Base Independence | All log bases differ by constants, so O(log₂ n) = O(log n) |
Key Formulas
What's Next
In the upcoming sections, we'll explore:
- Summation Formulas and Series: Essential for analyzing loops and recursive algorithms
- Proof Techniques: Mathematical induction and other methods for proving algorithm correctness
- Probability Basics: Randomized algorithms and expected case analysis
Knowledge Check
Test your understanding of logarithms in algorithms with this quiz:
What is log₂(1024)?
Exercises
Computation Exercises
- Calculate: log₂(256), log₂(2048), log₃(243), log₁₀(1,000,000)
- Simplify using logarithm properties: log₂(32 × 64), log₂(1024 / 32), log₂(8⁵)
- Convert log₂(1000) to use base 10. Verify your answer with a calculator.
- If log₂(n) = 20, what is n? If log₂(n) = 30?
Algorithm Analysis Exercises
- A sorted array has 1,000,000 elements. What is the maximum number of comparisons binary search will make?
- A balanced ternary tree (each node has 3 children) has 1,000,000 nodes. What is its height?
- How many times can you halve the number 1,000,000,000 before reaching 1?
- An algorithm has recurrence T(n) = 2T(n/2) + n. Write out the first 4 levels of the recursion tree for n = 64. What is the total work at each level?
Conceptual Questions
- Why do we say O(log₂ n) = O(log₁₀ n) = O(log n), but we can't say O(n) = O(n²)?
- A database can do binary search on an index in O(log n) time. Why might it use a B-tree with branching factor 100 instead of a binary tree?
- If doubling the input size adds one more step to an algorithm, what is its complexity?
- Explain intuitively why merge sort is O(n log n) while bubble sort is O(n²).
Programming Challenges
- Implement a function that computes log₂(n) using only addition and subtraction (no built-in log functions). Count how many times you halve n.
- Modify binary search to return the number of comparisons made. Test on arrays of size 100, 1000, 10000, and verify the results match log₂(n).
- Write a function to compute the height of a complete binary tree given the number of nodes. Verify using log₂(n).
Solution Hints
- Exercise 1.1: 256 = 2⁸, 2048 = 2¹¹, 243 = 3⁵, 1,000,000 = 10⁶
- Exercise 2.1: log₂(1,000,000) = log₂(10⁶) ≈ 6 × 3.32 ≈ 20 comparisons
- Exercise 3.1: Different log bases differ by constants; n and n² differ by a factor of n
In the next section, we'll explore Summation Formulas and Series—essential tools for analyzing loops and computing total work in algorithms.