Bite-Sized Mathematics for Computer Science

Notes on MIT's 6.042J

Eda Eren
Table of Contents

Introduction

Prerequisites & Resources

Unit 1: Proofs

00. Introduction to Proofs

01. (Two) Proof Methods

02. The Well Ordering Principle

03. Logic & Propositions

04. Quantifiers and Predicate Logic

05. Sets

06. Binary Relations

07. Induction

08. State Machines — Invariants

09. Recursive Definitions

10. Infinite Sets

Unit 2: Structures

11. GCDs

12. Congruences

13. Euler's Theorem

14. RSA Encryption

15. Digraphs: Walks and Paths

16. Directed Acyclic Graphs

17. Partial Orders and Equivalence

18. Degrees & Isomorphism

19. Coloring & Connectivity

20. Trees

21. Stable Matching

Unit 3: Counting

23. Asymptotics

24. Counting with Bijections

25. Repetitions & Binomial Theorem

26. Pigeonhole Principle, Inclusion-Exclusion

Unit 4: Probability

27. Intro to Discrete Probability

28. Conditional Probability

29. Independence & Causality

30. Random Variables, Density Functions

31. Expectation

32. Deviation: Markov & Chebyshev Bounds

33. Sampling & Confidence

34. Random Walks & PageRank

Sums & Products

Alright, let's start with arithmetic sums:

A= first term + last term 2 number of terms A = \frac{\text{ first term } + \text{ last term }}{2} \cdot \text{ number of terms }

The sum of the first nn integers (1+2+...+(n1)+n1 + 2 + ... + (n - 1) + n) is:

n(1+n)2\frac{n(1 + n)}{2}

Why?

Say that we are looking for the sum of numbers from 1 to 5. What we are looking for is the answer to 1+2+3+4+51 + 2 + 3 + 4 + 5.

There is a constant amount of difference between each term, which is 11. The difference between 22 and 11 is 11, between 33 and 22 is 11, and so on.
That means, we can write it as

1+(1+1)+...+(51)+51 + (1 + 1) + ... + (5 - 1) + 5

Let's use FF to stand for our first term 11, and LL to stand for our last term 55. We can also use dd for the difference between terms, which is 11.
AA is for the arithmetic sum, of course.

Then, we can write it as

A=F+(F+d)+...+(Ld)+LA = F + (F + d) + ... + (L - d) + L

Addition is commutative, we can reverse the order of the terms:

A=L+(Ld)+...+(F+d)+FA = L + (L - d) + ... + (F + d) + F

Let's add those two together, and see what we have, because why not:

A=F+(F+d)+...+(Ld)+LA = F + (F + d) + ... + (L - d) + L
A=L+(Ld)+...+(F+d)+FA = L + (L - d) + ... + (F + d) + F
+\underline{\quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad +}
2A=(F+L)+(F+L)+...+(F+L)2A = (F + L) + (F + L) + ... + (F + L)

If you didn't catch it, dd's are canceling out. And, what we have is

2A=(F+L) the number of terms 2A = (F + L) \cdot \text{ the number of terms }

Dividing by 22 to find AA, we get what we have started with:

A=F+L2nA = \frac{F + L}{2} \cdot n

Substituting our first and last terms, clearly we have 1+n2n\frac{1 + n}{2} \cdot n. We can also write it as

n(1+n)2\frac{n(1 + n)}{2}

With geometric sums, each sum is a multiple of the previous sum.

So, Gn=1+x+x2+...+xnG_n = 1 + x + x^2 + ... + x^n.

The formula we have to find that is this:

1xn+11x\frac{1 - x^{n + 1}}{1 - x}

Again, why?

Let's use another trick, different from the one we used for arithmetic sums.

This time let's multiply GnG_n by xx. This will increase the power of xx in each term:

xGn=x+x2+x3+...xn+1xG_n = x + x^2 + x^3 + ... x^{n + 1}

Let's subtract it from the original GnG_n, because again, why not:

Gn=1+x+x2+...+xnG_n = 1 + x + x^2 + ... + x^n
xGn=xx2x3...xn+1-xG_n = -x -x^2 -x^3 - ... x^{n + 1}
\underline{\quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad -}
GnxGn=1xn+1G_n - xG_n = 1 - x^{n + 1}

What we end up having is

GnxGn=1xn+1G_n - xG_n = 1 - x^{n + 1}

We can factor GnG_n out to have:

Gn(1x)=1xn+1G_n (1 - x) = 1 - x^{n + 1}

That means, GnG_n is

1xn+11x\frac{1 - x^{n + 1}}{1 - x}

We had simple formulas for arithmetic and geometric sums, but this time, we'll look at a formula that can give an estimation.

Let f:R+R+f : \mathbb{R}^+ \rightarrow \mathbb{R}^+ be a weakly increasing function.

The sum is:

S=i=1nf(i)S = \displaystyle\sum_{i = 1}^n f(i)

Think of each term as a unit-width (ii) rectangle, with a height of f(i)f(i).
Then, the number for the sum we are looking for has to be the total area under the curve of f(x)f(x).

The area under the curve f(x)f(x) from 11 to nn is:

I=1nf(x) dxI = \int_{1}^{n} f(x) \ dx

Then:

I+f(1)SI+f(n)I + f(1) \leq S \leq I + f(n)

Perhaps it would make more sense visually:

Sums1

And, shift it left by 11:

Sums2

Figures 13.2 and 13.3 from the course notes Mathematics for Computer Science.


Any product can be converted into a sum by taking its logarithm.

Let's look at a factorial, for example, n!n! is 123 ... (n1)n1 \cdot 2 \cdot 3 \cdot \ ... \ \cdot (n - 1) \cdot n.

We can also denote it as:

i=1ni\displaystyle\prod_{i = 1}^n i

It can be turned into a sum by taking the logarithm:

i=1nln(i)\displaystyle\sum_{i = 1}^n ln(i)

It is because of the product rule:

ln(n!)=ln(123 ... (n1)n)ln(n!) = ln(1 \cdot 2 \cdot 3 \cdot \ ... \ \cdot (n - 1) \cdot n)
=ln(1)+ln(2)+ln(3)+ ... +ln(n1)+ln(n) = ln(1) + ln(2) + ln(3) + \ ... \ + ln(n - 1) + ln(n)
=i=1nln(i) = \displaystyle\sum_{i = 1}^n ln(i)

We have to estimate this sum to get a closed-form bound.
In order to do that, we are going to use the formula we defined above: I+f(1)SI+f(n)I + f(1) \leq S \leq I + f(n).

So, plugging them in:

(1nln(x) dx)+ln(1)i=1nln(i)(1nln(x) dx)+ln(n)\bigg(\int_{1}^{n} ln(x) \ dx\bigg) + ln(1) \leq \displaystyle\sum_{i = 1}^n ln(i) \leq \bigg(\int_{1}^{n} ln(x) \ dx\bigg) + ln(n)

Looks beautiful.

ln(1)ln(1) is 00. So, it is just

1nln(x) dxi=1nln(i)(1nln(x) dx)+ln(n)\int_{1}^{n} ln(x) \ dx \leq \displaystyle\sum_{i = 1}^n ln(i) \leq \bigg(\int_{1}^{n} ln(x) \ dx\bigg) + ln(n)

After taking the integral, and exponentiating the whole thing (it was the product, remember, we are undoing it), the eventual result will be, according to the book:

nnen1n!nn+1en1\frac{n^n}{e^{n - 1}} \leq n! \leq \frac{n^{n + 1}}{e^{n - 1}}

One example of asymptotic equivalence is (n2+n)n2(n^2 + n) \sim n^2.

In order for two functions to be asymptotically equal, the limit of their quotient has to go to 11 as nn approaches infinity. Let's see.

limnn2+nn2=limn(1+1n)=1\lim_{n \to \infty} \frac{n^2 + n}{n^2} = \lim_{n \to \infty} \bigg(1 + \frac{1}{n}\bigg) = 1

So, simplifying n2+nn2\frac{n^2 + n}{n ^2} we have 1+1n1 + \frac{1}{n}. As nn goes to \infty, 1n\frac{1}{n} goes to 00, so we're left with 11. And, that proves that (n2+n)(n^2 + n) and n2n^2 are asymptotically equal.