140 likes | 278 Views
CS 330: Algorithms Elementary Data Structures, Dictionaries. Gene Itkis. Primitive DS. Linked lists (singly- and doubly- linked) “Stretchable”, efficient, but… Sequential access Arrays Even more efficient, but not “stretchable” Random Access. Simple Data Structures. Methods for all:
E N D
CS 330: AlgorithmsElementary Data Structures,Dictionaries Gene Itkis
Primitive DS • Linked lists (singly- and doubly- linked) • “Stretchable”, efficient, but… • Sequential access • Arrays • Even more efficient, but not “stretchable” • Random Access Gene Itkis
Simple Data Structures Methodsfor all: • isEmpty() • size() • insert(elem) • remove() • which element? • Stacks • last inserted • Queues • first inserted • Priority Queues • highest priority Gene Itkis
Dictionaries Search/Retrieve by key
Dictionary Examples • Phone directory • Name is key; phone # is the info • Reverse lookup: phone # is key • Student records • Possible keys: name, ID# • Credit cards DB; PKI Certificate Revocation Lists (CRLs) • E.g. check that the given credit card / certificate is valid • Extra: “Authenticate” the result • ETC. Gene Itkis
Dictionary Interface Dynamic • isEmpty() • size() • find(key) • insert(elem) • remove(key) Gene Itkis
Implementations • Simple/naïve • Lists • Better implementations • Hash Tables • Probabilistic methods, Expected values • Ordered trees • “Good enough” approximations; Augmenting (order stats) • Other • Skip-lists Gene Itkis
Naïve: Lists • Unordered List • insert & delete • O(1) – fast • find • O(n) – slow • Ordered list • Linked-List • find & insert • O(n) – slow • Array • find • Binary search: O(lg n) – pretty fast • insert • O(n) – slow Gene Itkis
Ordered Trees x y z heap • Order • x< y, z : min at root – heap () • y < x < z : search • Depth • Shallow Balanced • There might be exceptions: • e.g., Leftist heaps • “Strong” balance • Approximate • E.g., depth of leaves within factor of 2(R-Btrees) Gene Itkis
Ordered Trees • Searching • Easy • Insert/Delete • Naïve: destroys balance • Fix balance • How? • AVL trees • Nodes keep children heights • Rotate when needed: when children heights are >1 apart • Red-Black / 2-3-4 trees • “Almost balanced” Gene Itkis
Hash tables • Example • Find professors by office number • Find tools in a tool-box • Might not work for everyone • Idea • “Figure” info location from the key Gene Itkis
Hash Tables: Idea Hash table • Hash function • H(key)=i • If it works… • find • O(1) • insert & delete • O(1) • Problem? • Collisions: • H(key’)=H(key) key’ key … H i keyinfo … Gene Itkis
Hash Table: Issues Hash table key’ key H … d d … key” • Good Hash functions • Minimize collision chances • “Random looking” • Collision Resolution • Chaining • Open Addressing • Linear Probing: d=1 • Quadratic Probing: d=i2 • Double Hashing: d=H2(key’) keyinfo keyinfo key’info key’info key”info Gene Itkis
Collision Resolution Methods Comparison • Chaining • Requires extra space • As with linked list • Stretchable • Can degenerate to linked-list search • Open Addressing • No extra space needed • Has size limit • Also can degenerate to unordered list search • Perfect Hashing Gene Itkis