12. Hash Tables 2

Hash functions

A good hash function satisfies the assumption of simple uniform hashing:
Each key is equally likely to hash to any of the m slots in the hash table, independently of where any other key has hashed to.

Most hash functions assume that the universe of keys is the set N = {0, 1, 2, . . .}. We here consider the following schemes of hash function creation.

  1. Hashing by Division
  2. Hashing by Multiplication
  3. Universal Hashing

Hashing by Division

Map a key k into one of the m slots of a hash table by taking the remainder of k divided by m.

h(k) = k mod m

E.g., m = 23 and the key k = 107, then h(k) = 15.

Multiplication method

Step1. Multiply the key k by a constant A in the range 0<A<1, and extract the fractional part of k*A.
Step 2. Multiply this value by m and take the floor of the result.

h(k) = ?m · ((k*A) mod 1)?

The advantage of this method is that the value choice of m is not critical.

Universal hashing

Universal hashing is to select the hash function at random and at run time from a carefully designed collection of hash functions.

Let H = {h1,h2,...,hl} be a finite collection of hash functions that map a given universe U of keys into a range {0,1,...,m?1}.


以上都是建立在非Open Address上烟瞧。(相同的key存儲在一個linked list里)

Open Addressing

All elements in open addressing are stored in the hash table itself.

The sequence of positions probed depends on the key being inserted.
To determine which slots to probe, we extend the hash function to include the probe number as a second input.

So that every hash table position is eventually considered as a slot for a new key as the table fills up.

Probing

Linear Probing
Given an ordinary hash function h′:U → {0,1,...,m?1}, linear probing uses the hash function

h(k,i) = (h′(k)+i) mod m, for i = 0,1...,m?1.

描述:Given key k, the 1st slot is T [h′(k)], the 2nd slot is T [h′(k) + 1], and so on up to slot T [m ? 1]. Then, we wrap around to slots T [0], T [1], . . . until we finally probe slot T [h′(k) ? 1].

Disadvantage: the primary clustering problem.

Quadratic probing
uses a hash function of the form

h(k, i) = (h′(k) + c1i + c2i^2) mod m,
where h′ is an auxiliary hash function, c1 and c2 are constants.

Also, if two keys have the same initial probe position, then their probe sequences are the same since h(k1,0) = h(k2,0) implies h(k1,i) = h(k2,i).

Disadvantage: the secondary clustering problem.

在做題的時候放可,默認c1 = 0抚吠,具體的增量應該是 -1稻据,1,-4,4,-9特漩,9,……
具體步驟

Double hashing
One of the best methods available for open addressing because the permutations produced have many of the characteristics of randomly chosen permutations.

h(k,i) = [h1(k)+i·h2(k)] mod m,
where h1 and h2 are auxiliary hash functions.

  1. The initial position is T [h1(k)]
  2. A successive probe position is offset from its previous position by the amount h2(k) modulo m.
  3. The value h2(k) must be relatively prime to the hash table size m for the entire hash table to be searched.
  4. As we vary the value of key k, the initial probe position and the offset may vary independently.

Double hashing represents an improvement over linear or quadratic probings in that Θ(m^2) probe sequences, rather than Θ(m), are used, since each possible (h1(k), h2(k)) pair yields a distinct probe sequence with 0 ≤ h1(k), h2(k) ≤ m ? 1

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末犹菱,一起剝皮案震驚了整個濱河市拾稳,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌腊脱,老刑警劉巖访得,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡悍抑,警方通過查閱死者的電腦和手機鳄炉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來搜骡,“玉大人拂盯,你說我怎么就攤上這事〖敲遥” “怎么了谈竿?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長摸吠。 經(jīng)常有香客問我空凸,道長,這世上最難降的妖魔是什么寸痢? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任呀洲,我火速辦了婚禮,結果婚禮上啼止,老公的妹妹穿的比我還像新娘道逗。我一直安慰自己,他們只是感情好献烦,可當我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布滓窍。 她就那樣靜靜地躺著,像睡著了一般巩那。 火紅的嫁衣襯著肌膚如雪贰您。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天拢操,我揣著相機與錄音,去河邊找鬼舶替。 笑死令境,一個胖子當著我的面吹牛,可吹牛的內容都是我干的顾瞪。 我是一名探鬼主播舔庶,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼陈醒!你這毒婦竟也來了惕橙?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤钉跷,失蹤者是張志新(化名)和其女友劉穎弥鹦,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡彬坏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年朦促,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片栓始。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡务冕,死狀恐怖,靈堂內的尸體忽然破棺而出幻赚,到底是詐尸還是另有隱情禀忆,我是刑警寧澤,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布落恼,位于F島的核電站箩退,受9級特大地震影響,放射性物質發(fā)生泄漏领跛。R本人自食惡果不足惜乏德,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望吠昭。 院中可真熱鬧喊括,春花似錦、人聲如沸矢棚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蒲肋。三九已至蘑拯,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間兜粘,已是汗流浹背申窘。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留孔轴,地道東北人剃法。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像路鹰,于是被迫代替她去往敵國和親贷洲。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,475評論 0 23
  • 弟弟比姐姐矮了許多晋柱,我們帶他去上河殴梗看內分泌專家門診,專家建議住院系統(tǒng)檢查雁竞。一聽說要住院钦椭,金豆子就掛在眼里,垂垂欲墜...
    恬隆閱讀 464評論 7 2
  • 9.19--9.23 第7章 正則表達式 正則表達式是一個拆分字符串并查詢相關信息的過程。 推薦練習網(wǎng)站: js ...
    如201608閱讀 1,024評論 0 4
  • 材料:白蘿卜1個玉凯、五花肉100克势腮、尖紅椒4個、青尖椒3個漫仆、青蒜5根 調料:植物油捎拯、鹽、雞精盲厌、生抽署照、蠔油、料酒吗浩、干紅...
    蔚藍色的天空c閱讀 223評論 0 0