Skip to main content
Engineering LibreTexts

11.4: Hashing- Separate Chaining

  • Page ID
    34697
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    What is Collision?
    Since a hash function gets us a small number for a key which is a big integer or string, there is a possibility that two keys result in the same value. The situation where a newly inserted key maps to an already occupied slot in the hash table is called collision and must be handled using some collision handling technique.

    What are the chances of collisions with large table?
    Collisions are very likely even if we have big table to store keys. An important observation is Birthday Paradox. With only 23 persons, the probability that two people have the same birthday is 50%.

    How to handle Collisions?
    There are mainly two methods to handle collision:
    1) Separate Chaining
    2) Open Addressing
    In this article, only separate chaining is discussed. We will be discussing Open addressing in the next post.

    Separate Chaining:
    The idea is to make each cell of hash table point to a linked list of records that have same hash function value.

    Let us consider a simple hash function as “key mod 7” and sequence of keys as 50, 700, 76, 85, 92, 73, 101.
    Separate chaining when collisions occur

    Advantages:

    1. Simple to implement.
    2. Hash table never fills up, we can always add more elements to the chain.
    3. Less sensitive to the hash function or load factors.
    4. It is mostly used when it is unknown how many and how frequently keys may be inserted or deleted.

    Disadvantages:

    1. Cache performance of chaining is not good as keys are stored using a linked list. Open addressing provides better cache performance as everything is stored in the same table.
    2. Wastage of Space (Some Parts of hash table are never used)
    3. If the chain becomes long, then search time can become O(n) in the worst case.
    4. Uses extra space for links.

    There is also a video that covers Separate Chaining

    "Hashing | Set 2 (Separate Chaining)" by Connor Fehrenbach is licensed under CC BY-SA 4.0


    This page titled 11.4: Hashing- Separate Chaining is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.

    • Was this article helpful?