384 117 258KB
English Pages 10
Theoretical Computer Science Cheat Sheet
f(n) = O(g(n)) f(n) = (g(n)) f(n) = (g(n)) f(n) = o(g(n)) lim a = a n!1 n sup S inf S lim inf a n!1 n
lim sup an n!1 ,n k
14. 18. 22. 25. 28. 31. 34. 36.
De nitions i 9 positive c; n0 such that 0 f(n) cg(n) 8n n0. i 9 positive c; n0 such that f(n) cg(n) 0 8n n0. i f(n) = O(g(n)) and f(n) = (g(n)). i limn!1 f(n)=g(n) = 0. i 8 2 R, 9n0 such that jan , aj < , 8n n0. least b 2 R such that b s, 8s 2 S. greatest b 2 R such that b s, 8s 2 S. lim inf fai j i n; i 2 Ng. n!1 lim supfai j i n; i 2 Ng.
n!1
Series
n X i=1
i = n(n2+ 1) ;
In general: n X
i=1 nX ,1 i=1
n X i=1
im = m 1+ 1
i=0
i=0
ici =
k=0
k
Bk nm+1,k : 1 X
ci = 1 ,1 c ;
i=0 n +2 n +1 nc , (n + 1)c + c ; (c , 1)2
i=1
i=1
m m + 1 X
Harmonic series: n X Hn = 1i ; n X
2 2 i3 = n (n4+ 1) : i=1
n , X im = m 1+ 1 (n + 1)m+1 , 1 , (i + 1)m+1 , im+1 , (m + 1)im
Geometric series: n n+1 X ci = c c ,,1 1 ; c 6= 1; n X
n X
+ 1) ; i2 = n(n + 1)(2n 6
n X
c 6= 1;
1 X i=1
1 X i=0
ci = 1 ,c c ; c < 1;
ici = (1 ,c c)2 ; c < 1:
iHi = n(n2+ 1) Hn , n(n4, 1) :
i=1 n i X
1 n + 1 Combinations: Size k subHi = m + 1 Hn+1 , m + 1 : Hi = (n + 1)Hn , n; sets of a size n set. i=1 m i=1 n n n X Stirling numbers (1st kind): n n! n n k n 3. k = n , k ; 1. k = (n , k)!k! ; 2. Arrangements of an n elek =2 ; k =0 ment set into k cycles. n , 1 n n , 1 n , 1 n n n 5. k = k + k , 1 ; 4. k = k k , 1 ; Stirling numbers (2nd kind): k Partitions of an n element X r + k r + n + 1 ; 7. 6. mn mk = nk mn ,, kk ; set into k non-empty sets. n k =
n k n 1st order Eulerian numbers: n r s r + s n k n + 1 X X k Permutations 12 : : :n on ; 9. = 8. k n,k = n ; m+1 m f1; 2; : : :; ng with k ascents. k =0 k=0
n n = n = 1; n = (,1)k k , n , 1 ; 11. 10. 2nd order Eulerian numbers. k k 1 n k Cn Catlan Numbers: Binary n n n , 1 n , 1 n , 1 12. 2 = 2 , 1; 13. k = k k + k , 1 ; trees with n + 1 vertices. n = (n , 1)!; n = (n , 1)!H ; n = 1; n n ; 15. 16. 17. n , 1 2 n k k 1 n X 1 2n n n n n n = (n , 1) n , 1 + n , 1 ; 19. n , 1 = n , 1 = 2 ; 20. k=0 k = n!; 21. Cn = n + 1 n ; k,1 k k n = n = 1; n = n ; n = (k + 1) n , 1 + (n , k) n , 1 ; 23. 24. 0 n,1 k n, 1 , k k k k ,1 n 0 = 1 if k = 0, n n n , n , 1; n , (n + 1)2n + n + 1 ; 26. = 2 27. = 3 k 0 otherwise 1 2 2 n n x + k m n + 1 n n k X X X n n xn = ; 29. m = (m + 1 , k)n(,1)k ; 30. m! m = k n k k n,m ; k =0 k =0 k =0 n n n , k n =X n n n , k , m k!; 32. 0 = 1; 33. n = 0 for n 6= 0; m k m (,1) k =0 n n (2n)n X n = (k + 1) n , 1 + (2n , 1 , k) n , 1 ; 35. k k k,1 k = 2n ; k =0 X X n k n n x + n , 1 , k k n n + 1 x =X (m + 1)n,k ; = ; 37. m + 1 = m m k 2n k x,n k=0 k k=0
Theoretical Computer Science Cheat Sheet
Identities Cont. n + 1 X n k X n k n n,k = n! X 1 k ; n = 38. m + 1 = k m k=0 m k=0 k! m n X k n k+1
40. m = (,1)n,k ; k m + 1 m + n +k1 X m n + k 42. = k k ; m
Trees x X n n x + k tree with n ; Every 39. x , n = vertices has n , 1 2n k n X n +k=01 k edges.
41. m = (,1)m,k ; Kraft inequalk + 1 m m + n + 1k X n + k ity: If the depths m 43. = k(n + k) k ; of the leaves of m
a binary tree are k=0 n X n +k=0 X 1 k (,1)m,k ; 45. (n , m)! n = n + 1 k (,1)m,k ; for n m, d1; :n: :; dn: 44. m = X ,d k+1 m m 2 1; n k k +X1 mm, nm + n m + k m , nm + n m + k n k X i=1 i
46. n , m = k ; +k n ` +kmm +Xk kn n , k n 48. ` + m ` = ` m k ; k
Master method: T (n) = aT(n=b) + f(n); a 1; b > 1 If 9 > 0 such that f(n) = O(nlogb a, ) then T(n) = (nlogb a ): If f(n) = (nlogb a ) then T(n) = (nlogb a log2 n): If 9 > 0 such that f(n) = (nlogb a+ ), and 9c < 1 such that af(n=b) cf(n) for large n, then T(n) = (f(n)): Substitution (example): Consider the following recurrence i Ti+1 = 22 Ti2; T1 = 2: Note that Ti is always a power of two. Let ti = log2 Ti . Then we have ti+1 = 2i + 2ti; t1 = 1: Let ui = ti =2i. Dividing both sides of the previous equation by 2i+1 we get ti+1 = 2i + ti : 2i+1 2i+1 2i
Substituting we nd ui+1 = 21 + ui ; u1 = 12; which is simply ui = i=2. So we nd that Ti has the closed form Ti = 2i2i,1 . Summing factors (example): Consider the following recurrence Ti = 3Tn=2 + n; T1 = n: Rewrite so that all terms involving T are on the left side Ti , 3Tn=2 = n: Now expand the recurrence, and choose a factor which makes the left side \telescope"
47. n , m = k ; and equality holds n k` + mm+ k X n k+k n , k only if every inn 49. ` + m ` = ` m k : ternal node has 2 k
,
Recurrences
1 T (n) , 3T(n=2) = n , 3 T (n=2) , 3T (n=4) = n=2 .. .. .. . . . ,T(2) , 3T(1) = 2 log n , 1 2 3 , 3log2 n T(1) , 0 = 1 Summing the left side we get T (n). Summing the right side we get log X2 n n i i3 : i=0 2 Then we have Let c = 32 andm = log2 n. m m+1 , 1 X c n ci = n c , 1 i=0
= 2n(c clog2 n , 1) = 2n(c ck logc n , 1) = 2nk+1 , 2n 2n1:58496 , 2n;
where k = (log2 32 ),1 . Full history recurrences can often be changed to limited history ones (example): Consider the following recurrence i,1 X Ti = 1 + Tj ; T0 = 1: Note that
j =0
Ti+1 = 1 +
i X j =0
Tj :
Subtracting we nd i, 1 i X X Ti+1 , Ti = 1 + Tj , 1 , Tj j =0
= Ti : And so Ti+1 = 2Ti = 2i+1.
j =0
sons.
Generating functions: 1. Multiply both sides of the equation by xi . 2. Sum both sides over all i for which the equation is valid. 3. Choose a generatingPfunction i G(x). Usually G(x) = 1 i=0 x . 3. Rewrite the equation in terms of the generating function G(x). 4. Solve for G(x). 5. The coecient of xi in G(x) is gi . Example: gi+1 = 2gi + 1; g0 = 0: Multiply X andi sum: X X gi+1 x = 2gi xi + xi: i0
i0
i0
P We choose G(x) = i0 xi. Rewrite in terms of G(x): X i G(x) , g0 = 2G(x) +
x
i0
x:
Simplify: G(x) = 2G(x) + 1 : x 1,x Solve for G(x): x G(x) = (1 , x)(1 , 2x) : Expand this using partial fractions: G(x) = x 1 ,2 2x , 1 ,1 x
0 1 X X = x @2 2i xi , xi A 0 i0 X ii+1 i+1
=
i0
(2
So gi = 2i , 1.
, 1)x :
Theoretical Computer Science Cheat Sheet p e 2:71828,
0:57721, = 1:61803,
3:14159, i 2i pi 1 2 2 2 4 3 3 8 5 4 16 7 5 32 11 6 64 13 7 128 17 8 256 19 9 512 23 10 1,024 29 11 2,048 31 12 4,096 37 13 8,192 41 14 16,384 43 15 32,768 47 16 65,536 53 17 131,072 59 18 262,144 61 19 524,288 67 20 1,048,576 71 21 2,097,152 73 22 4,194,304 79 23 8,388,608 83 24 16,777,216 89 25 33,554,432 97 26 67,108,864 101 27 134,217,728 103 28 268,435,456 107 29 536,870,912 109 30 1,073,741,824 113 31 2,147,483,648 127 32 4,294,967,296 131 Pascal's Triangle 1 11 121 1331 14641 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 1 10 45 120 210 252 210 120 45 10 1
1+ 5 2
General Bernoulli Numbers (Bi = 0, odd i 6= 1): B0 = 1, B1 = , 12 , B2 = 61 , B4 = , 301 , B6 = 421 , B8 = , 301 , B10 = 665 . Change of base, quadratic formula: p2 log x b , 4ac : , b a logb x = log b ; 2 a a Euler's number e: 1 + e = 1 + 21 + 16 + 241 + 120 n lim 1 + nx = ex : n!1 , , 1 + n1 n < e < 1 + n1 n+1 : , 1 + n1 n = e , 2en + 2411ne2 , O n13 : Harmonic numbers: 49 363 761 7129 1, 32 , 116 , 25 , 137 , 20 , 140 , 280 , 2520 ; : : : 12 60 ln n < Hn < ln n + 1; Hn = ln n + + O n1 : Factorial, Stirling's approximation: 1, 2, 6, 24, 120, 720, 5040, 40320, 362880,
:::
p n n! = 2n ne 1 + n1 : Ackermann's 8 function and inverse: i=1 < 2j a(i; j ) = : a(i , 1; 2) j=1 a(i , 1; a(i; j , 1)) i; j 2 (i) = minfj j a(j; j ) ig: Binomial distribution: Pr[X = k] = nk pk qn,k ; q = 1 , p;
n X
n E[X ] = k = 1k k pk qn,k = np: Poisson distribution: , k Pr[X = k] = e k! ; E[X ] = : Normal (Gaussian) distribution: p(x) = p 1 e,(x,)2 =22 ; E[X ] = : 2 The \coupon collector": We are given a random coupon each day, and there are n dierent types of coupons. The distribution of coupons is uniform. The expected number of days to pass before we to collect all n types is nHn:
p ^ = 1,2 5 ,:61803 Probability Continuous distributions:Z If b Pr[a < X < b] = p(x) dx; a then p is the probability density function of X . If Pr[X < a] = P (a); then P is the distribution function of X . If P and p both existZthen a P ( a) = p(x) dx: ,1 Expectation: If XX is discrete E[g(X )] = g(x) Pr[X = x]: x
If X continuous then Z 1 Z 1 E[g(X )] = g(x)p(x) dx = g(x) dP (x): ,1 ,1 Variance, standard deviation: VAR[X ] = E[X 2] , E[X ]2; p = VAR[X ]: Basics: Pr[X _ Y ] = Pr[X ] + Pr[Y ] , Pr[X ^ Y ] Pr[X ^ Y ] = Pr[X ] Pr[Y ]; i X and Y are independent. X ^Y] Pr[X jY ] = Pr[Pr[ B] E[X Y ] = E[X ] E[Y ]; i X and Y are independent. E[X + Y ] = E[X ] + E[Y ]; E[cX ] = c E[X ]: Bayes' theorem: B jAi] Pr[Ai ] : Pr[AijB ] = PnPr[Pr[ A ] Pr[B jA ] j =1
j
j
Inclusion-exclusion: n n h_ i X Xi = Pr[Xi ] + Pr i=1
i=1 n X
(,1)k+1
k=1
X
ii