kjh

Download kjh

If you can't read please download the document

Upload: tony-jiang

Post on 11-Nov-2015

212 views

Category:

Documents


0 download

DESCRIPTION

k

TRANSCRIPT

Cheat Sheet (Review) for 600.226Abbreviations: ra = arraysll = singly linked listdll = doubly linked listref = referenceseq = sequenceCh = chapterBST = binary search treeBBST = balanced binary search treeNote that much of the first part is written in loosely tabular form:Stack --------------------------------------------------------------- array singly linked listpush O(1) [size-1] O(1) headpop O(1) [size-1] O(1) headtop O(1) [size-1] O(1) headQueue --------------------------------------------------------------- circular array singly linked listenqueue O(1) [right] O(1) tail (keep tail ref)dequeue O(1) [left] O(1) headfront O(1) [left] O(1) headDeque --------------------------------------------------------------- circular array doubly linked listaddFront O(1) [left] O(1) head addBack O(1) [right] O(1) taildeleteFront O(1) [left] O(1) headdeleteBack O(1) [right] O(1) tailfront O(1) [left] O(1) headback O(1) [right] O(1) tailCollection (Set,Map,Dictionary) ------------------------------------- unordered ra ordered ra unordered sll ordered sllfind 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 casePriority Queue ------------------------------------------------------ unord seq ord seq binary heapmin 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) opsSorting --------------------------------------------------------------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 & spaceradix - 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) expectedquick select Medians of Medians: O(N) worst case (Ch 10)Graphs - V vertices, E edges ----------------------------------------- Edge list Adjacency Matrix Adjacency Listspace 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 tooSingle 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 variationsPartitions (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 setlet N be size of text to encodefixed-length encodings - log C bits per charactervariable-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