|
COT 5405 Advanced Algorithms Chris Lacher Notes 7: UUSet |
Hashing
- Review: Hash Functions
- Review: Hash Tables
UUSet
- HashSet Structure
- T = type of elements in Set
- n = number of elements in set = size of Set
- H = hash function for T (may be for defined in terms of a key type K)
- B = Bucket type (a container holding elements of type type T)
- v = Vector of B<T> (aka "bucket vector")
- b = number of buckets = size of bucket vector
- HashSearch Algorithm: given t = search value of type T
- Calculate hashValue = H(t)
- Calculate index = hashValue % b
- Search v[index] for t
- Return result of search as (SizeType, B::Iterator)
- Set::Includes(t):
- (i,j) = HashSearch(t)
- if (j.Valid() && t == *j) t = *j
- (alternate) return SetIterator pointing to (i,j)
- Set::Insert(t):
- (i,j) = HashSearch(t)
- if (!j.Valid() || t != *j) v[i].Insert(j,t)
- else *j = t;
- Set::Remove(t):
- (i,j) = HashSearch(t)
- v[i].Remove(j)
For each operation: Expected (Average Case) Runtime = Expected runtime of HashSearch(t) = Θ(average search time for bucket) Assuming B = List and Sequential Search: Expected Runtime = Θ(average size of v[i]) = Θ(size of set / number of buckets) = Θ(s/b) Structural Foot Print = Θ(s + b) Runspace = + Θ(1) Assuming Θ(s) = Θ(b): Expected Runtime = Θ(1) Foot Print = Θ(s) Runspace = + Θ(1)
UUSet::Iterator
- Data
- Interface: Bidirectional Iterator
- Set::Begin()
- Iterator::++()
- Operations have amortized runtime = Θ(b/(s+1))