















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















(Chapters 10-12)
Acknowledgement: These notes are compiled by Nancy Amato at Texas A&M University. Parts of these course notes are based on notes from courses given by Jennifer Welch at Texas A&M University.
Data Structures for (Dynamic) Sets
Algorithms operate on data, which can be thought of as forming a set S. Unlike mathematical sets, the (data) sets manipulated by algorithms are dynamic – they can grow, shrink, or otherwise change over time.
Data Structures are structured ways to represent finite dynamic sets. Dif- ferent data structures support different kinds of data manipulations, e.g.,
Operations on Dynamic Sets:
Running Time: usually measure time of an operation in terms of the number of elements (currently) in the set.
Binary Search Trees
Binary Search Trees should also be review from CPSC 211 (Chapt 12). Recall, every tree node (internal or leaf) contains a key.
Binary Search Tree Property: For every node x in tree
(Note there is no condition on the height h of tree)
Operations on BSTs (supports all dynamic data set ops)
Running Time: All operations take O(h) time
Traversing (visiting) BSTs
Binary Search Tree Operations
Tree-Search(x, k) if (x = N IL) or (k = key[x]) return x if (k < key[x]) then return Tree-Search(lef t[x], k) else return Tree-Search(right[x], k)
Tree-Successor(x, k) if right[x] 6 = N IL then return Tree-Minimum(right[x]) temp := parent[x] while (temp 6 = N IL and x = right[temp]) x := temp temp := parent[temp] return temp
Note:
Hash Tables
Dictionary operations
Definition: U = Universe from which keys are drawn (often N , natural numbers) K ⊆ U set of keys seen
Goals:
Approach 1: Linked Lists
Linked List Implementation
Running Times: (assume n elements in list)
This is pretty bad.... we’d like O(1) time on average for all operations....
Space Usage: (assume n elements in list)
This is great!
Approach 3: Hashing
Hashing
Problem: there will be some collisions, that is, h will map some keys to the same position in H (e.g., h(k 1 ) = h(k 2 ) for k 1 6 = k 2 )
Different Methods of Resolving Collisions
Later we’ll consider how to pick good hash functions h. Assume for now our hash function h satisfies:
Simple Uniform Hashing Assumption: Any key is equally likely to hash to any location (index,slot) in hash table H.
Collision Resolution by Chaining
Chaining: put all keys that hash to the same location in a linked list
0 1 i m-
y
x h(x) = h(y) = i
H:
Note: even though worst-case behavior is Θ(n), the idea of hashing is to achieve good average-case behavior. Usually, we can get average-case time of operations down to Θ(1).
Average-Case Analysis for search(x) with Chaining
Case 2: average search(x) time when x is in table
Note: this is easier to analyze if we assume elements are added at end of lists (can prove average successful search time is the same regardless).
Fact: if x is the ith element added to H, then the expected length of H[h(x)]’s list before adding x is i− m^1
∑n i=
Pr(x is ith elt added) · (length of H[h(x)] + 1)
= Θ(1) + ∑n i=
n
i^ −^1 m
∑n i=
i − 1 nm
∑n i=
n = Θ(1) +
nm
∑^ n i=
(i − 1) +
n
∑^ n i=
nm
(n − 1)n 2
n
· n
= Θ(1) + n 2 m
2 m
· α −
2 m
So... average search(x) time is Θ(1 + α)
Choosing Hash Functions
Ideally, hash function satisfies:
Simple Uniform Hashing Assumption: Any key is equally likely to hash to any location (index,slot) in hash table H.
unfortunately, we cannot usually achieve this... so we use heuristics
For indexing in H is is convenient for keys to be natural numbers (0, 1 , 2 ,.. .)
Division Method for creating hash functions
Multiplicatoin Method for creating hash functions
Collision Resolution by Open Addressing
Ideally, we have
Uniform Hashing Assumption: each key is equally likely to have any of the m! permutations (of indices in H) as its probe sequence
Note: this is different from simple uniform hashing:
Again, this is hard to achieve, so we usually settle for heurestics...
Linear Probing (Open Addressing)
Linear Probing: the ith probe h(k, i) is
h(k, i) = (h′(k) + i) mod m
How many distinct probe sequences are there? m
Pluses and Minuses
Double Hashing (Open Addressing)
Double Hashing: the ith probe h(k, i) is
h(k, i) = (h 1 (k) + h 2 (k) · i) mod m
Important: to make sure probe sequence hits all slots in H we must have h 2 (k) be relatively prime to m
Example 1:
Example 2:
How many distinct probe sequences are there? m^2
Analyzing Open Addressing
We now analyze the expected number of probes for open addressing
Theorem: If α < 1 , then the expected number of probes in an unsuccessful search is ≤ (^1) −^1 α
Proof: In an unsuccessful search when α < 1 (table not full), some number of probes access occupied slots and the last probe accesses an empty slot.
E(#probes) = 1 + E(#probes that access occupied slots)
= 1 + ∑∞ i=
i · Pr[exactly i probes access occupied slots]
= 1 + ∑∞ i=
Pr[exactly i probes access occupied slots] /identity/
≤ 1 + ∑∞ i=
αi/proof below/
=
1 − α 2