Download presentation
Presentation is loading. Please wait.
1
Computer Science Elementary Data Structures, Dictionaries CS 330: Algorithms Elementary Data Structures, Dictionaries Gene Itkis
2
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis2 Primitive DS Linked lists (singly- and doubly- linked) “Stretchable”, efficient, but… Sequential access Arrays Even more efficient, but not “stretchable” Random Access
3
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis3 Simple Data Structures SStacks llllast inserted QQueues ffffirst inserted PPriority Queues hhighest priority Methods for all: isEmpty() size() insert() insert(elem) remove() which element ?
4
Computer Science Dictionaries by key Search/Retrieve by key
5
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis5 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.
6
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis6 Dictionary Interface isEmpty() size() find(key) insert() insert(elem) remove() remove(key) Dynamic
7
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis7 Implementations Simple/naïve Lists Better implementations Hash Tables Probabilistic methods, Expected values Ordered trees “Good enough” approximations; Augmenting ( order stats ) Other Skip-lists
8
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis8 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 O(lg n) Binary search: O(lg n) – pretty fast insert O(n) – slow
9
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis9 Ordered Trees Order x< y, z : min at root – heap ( ) y < x < z 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-B trees) heap x z y
10
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis10 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-Black2-3-4 Red-Black / 2-3-4 trees “Almost balanced”
11
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis11 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
12
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis12 Hash Tables: Idea Hash function H(key)=i If it works… find O(1) insert & delete O(1) Problem? Collisions: H(key’)=H(key) … … key H Hash table i key info key’
13
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis13 Hash Table: Issues Good Hash functions Minimize collision chances “Random looking” Collision Resolution Chaining Open Addressing Linear Probing: d=1 Quadratic Probing: d=i 2 Double Hashing: d=H2(key’) key info … … key H Hash table key’ key’ info key info key’ info d key” key” info d
14
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis14 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.