ADS Unit 3
ADS Unit 3
Definition of Dictionary
A dictionary is an abstract data type (ADT) that stores key-value pairs, where each key is
unique and maps to a corresponding value. Dictionaries provide efficient operations for
inserting, deleting, and searching for elements. In programming, dictionaries are often
implemented using data structures such as hash tables, binary search trees, or balanced trees.
When duplicates are allowed, the dictionary can be modified to store multiple values for a
single key, enabling use cases that require multi-mapping.
Applications of Dictionary with Duplicates in Sequential Access
Dictionaries that allow duplicate keys and provide sequential access are useful in various
applications, including:
1. Database Indexing
o In databases, dictionaries are used to maintain indexes that support efficient
searching and retrieval of data records.
o When duplicates exist (e.g., multiple employees with the same name),
sequential access ensures proper organization and retrieval of data.
2. Full-Text Search Engines
o Search engines use inverted indexes (a type of dictionary) to map words to
document IDs.
o If a word appears in multiple documents, sequential access helps retrieve all
related documents efficiently.
3. Multi-Valued Caching Systems
o Caching mechanisms in web applications store multiple responses for the
same request (e.g., different versions of a webpage for different users).
o Dictionary with duplicates ensures sequential access to retrieve the appropriate
version when needed.
4. Symbol Tables in Compilers
o Compilers use dictionaries to store information about identifiers (variables,
functions, etc.).
o If an identifier is overloaded (i.e., multiple functions with the same name but
different parameters), sequential access helps resolve function calls.
5. Log Management Systems
o Log management tools store multiple log entries under the same category
(e.g., errors, warnings).
o Sequential access allows analyzing logs in the order they were recorded.
6. Multimap in Graph Representations
o Graphs can be represented using adjacency lists stored in dictionaries where
multiple edges exist for the same node.
o Sequential access helps traverse connections in an ordered manner.
7. Auto-Suggestion and Predictive Text
o Dictionaries with duplicate entries store possible word completions for a given
prefix.
o Sequential access allows smooth navigation through suggestions.
2. Explain how open hashing and closed hashing is done with examples.
Open Hashing (Separate Chaining)
Concept
In Open Hashing, also known as Separate Chaining, each index of the hash table
contains a linked list (or another structure) to store multiple elements that hash to the
same index.
When a collision occurs (i.e., multiple keys map to the same index), the new key is
stored in a linked list at that index.
Example of Open Hashing
Given Data
Let’s consider a hash table of size 7 and use the hash function:
hash(key)=key mod 7
We insert the following keys: 50, 700, 76, 85, 92, 73, 101
Step-by-Step Insertion
1. 50 mod 7 = 1 → Insert 50 at index 1
2. 700 mod 7 = 0 → Insert 700 at index 0
3. 76 mod 7 = 6 → Insert 76 at index 6
4. 85 mod 7 = 1 → Collision! Append 85 to the linked list at index 1
5. 92 mod 7 = 1 → Collision! Append 92 to the linked list at index 1
6. 73 mod 7 = 3 → Insert 73 at index 3
7. 101 mod 7 = 3 → Collision! Append 101 to the linked list at index 3
Final Hash Table (After Insertions)
Index Elements (Linked List)
0 700
1 50 → 85 → 92
2 —
3 73 → 101
4 —
5 —
6 76
Searching in Open Hashing
To search for 92:
Compute 92 mod 7 = 1 → Go to index 1
Traverse 50 → 85 → 92 → Found ✅
To search for 100:
Compute 100 mod 7 = 2 → Go to index 2
No elements → Not found ❌
Deleting in Open Hashing
To delete 85:
Compute 85 mod 7 = 1 → Go to index 1
Traverse 50 → 85 → 92
Remove 85, update list to 50 → 92
Final Hash Table (After Deleting 85)
Index Elements (Linked List)
0 700
1 50 → 92
Index Elements (Linked List)
2 —
3 73 → 101
4 —
5 —
6 76
Conclusion
A hash table is a highly efficient data structure for storing and retrieving key-value pairs. It
uses a hash function to map keys to indices in an array, and there are different techniques for
handling collisions, such as chaining and open addressing. Hash tables offer constant time
complexity for operations in the average case, making them ideal for many real-world
applications like databases, caches, and sets.
Where:
The second hash function h2(key) should never return 0 to avoid infinite loops.
Given Data
Step 1: Insert 50
h1(50)=50mod 7
No collision → Insert 50 at index 1
Step 3: Insert 76
h1(76)=76mod 7=6
No collision → Insert 76 at index 6
Step 4: Insert 85
Step 5: Insert 92
Step 6: Insert 73
h1(73)=73mod 7=3
No collision → Insert 73 at index 3
Index Key
0 700
1 50
2 85
3 73
4 92
5 101
6 76
❌ Slightly slower than linear probing due to extra hash function calculations
❌ If the table is almost full, probing might take longer
Conclusion
5. What do you mean by collision and how can you handle it by using linear
probing
Collision in Hashing
A collision occurs when two or more keys hash to the same index in a hash table. Since a
hash table can only store one element per index, if two keys generate the same hash value
(i.e., the same index), a collision happens.
For example, given a hash table size of 7, if two keys like 13 and 20 both hash to the index 3,
a collision occurs.
Conclusion
In linear probing, when a collision occurs, the algorithm searches the next sequential index
in the hash table. It provides a simple way to resolve collisions but may suffer from
clustering, which can degrade performance if the table is almost full. Proper resizing and
load factor management can help mitigate this issue.
Linear Probing is a method used to handle collisions in open addressing (a type of collision
resolution technique in hash tables). When a collision occurs, linear probing checks the next
consecutive position (in a linear manner) in the hash table until it finds an empty slot.
In linear probing, when a key hashes to an index, if that index is already occupied, the
algorithm searches for the next index by increasing the index by 1, wrapping around to the
beginning of the table if necessary.
Where:
i is the probe number, starting from 0 and incremented by 1 for each subsequent
probe.
hash(key) is the index computed by the hash function.
Given Data
Step 1: Insert 50
Step 3: Insert 76
Step 4: Insert 85
Step 6: Insert 73
Index Key
0 700
1 50
2 85
3 92
4 73
5 101
6 76
Conclusion
Linear probing is a simple and effective collision resolution strategy in hash tables. When a
collision occurs, it sequentially checks the next index in the array for an empty slot. Though
simple and memory-efficient, linear probing can suffer from clustering, especially when the
hash table is nearly full, which can slow down the insertion and search operations.