## A.5 Logarithms
Logarithms are one of the mathematical tools used most in analysis of algorithms. We briefly review their properties.
### A.5.1 Definition and Properties of Logarithms
The ***common Logarithm*** of a number is the power to which 10 must be raised to produce the number. If *x* is a given number, we denote its common logarithm by log *x*.
Example A.6**![Start example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**Some common logarithms follow:
[![Click To expand](https://box.kancloud.cn/73447a8bbbdfa021f6deac926e51f5ee_402x117.jpg)](figu522_3.jpg)
Recall that the value of any nonzero number raised to the 0th power is 1.
**![End example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**
In general, the logarithm of a number *x* is the power to which another number *a*, called the *base*, must be raised to produce *x*. The number *a* can be any positive number other than 1, whereas *x* must be positive. That is, there is no such thing as the logarithm of a negative number or of 0. In symbols, we write log*a**x*.
Example A.7**![Start example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**Some examples of log*a**x* follow:
![](https://box.kancloud.cn/304e919f54f941b70764220f98b5d128_366x121.jpg)
**![End example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**
Notice that the last result in [Example A.7](#ap-aex10) is for a number that is not an integral power of the base. Logarithms exist for all positive numbers, not just for integral powers of the base. A discussion of the meaning of the logarithm when the number is not an integral power of the base is beyond the scope of this appendix. We note here only that the logarithm is an increasing function.
That is,
![](https://box.kancloud.cn/a6ee3f1a3684a6027b26809f2baa87be_298x21.jpg)
Therefore,
![](https://box.kancloud.cn/b0a69187c8f7407e48728a1455e34268_250x21.jpg)
We saw in [Example A.7](#ap-aex10) that log2 7 is about 2.807, which is between 2 and 3.
Listed below are some important properties of logarithms that are useful in the analysis of algorithms.
**Some Properties of Logarithms** (In all cases, *a* > 1, *b* > 1, *x* > 0, and *y* > 0):
![](https://box.kancloud.cn/7cc0de8a052b4dd9f168a90fbdd07099_227x228.jpg)
Example A.8**![Start example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**Some examples of applying the previous properties follow:
![](https://box.kancloud.cn/dbc8e374ffc97c5e688a824c0df00887_400x299.jpg)
**![End example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**
Because many calculators have a log function (recall that log means log10), the last two results in [Example A.8](#ap-aex11) show how one can use a calculator to compute a logarithm for an arbitrary base. (This is how we computed them.)
We encounter the logarithm to base 2 so often in analysis of algorithms that we give it its own simple symbol. That is, we denote log2*x* by **lg *x***. From now on, we use this notation.
### A.5.2 The Natural Logarithm
You may recall the number *e*, whose value is approximately 2.718281828459. Like 蟺, the number *e* cannot be expressed exactly by any finite number of decimal digits, and indeed there is not even a repeating pattern of digits in its decimal expansion. We denote log*e**x* by **ln *x***, and we call it the ***natural logarithm*** of *x*. For example,
![](https://box.kancloud.cn/bf3253689209c0b5a0423591ce11c6cd_145x19.jpg)
You may wonder where we got this answer. We simply used a calculator that has an ln function. Without studying calculus, it is not possible to understand how the natural logarithm is computed and why it is called "natural." Indeed, when we merely look at *e*, the natural logarithm appears most unnatural. Although a discussion of calculus is beyond our present scope (except for some material marked ![](https://box.kancloud.cn/df1fb8c1cff85483852ba013a7c9aab6_22x22.jpg)), we do want to explore one property of the natural logarithm that is important to the analysis of algorithms. With calculus it is possible to show that ln *x* is the area under the curve 1/*x* that lies between 1 and *x*. This is illustrated in the top graph in [Figure A.3](#ap-afig03) for *x* = 5. In the bottom graph in that figure, we show how that area can be approximated by summing the areas of rectangles that are each one unit wide. The graph shows that the approximation to ln 5 is
![](https://box.kancloud.cn/c7490893419cf03c47bb95a936a39a59_400x45.jpg)
[![Click To expand](https://box.kancloud.cn/07a7450da9361fd6bd9573824332f58a_247x500.jpg)](figap-a-3_0.jpg)
Figure A.3: The shaded area in the top graph is ln 5. The combined area of the shaded rectangles in the bottom graph is an approximation to ln 5.
Notice that this area is always larger than the true area. Using a calculator,. we can determine that
![](https://box.kancloud.cn/3059da63eccbf0bc7573ceac59c23f27_118x19.jpg)
The area of the rectangles is not a very good approximation to ln 5. However, by the time we get to the last rectangle (the one between the *x*-values 4 and 5), the area is not much different from the area under the curve between *x* = 4 and *x* = 5. This is not the case for the first rectangle. Each successive rectangle gives a better approximation than its predecessor. Therefore, when the number is not small, the sum of the areas of the rectangles is close to the value of the natural logarithm. The following example shows the usefulness of this result.
Example A.9**![Start example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**Suppose we wish to compute
![](https://box.kancloud.cn/8db18bd1559211344abb6787ce97b6eb_131x40.jpg)
There is no closed-form expression for this sum. However, in accordance with the preceding discussion, if *n* is not small,
![](https://box.kancloud.cn/b334bb13319096113d7a14ccb6d13885_372x46.jpg)
When *n* is not small, the value of 1/*n* is negligible in comparison with the sum. Therefore, we can also add that term to get the result
![](https://box.kancloud.cn/c574092621d8fe1dece4f972d579ab77_184x42.jpg)
We will use this result in some of our analyses. In general, it is possible to show that
![](https://box.kancloud.cn/c33873ad11525d75aa7e538792d3e705_329x40.jpg)
**![End example](https://box.kancloud.cn/e95df9d604ab2d89febe370ae4d88fb1_1x1.gif)**
- Table of Contents
- BackCover
- Foundations of Algorithms Using C++ Pseudocode, Third Edition
- Preface
- Chapter Contents
- Pedagogy
- Course Outlines
- Acknowledgments
- Errors
- Chapter 1: Algorithms - Efficiency, Analysis, and Order
- 1.2 The Importance of Developing Efficient Algorithms
- 1.3 Analysis of Algorithms
- 1.4 Order
- 1.5 Outline of This Book
- Exercises
- Chapter 2: Divide-and-Conquer
- 2.1 Binary Search
- 2.2 Mergesort
- 2.3 The Divide-and-Conquer Approach
- 2.4 Quicksort (Partition Exchange Sort)
- 2.5 Strassen's Matrix Multiplication Algorithm
- 2.6 Arithmetic with Large Integers
- 2.7 Determining Thresholds
- 2.8 When Not to Use Divide-and-Conquer
- Exercises
- Chapter 3: Dynamic Programming
- 3.1 The Binomial Coefficient
- 3.2 Floyd's Algorithm for Shortest Paths
- 3.3 Dynamic Programming and Optimization Problems
- 3.4 Chained Matrix Multiplication
- 3.5 Optimal Binary Search Trees
- 3.6 The Traveling Salesperson Problem
- Exercises
- Chapter 4: The Greedy Approach
- 4.1 Minimum Spanning Trees
- 4.2 Dijkstra's Algorithm for Single-Source Shortest Paths
- 4.3 Scheduling
- 4.4 Huffman Code
- 4.5 The Greedy Approach versus Dynamic Programming: The Knapsack Problem
- Exercises
- Chapter 5: Backtracking
- 5.2 The n-Queens Problem
- 5.3 Using a Monte Carlo Algorithm to Estimate the Efficiency of a Backtracking Algorithm
- 5.4 The Sum-of-Subsets Problem
- 5.5 Graph Coloring
- 5.6 The Hamiltonian Circuits Problem
- 5.7 The 0-1 Knapsack Problem
- Exercises
- Chapter 6: Branch-and-Bound
- 6.1 Illustrating Branch-and-Bound with the 0 - 1 Knapsack problem
- 6.2 The Traveling Salesperson Problem
- 6.3 Abductive Inference (Diagnosis)
- Exercises
- Chapter 7: Introduction to Computational Complexity - The Sorting Problem
- 7.2 Insertion Sort and Selection Sort
- 7.3 Lower Bounds for Algorithms that Remove at Most One Inversion per Comparison
- 7.4 Mergesort Revisited
- 7.5 Quicksort Revisited
- 7.6 Heapsort
- 7.6.1 Heaps and Basic Heap Routines
- 7.6.2 An Implementation of Heapsort
- 7.7 Comparison of Mergesort, Quicksort, and Heapsort
- 7.8 Lower Bounds for Sorting Only by Comparison of Keys
- 7.8.1 Decision Trees for Sorting Algorithms
- 7.8.2 Lower Bounds for Worst-Case Behavior
- 7.8.3 Lower Bounds for Average-Case Behavior
- 7.9 Sorting by Distribution (Radix Sort)
- Exercises
- Chapter 8: More Computational Complexity - The Searching Problem
- 8.1 Lower Bounds for Searching Only by Comparisons of Keys
- 8.2 Interpolation Search
- 8.3 Searching in Trees
- 8.4 Hashing
- 8.5 The Selection Problem: Introduction to Adversary Arguments
- Exercises
- Chapter 9: Computational Complexity and Interactability - An Introduction to the Theory of NP
- 9.2 Input Size Revisited
- 9.3 The Three General Problem Categories
- 9.4 The Theory of NP
- 9.5 Handling NP-Hard Problems
- Exercises
- Chapter 10: Number-Theoretic Algorithms
- 10.1 Number Theory Review
- 10.2 Computing the Greatest Common Divisor
- 10.3 Modular Arithmetic Review
- 10.4 Solving Modular Linear Equations
- 10.5 Computing Modular Powers
- 10.6 Finding Large Prime Numbers
- 10.7 The RSA Public-Key Cryptosystem
- Exercises
- Chapter 11: Introduction to Parallel Algorithms
- 11.1 Parallel Architectures
- 11.2 The PRAM Model
- Exercises
- Appendix A: Review of Necessary Mathematics
- A.2 Functions
- A.3 Mathematical Induction
- A.4 Theorems and Lemmas
- A.5 Logarithms
- A.6 Sets
- A.7 Permutations and Combinations
- A.8 Probability
- Exercises
- Appendix B: Solving Recurrence Equations - With Applications to Analysis of Recursive Algorithms
- B.2 Solving Recurrences Using the Characteristic Equation
- B.3 Solving Recurrences by Substitution
- B.4 Extending Results for n, a Power of a Positive Constant b, to n in General
- B.5 Proofs of Theorems
- Exercises
- Appendix C: Data Structures for Disjoint Sets
- References
- Index
- Index_B
- Index_C
- Index_D
- Index_E
- Index_F
- Index_G
- Index_H
- Index_I
- Index_J
- Index_K
- Index_L
- Index_M
- Index_N
- Index_O
- Index_P
- Index_Q
- Index_R
- Index_S
- Index_T
- Index_U
- Index_V
- Index_W-X
- Index_Y
- Index_Z
- List of Figures
- List of Tables
- List of Algorithms, Examples, and Theorems
- List of Sidebars