Podcast
Questions and Answers
Which of the following best describes the role of a hash function in a hash table?
Which of the following best describes the role of a hash function in a hash table?
- To resolve collisions by comparing keys directly.
- To map keys to specific indices in the hash table. (correct)
- To sort keys in ascending order before insertion.
- To encrypt the keys for secure storage.
In Python dictionaries, keys must be mutable to allow for modification after creation.
In Python dictionaries, keys must be mutable to allow for modification after creation.
False (B)
What is the primary goal of a well-designed hash function?
What is the primary goal of a well-designed hash function?
minimize collisions
A hash collision occurs when two different ______ produce the same hash value.
A hash collision occurs when two different ______ produce the same hash value.
Match the following collision resolution techniques with their descriptions:
Match the following collision resolution techniques with their descriptions:
Which of the following is NOT a basic operation of the mapping ADT?
Which of the following is NOT a basic operation of the mapping ADT?
Linear search has a time complexity of O(log n).
Linear search has a time complexity of O(log n).
What is the time complexity of accessing an element in a dictionary using its key, assuming a good hash function and minimal collisions?
What is the time complexity of accessing an element in a dictionary using its key, assuming a good hash function and minimal collisions?
The process of increasing the size of a hash table and remapping the keys is known as ______.
The process of increasing the size of a hash table and remapping the keys is known as ______.
Match the following types to whether they are hashable or non-hashable.
Match the following types to whether they are hashable or non-hashable.
What is the purpose of the len()
operation in a mapping ADT?
What is the purpose of the len()
operation in a mapping ADT?
A binary search has a time complexity of O(n).
A binary search has a time complexity of O(n).
In the context of hash tables, what is a 'bucket'?
In the context of hash tables, what is a 'bucket'?
The ratio of the number of entries to the table size in a hash table is known as the ______ factor.
The ratio of the number of entries to the table size in a hash table is known as the ______ factor.
Match the ADT operation with its intended purpose
Match the ADT operation with its intended purpose
Why is it important for keys in a hash table to be immutable?
Why is it important for keys in a hash table to be immutable?
Using a simple array is not an efficient way to implement a hash table.
Using a simple array is not an efficient way to implement a hash table.
What is the purpose of separate chaining?
What is the purpose of separate chaining?
When a collision occurs, ______ probing searches for the next available index.
When a collision occurs, ______ probing searches for the next available index.
Match items to being hashable or not hashable.
Match items to being hashable or not hashable.
Which collision-handling scheme stores colliding keys outside the table at the same index?
Which collision-handling scheme stores colliding keys outside the table at the same index?
Quadratic probing is always guaranteed to find an empty slot if one exists.
Quadratic probing is always guaranteed to find an empty slot if one exists.
What is the purpose of rehashing in a dynamic hash table?
What is the purpose of rehashing in a dynamic hash table?
When the load factor of a hash table increases, the average time complexity of get
and put
operations tends to ______.
When the load factor of a hash table increases, the average time complexity of get
and put
operations tends to ______.
Match what each ADT operation does:
Match what each ADT operation does:
What is the primary advantage of using a hash table for storing and retrieving data?
What is the primary advantage of using a hash table for storing and retrieving data?
Separate chaining is an example of closed hashing.
Separate chaining is an example of closed hashing.
What does a Hash Collision indicate?
What does a Hash Collision indicate?
0(1)
is the rehashing cost when using ______ update.
0(1)
is the rehashing cost when using ______ update.
Match the python function with tuple, list, or set.
Match the python function with tuple, list, or set.
Given a hash table with a load factor close to 1, which of the following techniques would be most effective in improving performance?
Given a hash table with a load factor close to 1, which of the following techniques would be most effective in improving performance?
In open addressing, the number of possible probe sequences is limited by table size.
In open addressing, the number of possible probe sequences is limited by table size.
What is the time complexity of accessing an element using separate chaining collision-resolution?
What is the time complexity of accessing an element using separate chaining collision-resolution?
With a direct-access table, there is ______ wasted space.
With a direct-access table, there is ______ wasted space.
Match these properties to whether they are advantages or disadvantages of using keys as indexes.
Match these properties to whether they are advantages or disadvantages of using keys as indexes.
In separate chaining, how does the worst-case time complexity of get
and put
operations compare to the average-case complexity?
In separate chaining, how does the worst-case time complexity of get
and put
operations compare to the average-case complexity?
Any potential collision can be fixed by applying a second hashing function.
Any potential collision can be fixed by applying a second hashing function.
Other than using a linked list, what is another example of a data structure to minimize collision?
Other than using a linked list, what is another example of a data structure to minimize collision?
With quadratic probing, quadratic provides the ______ probes.
With quadratic probing, quadratic provides the ______ probes.
Match the following Linear and Quadratic probing values.
Match the following Linear and Quadratic probing values.
Flashcards
What is mapping?
What is mapping?
Association between two sets of things
Key-value pair
Key-value pair
Associates a value to a key
What are buckets?
What are buckets?
Data stored in a hash table
What is a hash function?
What is a hash function?
Signup and view all the flashcards
put(key,value)
put(key,value)
Signup and view all the flashcards
get(key)
get(key)
Signup and view all the flashcards
What is KeyError?
What is KeyError?
Signup and view all the flashcards
del(key)
del(key)
Signup and view all the flashcards
What is len()?
What is len()?
Signup and view all the flashcards
What are keys?
What are keys?
Signup and view all the flashcards
What are values?
What are values?
Signup and view all the flashcards
What are items?
What are items?
Signup and view all the flashcards
Using keys as indices
Using keys as indices
Signup and view all the flashcards
Using f(key)
Using f(key)
Signup and view all the flashcards
What is a hash function?
What is a hash function?
Signup and view all the flashcards
What is a hash collision?
What is a hash collision?
Signup and view all the flashcards
Separate chaining
Separate chaining
Signup and view all the flashcards
Open addressing
Open addressing
Signup and view all the flashcards
Linear probing
Linear probing
Signup and view all the flashcards
Quadratic probing
Quadratic probing
Signup and view all the flashcards
What is double hashing?
What is double hashing?
Signup and view all the flashcards
Dictionaries rehashing
Dictionaries rehashing
Signup and view all the flashcards
When to Rehash?
When to Rehash?
Signup and view all the flashcards
What is load factor?
What is load factor?
Signup and view all the flashcards
What is object?
What is object?
Signup and view all the flashcards
What are hashable types?
What are hashable types?
Signup and view all the flashcards
What are non-hashable types?
What are non-hashable types?
Signup and view all the flashcards
Study Notes
Chapter 15: Mapping and Hash Tables
- Objectives include understanding mapping vocabulary, listing basic operations of mapping ADTs, explaining hashing, listing hash function requirements, explaining key immutability, and implementing mapping ADTs.
Search Algorithms
- Linear search has a time complexity of O(n).
- Binary search has a time complexity of O(log n).
- Hashing can achieve search operations in O(1) time.
Dictionaries
- Python dictionaries store elements with keys of any hashable types and values of any types.
- Dictionaries store unordered collections of unique values within key-value pairs.
- Keys in dictionaries must be unique.
- Dictionaries are mutable, enabling adding, modifying, and removing key-value pairs after creation.
Mapping
- A mapping is an association between two sets of things, specifically associating a value to a key.
- These associated pairs are referred to as key-value pairs.
- Keys within a mapping must be unique.
Concept of Hashing
- Information to be retrieved is stored in a hash table, which is visualized as an array of "m" locations termed buckets.
- A hash function is a mapping between a key and a bucket.
- The time required to store and retrieve data is proportional to hash function computation time.
Mapping ADT (Abstract Data Type)
- Required operations:
put(key, value)
: Adds a key-value pair.get(key)
: Returns the value, raising aKeyError
if the key is absent.
- Optional operations:
del(key)
: Removes a key-value pair.len()
: Returns the number of keys.keys()
: Returns a set of keys.values()
: Returns a set of values.items()
: Returns a set of key-value pairs.
Direct Addressing
- Idea: If we know exactly where a key should be, keys can be directly used as indices.
- This approach assigns a unique index to each key.
- Problem: Potentially too much wasted space
Hash Functions and Tables
- Instead of directly using the key as an index, hash functions map the entire key space into a smaller set of keys to use just the right sized array.
- A hash function (f) converts a key to an index; for example, h(x) = x % Table_size.
- Modify hash function.
Hash Collision
- This occurs when two different keys have the same hash value.
- Mathematically, h(k1) = h(k2) when k1 ≠k2.
Collision Handling Schemes
- Collision occurs when different elements are mapped to the same cell.
- Collision resolution methods incluse separate chaining and open addressing.
- Open addressing involves linear probing, quadratic probing, and double hashing.
Separate Chaining (Open Hashing)
- Keys are stored in the same slot using a list, a linked list or by some other data structure .
Open Addressing (Closed Hashing)
- Open addressing is a collision resolution strategy where collisions are resolved by storing the colliding key in a different location when the natural choice is full.
- The methods are:
- Linear Probing: Search the table for the closest following free location and insert the new key there.
- Quadratic Probing: When collision occurs, probe for i²'th bucket in ith iteration.
- Double Hashing: Use a secondary hash function algorithm to find the next free available slot where it is very unlikely that for some key, h(key) equals g(key).
Rehashing
- Dictionaries support O(1) for put and get, regardless of n
- Achieved when:
- bucket = hash(key) % m, where keys are split into m buckets used with ~m items.
- If n gets too big/small, Rehash is used:
- Create a list with p buckets
- Rehash every key: bucket = hash(key) % p
- Cost: O(n), Average cost: O(1); Lazy Update!
- Rehashing is prompted by load factor.
Load Factor
- Load Factor (α) is the Ratio of the number of entries in the table to table size.
- If n is the total number of (key, value) pairs stored in the table and c is the capacity of the table (i.e., array), where α = n/c
- This decides when to increase the HashMap capacity to maintain the get() and put() operation complexity of 0(1).
Python's hash()
Function
- Uses only one parameter: the object whose hash value is to be returned.
- Only immutable objects can be hashed.
- Hashable objects: bool, int, long, float, string, tuple, etc.
- Non-hashable objects incluse lists, sets, and dictionaries.
Implementing the Mapping ADT
- Involves using
ListMapping
andHashMapping
classes.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.