zoukankan      html  css  js  c++  java
  • Hash Table Collision Handling

    Two basic methods; separate chaining and open address.

    Separate Chain

    Hangs an additional data structure off of the buckets.  For example the bucket array becomes an array of link list.  So to find an item we first go to the bucket then compare keys..  This is a popular method, and if link list is used the hash never fills up.

    Illustrate

    load factorn/N where n is number of items stored in the hash table. Like for the load factor to be less then 1.

    The cost for get(k) is on average O(n/N)

    Open Addressing

    The problem with separate chaining is that the data structure can grow with out bounds.  Sometimes this is not appropriate because of finite storage, for example in embedded processors.

    Open addressing does not introduce a new structure.  If a collision occurs then we look for availability in the next spot generated by an algorithm. Open Addressing is generally used where storage space is a premium, i.e. embedded processors. Open addressing not necessarily faster then separate chaining.

    Methods for Open Addressing:

      1. Linear Probing:

        We try to insert Item = (ke) into bucket A[i] and find it full so the next bucket we try is:

        A[(i + 1) mod N]

        then try A[(i + 1) mod N], etc.

        Illustrate with 11 buckets: Note the probing is linear.

        Note the hash table can be filled up.

        Also what to do if we remove an Item.  Should repair the array A but this is too costly.  Instead we mark the bucket as available/deactivated.  Then the next use of findElement(k) would skip over the available/deactivated bucket.  insertItem(ke) would insert into a available/deactivated.

        Clustering slows down searches. 

      2. Quadratic Probing:

        A[ (i + f(j) )mod N]  where j = 0, 1, 2, ... and f(j) = j2

        Helps avoids clustering.  Secondary clustering can occur. We can imagine a more complicated function for f

      3. Double Hashing:

        Use a second hash function h'.

        A[ (i + f(j) )mod N]   where  f(j) = j*h'(k) should not evaluate to zero. Example: h'(k) = q - (k mod q).  Note that still i = h(k).

    http://www.csl.mtu.edu/cs2321/www/newLectures/17_Hash_Tables_Collisions.html

  • 相关阅读:
    3927Circular Sequence 思维题(求环形最大子列和)
    Rotational Painting(hdu 3685 凸包+多边形重心 模板题
    模拟 3897: Catch the Mouse
    L3-010 是否完全二叉搜索树 (30分)
    已知两种遍历顺序 推剩下的一种
    进阶实验4-3.3 完全二叉搜索树 (30分)->排序得出搜索树中序遍历->已知搜索树中序求层序
    任意进制转化/模板(c++/ java)
    4038: Robot Navigation --bfs(求最短路及其路径条数)
    A Simple Math Problem(hdu 5974 数论题
    LCM Walk(hdu 5584;数论题
  • 原文地址:https://www.cnblogs.com/grainy/p/7244183.html
Copyright © 2011-2022 走看看