0% found this document useful (0 votes)
14 views5 pages

Chap-1 ADS

Uploaded by

Yash Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Chap-1 ADS

Uploaded by

Yash Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.Dictonary.

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.

Fundamental Operations of Dictionary

• Insert: Add a new key-value pair to the dictionary.

• Search: Retrieve the value associated with a particular key.

• Delete: Remove a key-value pair from the dictionary.

• Update: Change the value associated with a given key.

• Keys: Return a collection of all the keys in the dictionary.

• Values: Return a collection of all the values in the dictionary.

Types of Dictionary

There are two major variations of dictionaries:

Ordered dictionary.

• In an ordered dictionary, the relative order is determined by comparison on keys.

• The order should be completely dependent on the key.

Unordered dictionary.

• In an unordered dictionary, no order relation is assumed on keys.

• Only equality operation can be performed on the keys.


Implementation of Dictionary

A dictionary can be implemented in several ways, such as

• Fixed length array.

• Linked lists.

• Hashing

• Trees (BST, Balancing BSTs, Splay trees, Tries etc.)

https://studyglance.in/ds/display.php?tno=12&topic=Introduction-to-
Dictionaries#:~:text=In%20computer%20science%2C%20a%20dictionary,Value.

2.Hashing and Hash Table.

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.

Key Concepts of hash table

• 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

• Insertion: Add a new key-value pair to the table.


• Deletion: Remove a key-value pair from the table.

• Search: Retrieve the value associated with a given key.

Advantages of hash table

• 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.

Disadvantages of hash table:

• Collision Management: Requires additional structures or algorithms to handle collisions


effectively.

• Hash Function Design: Creating an effective hash function can be challenging for certain
types of keys.

• Memory Overhead: Some implementations may consume more memory, especially to


handle collisions and resizing.

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.

How Separate Chaining Works

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 Apply the hash function to the key to get the index.

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 Find the index using the hash function.

o Search the linked list for the key and remove the corresponding node.
4. Open addressing.

Open addressing is a collision resolution technique used in hash tables.

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.

How Linear Probing Works

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.

• If the end of the table is reached, wrap around to the beginning.

3. Searching:

• Compute the hash for the key to find the initial index.

• If the slot is empty, the key is not in the table.

• If the slot contains the key, return the item.

• 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

1. Insertion using Quadratic Probing:

o Use the hash function hash(key) to calculate the initial index for the given key.

o Check if the slot at the calculated initial index is empty.

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 next_index = (initial_index + (i2)) % table_size

o Check if the slot at the calculated next_index is empty.

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.

You might also like