ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3138Analysis Collision Res…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3138

    Design and Analysis of Algorithms

    Progress0 / 53 done
    Topics53
    1Introduction to Algorithm Design2Data Structures3Efficiency in Algorithms4Analysis of Algorithms5Mathematical Review6Mathematical Analysis of Algorithms7Types of Functions8Order of Growth9Asymptotic Notations10Sorting Algorithms11Selection Sort Algorithm12Example and Analysis of Selection Sort13Insertion Sort Algorithm14Divide and Conquer Algorithms15Merge Sort Algorithm16Quick Sort Algorithm17Bucket Sort Algorithm18Radix Sort Algorithm19Counting Sort Algorithm20Heap Sort Basics21Heap Algorithms22Heap Properties and Examples23Heap Operations24Heap Sort Algorithm Analysis25Heap Insertion and Deletion26Tree Based Algorithms and Hashing27Red Black Tree Basics28Binary Search Tree Basics29Tree Searching Algorithms30Analysis of Tree Searching Algorithms31Analysis of Insertion and Deletion in BST32Hashing Basics33Examples of Hash Functions34Analysis of Collision Resolution Techniques35Dynamic Programming360-1 Knapsack Problem37Fractional Knapsack Problem38Longest Common Subsequence39Shortest Path Finding40Matrix Chain Multiplication41Assembly Line Chain Problem42Greedy Algorithms43Prim's Algorithm44Kruskal's Algorithm45Dijkstra's Algorithm46Huffman Coding47NP-Completeness48Polynomial Time Verification49Reducibility50NP-Completeness Proofs51Randomized Algorithms52Particle Swarm Optimization53Genetic Algorithms
    7 min read
    Topic 34 of 53
    Topic Study Note Illustration
    34

    Analysis of Collision Resolution Techniques

    Collisions in hash tables occur when two different keys hash to the same index. When a collision happens, we need a mechanism to resolve it in a...

    Analysis of Collision Resolution Techniques

    Collisions in hash tables occur when two different keys hash to the same index. When a collision happens, we need a mechanism to resolve it in a way that allows the hash table to still function correctly. There are several techniques for collision resolution, each with its own trade-offs in terms of time complexity, space usage, and efficiency.

    The most commonly used collision resolution techniques are:

    1. Chaining (Separate Chaining)
    2. Open Addressing (with strategies like Linear Probing, Quadratic Probing, and Double Hashing)

    Let's analyze each technique in detail, comparing them based on various factors like performance, space usage, and ease of implementation.


    1. Chaining (Separate Chaining)

    In chaining, each bucket (or slot) in the hash table holds a collection (typically a linked list or other data structure like a balanced tree or dynamic array) to store multiple key-value pairs that hash to the same index. When a collision occurs, the new element is simply added to the list at the corresponding index.

    How Chaining Works:

    1. Hash the key to find the appropriate index in the array.
    2. If the bucket at that index is empty, store the key-value pair in the bucket.
    3. If the bucket is already occupied (i.e., a collision), add the new key-value pair to the list (or another collection) at that bucket.

    Advantages of Chaining:

    • Simple and intuitive: It's relatively easy to implement, and no rehashing is required when there are collisions.
    • Handles collisions gracefully: Even if many collisions occur, the hash table size does not have to be resized as in open addressing.
    • Flexible space usage: Each bucket can grow dynamically without the need for resizing the entire hash table.

    Disadvantages of Chaining:

    • Extra space: Each bucket needs to store additional information (i.e., linked list nodes or other data structures). This leads to higher space complexity than open addressing, particularly if there are many collisions.
    • Poor performance under heavy collision: If the hash function results in many collisions (i.e., many keys end up in the same bucket), the linked list at that bucket will grow large, which leads to poor performance for search, insert, and delete operations. In the worst case, where all keys hash to the same index, the time complexity for all operations can degrade to O(n).
    • Cache locality issues: Since the elements are scattered across memory (due to the linked list), accessing them may not be cache-friendly, resulting in slower access compared to open addressing.

    Time Complexity:

    • Search: On average O(1 + α), where α is the load factor (the ratio of the number of elements to the number of buckets). In the worst case (all elements hash to the same bucket), it becomes O(n).
    • Insertion: O(1) on average. In the worst case, O(n).
    • Deletion: O(1) on average. In the worst case, O(n).

    2. Open Addressing

    In open addressing, when a collision occurs, the hash table searches for the next available slot within the array itself, without using additional data structures like linked lists. The primary methods of open addressing are:

    • Linear Probing
    • Quadratic Probing
    • Double Hashing

    How Open Addressing Works:

    1. Hash the key to find the index.
    2. If the index is occupied, search for the next available slot using a probing technique (either linear, quadratic, or double hashing).
    3. Once an empty slot is found, store the key-value pair in that slot.
    Linear Probing:
    • When a collision occurs at index i, we check the next index (i + 1) % table_size, then (i + 2) % table_size, and so on, until an empty slot is found.
    • This method is prone to clustering, where groups of contiguous slots become filled, leading to increased search times.
    Quadratic Probing:
    • Instead of checking adjacent slots in a linear fashion, quadratic probing checks slots at intervals of 1^2, 2^2, 3^2, etc. (i.e., (i + j^2) % table_size for j = 1, 2, 3, ...).
    • This reduces primary clustering but may still result in secondary clustering (collisions that happen in the same pattern for different keys).
    Double Hashing:
    • If a collision occurs, double hashing uses a second hash function to compute a new index. The formula is:
      hash2(key) = secondary_hash_function(key)
      new_index = (hash1(key) + i * hash2(key)) % table_size
      
    • This approach reduces clustering significantly but requires careful design of the second hash function.

    3. Comparison of Open Addressing Techniques

    Advantages of Open Addressing:

    • Space efficiency: Open addressing doesn't require extra space for linked lists or other structures, as it stores all elements directly in the array.
    • Better cache locality: Since all elements are stored in a single array, access to them is more cache-friendly, leading to better performance in terms of memory access.
    • Simple implementation: It is easy to implement since no extra data structures (like linked lists or trees) are required.

    Disadvantages of Open Addressing:

    • Performance degradation: When the load factor increases, the table becomes more crowded, leading to more probing and slower performance. As a result, insertion and search times can degrade significantly if the table is not resized or rehashed in time.
    • Clustering: Methods like linear probing are prone to clustering (i.e., groups of consecutive slots become filled, making further searches and insertions slow).
    • Resizing: Open addressing requires careful resizing (rehashing) of the table when the load factor exceeds a certain threshold (usually around 0.7) to maintain efficient performance.

    Time Complexity:

    • Search:

      • Linear Probing: O(1) on average. In the worst case, O(n).
      • Quadratic Probing: O(1) on average. Worst-case can still degrade to O(n) in cases of high clustering.
      • Double Hashing: O(1) on average. Worst case can degrade to O(n) but typically performs better than linear or quadratic probing under similar conditions.
    • Insertion: O(1) on average. In the worst case, O(n).

    • Deletion: O(1) on average. In the worst case, O(n).


    4. Performance Comparison

    Let's compare the performance of chaining and open addressing based on the load factor and table resizing.

    Collision Resolution Technique Time Complexity (Average) Time Complexity (Worst Case) Space Complexity
    Chaining (Separate Chaining) O(1 + α) O(n) O(n)
    Linear Probing O(1) O(n) O(n)
    Quadratic Probing O(1) O(n) O(n)
    Double Hashing O(1) O(n) O(n)
    • Chaining has the advantage that it is more resilient to high load factors and doesn't require complex probing or resizing strategies, but it uses extra space for linked lists.
    • Open addressing is more space-efficient and has better cache locality but requires resizing and can suffer from clustering if the probing strategy is not well-designed.

    Conclusion

    Each collision resolution technique has its advantages and trade-offs.

    • Chaining is easier to implement and more robust against high load factors but requires additional space to store linked lists.
    • Open addressing is space-efficient and offers better memory locality, but its performance degrades when the load factor is high or if the probing strategy results in clustering.

    The choice of collision resolution method should depend on factors like:

    • The expected load factor (how full the hash table will be).
    • The frequency of collisions.
    • The available memory.
    • The desired time complexity for operations.

    For example:

    • For a hash table with a low load factor and frequent insertions and deletions, open addressing (especially with double hashing) might be preferred.
    • For a hash table with a high load factor or when frequent rehashing isn't feasible, chaining might be a better choice.
    ← Previous topic 33
    Examples of Hash Functions
    Next topic 35 →
    Dynamic Programming

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Design and Analysis of Algorithms