computer science elementary data structures, dictionaries cs 330: algorithms elementary data...
Post on 21-Dec-2015
246 views
TRANSCRIPT
Computer Science
CS 330: AlgorithmsElementary Data Elementary Data
Structures,Structures,DictionariesDictionaries
Gene Itkis
Computer Science
CS-330: Algorithms, Fall 2004 Gene Itkis 2
Primitive DS
Linked lists (singly- and doubly- linked) “Stretchable”, efficient, but… Sequential access
Arrays Even more efficient, but not
“stretchable” Random Access
Computer Science
CS-330: Algorithms, Fall 2004 Gene Itkis 3
Simple Data Structures
Stacks
lastlast inserted
Queues
firstfirst inserted
Priority Queues
highest priority
MethodsMethodsfor all:for all:
isEmpty()isEmpty() size()size()
insert(insert(elem))
remove()remove() which element?
Computer Science
CS-330: Algorithms, Fall 2004 Gene Itkis 5
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.
Computer Science
CS-330: Algorithms, Fall 2004 Gene Itkis 6
Dictionary Interface
isEmpty()
size()
findfind(key)(key)
insert(insert(elem))
remove(remove(key))
Dynamic
Computer Science
CS-330: Algorithms, Fall 2004 Gene Itkis 7
Implementations
Simple/naïve
ListsLists
Better implementations
Hash TablesHash Tables
Probabilistic methods, Expected values
Ordered treesOrdered trees
“Good enough” approximations; Augmenting (order stats)
Other
Skip-lists
Computer Science
CS-330: Algorithms, Fall 2004 Gene Itkis 8
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)O(lg n) – pretty fast
insert O(n) – slow
Computer Science
CS-330: Algorithms, Fall 2004 Gene Itkis 9
Ordered Trees
Order x< y, z : min at root – heap () y < x < zy < 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
zy
Computer Science
CS-330: Algorithms, Fall 2004 Gene Itkis 10
Ordered Trees
Searching Easy
Insert/Delete Naïve: destroys balance Fix balance
How? AVL trees
Nodes keep children heightsRotate when needed: when children heights are >1
apart
RedRed--BlackBlack / 2-3-42-3-4 trees
“Almost balanced”
Computer Science
CS-330: Algorithms, Fall 2004 Gene Itkis 11
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
Computer Science
CS-330: Algorithms, Fall 2004 Gene Itkis 12
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
Hashtable
ikeyinfo
key’
Computer Science
CS-330: Algorithms, Fall 2004 Gene Itkis 13
Hash Table: Issues
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
…
…
key
H
Hashtable key’
key’info
keyinfo
key’info
d
key”
key”info
d
Computer Science
CS-330: Algorithms, Fall 2004 Gene Itkis 14
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