y = b (x) x = b y b (xy) = b (x) + b (y) b (x) = b (c) c (x) = c (x) c (b) b (x n ) = n b (x) x a x b = x (a+b) (x a ) b = x (ab) x ( 1 2 ) = x x (a b) = xa x b k = n(n+1) 2 2k 1 = n 2 k 2 = n(n+1)(2n+1) 6 ar k 1 = a(1 rn ) 1 r ar k 1 = a 1 r O, Θ, and Ω f(n) n g(n) O(V + E) 0( ) f(n) Ω(g(n)) < (0) f(n) O(g(n)) = c, 0 < c < f(n) Θ(g(n)) (v i, v j ) i < j pre/post [u[v v]u] is a Tree/Forward Edge [v[u u]v] is a Back Edge [v v][u u] is a Cross Edge T (n) = at ( n/b ) + O(n d ) for a > 0, b > 1, and d 0, O(n d ) T (n) = O(n d logn) O(n logba ) a log b n a log bn = n log ba ifd > log b a ifd = log b a ifd < lob b a O(n n) (p q)(x) p(x) q(x) O(n 2 ) p(x) q(x) 2n + 1 O(n n) p(x) q(x) 2n + 1 (p q)(x) O(n) (p q)(x) O(n n) < values > < coeff > w < coeff > 1/n < values > w 1 function FFT(A, w) Input: Coefficient representation of a polynomial A(x) of degree less than or equal to n 1, where n is a power of 2w, an nth root of unity Output: Value representation A(w_0),...,A(w_n 1) if w = 1: return A(1) express A(x) in the form A_e(x^2) + xa_o(x^2) call FFT(A_e, w^2) to evaluate A_e at even powers of w call FFT(A_o, w^2) to evaluate A_o at even powers of w for j = 0 to n 1: compute A(w_j) = A_e(w^2j) + w^j A_o(w^2j) return A(w_0),...,A(w_n 1) O(V + E) (pre(u) < pre(v) < post(v) < post(u)) def explore(g,v): #Where G = (V,E) of a Graph Input: G = (V,E) is a graph; v V Output: visited(u) is set to true for all nodes u reachable from v visited(v) = true previsit(v) for each edge(v,u) in E: if not visited(u): postvisit(v) def dfs(g): explore(u) for all v in V: if not visited(v): explore(v) O(n) O(n) O(V + E) Input: Graph G = (V, E), directed or undirected; vertex s V Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u. def bfs(g,s): Q = [s] (Queue containing just s) while Q is not empty: u = eject(u) for all edges (u,v) in E: if dist(v) = infinity: inject(q,v) dist(v) = dist(u) + 1 O(V + E) V def dijkstra(g,l,s): H = makequeue(v) # using dist values as keys while H is not empty: u = deletemin(h) for all edges (u,v) in E: if dist(v) > dist(u)+l(u,v) dist(v) = dist(u)+l(u,v) prev(v) = u decreasekey(h,v)
O(V E) procedure shortest-paths(g, l, s) Input: Directed graph G = (V, E); edge lengths {l_e: e in E} with no negative cycles; vertex s in V Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u. repeat V -1 times: for all e in E: update(e) O(n) V 1 V 1 O(E E) E V 2 Input: A connected undirected graph G = (V,E) with edge weights w Output: A minimum spanning tree defined by the edges X X = {} makeset(u) Sort the edges E by weight for all edges {u,v} in E, in increasing order of weight: if find(u)!= find(v): add edge {u,v} to X union(u,v) E = V 1 G = (V, E) X e O(E E) E V 2 procedure prim(g, w) Input: A connected undirected graph G = (V, E) with weights Output: A minimum spanning tree defined by the array prev for all u in V : cost(u) = infinity Pick any initial node u_0 cost(u_0) = 0 H = makequeue (V) (priority queue with cost-values as keys) while H is not empty: v = deletemin(h) for each {v, z} in E: if cost(z) > w(v, z): cost(z) = w(v, z) prev(z) = v decreasekey(h, z) Input: A set of elements B; sets S1,...,Sm Output: A selection of the S_i whose union is B. Cost: Number of sets picked. Repeat until all elements of B are covered: Pick the set Si with the largest number of uncovered elements. pi rank x, rank(x) < rank(π(x)) k 2 k n n 2k k logn def makeset(x): // O(1) pi(x) = x rank(x) = 0 def find(x): // O(E log V) while x!= pi(x): x=pi(x) return x def union(x,y): // O(E log V) if find(x) == find(y): return elif rank(find(x)) > rank(find(y)): else: pi(find(y)) = find(x) pi(find(x))=find(y) if rank(find(x)) == rank(find(y)): function find(x): rank(find(y)) = rank(find(y)) + 1 if x!= pi(x): pi(x) = find(pi(x)) return pi(x) union(x, y) find(x) Olog n O(logN) O(nlog n) func huffman(f): Input: An array f[1...n] of frequencies Output: An encoding tree with n leaves let H be a priority queue of integers, ordered by f for i=1 to n: insert(h,i) for k = n+1 to 2n-1: i=deletemin(h), j=deletemin(h) create a node numbered k with children i,j f[k] = f[i]+f[j] insert(h,k)
θ( subproblems time/subproblem) i x[i :] i O(n) x[: i] i O(n) x[i : j] i, j O(n 2 ) O(n) memo = {} fib(n): if n in memo: return memo[n] if n <= 2: f = 2 else: f = fib(n-1) + fib(n-2) memo[n] = f return f fib = [] fib(n): for k in range(1, n): if k <= 2: f = 2 else: f = fib[k-1] + fib[n-2] fib[k] = f return fib[n] θ(v E) S k (s, v) = k S k (s, v) = min (u,v)ine (S k 1 (s, u) + w(u, v) O(n 2 ) L = {} for j=1,2,...,n: L[j] = 1+max{L[i]:(i,j) in E} # The (i,j) represents all the edges that go from # a node to j. return max(l) S _ N O W Y S U N N _ Y for i = 0,1,2,...,m: E(i,0) = i for j = 1,2,...,n: E(0,j) = j for i = 1,2,...,m: for j = 1,2,...,n: return E(m,n) E(i,j) = min{e(i-1,j)+1,e(i,j-1)+1,e(i-1,j-1) O(nW ) +diff(i,j)} K(ω) = max items {K(ω ω item ) + value} K(ω, j) = max available items {K(ω ω j, j 1) + V j, k(ω, j 1)} O(n 3 ) C(i, j) = min{c(i, k) + c(k + 1, j) + m i 1 m k m j } O( V 3 ) for i=1 to n: for j=1 to n: dist(i,j,0) = infinity for all (i,j) in E: for k = 1 to n: dist(i,j,0) = l(i,j) for i = 1 to n: for j = 1 to n: dist(i,j,k) = min{dist(i,k,k-1)+ dist(k,j,k-1), dist(i,j,k-1)} O(n 2 2 n ) C({1},1)=0 for s = 2 to n: for all subsets S in {1,2,...,n} of size s and has l: C(S,1) = infinity for all j in S,j!= 1: C(S,j) = min{c(s-{j},i)+dij:i in S,i not in j} return min over j, C({1,...,n},j)+dj1 (x + 2y) x 0, y 600 n i=1 a ix i b n i=1 a ix i + s = b s 0 ax = b ax b and ax b let v be any vertex of the feasible region while there is a neighbor v of v with a better value: return v set v = v
O(E M) M E O( E) O(V (E) 2 ) s t s t t 2 n n! x 1 x 2 x 3 (x 1 x 2 x 3 ) (x 4 x 5 x 6 ) x 1...x 6 s t g g B A A B A B A B B A A p B A B A B B C A C
Start with some problem P0 Let S = {P_0}, the set of active subproblems Repeat while S is nonempty: choose a subproblem P S and remove it from S expand it into smaller subproblems P_1, P_2,..., P_k For each P_i: If test (P_i) succeeds: halt and announce this solution If test (P_i) fails: discard Pi Otherwise: add P_i to S Announce that there is no solution A B Start with some problem P0 Let S = {P0}, the set of active subproblems bestsofar = infinity Repeat while S is nonempty: choose a subproblem (partial solution) P in S and remove it from S expand it into smaller subproblems P_1, P_2,..., P_k For each P_i: If P_i is a complete solution: update bestsofar else if lowerbound(pi) < bestsofar: add Pi to S return bestsofar A(I) α A = max(i) OP T (I) OP T (I) α A = max(i) A(I) (logn) 80 θ(nd) def Classify(x): set i* = 1 for i - 2, 3,..., n: if x-x_i < x-x_i*, set i* = i return y_i* θ(n(d + lgk))