Download - 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
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]