GROUP 15.Pptx Presentation
GROUP 15.Pptx Presentation
Members:
1. Alex N Moyo N02425688J
2. Simbarashe Bope N02423343F
3. Russell Mashinya N02424758M
4. Ashley Mufundisi N02420358W
5. Nothando Sithole N02428220W
6. Takunda Mushambi N02427662R
7. Needmore A Muzenda N02420804Q
8. Blessing Hoto N02423763T
9. Elbethlem Moyo N02421199Y
10. Yeukai Kubiku N02422680F
11. Tadiwanashe Maradze N02425592P
HASH TABLES
• Introduction to hash tables
• A hash table is a dynamic data structure that implements
an associative array , which is a structure that can map
keys to values
• It is designed to store data in a way that allows for
efficient retrieval
• Hash Table is a data structure which stores data in an
associative manner. In a hash table, data is stored in an
array format, where each data value has its own unique
index value. Access of data becomes very fast if we know
the index of the desired data.
• Thus, it becomes a data structure in which insertion and
search operations are very fast irrespective of the size of the
data. Hash Table uses an array as a storage medium and
uses hash technique to generate an index where an element
is to be inserted or is to be located from.
- Hashing by division: This straightforward hashing technique uses the key’s remaining value after
dividing it by the array’s size as the index. When an array size is a prime number and the keys are
evenly spaced out, it performs well.
-Hashing by multiplication: This straightforward hashing operation multiplies the key by a constant
between 0 and 1 before taking the fractional portion of the outcome. After that, the index is
determined by multiplying the fractional component by the array’s size. Also, it functions effectively
when the keys are scattered equally.
Characteristics of hash tables
Key-Value Pair Storage:
• Hash tables store data as pairs, where each key is unique
and maps to a specific value.
Fast Access Time:
• Average-case time complexity for search, insert, and delete
operations is O(1), making hash tables very efficient.
Collision Handling:
• Mechanisms (like chaining or open addressing) are
implemented to manage situations where multiple keys hash
to the same index.
Dynamic Resizing:
• Hash tables can automatically resize when the load factor
exceeds a certain threshold, usually by doubling the array
size and rehashing existing entries.
Hash Function Dependency:
• The efficiency of a hash table heavily depends on the
quality of the hash function, which should distribute keys
uniformly across the array.
Load Factor:
• This is a measure of how full the hash table is. It
influences performance and helps in deciding when to
resize.
Memory Usage:
• Hash tables can have a higher memory overhead due to
unused slots, especially when the load factor is low.
Order of Elements:
• Unlike ordered data structures (like arrays or linked lists),
hash tables do not maintain the order of elements. The order
of retrieval may not match the order of insertion.
Data Types:
• Hash tables can store various data types as values, allowing
for flexible data management.
• Concurrency:
• Some implementations of hash tables support concurrent
Choosing a hash function:
Selecting a decent hash function is based on the properties of the keys and the
intended functionality of the hash table. Using a function that evenly distributes the
keys and reduces collisions is crucial.
Criteria based on which a hash function is chosen:
To ensure that the number of collisions is kept to a minimum, a good hash function
should:
-Distribute the keys throughout the hash table in a uniform manner. This implies that
for all pairings of keys, the likelihood of two keys hashing to the same position in the
table should be rather constant.
-To enable speedy hashing and key retrieval, the hash function should be
computationally efficient.
-It ought to be challenging to deduce the key from its hash value. As a result, attempts
to guess the key using the hash value are less likely to succeed.
HOW THEY WORK
Closed addressing
Direct Chaining
• Rather than reserving entire sub-arrays (the columns
above) for keys that collide, one can instead create a
linked list for the set of entries corresponding to each key.
Open addressing
Linear probing
• Linear probing involves trying to place the key into the
next slot if the calculated index(address) is occupied until
an open slot is found.
• Linear probing reduces the index by one to 3, and finds
an empty location in that position.
• However this results in primary clustering.
• Primary clustering refers to the bunching of keys together
inside the array while large proportions of it remain
unoccupied.
+3 hash
• This involves looking at every third slot along until a free
index is found.
Double hashing
Many programming languages have built-in implementations, A poorly designed hash function can lead to many collisions,
simplifying development. degrading performance.
• LinkedList* allocate_list()
• {
• // Allocates memory for a LinkedList pointer.
• LinkedList* list = (LinkedList*) malloc(sizeof(LinkedList));
• return list;
• }