DAA Study Hub

Cheatsheet

Every formula on one page.

One page, every formula. If something here is unclear, follow the link to its topic.

Logs & exponents

Details: Topic 0

log⁡bx=y  ⟺  by=x\log_b x = y \iff b^y = xlog⁡b(xy)=log⁡bx+log⁡by\log_b(xy) = \log_b x + \log_b y
log⁡b(x/y)=log⁡bx−log⁡by\log_b(x/y) = \log_b x - \log_b ylog⁡bxa=alog⁡bx\log_b x^a = a\log_b x
blog⁡bx=xb^{\log_b x} = xlog⁡cx=log⁡bx/log⁡bc\log_c x = \log_b x / \log_b c
alog⁡bc=clog⁡baa^{\log_b c} = c^{\log_b a}log⁡2(n/2)=log⁡2n−1\log_2(n/2) = \log_2 n - 1
210≈1032^{10} \approx 10^3log⁡2106≈20\log_2 10^6 \approx 20

Sums

Details: Topic 0

SumClosed formΘ
∑i=1n1\sum_{i=1}^n 1nnnn
∑i=1ni\sum_{i=1}^n in(n+1)2\frac{n(n+1)}{2}n2n^2
∑i=1ni2\sum_{i=1}^n i^2n(n+1)(2n+1)6\frac{n(n+1)(2n+1)}{6}n3n^3
∑i=0k2i\sum_{i=0}^k 2^i2k+1−12^{k+1} - 1last term
∑i=0kri\sum_{i=0}^k r^i (r≠1r \ne 1)rk+1−1r−1\frac{r^{k+1} - 1}{r - 1}
∑i=0∞ri\sum_{i=0}^\infty r^i (r<1r < 1)11−r\frac{1}{1-r}constant
∑i=1n1i\sum_{i=1}^n \frac1i≈ln⁡n\approx \ln nlog⁡n\log n

Proof templates

Details: Topic 2, Topic 3

  • Induction: base case → hypothesis S(k)S(k) → prove S(k+1)S(k+1) by writing it as "S(k)S(k) + new piece".
  • Contradiction: assume the opposite → derive something impossible → so the claim holds.
  • Contraposition: to prove P⇒QP \Rightarrow Q, prove ¬Q⇒¬P\neg Q \Rightarrow \neg P.
  • Loop invariant: Initialization · Maintenance · Termination (substitute the exit value, e.g. i=n+1i = n + 1).
  • Correctness = partial correctness + termination.

Algorithms

Details: Topic 4, Topic 7

BestWorstExtra spaceStable
Insertion sortΘ(n)\Theta(n) sortedΘ(n2)\Theta(n^2) reverseΘ(1)\Theta(1)yes
Merge sortΘ(nlog⁡n)\Theta(n\log n)Θ(nlog⁡n)\Theta(n\log n)Θ(n)\Theta(n)yes
Binary searchΘ(1)\Theta(1)Θ(log⁡n)\Theta(\log n)Θ(1)\Theta(1) iterative—
  • Insertion sort worst case: shifts =n(n−1)2= \frac{n(n-1)}{2}. Shifts = number of inversions.
  • Merging lists of sizes pp and qq: at most p+q−1p + q - 1 comparisons.

Loops

Details: Topic 5

PatternΘ
independent nested n×nn \times nn2n^2
j ← 1 to i inside i ← 1 to nn2n^2 (≈ n2/2n^2/2)
counter doubles / halveslog⁡n\log n
halving outer, inner does ii worknn
header for i ← 1 to n runsn+1n + 1 times

Asymptotics

Details: Topic 6

  • f=O(g)f = O(g): ∃c,n0\exists c, n_0 with f(n)≤c g(n)f(n) \le c\,g(n) for all n≥n0n \ge n_0
  • f=Ω(g)f = \Omega(g): ∃c,n0\exists c, n_0 with f(n)≥c g(n)f(n) \ge c\,g(n) for all n≥n0n \ge n_0
  • f=Θ(g)f = \Theta(g): both, i.e. c1g≤f≤c2gc_1 g \le f \le c_2 g
  • 1<log⁡n<n<n<nlog⁡n<n2<n3<2n<n!<nn1 < \log n < \sqrt n < n < n\log n < n^2 < n^3 < 2^n < n! < n^n
  • Disprove f=O(g)f = O(g): assume c,n0c, n_0 exist, derive n≤n \le constant, contradiction.

Recurrences

Details: Topic 8

For T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n): level ii has aia^i nodes of size n/bin/b^i, there are log⁡bn+1\log_b n + 1 levels, and the leaves number nlog⁡ban^{\log_b a}.

RecurrenceΘ
T(n/2)+1T(n/2) + 1log⁡n\log n
T(n/2)+nT(n/2) + nnn
2T(n/2)+12T(n/2) + 1nn
2T(n/2)+n2T(n/2) + nnlog⁡nn\log n
2T(n/2)+n22T(n/2) + n^2n2n^2
4T(n/2)+n4T(n/2) + nn2n^2
T(n−1)+1T(n-1) + 1nn
T(n−1)+nT(n-1) + nn2n^2

On this page