Brute Force O(n)
Convex Hull O(ππ )
Lexicographic Order // Dictionary
Searching an unsorted collection. Loop till index if found.
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
Input: π! π! π! β¦ π!
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
Select smallest from each loop and place it at i.
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])
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
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
1 bit 0 1
2 bit 00 01 11 10
3 bit 000 001 011 010 110 111 101 100
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.
A B C D
1 3 8 1 2
2 8 7 9 8
3 5 5 3 7
4 4 3 8 6
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
{b:1}:27
{a:2}:14
{b:2}:15 {b:1}:21
{c:1}:17
{c:2}:19
3 bit 000 001 011 010 110 111 101 100
Binary 000 001 010 011 100 101 110 111
*flip least significant 1 from Binary
Branch and Bound
Convex Hull O(ππ ) // Brute Force Compare all combination of points and check the distance. Return smallest.
Grey Code O(ππ )
Find a subset of A which sub is equal to a positive integer d. *note: Generic algo
Bubble Sort O(ππ )
!!! EXAMPLE HERE !!!
Subset Sum // Backtracking
Selection Sort O(ππ )
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])
Generate next lexicographic perm Find largest π! such that π! < Β π! + 1 Find π! such that π! > Β π! (largest j) Swap π! with π! and reverse the order π! + 1 β¦ Β π!
{a:3}:11
{a:4}:12
{b:4}:11 {b:2}:14
{b:3}:12
{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
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 = do count++ Find pos of Flip tt pos While (seq !=
000 least sig 1 in count in 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 6 5+1 4+2 3+3 3+2+1
K 6 5 2 3 2
Kβ 5 4 1 2 1
Eqn 3+1+1+1 2+2+2 2+2+1+1 2+1+1+1+1 1+1+1+1+1+1
K 3 2 2 2
Kβ 2 1 1 1
Divide and Conquer
Merge Sort O(nlog ! n)
Inversion Counting
MergeSort(A[0β¦n-1]) if(n>1)
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)
copy A[0β¦ copy B[
! !
! !
-1] to B[0β¦
β¦n-1] to C[0β¦
mergesort(B[0β¦ mergesort(C[0β¦ merge(B,C,A)
! ! ! !
! ! ! !
-1] -1]
-1]) -1])
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] !
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 copy same
! !
of P to Pl
!
!
! !
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|
T(n) = 2T( )+ f(n) !
f(n) β π(π) T(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)
of Q to Ql
copy remaining
T(n) β 2( ) + T(n) for n > i, C(1)=0 ! Tworst = nlogn β n + 1 = O(nlog ! n)
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]