EECS 281 FINAL QUESTIONS WITH CORRECT
ANSWERS
Hashing Basic Functionality Complexity - ans-
xz xz xz xz xzxz
Efficient: Insert O(1), Search O(1), Remove O(1)
xz xz xz xz xz xz
Inefficient: Sort, Kth largest, Joint/Merge xz xz xz xz
Hashing Load Factor - ans-Alpha = α = N/M xz xz xz xzxz xz xz xz xz
N is number of keys in table
xz xz xz xz xz xz
M is size of table
xz xz xz xz
Need to keep below 0.5 so functions are efficient.
xz xz xz xz xz xz xz xz
Hashing Load Factor - Separate Chaining - ans-
xz xz xz xz xz xz xzxz
Load factor, α, is average number of items per table entry
xz xz xz xz xz xz xz xz xz xz
Hashing Load Factor - Open Addressing - ans-Load factor, α, is percent of table filled
xz xz xz xz xz xz xzxz xz xz xz xz xz xz xz
Hashing Insert Complexity - ans-O(1) if duplicates are allowed
xz xz xz xzxz xz xz xz xz
O(α) if no duplicates allowed.
xz xz xz xz
Reasoning: Need to search for item to see if it already exists. Search is expensive.
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
Hashing Search Complexity - ans-O(α) xz xz xz xzxz
Need to hash for the key but then need to linear search table if rehashing or chain if separat
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
e chaining
xz
Hashing Remove Complexity - ans- xz xz xz xzxz
O(α). Need to search for item before removing it. Could be even worse depending on separ
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
ate chaining container.
xz xz
Hashing Complexity - Normal Vector - ans-Insert and Search both O(α)
xz xz xz xz xz xzxz xz xz xz xz
Hashing Complexity - Sorted Vector - ans-Insert is O(α) and Search is O(log α)
xz xz xz xz xz xzxz xz xz xz xz xz xz xz
Hashing Complexity - Binary Tree - ans-Insert and search O(log a).
xz xz xz xz xz xzxz xz xz xz xz
Large memory over head to maintain tree.
xz xz xz xz xz xz
Hashing Linear vs Quadratic Probing Complexity - ans-
xz xz xz xz xz xz xzxz
Number of probes for hit and for miss grow exponentially with α for both.
xz xz xz xz xz xz xz xz xz xz xz xz xz
, Quadratic has slightly slower growth for both hit and miss. xz xz xz xz xz xz xz xz xz
Number of probes for hit < number of probes for miss
xz xz xz xz xz xz xz xz xz xz
Dynamic Hashing Summary - ans-When α ≥ 0.5, grow table size (M) and rehash.
xz xz xz xzxz xz xz xz xz xz xz xz xz xz
Keeps load factor α low, but occasional expensive regrow operation
xz xz xz xz xz xz xz xz xz
Dynamic Hashing Amortized Complexity - ans-O(M) - Linear
xz xz xz xz xzxz xz xz
1) Initial inserts have cost until α ≥ 0.5
xz xz xz xz xz xz xz xz
=> Max 2.5 probes for insertion hit/miss if α ≤ 0.5
xz xz xz xz xz xz xz xz xz xz
=> Insert M/α -1 keys before α exceeds 0.5
xz xz xz xz xz xz xz xz
=> 2.5 * (M/α -1) = O(M)
xz xz xz xz xz xz
2) Grow and rebuild table
xz xz xz xz
=> Max 1.5 probes for insertion hit/miss if α ≤ 0.25
xz xz xz xz xz xz xz xz xz xz
=> 1.5 * M/2 = O(M)
xz xz xz xz xz
Thus 2O(M) = O(M) = Linear amortized cost.... O(1)!
xz xz xz xz xz xz xz xz
Simple Tree - ans-Acyclic, connected graph.
xz xz xzxz xz xz xz
Undirected.
Rooted Tree - ans-Simple Tree w/ selected vertex as root.
xz xz xzxz xz xz xz xz xz xz
Edges directed AWAY from root. xz xz xz xz
Internal Node - ans-A node with children xz xz xzxz xz xz xz
ordered tree - ans-A rooted tree in which all children of each vertex are ordered left to right.
xz xz xzxz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
Binary Tree - ans-a tree in which each node has at most 2 children.
xz xz xzxz xz xz xz xz xz xz xz xz xz xz
Complete Binary Tree - ans-A binary tree with depth d where: xz xz xz xzxz xz xz xz xz xz xz
1) Depths 1,...,d-1 are all full
xz xz xz xz xz xz
(each level of the tree except the last is full)
xz xz xz xz xz xz xz xz xz
2) All internal nodes are to the left of external nodes at depth d-1
xz xz xz xz xz xz xz xz xz xz xz xz xz
(All leaves are at the second to last level or left most at the last level)
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
Complete Binary Tree Implementation - ans-Root: Index 1 xz xz xz xz xzxz xz xz
Left Child of node i = 2*i
xz xz xz xz xz xz
Right child of node i = 2*i+1
xz xz xz xz xz xz
Inefficient use of space for sparse trees xz xz xz xz xz xz
Binary Tree Insert Complexity - ans-Best: O(1)
xz xz xz xz xzxz xz
Worst: O(n) xz
ANSWERS
Hashing Basic Functionality Complexity - ans-
xz xz xz xz xzxz
Efficient: Insert O(1), Search O(1), Remove O(1)
xz xz xz xz xz xz
Inefficient: Sort, Kth largest, Joint/Merge xz xz xz xz
Hashing Load Factor - ans-Alpha = α = N/M xz xz xz xzxz xz xz xz xz
N is number of keys in table
xz xz xz xz xz xz
M is size of table
xz xz xz xz
Need to keep below 0.5 so functions are efficient.
xz xz xz xz xz xz xz xz
Hashing Load Factor - Separate Chaining - ans-
xz xz xz xz xz xz xzxz
Load factor, α, is average number of items per table entry
xz xz xz xz xz xz xz xz xz xz
Hashing Load Factor - Open Addressing - ans-Load factor, α, is percent of table filled
xz xz xz xz xz xz xzxz xz xz xz xz xz xz xz
Hashing Insert Complexity - ans-O(1) if duplicates are allowed
xz xz xz xzxz xz xz xz xz
O(α) if no duplicates allowed.
xz xz xz xz
Reasoning: Need to search for item to see if it already exists. Search is expensive.
xz xz xz xz xz xz xz xz xz xz xz xz xz xz
Hashing Search Complexity - ans-O(α) xz xz xz xzxz
Need to hash for the key but then need to linear search table if rehashing or chain if separat
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
e chaining
xz
Hashing Remove Complexity - ans- xz xz xz xzxz
O(α). Need to search for item before removing it. Could be even worse depending on separ
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
ate chaining container.
xz xz
Hashing Complexity - Normal Vector - ans-Insert and Search both O(α)
xz xz xz xz xz xzxz xz xz xz xz
Hashing Complexity - Sorted Vector - ans-Insert is O(α) and Search is O(log α)
xz xz xz xz xz xzxz xz xz xz xz xz xz xz
Hashing Complexity - Binary Tree - ans-Insert and search O(log a).
xz xz xz xz xz xzxz xz xz xz xz
Large memory over head to maintain tree.
xz xz xz xz xz xz
Hashing Linear vs Quadratic Probing Complexity - ans-
xz xz xz xz xz xz xzxz
Number of probes for hit and for miss grow exponentially with α for both.
xz xz xz xz xz xz xz xz xz xz xz xz xz
, Quadratic has slightly slower growth for both hit and miss. xz xz xz xz xz xz xz xz xz
Number of probes for hit < number of probes for miss
xz xz xz xz xz xz xz xz xz xz
Dynamic Hashing Summary - ans-When α ≥ 0.5, grow table size (M) and rehash.
xz xz xz xzxz xz xz xz xz xz xz xz xz xz
Keeps load factor α low, but occasional expensive regrow operation
xz xz xz xz xz xz xz xz xz
Dynamic Hashing Amortized Complexity - ans-O(M) - Linear
xz xz xz xz xzxz xz xz
1) Initial inserts have cost until α ≥ 0.5
xz xz xz xz xz xz xz xz
=> Max 2.5 probes for insertion hit/miss if α ≤ 0.5
xz xz xz xz xz xz xz xz xz xz
=> Insert M/α -1 keys before α exceeds 0.5
xz xz xz xz xz xz xz xz
=> 2.5 * (M/α -1) = O(M)
xz xz xz xz xz xz
2) Grow and rebuild table
xz xz xz xz
=> Max 1.5 probes for insertion hit/miss if α ≤ 0.25
xz xz xz xz xz xz xz xz xz xz
=> 1.5 * M/2 = O(M)
xz xz xz xz xz
Thus 2O(M) = O(M) = Linear amortized cost.... O(1)!
xz xz xz xz xz xz xz xz
Simple Tree - ans-Acyclic, connected graph.
xz xz xzxz xz xz xz
Undirected.
Rooted Tree - ans-Simple Tree w/ selected vertex as root.
xz xz xzxz xz xz xz xz xz xz
Edges directed AWAY from root. xz xz xz xz
Internal Node - ans-A node with children xz xz xzxz xz xz xz
ordered tree - ans-A rooted tree in which all children of each vertex are ordered left to right.
xz xz xzxz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
Binary Tree - ans-a tree in which each node has at most 2 children.
xz xz xzxz xz xz xz xz xz xz xz xz xz xz
Complete Binary Tree - ans-A binary tree with depth d where: xz xz xz xzxz xz xz xz xz xz xz
1) Depths 1,...,d-1 are all full
xz xz xz xz xz xz
(each level of the tree except the last is full)
xz xz xz xz xz xz xz xz xz
2) All internal nodes are to the left of external nodes at depth d-1
xz xz xz xz xz xz xz xz xz xz xz xz xz
(All leaves are at the second to last level or left most at the last level)
xz xz xz xz xz xz xz xz xz xz xz xz xz xz xz
Complete Binary Tree Implementation - ans-Root: Index 1 xz xz xz xz xzxz xz xz
Left Child of node i = 2*i
xz xz xz xz xz xz
Right child of node i = 2*i+1
xz xz xz xz xz xz
Inefficient use of space for sparse trees xz xz xz xz xz xz
Binary Tree Insert Complexity - ans-Best: O(1)
xz xz xz xz xzxz xz
Worst: O(n) xz