CS 70 Cheatsheet Note 1 (Propositional Logic) • • • • π =⇒ π ≡ ¬π ∨ π π =⇒ π ≡ ¬π =⇒ ¬π ¬(π ∧ π) ≡ ¬π ∨ ¬π ¬(π ∨ π) ≡ ¬π ∧ ¬π Note 7 (Modular Artithmetic) • • • • • π₯ −1 (modular inverse) exists mod π if and only if gcd(π₯, π) = 1 π ∧ (π ∨ π ) ≡ (π ∧ π) ∨ (π ∧ π ) • Extended Euclidean Algorithm: π ∨ (π ∧ π ) ≡ (π ∨ π) ∧ (π ∨ π ) π₯ π¦ bπ₯/π¦c π π ¬(∀π₯π(π₯)) ≡ ∃π₯¬π(π₯) – new π = old π j k ¬(∃π₯π(π₯)) ≡ ∀π₯¬π(π₯) 35 12 2 −1 3 answer – new π = π − π π¦π₯ 12 11 1 1 −1 – if gcd(π₯, π¦) = 1, then π = 11 1 11 0 1 π₯ −1 mod π¦, π = π¦ −1 mod π₯ start 1 0 Note 2/3 (Proofs) • Direct proof • Proof by contraposition • Proof by cases • Proof by induction – Base case (prove smallest case is true) – Inductive hypothesis (assume π = π true for weak induction, assume π ≤ π true for strong induction) – Inductive step (prove π = π + 1 is true) • Pigeonhole principle – Putting π + π balls in π bins =⇒ ≥ 1 bin has ≥ 2 balls Note 4 (Sets) gcd • π«(π) = powerset/set of all subsets; if |π| = π, |π«(π)| = 2 π • One to one (injection); π (π₯) = π (π¦) =⇒ π₯ = π¦ • Onto (surjection); (∀π¦∃π₯)( π (π₯) = π¦); “hits" all of range • Bijection: both injective and surjective Note 5 (Countability & Computability) • Countable if bijection to N • Cantor-Schroder-Bernstein Theorem: bijection between π΄ and π΅ if there exists injections π : π΄ → π΅ and π : π΅ → π΄ • Cantor diagonalization: to prove uncountability, list out possibilities, construct new possibility different from each listed at one place (ex. reals ∈ (0, 1), infinite binary strings, etc) • π΄ ⊆ π΅ and π΅ is countable =⇒ π΄ is countable • π΄ ⊇ π΅ and π΄ is uncountable =⇒ π΅ is uncountable • Infinite cartesian product sometimes countable (∅ × ∅ × · · · ), sometimes uncountable ({0, 1}∞ ) • Halting Problem: can’t determine for every program whether it halts (uncomputable) • Reduction of TestHalt(P, x) to some task (here, TestTask) – define inner function that does the task if and only if P(x) halts – call TestTask on the inner function and return the result in TestHalt Note 6 (Graph Theory) • πΎ π has 2 edges • Handshaking lemma: total degree = 2π • Trees: (all must be true) – connected & no cycles – connected & has π − 1 edges (π = |π |) – connected & removing an edge disconnects the graph – acyclic & adding an edge makes a cycle • Hypercubes: – π-length bit strings, connected by an edge if differs by exactly 1 bit – π-dimensional hypercube has π2π−1 edges, and is bipartite (even vs odd parity bitstring) • Eulerian walk: visits each edge once; only possible if connected and all even degree or exactly 2 odd degree • Eulerian tour: Eulerian walk but starts & ends at the same vertex; only possible if all even degree and connected • Planar graphs – π£+ π = π+2 Pπ – π=1 π π = 2π where π π = number of sides of face π – π ≤ 3π£ − 6 if planar (because π π ≥ 3) – π ≤ 2π£ − 4 if planar for bipartite graphs (because π π ≥ 4) – nonplanar if and only if the graph contains πΎ5 or πΎ3,3 – all planar graphs can be colored with ≤ 4 colors π(π−1) • Chinese Remainder Theorem: – find bases π π that are ≡ 1 mod π π and ≡ 0 mod π π for π ≠ π Q → π π = π π (π−1 π≠ π π π π modQπ π ) where π π = P – π₯ ≡ π π π π (mod ππ ) Q – solution is unique mod π π – π π must be pairwise relatively prime in order to use CRT Note 8 (RSA) • Scheme: for primes π, π, find π coprime to ( π − 1)(π − 1) – public key: π = ππ and π – private key: π = π −1 mod ( π − 1)(π − 1) – encryption of message π: π π (mod π) = π¦ – decryption of encrypted message π¦: π¦ π (mod π) = π • Fermat’s Little Theorem (FLT): π₯ π ≡ π₯ (mod π), or π₯ π−1 ≡ 1 (mod π) if π₯ coprime to π • Prime Number Theorem: π(π) ≥ lnππ for π ≥ 17, where π(π) = # of primes ≤ π • Breaking RSA if we know π: – we know ππ − 1 = π ( π − 1)(π − 1), where π ≤ π because π < ( π − 1)(π − 1) – so ππ−1 π = ππ − π − π − 1; ππ = π, so we can find π, π because we know π, π, π Note 9 (Polynomials) • Property 1: nonzero polynomial of degree π has at most π roots • Property 2: π + 1 pairs of points (π₯π distinct) uniquely defines a polynomial of degree at most π • Lagrange Interpolation: Q π₯−π₯ – Δπ (π₯) = π≠ π π₯π −π₯ππ P – π(π₯) = π π¦ π Δπ (π₯) • Secret Sharing (normally under πΊπΉ ( π)): – π(0) = secret, π(1), . . . , π(π) given to all people – π(π₯) = polynomial of degree π − 1, where π people are needed to get the secret • Rational Root Theorem: for π(π₯) = π π π₯ π + · · · + π 0 , the roots of π(π₯) π that are of the form π must have π | π 0 , π | π π Note 10 (Error Correcting Codes) • Erasure Errors: π packets lost, message length π; need to send π + π packets because π(π₯) of degree π − 1 needs π points to define it • General Errors: π packets corrupted, message length π; send π + 2π packets • Berlekamp Welch: – π(π₯) encodes message (degree π − 1) – πΈ (π₯) constructed so that roots are where the errors are (degree π); coefficients unknown – π(π₯) = π(π₯)πΈ (π₯) (degree π + π − 1) – substitute all (π₯π , π π ) into π(π₯π ) = π π πΈ (π₯π ), make system of equations π ( π₯) – solve for coefficients; π(π₯) = πΈ ( π₯) 1 CS 70 Cheatsheet Note 11 (Counting) • 1st rule of counting: multiply # of ways for each choice • 2nd rule of counting: count ordered arrangements, divide by # of ways to get unordered to order π! • ππ = π!(π−π)! = # ways to select π from π • Stars and bars: π objects, π groups → π stars, π − 1 bars π+π−1 → π+π−1 π−1 = π • Zeroth rule of counting: if bijection between π΄ and π΅, then | π΄| = |π΅| P • Binomial theorem: (π + π) π = ππ=0 ππ π π π π−π π−2 π π • Hockey-stick identity: π+1 = π−1 π + π +···+ π P (−1) π • Derangements: π· π = (π − 1)(π· π−1 + π· π−2 ) = π! ππ=0 π! • Principle of Inclusion-Exclusion: | π΄1 ∪ π΄2 | = | π΄1 | + | π΄2 | − | π΄1 ∩ π΄2 | More generally, alternate add/subtract all √ πcombinations • Stirling’s approximation: π! ≈ 2ππ ππ Note 12 (Probability Theory) • Sample points = outcomes • Sample space = Ω = all possible outcomes • Probability space: (Ω, P(π)); (sample space, probability function) P • 0 ≤ P(π) ≤ 1, ∀π ∈ Ω; π ∈Ω π(π) = 1 1 , ∀π ∈ Ω • Uniform probability: P(π) = |Ω| P • P( π΄) = π ∈ π΄ P(π) where π΄ is an event # sample points in π΄ | π΄| • If uniform: P( π΄) = # sample points in Ω = |Ω| • P( π΄) = 1 − P( π΄) Note 13 (Conditional Probability) π) • P(π | π΅) = P( π (π΅) for π ∈ π΅ π΄∩π΅) → P( π΄ ∩ π΅) = P( π΄ | π΅) P(π΅) • P( π΄ | π΅) = P(P(π΅) • Bayes’ Rule: P(π΅ | π΄) P( π΄) P(π΅ | π΄) P( π΄) . P( π΄ | π΅) = = P(π΅) P(π΅ | π΄) P( π΄) + P(π΅ | π΄) P( π΄) • Total Probability Rule (denom of Bayes’ Rule): π π ∑οΈ ∑οΈ P(π΅) = P(π΅ ∩ π΄π ) = P(π΅ | π΄π ) P( π΄π ) π=1 π=1 for π΄π partitioning Ω • Independence: P( Sπ΄ ∩ π΅) = P(Pπ΄) P(π΅) or P( π΄ | π΅) = P( π΄) π π΄ ≤ π P( π΄ ) • Union bound: P π=1 π π π=1 Note 16 (Geometric/Poisson Distributions • Geometric distribution: P(π = π) = exactly π trials until success with probability π; use π − 1 for failures until success – Memoryless Property: P(π > π + π | π > π) = P(π > π); i.e. waiting > π units has same probability, no matter where we start • Poisson distribution: π = average $ of successes in a unit of time – π ∼ Pois(π), π ∼ Pois(π) independent: π + π ∼ Pois(π + π) – π ∼ Bin(π, ππ ) where π > 0 is constant, as π → ∞, π → Pois(π) Note 20 (Continuous Distributions) • Probability density function: – ∫π π (π₯) ≥ 0 for π₯ ∈ R ∞ – −∞ π π (π₯) dπ₯ = 1 ∫π₯ • Cumulative density function: πΉπ (π₯) = P(π ≤ π₯) = −∞ π π (π‘) dπ‘, d πΉ (π₯) π π (π₯) = dπ₯ π ∫∞ • Expectation: E[π] = −∞ π₯ π π (π₯) dπ₯ ∫∞ • LOTUS: E[π(π)] = −∞ π(π₯) π π (π₯) dπ₯ • Joint distribution: P(π ≤ π ≤ π, π ≤ π ≤ π) – ∫π ππ ∫(π₯, π¦) ≥ 0, ∀π₯, π¦ ∈ R ∞ ∞ – −∞ −∞ π ππ (π₯, π¦) dπ₯ dπ¦ = 1 ∫∞ • Marginal distribution: π π (π₯) = −∞ π ππ (π₯, π¦) dπ¦; integrate over all π¦ • Independence: π ππ (π₯, π¦) = π π (π₯) ππ (π¦) πππ ( π₯,π¦) ππ ( π₯) • Conditional probability: π π | π΄ (π₯) = P( ππ ( π¦) π΄) , π π |π (π₯ | π¦) = • Exponential distribution: continuous analog to geometric distribution – Memoryless property: P(π > π‘ + π | π > π‘) = P(π > π ) – Additionally, P(π < π | min(π, π ) > π‘) = P(π < π ) – If π ∼ Exp(π π ), π ∼ Exp(ππ ) independent, then π min(π, π ) ∼ Exp(π π + ππ ) and P(π ≤ π ) = πππ+π π • Normal distribution (Gaussian distribution) 2 2 – If π ∼ N (π π , ππ ), π ∼ N (ππ , ππ ) independent: π = ππ + ππ ∼ N (ππ π + πππ , π 2 ππ2 + π 2 ππ2 ) Pπ ππ , all ππ iid with mean π, • Central Limit Theorem: if π π = π=1 variance π 2 , ππ π2 π π − ππ ≈ N π, ; ≈ N (0, 1). √ π π π π Note 17 (Concentration Inequalities, LLN) ] • Markov’s Inequality: P(π ≥ π) ≤ E[π π , if π nonnegative, π > 0 Note 14 (Random Variables) • Bernoulli distribution: used as an indicator RV • Binomial distribution: P(π = π) = π successes in π trials, success probability π – If π ∼ Bin(π, π), π ∼ Bin(π, π) independent, π +π ∼ Bin(π+π, π) • Hypergeometric distribution: P(π = π) = π successes in π draws w/o replacement from size π population with π΅ objects (as successes) • Joint distribution: P(π = π, π = π) P • Marginal distribution: P(π = π) = π ∈π΅ P(π = π, π = π) • Independence: P(π = π, π = π) = P(π = π) P(π = π) P • Expectation: E[π] = π₯ ∈X π₯ P(π = π₯) P • LOTUS: E[π(π)] = π₯ ∈X π(π₯) P(π = π₯) • Linearity of expectation: E[ππ + ππ ] = π E[π] + π E[π ] • π, π independent: E[ππ ] = E[π] E[π ] Note 15 (Variance/Covariance) • Variance: Var(π) = E[(π − π) 2 ] = E π 2 − E[π] 2 – Var(ππ) = π2 Var(π), Var(π +π ) = Var(π) + Var(π ) + 2 Cov(π, π ) – if indep: Var(π + π ) = Var(π) + Var(π ) • Covariance: Cov(π, π ) = E[ππ ] − E[π] E[π ] Cov(π ,π ) • Correlation: Corr(π, π ) = ππ ππ , always in [−1, 1] • Indep. implies uncorrelated (Cov = 0), but not other way around, ex. ( π= 1 −1 π = 0.5 , π = −0.5 ο£±  1  ο£²  π = −1   0 ο£³ π = −1, π = 0.5 π = −1, π = 0.5 π=1 2 |π | π ] • Generalized Markov: P(|π | ≥ π) ≤ E[ ππ for π, π > 0 Var(π ) • Chebyshev’s Inequality: P(|π − π| ≥ π) ≤ π 2 for π = E[π], π>0 √οΈ – Corollary: P(|π − π| ≥ ππ) ≤ π12 for π = Var(π), π > 0 • Confidence intervals: Var( π) ˆ – For proportions, P(| πˆ − π| ≥ π) ≤ π 2 ≤ πΏ, where πΏ is the confidence level (95% interval → πΏ = 0.05) π (1− π) – πˆ = proportion of successes in π trials, Var( π) ˆ = π 1 – =⇒ π ≥ 4π 2 πΏ 2 – For means, P π1 π π − π ≥ π ≤ πππ 2 = πΏ Pπ – π π = π=1 ππ , all ππ ’s iid mean π, variance π 2 – =⇒ π = √π , interval = π π ± √π ππΏ ππΏ – With CLT, √ √ P(| π΄π − π| ≤ π) = P ( π΄π −π) π π π π ≤ π √ π π ≈ 1 − 2Φ − π = 1 − πΏ. 2 Here, π΄π = π1 π π and CLT gives π΄π ≈ N π, ππ ; use inverse cdf to get π • Law of large numbers: as π → ∞, sample average of iid π1 , . . . ππ tends to population mean CS 70 Cheatsheet Note 24 (Markov Chains) • Markov chain = sequence of states • ππ = state at time step π • X = state space (finite) • π π = distribution of states at time step π • Markov property (memoryless): P(ππ+1 = π | ππ , ππ−1 , . . . , π0 ) = P(ππ+1 = π | ππ ) • Transition matrix (P): transition probabilities, from row to column • π π = π0 Pπ • Invariant distribution: π = πP; solve balance equations in addition to P π(π) = 1 • Irreducible Markov chain: any state can be reached from any other • All irreducible Markov chains have a unique invariant distribution: ∑οΈ 1 π−1 1{ππ = π}. π(π) = lim π→∞ π π=0 That is, average time spent at state π = π(π) • Periodicity: let π (π) = gcd{π > 0 : P(ππ = π | π0 = π) > 0} – If π (π) > 1, periodic with period π – If π (π) = 1, aperiodic – If irreducible, all states have the same π (π) • Aperiodic irreducible Markov chains will always converge to the invariant distribution • Hitting time: # of steps before first reaching state π – Let π½( π) denote this value, starting at state π; set up system of equations and solve • P( π΄ before π΅): similarly, let πΌ( π) denote this probability, starting at state π; set up system of equations and solve (use TPR) • Similar problems: let π (π) denote # of steps or probability, starting at state π; set up equations and solve • 2-state Markov chain: π 1−π 1 2 1−π π π 1−π π π P= and π = π+π π+π π 1−π Other • In CS70, naturals and whole numbers both include 0 π • Sum of finite geometric series: 1−π 1−π π where π is ratio, π is first term, π is number of terms • Memoryless property independence: if π, π both memoryless (i.e. geometric or exponential), then min(π, π ) and max(π, π ) − min(π, π ) are independent Pπ P Pπ • E π 2π = π=1 E ππ2 + 2 π≠ π E ππ π π = π=1 E ππ2 , where Pπ π π = π=1 ππ ; useful for variance of indicators • Coupon Collector Problem: – π distinct items, each with equal probability; ππ = # of tries before πth new item, given π − 1 already P – π π = ππ = total tries before getting all items – We have ππ ∼ Geom( π−π+1 π ) because π − 1 old items, so π − π + 1 new items – Hence, π π π 1 ∑οΈ ∑οΈ ∑οΈ π E[π π ] = E[ππ ] = =π ≈ π(ln π + 0.5772) π=1 π=1 π − π + 1 π=1 π 3 CS 70 Table 1: Common Discrete Distributions Distribution Parameters PMF (P(π = π)) Uniform Uniform(π, π) Bernoulli Bernoulli( π) 1 π−π+1 ( 1 π 0 1− π Binomial Bin(π, π) π π π (1 − π) π−π π Geometric Geom( π) π(1 − π) π−1 Poisson π π π −π Pois(π) CMF (P(π ≤ π)) Expectation (E[π]) π −π+1 π−π+1 π+π 2 Variance (Var(π)) (π − π + 1) 2 −1 12 πΎ π −πΎ π π−π π π Hypergeometric(π, πΎ, π) π ∈ [π, π] — π π(1 − π) π ∈ {0, 1} — ππ ππ(1 − π) π∈N 1 − (1 − π) π 1 π 1− π π2 π∈N — π π π∈N πΎ (π − πΎ)(π − π) π 2 (π − 1) π∈N π! Hypergeometric Support — π πΎ π π 4 Distribution Uniform Exponential Normal/Gaussian PDF ( π π (π₯)) Parameters CDF (πΉπ (π₯) = P(π ≤ π₯)) Expectation (E[π]) Uniform(π, π) 1 π−π π₯−π π−π Exp(π) ππ −ππ₯ 1 − π −ππ₯ π+π 2 1 π Φ(π₯) π N (π, π 2 ) √ (π₯ − π) 2 exp − 2π 2 2ππ 1 Variance (Var(π)) (π − π) 2 12 1 π2 π2 Support π ∈ [π, π] π ∈ [0, ∞) π∈R Cheatsheet Table 2: Common Continuous Distributions