FROM THEORY TO INTERVIEW:
Logarithm, bridge
between math and
computer science
Mario Núñez Zavala
@mario_nezzvla
1
Definition
We say the logarithm, log of the
number n, given a base b is equal
to y if and only if gives the base b
to the power of y, is equal to n. → In computer science when we
say log of n, we always assume
that we are dealing with binary
logarithm which is logarithm base
2.
→
2
log(n)
log(2) = y → 2^y = 2 => y = 1
log(4) = y → 2^y = 4 => y = 2
log(8) = y → 2^y = 8 => y = 3
We went through these examples to
understand this relation.
To solve log of n we have to ask
ourselves: two to the power of what
is equal to n?
Every time we increase a power of
two in one (y + 1) we’re doubling
whatever number we had, for
instance 2^4 = (2^3)*2. So, n is found
multiplying the previous log(n) by 2.
2
log(n) II
2^10 = 1024 => log(1024) = 10
2^11 = 2048 => log(2048) = 11
2^12 = 4096 => log(4096) = 12
From these more examples we
observe then: When (N) doubles, (log of (N))
increases by only one! This highlights why, in complexity
analysis, algorithms with (log(N))
complexity are highly efficient. As
the input size doubles, the number
of elementary operations grows by
just one.
2
log(n) III
2^10 = 1024 => log(1024) = 10
2^20 = 1048576 => log(1048576) = 20
2^30 = 1073741824 => log(1073741824) =
30
Even as (N) grows exponentially,
(log(N)) increases minimally. This
demonstrates its efficiency complexity,
where only a small number of
operations are performed relative to the
input size (N).
Compared to (O(N)) (linear time
complexity), (log(N)) scales significantly
better as input size increases, making it
an excellent choice for algorithms
dealing with large datasets.
3
log(n)
graph
Compared to linear complexity
(green line), the larger (N) is (xaxis), the less the logarithmic time
function (log(N)) changes (purple
line). It simply flattens out.
4
log(n)
Core idea
(Log(n)) is really powerful because
represents a complexity that doesn’t
increase fast as the size of the input
increases. This is why (log(n)) is such a good
complexity to have.
Common algorithms with log(n)
complexity
Binary searc
Heap Operation
Balanced Binary search Tree
Divide and Conquer algorith
Exponentiation by Squaring
5
log(n)
in practice
[1, 21, 7, -18, 5, 101, -34, 207, 0]
let’s apply (log(n)) using an algorithm
that takes this array of length 9 as input.
How?
By cutting it in half each time:
[1, 21, 7, -18, 5, 101, -34, 207, 0] (9/2)~4
[1, 21, 7, -18] (4/2)=2
[1, 21] (2/1)=1
In the first iteration, we took 9/2 and
rounded to 4 (or possibly 5 if rounded up),
since the array’s length is not even. Then we
discarded the right side. We repeat this until
we retain a single value (1 in this case).
5
log(n)
in practice II
[1, 21, 7, -18, 5, 101, -34, 207, 0]
If we add the double of values, we just
increase in one the number of operations
to performance:
[
1, 21, 7, -18, 5, 101, -34, 207, 0, 6, 10, 3, 20, -2, -10, 19, 15, 19 ]
This algorithm we have created here,
corresponds to how binary search
operates through an array to find a
number in it.
6
Take away
I hope the concept of logarithms is clear,
and why it is so important to computer
science. Last example: A linear-time complexity algorithm with
an input size of 1000 might take roughly
1000 operations (going through all 1000
values) to complete. In contrast, a
logarithmic-time complexity algorithm
with the same input size would take
roughly 10 operations to complete, since
2^10 ≈ 1000.
What’s next?
I am going to talk about
Arrays, Linked lists, Hash
Tables, and Stacks and
Queues Soon here on
LinkedIn!
“Keep in learning state”
Mario Núñez Zavala
@mario_nezzvla