Вы находитесь на странице: 1из 2

Stacks, queues 513 Lists 537 Hashing 683 Analysis 147 BSTs 603, 751 Heaps 715 Games

347 Hashing Chaining array of linked lists. Linear Probing Picking + 1, +2, +3, Quadratic Probing Picking 1, 2, 4, pick relatively prime numbers Rehashing Hashing the result upon collision Insert, delete, find = O(1) Heaps Root < children. Tree complete. Insert: put into hole and percolate up. O(logN) Delete, swap min with end of array. Delete min. Percolate down. O(logN) Build. Percolate down all elements in n-1st row. Then n-2nd, etc. O(N) HeapSort. BuildHeap, then removeMin for all elements. O(NlogN) No memory. Left Child = 2*parent. Right Child = 2*parent + 1 Priority: FIFO for the values inserted (priorities). Implementation is a heap. Stacks, Queues All operations O(N). Stacks: LIFO. Push, Pop. Queues: FIFO. Enqueue, dequeue. Lists Need mutability for IntList L = new NilList(); L.append(123); wrapper class Head: O(1). Tail: O(1). Append: O(N) Reverse: O(N^2) (bad algorithm) Public class EmptyList implements IntList { Public IntList reverse() { return this; }} Public class NonfullList implements IntList { public IntList reverse() { IntList t = next.reverse(); return t.append(element); } BSTs Insert, Find: worst case: N, amortized: logN. Remove: Go to node, that node = findMin(t.right), that node.right = removeMin(t.right). AVL tree: Balanced tree. Balances on insertion. Everything logN Single rotation: Double rotation: 628 Splay Trees: Splays up when accessed. Best with 90-10 situation 90% accesses to 10% data. See 756 for zig, zig-zag, zig-zig cases
Specifier private protected public class X X X X* X X X X subclass package world

Analysis T(N) = O(F(N)): growth T(N) <= growth F(N) T(N) = (F(N)): growth T(N) >= growth F(N) T(N) = (F(N)): growth T(N) = growth F(N) public class Asdf { public static void main() {}} public class Block extends Statement {} public abstract class Statement { } Compression Huffman: get freq count. Build tree (full). Get code table. Compress. | get table. Build tree. Decompress LZW: s = empty. While data(read ch. If dict. contains s+ch, s=s+ch. Else output s, add s+ch to dict, s = ch) prev = read. While data (curr = read. Translate curr. Output trans. Ch = first char of trans. Add trans (prev)+ch to dict. prev = curr.) SVD A = USVt store columns of U, V, scalar S. A = U1S1Vt1 + 2S2Vt2 + Sorting Insertion: O(I+N-1) Shellsort (incremented insertion) O(n^(7/6)) Merge: O(nlogn) Quick O(nlogn) T(n) = aT(N/c) + bN | T(N) = O(n) if a<c. T(N) = O(nlogn) if a=c. T(N) = N^log_c(a) if a > c FSM: M = (, S, q0, F, ) = (alphabet, state set, initial state, final states, transition function) Language(M) = {w | (q0, w) isin F} (q, ) = q. (q, aw) = ((q, a), w) NFA (vs DFA). Can be in more than one state at once NFA. means transition sans input + = 1 or more * = 0 or more | = or Graphs Dijkstra (472-)

graph = G(V, E) V=vertices. E=edges DAG: directed acyclic graph Tree iff has n-1 edges. Sparse if edges < O(n). else dense. Bipartite exists if partition V into A and B such that all edges go from set A to set B. Adjacency representation: linked list represents adj(v) Matrix rep: array in which arr[i][j] = 1 signifies edge from i to j. DFS, Topological Sort (reverse DFS), Check Cyclic, Check bipartite, all O(|E| + |V|) MST Kruskal (808): For each (u,v) in increasing order, is v and u disjoint (V, U). add edge to MST, merge U, V (F=F-U-V+(UUnV). Kruskal = Prim = O(|E|log|E|) = O(|E|log|V|) TSP(Traveling salesperson): shortest route that starts somewhere visits all vertices and returns to orig spot. Its NP-complete (prob not solvable in poly time). However, approx with: MST, printouts on DFS Dynamic Programming Greedy: making optimally local decisions in hopes for optimal solution. Dynamic: Solve all smaller subproblems in hope for substantial overlap between sub problems Game Trees (347) Minimax: O(b^d) (avg b moves per level, d levels) AB prune: O(b^(d/2)) AB (538) If (depth == 0) return eval. If (myTurn) { for each move i && < (alpha = max(AB(b.move(i), !myTurn, depth-1, ,) return alpha } else do same except beta = min and return beta. Union Find (817) Simple Forest: find O(N) mixed O(MN) Avg tricky. By Height: find O(logN) mixed O(MlogN) avg O(M). Path compression: mixed: nearly linear Forest: Each own tree. Array Rep: s[x] is the parent. FIND: If (s[x] < 0) return x else return find(s[x]) UNION s[find(x)] = y Height: UNION s[find(x)] = find(y) Path Compress: find flattens trees: FIND if (s[x] < 0) return x else return s[x] = find(s[x]) String Matching Boyer Moore: Starts from end of P. if equal, go back until found. If not, jump distance to end of P Works well for large alphabets. Works well in practice. But i must be able to be decremented O(M+N) KMP: compute in advance how far to jump in P when a match fails For text, similar to brute force (can be worse) works well for patterns, never decrements I, like FSM Osame f=2 means P[01] = P[ j-2j-1] n=T.len m=P.len f=computeF(P) i=j=0; while(i<n) {if(p[j]==T[i]) {if (j==m-1) return i-m+1 i++ j++} else if (j>0) j=f[j-1] else i++ } return -1 Randomness: linear congruential generators. (324) x_{i+1} = (A*x_i)modM, M=2^31-1, A=48217, x_0=1 x_{i+1} = A(x_imodQ) R*floor(x_i/Q)+M*d(x_i), R=MmodA, Q=M/A, d(x_i) = 0 if first two terms > 0, else 1 another one: x_{i+1} = (Ax_i + C)mod2^B, A=25214917 B=48 C=13 problem: low order bits not as random (java uses 48 bits and takes 32) properties random seq: first number is equally likely 0N. The ith number is equally likely 0N, given numbers 0i-1. expected avg is N/2

DFS
for all v in V do visited(v) = 0 for all v in V do if (visited(v)==0) DFS(v) DFS(v) { visited(v) = 1 for all w in adj(v) do if (visited(w)==0) DFS(w) }

Topological Sorting
for all v in V do visited(v) = 0 for all v in V do if (visited(v)==0) DFS(v) DFS(v) { visited(v) = 1 for all w in adj(v) do if (visited(w)==0) DFS(w) print v }

Finding Cycles
for all v in V do visited(v) = 0 for all v in V do mark(v) = 0 for all v in V do if (visited(v)==0) DFS(v) DFS(v) { mark(v) = 1 visited(v) = 1 for all w in adj(v) do if (visited(w)==0) DFS(w) else if (mark(w)==1) print "found a cycle" mark(v) = 0 }

Determining Bipartiteness
for all v in V do visited(v) = 0 for all v in V do if (visited(v)==0) DFS(v,0) DFS(v,p) { visited(v) = 1 part(v) = p for all w in adj(v) do if (visited(w)==0) { DFS(w,1-p) } else {
if (part(v) == part(w)) print "graph not bipartite"

Connected Components
for all v in V do visited(v) = 0 c = 0 (a global variable) for all v in V do if (visited(v)==0) { DFS(v) c = c+1 } DFS(v) { visited(v) = 1 comp(v) = c for all w in adj(v) do if (visited(w)==0) DFS(w) }

} }

C, lognlogn, logn, (logn)^k, n, nlogn, n^2, n^k, 2^n, n!, n^na

Вам также может понравиться