Professional Documents
Culture Documents
Unordered Sequence
Dictionary ADT (§8.1.1) - Log File (§8.1.2)
The dictionary ADT models a Dictionary ADT methods: • A log file is a dictionary implemented by means of an unsorted
searchable collection of key- find(k): if the dictionary has sequence
element items an item with key k, returns • We store the items of the dictionary in a sequence (based on a doubly-
The main operations of a linked lists or a circular array), in arbitrary order
the position of this element,
dictionary are searching, else, returns a null position. • Performance:
inserting, and deleting items • insertItem takes O(1) time since we can insert the new item at the
insertItem(k, o): inserts item beginning or at the end of the sequence
Multiple items with the same (k, o) into the dictionary • find and removeElement take O(n) time since in the worst case (the item is
key are allowed removeElement(k): if the not found) we traverse the entire sequence to look for an item with the
Applications: dictionary has an item with given key
key k, removes it from the Space - can be O(n), where n is the number of elements in the dictionary
address book
•
dictionary and returns its • The log file is effective only for dictionaries of small size or for
credit card authorization
element. An error occurs if dictionaries on which insertions are the most common operations, while
mapping host names (e.g., searches and removals are rarely performed (e.g., historical record of
cs16.net) to internet addresses there is no such element. logins to a workstation)
(e.g., 128.148.34.101) size(), isEmpty()
keys(), Elements()
1
Dictionaries 2/19/08 14:15
…
The integer h(x) is called the hash value of key x Our hash table uses an
array of size N = 10,000 and 9997 ∅
9998 200-751-9998
A hash table for a given key type consists of the hash function 9999 ∅
Hash function h h(x) = last four digits of x
Array (called table) of size N
We evaluate the polynomial polynomials are hash table is usually a and b are
successively computed, chosen to be a prime
p(z) = a0 + a1 z + a2 z2 + …
each from the previous
nonnegative integers
… + an−1zn−1
at a fixed value z, ignoring one in O(1) time The reason has to do such that
overflows p0(z) = an−1 with number theory a mod N ≠ 0
Especially suitable for strings pi (z) = an−i−1 + zpi−1(z) and is beyond the ν Otherwise, every
(e.g., the choice z = 33 gives at
most 6 collisions on a set of (i = 1, 2, …, n −1) scope of this course integer would map to
50,000 English words) We have p(z) = pn−1(z) the same value b
2
Dictionaries 2/19/08 14:15
Collision Handling
(§8.2.5) Exercise: chaining
Collisions occur when 0 ∅
Assume you have a hash table H with
1 025-612-0001
different elements are 2 ∅ N=9 slots (H[0,8]) and let the hash
mapped to the same 3 ∅ function be h(k)=k mod N.
cell 4 451-229-0004 981-101-0004
Demonstrate (by picture) the insertion
Chaining: let each of the following keys into a hash table
cell in the table point Chaining is simple, with collisions resolved by chaining.
to a linked list of but requires 5, 28, 19, 15, 20, 33, 12, 17, 10
elements that map additional memory
there outside the table
3
Dictionaries 2/19/08 14:15
1 / (1 − α)
4
Dictionaries 2/19/08 14:15
Ordered Dictionaries
Proof of Universality (Part 2) (§8.3)
If f causes no collisions, only g can make h cause collisions. In an ordered Dictionary, we Ordered Dictionary ADT:
Fix a number x. Of the p integers y=f(k), different from x, the number wish to perform the usual In addition to the generic
such that g(y)=g(x) is at most p / N − 1 dictionary operations, but also dictionary ADT, the ordered
maintain an order relation for dictionary ADT supports the
Since there are p choices for x, the number of h’s that will cause a the keys in the dictionary.
collision between j and k is at most following functions:
p( p − 1) closestBefore(k): return the
p (p / N − 1) ≤ Naturally supports position of an item with the
N Look-Up Tables - store largest key less than or
There are p(p-1) functions h. So probability of collision is at most dictionary in a vector by equal to k
p( p − 1) / N 1 nondecreasing order of the keys closestAfter(k): return the
= Binary Search position of an item with the
p( p − 1) N
smallest key greater than or
equal to k
Therefore, the set of possible h functions is universal.
S1 −∞ 15 23 +∞
Analysis (§8.4.3)
S0 −∞ 10 15 23 36 +∞ Space usage
Search and update times
5
Dictionaries 2/19/08 14:15
S3 −∞ +∞ S3 −∞ +∞
S2 −∞ 31 +∞ S2 −∞ 31 +∞
S1 −∞ 23 31 34 64 +∞ S1 −∞ 23 31 34 64 +∞
S0 −∞ 12 23 26 31 34 44 56 64 78 +∞ S0 −∞ 12 23 26 31 34 44 56 64 78 +∞
Deletion Implementation
To remove an item with key x from a skip list, we proceed as We can implement a skip list
follows: with quad-nodes
We search for x in the skip list and find the positions p0, p1 , …, pi A quad-node stores:
of the items with key x, where position pj is in list Sj item
quad-node
We remove positions p0, p1 , …, pi from the lists S0, S1, … , Si link to the node before
We remove all but one list containing only the two special keys link to the node after
Example: remove key 34 link to the node below x
link to the node after
S3 −∞ +∞ Also, we define special keys
p2
PLUS_INF and MINUS_INF,
S2 −∞ 34 +∞ S2 −∞ +∞
p1 and we modify the key
S1 −∞ 23 34 +∞ S1 −∞ 23 +∞ comparator to handle them
p0
S0 −∞ 12 23 34 45 +∞ S0 −∞ 12 23 45 +∞
6
Dictionaries 2/19/08 14:15
steps, we use yet another skip list takes O(log n) expected is O(n) simple to implement in
probabilistic fact: time The expected search, practice
Fact 4: The expected number of The analysis of insertion and insertion and deletion
coin tosses required in order deletion gives similar results time is O(log n)
to get tails is 2