Chap-1 ADS
Chap-1 ADS
In computer science, a dictionary is an abstract data type that represents an ordered or unordered list
of key-value pair elements where keys are used to search/locate the elements in the list. In a
dictionary ADT, the data to be stored is divided into two parts:
• Key
• Value.
Each item stored in a dictionary is represented by a key-value pair. Key is used to access the elements
in the dictionary. With the key we can access value which has more information about the element.
Characteristics of Dictionary
• Key-Value Pairs: Dictionaries store data as key-value pairs where each key is unique and
maps to exactly one value.
• Direct Access: The primary feature of dictionaries is to provide fast access to elements not by
their position, as in lists or arrays, but by their keys.
• Dynamic Size: Like many abstract data types, dictionaries typically allow for dynamic resizing.
New key-value pairs can be added, and existing ones can be removed.
• Ordering: Some dictionaries maintain the order of elements, such as ordered maps or sorted
dictionaries. Others, like hash tables, do not maintain any particular order.
• Key Uniqueness: Each key in a dictionary must be unique, though different keys can map to
the same value.
Types of Dictionary
Ordered dictionary.
Unordered dictionary.
• Linked lists.
• Hashing
https://studyglance.in/ds/display.php?tno=12&topic=Introduction-to-
Dictionaries#:~:text=In%20computer%20science%2C%20a%20dictionary,Value.
Hashing is a method of directly computing the index of the table by using some suitable
mathematical function called hash function.
A hash table is a widely used data structure that stores data in an associative manner. In a hash table,
data is stored in an array format, where each data value has a unique key associated with it. The
efficiency of a hash table comes from its ability to convert these keys into indexes of an array through
a process called hashing.
• Hash Function: The heart of a hash table is the hash function. This function takes a key and
computes an index (an integer) which determines where the data associated with that key
should be stored in the table. A good hash function distributes data uniformly across the
table to minimize collisions (situations where different keys hash to the same index).
• Bucket: The hash function H(key) is used to map several dictionary entries in the hash table.
Each position of the hash table is called bucket.
• Collision Resolution: Since a hash function may map multiple keys to the same index,
collision resolution strategies are crucial. Common methods include chaining (where each
array element in the hash table stores a linked list of entries that hash to the same index)
and open addressing (where you probe for the next available slot using techniques like linear
probing, quadratic probing, or double hashing).
• Load Factor: The load factor of a hash table is the number of elements divided by the
number of slots available in the array. It's a measure of how full the hash table is. A higher
load factor increases the likelihood of collisions, leading to a decrease in performance. This
often necessitates resizing the hash table.
• Dynamic Resizing: To maintain efficient operations, hash tables may need to be resized as
elements are added or removed. This involves creating a new, larger (or smaller) table, and
rehashing all the existing elements into it.
Operations
• Efficient Data Retrieval: Offers near-constant time complexity for insertion, deletion, and
search operations in the average case.
• Direct Access: Data can be directly indexed and is not dependent on the number of elements
in the table.
• Hash Function Design: Creating an effective hash function can be challenging for certain
types of keys.
3. Separate chaining.
Separate chaining is a widely used method to resolve collisions in hash tables. When two or more
elements are hash to the same location, these elements are represented into a singly-linked list
like a chain. Since this method uses extra memory to resolve the collision, therefore, it is also
known as open hashing.
1. Hash Table Structure: The hash table is an array of a fixed size. Each element of this array is a
head pointer to a linked list.
2. Hash Function: The hash function takes a key and computes an index in the array.
3. Insertion:
o Insert the key-value pair at the head of the linked list at this index.
4. Searching:
o Compute the index for the key using the hash function.
o Search through the linked list at that index for the desired key.
5. Deletion:
o Search the linked list for the key and remove the corresponding node.
4. Open addressing.
In open addressing, all elements are stored directly in the hash table itself.
When a collision occurs (i.e., two items hash to the same slot), the method seeks to find another
slot to accommodate one of the items using a probing sequence.
Linear probing:
Linear probing is a type of open addressing where the probing sequence is linear.
1. Hash Function: Like any hash table, linear probing starts with a hash function that computes
an initial index for a given key.
2. Insertion:
• Compute the hash for the key to find the initial index.
• If the slot at the computed index is empty, insert the item there.
• If the slot is occupied, check the next slot (i.e., move linearly forward) until an empty slot
is found.
3. Searching:
• Compute the hash for the key to find the initial index.
• If the slot contains a different key, move linearly forward until the key is found or an
empty slot is encountered.
4. Deletion:
• Find the item using the search operation.
• Remove the item and mark the slot as deleted (a special marker different from empty
and occupied).
• Note: Deletion complicates the search and insert operations because the search must
continue past deleted items, and insert can use slots marked as deleted.
Quadratic probing
Quadratic probing is another method of open addressing used in hash tables to resolve
collisions.
Unlike linear probing, where the interval between probes is fixed, quadratic probing uses
a quadratic function to calculate the interval between probes.
This approach helps to reduce the clustering problem seen in linear probing.
How Quadratic Probing Works
o Use the hash function hash(key) to calculate the initial index for the given key.
o If it's empty, place the key-value pair in that slot, and the insertion is complete.
o If the slot at the initial index is occupied (collision occurred), quadratic probing
comes into play.
o Initialize a variable i to 1 (for the first iteration). Calculate the next probe index using
the quadratic probing formula:
o If it's empty, place the key-value pair in that slot, and the insertion is complete.
o If the slot is occupied, repeat the probing process by incrementing i and recalculating
the next_index until an empty slot is found.