DC2: Binary Search & Repeated Squaring
San Skulrattanakulchai
December 4, 2018
Logarithms
- DPV p50, Ch 1.2.2
- A \(\log n\) factor occurs in time bounds of several algorithms, e.g., binary search, binary search tree, efficient sorting, etc. It helps to understand these algorithms by thinking base 2.
Base 2 notation
- Fact. The binary numeral of any positive integer \(n\) has \(\Theta(\log n)\) bits.
- For any integer \(k\ge0\), the number \(2^k\) is represented by the bit string \(1\overbrace{0\ldots 0}^{k \textrm{ 0's}}\). In general, if \(2^k\le n < 2^{k+1}\), then the base-2 numeral for \(n\) has \(k+1\) bits. Thus, the base-2 numeral for \(n\) has exactly \(\lfloor\log n\rfloor + 1\) bits.
- Many algorithms get a \(\log n\) factor in their running time because they process numbers bit-by-bit.
- Here are two examples.
Binary Search
- We do a binary search on an original array of size 10, and so we processed arrays of sizes 10, 5, 2, and 1, in order. In binary notation the sizes are 1010, 101, 10, 1.
- In general, an iteration starting with a size \(s\) array derives an array of size \(\le\lfloor s/2\rfloor\), e.g. size 10 can give size 5 or 4.
- In binary notaion we obtain \(\lfloor s/2\rfloor\) by right-shifting \(s\) one position.
- We can right-shift a number \(\lfloor \log n\rfloor\) times before it reaches 1.
- Therefore, binary search does \(O(\log n)\) probes, and it runs in time \(O(\log n)\).
Fast exponentiation algorithm
- The method of “repeated squaring” computes \(a^n\) in \(\Theta(\log n)\) arithmetic operations.
- The idea is to compute the sequence
\(a\),
\(a^2\),
\((a^2)^2 = a^4\),
\((a^4)^4 = a^8\),
\((a^8)^8 = a^{16}\),
\(\ldots\)
- The binary numeral of \(n\) tells us which powers of \(a\) to use. For instance, \(a^{19} = a^{16}a^2a^1\), or in binary natation: \(a^{10011} = a^{10000}a^{10}a^1\).