algorithm analysis cheat sheet

2
Brute Force O(n) Searching an unsorted collection. Loop till index if found. BruteForceSearch (set A, item a) for i:=0 to n–1 do if(A[i]==a) return i Insertion Sort O( ) Each loop, we find next element’s pos in the left sorted array and insert it. InsertionSort (A[0…n-1]) for i:=1 to n–1 do v <- A[i], j <- i-1 while(j>=0 and A[j]>V) A[j+1] <- A[j] J <- j-1 A[j+1] <- V Selection Sort O( ) Select smallest from each loop and place it at i. SelectionSort (A[0…n-1]) for i:=0 to n–2 do min <- i for j:=i+1 to n-2 do if(A[j] < A[min]) min = j Swap(A[i], A[min]) Bubble Sort O( ) For each loop, bubble the largest element to its largest pos. (n-2-i) BubbleSort (A[0…n-1]) for i:=0 to n–2 do for j:=0 to n-2–i do if A[j]>A[j+1] then Swap A[j] with A[j+1] Snail Sort O(n!) Randomly permutes all numbers till array is sorted. SnailSort (set A) done <- 0 n <- length(A) while done==0 do p <- ranPerm(n) A = A(p) if(IsSortet(A)) return A done = 1 Convex Hull O( ) // Brute Force Compare all combination of points and check the distance. Return smallest. ClosestPair (P) d <- for i:=1 to n-1 do for j:=i+1 to n do d <- min(d, dist(P[i], P[j])) return d Convex Hull O( ) // Brute Force Dumb For each lowest loop, find if combination of point form outer edge of convex hull. ConvexHull (P, N) s <- 0 for i:=0 to n-2 do for j:=i+1 to n-1 do sameSide <- true for k:=0 to n-1 do sameSide <- sameSide && Test[P(k)] on the same side as other points if(sameSide) add P(i), P(j) to S return S Convex Hull O( ) ConvexHull (P) N <- P.length, K <- 0, H <- (2*N) // lower hull for i:=0 to n do while (K>=2 && cross(H(K-2), H[K-1],P[i] <= 0)) K-- H[K++] = P[i] // upper hull for i:=n-2 to 0 and T = K+1 do //-i while (K>=T && cross(H(K-2), H[K-1],P[i] <= 0)) K-- H[K++] = P[i] H.resize(K) return H Subset Sum // Backtracking Find a subset of A which sub is equal to a positive integer d. *note: Generic algo Backtrack (A[1…i]) if A[1…i] is solution then write A[1…i] else for each element x !!! consistent with A[1…i] and the constrain A[i+1] <- x Backtrack(A[1…i+1]) Branch and Bound Intuition: No need to search deeper into the tree if we know that node in subtree != soln Job assignment (worker/job) problem: Find unique assignment with minimum cost. 1. Find lower bound for each node, ignoring constrains 2. Only expand a node when “worth expanding” 3. Stop when there is a soln better than all other solns {}:9 // 3 + 3 + 1 + 2 {a:1}:15 {a:2}:14 {a:3}:11 {a:4}:12 {b:1}:27 {b:2}:15 {b:4}:11 {b:1}:21 {b:2}:14 {b:3}:12 {c:1}:17 {c:2}:19 {c:1}:18 {c:2}:20 Permutations and Combinations Johnson Trotter’s Algo Want: Given n items, generate all possible permutations of n items. Approach: Number items from 1 to n, deal with items via numbers. Property: Generate perm s.t. consecutive perms only differ by 1 swap (minimal swap). 123 -> 312 A number is mobile if its arrow is pointing towards a smaller number. JohnsonTrotter () while mobile element find largest mobile element K swap(K, element its pointing to) reverse all arrows larger than K 1 2 3 1 3 2 3 1 2 3 1 2 2 3 1 2 1 3 Lexicographic Order // Dictionary Input: ! ! ! ! Generate next lexicographic perm Find largest ! such that ! < ! + 1 Find ! such that ! > ! (largest j) Swap ! with ! and reverse the order ! + 1 ! !!! EXAMPLE HERE !!! Grey Code O( ) 1 bit 2 bit 3 bit 3 bit Binary 0 00 000 000 000 1 01 001 001 001 11 011 011 010 10 010 010 011 110 110 100 111 111 101 101 101 110 100 100 111 *flip least significant 1 from Binary Recursive version is more exp in terms of space. IterativeGC () for i:=0 to 2 ! // set size grey = ! (I >> 1) OR count <- 0 Initial Seq = 000 do count++ Find pos of least sig 1 in count Flip tt pos in seq While (seq != 000) RecursiveGC(N) if(N==1) return {0, 1} L1 <- RecursiveGC(N-1) L2 <- Reverse(L1) Insert 0 infront of all elements L1 Insert 1 infront of all elements L2 return L1 + L2 Integer Partitioning Finding all ways to represent an integer as a sum of positive integers. Eg. 3 = 2 + 1 = 1 + 1 + 1 IntPartitioning () // terminate if not found Find smallest part K > 1. Let K’ = K-1. Rewrite 1K as K’ + 1 Collect all 1s into parts !> K’ Eg. Eqn K K’ Eqn K K’ 6 6 5 3+1+1+1 3 2 5+1 5 4 2+2+2 2 1 4+2 2 1 2+2+1+1 2 1 3+3 3 2 2+1+1+1+1 2 1 3+2+1 2 1 1+1+1+1+1+1 1 2 3 4 A 3 8 5 4 B 8 7 5 3 C 1 9 3 8 D 2 8 7 6

Upload: eplusie

Post on 28-Oct-2015

49 views

Category:

Documents


1 download

DESCRIPTION

This cheat sheet is created and typed by yours truly. I am aware the difficulties of creating it and I hope this will help. :)Includes

TRANSCRIPT

Page 1: Algorithm Analysis Cheat Sheet

Brute Force O(n) Searching an unsorted collection. Loop till index if found. BruteForceSearch (set A, item a) for i:=0 to n–1 do if(A[i]==a) return i Insertion Sort O(𝒏𝟐) Each loop, we find next element’s pos in the left sorted array and insert it. InsertionSort (A[0…n-1]) for i:=1 to n–1 do v <- A[i], j <- i-1 while(j>=0 and A[j]>V) A[j+1] <- A[j] J <- j-1 A[j+1] <- V Selection Sort O(𝒏𝟐) Select smallest from each loop and place it at i. SelectionSort (A[0…n-1]) for i:=0 to n–2 do min <- i for j:=i+1 to n-2 do if(A[j] < A[min]) min = j Swap(A[i], A[min]) Bubble Sort O(𝒏𝟐) For each loop, bubble the largest element to its largest pos. (n-2-i) BubbleSort (A[0…n-1]) for i:=0 to n–2 do for j:=0 to n-2–i do if A[j]>A[j+1] then Swap A[j] with A[j+1] Snail Sort O(n!) Randomly permutes all numbers till array is sorted. SnailSort (set A) done <- 0 n <- length(A) while done==0 do p <- ranPerm(n) A = A(p) if(IsSortet(A)) return A done = 1 Convex Hull O(𝒏𝟐) // Brute Force Compare all combination of points and check the distance. Return smallest. ClosestPair (P) d <- ∞ for i:=1 to n-1 do for j:=i+1 to n do d <- min(d, dist(P[i], P[j])) return d Convex Hull O(𝒏𝟑) // Brute Force Dumb For each lowest loop, find if combination of point form outer edge of convex hull. ConvexHull (P, N) s <- 0 for i:=0 to n-2 do for j:=i+1 to n-1 do sameSide <- true for k:=0 to n-1 do sameSide <- sameSide && Test[P(k)] on the same side as other points if(sameSide) add P(i), P(j) to S return S

Convex Hull O(𝒏𝟐) ConvexHull (P) N <- P.length, K <- 0, H <- (2*N) // lower hull for i:=0 to n do while (K>=2 && cross(H(K-2), H[K-1],P[i] <= 0)) K-- H[K++] = P[i] // upper hull for i:=n-2 to 0 and T = K+1 do //-i while (K>=T && cross(H(K-2), H[K-1],P[i] <= 0)) K-- H[K++] = P[i] H.resize(K) return H Subset Sum // Backtracking Find a subset of A which sub is equal to a positive integer d. *note: Generic algo Backtrack (A[1…i]) if A[1…i] is solution then write A[1…i] else for each element x ∈ 𝑆!!! consistent with A[1…i] and the constrain A[i+1] <- x Backtrack(A[1…i+1]) Branch and Bound Intuition: No need to search deeper into the tree if we know that node in subtree != soln Job assignment (worker/job) problem: Find unique assignment with minimum cost.

1. Find lower bound for each node, ignoring constrains 2. Only expand a node when “worth

expanding” 3. Stop when there is a soln better than all other solns

{}:9 // 3 + 3 + 1 + 2

{a:1}:15 {a:2}:14 {a:3}:11 {a:4}:12 {b:1}:27 {b:2}:15 {b:4}:11

{b:1}:21 {b:2}:14 {b:3}:12 {c:1}:17 {c:2}:19 {c:1}:18 {c:2}:20 Permutations and Combinations Johnson Trotter’s Algo Want: Given n items, generate all possible permutations of n items. Approach: Number items from 1 to n, deal with items via numbers. Property: Generate perm s.t. consecutive perms only differ by 1 swap (minimal swap). 123 -> 312 A number is mobile if its arrow is pointing towards a smaller number. JohnsonTrotter () while ∃ mobile element find largest mobile element K swap(K, element its pointing to) reverse all arrows larger than K

 1  2  3 →    1  3  2 →    3  1  2 →   3  1  2  →    2  3  1 →  2  1  3

Lexicographic Order // Dictionary Input: 𝑎!𝑎!𝑎! … 𝑎! Generate next lexicographic perm Find largest 𝑎! such that 𝑎! <  𝑎! + 1 Find 𝑎! such that 𝑎! >  𝑎! (largest j) Swap 𝑎! with 𝑎! and reverse the order 𝑎! + 1…  𝑎! !!! EXAMPLE HERE !!! Grey Code O(𝟐𝒏) 1 bit 2 bit 3 bit 3 bit Binary

0 00 000 000 000 1 01 001 001 001 11 011 011 010 10 010 010 011 110 110 100 111 111 101 101 101 110 100 100 111

*flip least significant 1 from Binary Recursive version is more exp in terms of space. IterativeGC () for i:=0 to 2 ! // set size grey = 𝑖! (I >> 1) OR count <- 0 Initial Seq = 000 do count++ Find pos of least sig 1 in count Flip tt pos in seq While (seq != 000) RecursiveGC(N) if(N==1) return {0, 1} L1 <- RecursiveGC(N-1) L2 <- Reverse(L1) Insert 0 infront of all elements L1 Insert 1 infront of all elements L2 return L1 + L2 Integer Partitioning Finding all ways to represent an integer as a sum of positive integers. Eg. 3 = 2 + 1 = 1 + 1 + 1 IntPartitioning () // terminate if not found Find smallest part K > 1. Let K’ = K-1. Rewrite 1K as K’ + 1 Collect all 1s into parts !> K’ Eg. Eqn K K’ Eqn K K’ 6 6 5 3+1+1+1 3 2 5+1 5 4 2+2+2 2 1 4+2 2 1 2+2+1+1 2 1 3+3 3 2 2+1+1+1+1 2 1 3+2+1 2 1 1+1+1+1+1+1

1 2 3 4 A 3 8 5 4 B 8 7 5 3 C 1 9 3 8 D 2 8 7 6

Page 2: Algorithm Analysis Cheat Sheet

Divide and Conquer Inversion Counting To find how close preferences are. X: BCDAE, Y: DBCEA Let X be “sorted”. BCDAE : 12345 12345 DBCEA : 31254 31254 3 inversions. Def: inversion is when a seq is 0,1,2,n. yet there exist i < j, but 𝑎! >  𝑎!. SortNCount (L) if |L| == 1 return count = 0 and L Split L into L1, L2 Do SortNCount(L2) n obtain SL1 and count 1 Do SortNCount(L1) n obtain SL2 and count 2 Call MergeNCount(L1, L2) n obtain SL3, count3 Return sorted list SL and (count1 + count2 + count3) MergeNCount(A,B) Let A = 𝑎!, 𝑎!, 𝑎! … 𝑎! Let B = 𝑏!, 𝑏!, 𝑏! … 𝑏! minSize = min(n,m) maintain output list 0 n current index j = 0 pt Ap = 0 for A, Bp = 0 for B while (Ap!=M || Ap!=N) if Ap!=N && Bp!=M if Aap < Abp then Oj = Aap Ap++ Else Oj = Bbp ++Bp ++count j++ while (Ap!=N) Oj = Aap j++, Ap++ while (Bp!=M) Oj = Bbp j++, Bp++ return 0 as output Closest Pair ClosestPair(P,Q) if(|P| <= 3) return brute force minimal dist else

copy first !! of P to Pl

copy same !! of Q to Ql

copy remaining !! from P to Pr

copy same !! from Q to Qr

d1 <- ClosestPair(Pl,Ql) d2 <- ClosestPair(Pr,Qr)

d <- min(d1, d2), m <= P[!!− 1].x

copy all pts of Q for which |x-m|<d into arr S[0…num-1] dminsq <= 𝑑! for i:=0 to num-2 fo k <- i + j while K<=num-1 and (S[K]. y  –  S[i]. y)! < dminsq dminsq <- min(dminsq, dist(S[K], S[i])) K <- K + 1 Return sqrt(dminsq) T(n) = 2T(

!!)+ f(n)

f(n) ∈ 𝜃(𝑛) T(n) ∈  𝜃  (𝑛𝑙𝑜𝑔𝑛)

Merge Sort O(nlog!n) MergeSort(A[0…n-1]) if(n>1)

copy A[0…!!-1] to B[0…

!!-1]

copy B[!!…n-1] to C[0…

!!-1]

mergesort(B[0…!!-1])

mergesort(C[0…!!-1])

merge(B,C,A) Merge(B[0…p-1], C[0…q-1], A[0…p+q-1]) i <- 0, j <- 0, k <- 0 while i < p and j < q if B[i] <= C[j] A[k] <- B[i] i <- i + 1 else A[k] <- C[j] j <- j + 1 k <- k + 1 if (i==p) copy C[j…q-1] to A[k…p+q-1] else copy B[i…p-1] to A[k…p+q-1] T(n) – 2(

!!) + T(n) for n > i, C(1)=0

Tworst = nlogn – n + 1 = O(nlog!n) Quick Sort O(log n) QuickSort(A[l…r]) if(l>r) S <- Partition(A[l…r]) QuickSort(A[l…S-1]) QuickSort(A[S+1…r]) Partition(A[l…r]) P <- A[l], i <- l, j <- r + 1 repeat repeat i <- i + 1 until A[i] >= P repeat j <- j – 1 until A[j] <= P until i >= j // undo last swap when i >= j Swap(A[i], A[j]) Swap(A[i], A[j]) return j Tbest(n) = nlog!n Tworst(n) = 𝜃(𝑛!) Tavg(n) = 2nlnn ≈ 1.39 nlog!n Bubble Sort O(𝒏𝟐) For each loop, bubble the largest element to its largest pos. (n-2-i) BubbleSort (A[0…n-1]) for i:=0 to n–2 do for j:=0 to n-2–i do if A[j]>A[j+1] then Swap A[j] with A[j+1]