Sunteți pe pagina 1din 2

Brute Force O(n) Searching an unsorted collection. Loop till index if found.

BruteForceSearch (set A, item a) for i:=0 to n1 do if(A[i]==a) return i Insertion Sort O( ) Each loop, we find next elements pos in the left sorted array and insert it. InsertionSort (A[0n-1]) for i:=1 to n1 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[0n-1]) for i:=0 to n2 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[0n-1]) for i:=0 to n2 do for j:=0 to n-2i 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[1i]) if A[1i] is solution then write A[1i] else for each element x ! !! consistent with A[1i] and the constrain A[i+1] <- x Backtrack(A[1i+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 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

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 0 1 2 bit 00 01 11 10 3 bit 000 001 011 010 110 111 101 100 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 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)

A B C D

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

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:1}:21

{b:4}:11 {b:2}:14 {b:3}:12

{c:1}:17

{c:2}:19

{c:1}:18

{c:2}:20

Permutations and Combinations Johnson Trotters 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

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 copy same
! ! ! ! !

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


! ! ! !

-1] to B[0
! ! ! !

! ! ! !

-1] -1]

n-1] to C[0 -1]) -1])

mergesort(B[0 mergesort(C[0 merge(B,C,A)

Merge(B[0p-1], C[0q-1], A[0p+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[jq-1] to A[kp+q-1] else copy B[ip-1] to A[kp+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[lr]) if(l>r) S <- Partition(A[lr]) QuickSort(A[lS-1]) QuickSort(A[S+1r]) Partition(A[lr]) 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[0n-1]) for i:=0 to n2 do for j:=0 to n-2i do if A[j]>A[j+1] then Swap A[j] with A[j+1]
!

of P to Pl 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[0num-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) ()

S-ar putea să vă placă și