Cheat Sheet (Review) for 600.226 Abbreviations: ra = array sll = singly linked list dll = doubly linked list ref = reference seq = sequence Ch = chapter BST = binary search tree BBST = balanced binary search tree Note that much of the first part is written in loosely tabular form: Stack --------------------------------------------------------------- array singly linked list push O(1) [size-1] O(1) head pop O(1) [size-1] O(1) head top O(1) [size-1] O(1) head Queue --------------------------------------------------------------- circular array singly linked list enqueue O(1) [right] O(1) tail (keep tail ref) dequeue O(1) [left] O(1) head front O(1) [left] O(1) head Deque --------------------------------------------------------------- circular array doubly linked list addFront O(1) [left] O(1) head addBack O(1) [right] O(1) tail deleteFront O(1) [left] O(1) head deleteBack O(1) [right] O(1) tail front O(1) [left] O(1) head back O(1) [right] O(1) tail Collection (Set,Map,Dictionary) ------------------------------------- unordered ra ordered ra unordered sll ordered sll find O(N) O(log N) O(N) O(N) add O(1) O(N) O(1) O(N) delete O(N) O(N) O(N) O(N) BST BBST (AVL, Red-Black) find (impacts others) O(h) O(log N) add O(h) O(log N) delete O(h) O(log N) Splay Tree: amortized O(log N) for all ops (Ch 11 also) Hash Table: O(N) worst case, O(1) average case for all ops, assuming constant time hash function computation (not always covered) Skip List: O(log N) expected time for all operations, O(N) worst case Treap: O(log N) expected time for all operations, O(N) worst case Priority Queue ------------------------------------------------------ unord seq ord seq binary heap min O(N) O(1) O(1) insert O(1) O(N) O(log N) removeMin O(N) O(1) O(log N) build O(N) O(N log N) O(N) Adaptable Priority Queue - need location aware lookup for O(log n) ops Sorting -------------------------------------------------------------- bubble - O(N^2) selection - O(N^2) insertion - O(N^2) heap - O(N log N) merge - O(N log N) quick - O(N^2 worst, N log N best & expected) lower bound for comparison based sorting - O(N log N) external merge variation (internal mem holds M) - O(N (log (N/M) + log M)) reduces to O(N log N) bucket - values in range 0..M-1 - O(M+N) time & space radix - d-tuples of values in range - O(d(M+N)) Selection Problem - find kth smallest -------------------------------- full heap: put all values in, do k removeMin = O(N + k log N) partial heap: put first k values in, for other values put in if smaller = O(k + (N-k) log N)) == O(N log k) (not always covered) quick select randomized: O(N^2) worst case, O(N) expected quick select Medians of Medians: O(N) worst case (Ch 10) Graphs - V vertices, E edges ----------------------------------------- Edge list Adjacency Matrix Adjacency List space O(E) O(V^2) O(E + V) adjacent(u,v) O(E) O(1) O(degree(u)) depth first traversal O(V^2) O(E) (recursion = stack) breadth first traversal O(V^2) O(E) (queue) - use for shortest unweighted (min hops) paths too Single Source Min Weight Directed Path - Dijkstra's - adjacency list for graph rep O(Dv) to get edges of v - unsorted sequence priority queue (good if dense graph) - O(V) to extract min (* V total) - O(1) updates (* E total) - O(V^2 + E) total = O(V^2) - adaptable priority queue with heap - O(log V) to remove min (* V total) - need to update O(degree(v)) distances (E total) - O(log V) per update with location aware - O((V+E)log V) time = O(E log V) - PQ where you insert nb vertex (copy) with each distance improvement - size of PQ could be O(E) worst case (* log E each insert) - log E remove min = O(log V) since E < V^2 (* V of them) - good when graph is sparse (bad for space otherwise) - O((V+E)log V) time = O(E log V) Min Cost Spanning Tree Kruskal's - O(E log V) Prim's - O(V^2 or E log V) (basically choose a Dijkstra variation) All pairs shortest paths (routing table) [ Dynamic programming - O(V^3) ] Set - ordered collection ------------------------------------------------ union, intersect, difference - O(N) merge variations Partitions (Equivalence Problem) ---------------------------------------- makeSet (singleton) is O(1) Sequence implementation: each position stores element & reference to it's containing set name union - O(N) worst, O(log N) amortized if union by size find - O(1) (just a lookup operation) Forest of trees implentation: value at root of tree is set name M union/find ops are O(M log* N) amortized cost (log* N per op) Text Compression & Searching -------------------------------------------- let C be size of character set let N be size of text to encode fixed-length encodings - log C bits per character variable-length encodings - based on character frequencies - "prefix code" means no code is prefix of another one - Huffman Code O(N) to read file and compute character frequences O(C log C) to create optimal code Tries - trees for storing strings (text to be searched) - goal is fast word searching - can build trie in O(CN) - search for a pattern w/M characters takes O(CM) - compressed version is Patricia Trie