580 likes | 598 Views
Hashing in Action. Nattee Niparnan. Operation. Insert Depends on Find Delete Depends on Find, also. Find. Find a room that should contain the value. Simple Hash. Room index. Hash Function. Name. Trying to add. Add Alice. 1. Hash Function. Alice. Add Alice. 1. Hash Function.
E N D
Hashing in Action Nattee Niparnan
Operation • Insert • Depends on Find • Delete • Depends on Find, also
Find • Find a room that should contain the value
Simple Hash Room index Hash Function Name
Add Alice 1 Hash Function Alice
Add Alice 1 Hash Function Alice
Add Peter 9 Hash Function Peter
Add Bob 2 Hash Function Bob
Find Vicky 7 Hash Function Vicky
Add Vicky 7 Hash Function Vicky
Two approach • Separate chaining • Create another “space” for the collision • Need more memory • Open addressing • Try to squeeze in another room • Does not need more memory • Deletion problem
Separate chaining • Try adding • The new 3 guys will collide with “Alice”
Add Adam Adam Collide!! 1 Hash Function
We use “LinkedList” as the storage Adam Alice 1 Bob Hash Function Vicky Peter
Add Adam as a new node Adam Alice Adam 1 Bob Hash Function Vicky Peter
Add Able as a new node Able Alice Adam 1 Bob Hash Function Able Vicky Peter
Open Addressing • Find another room • Linear Probing (LP) • Quadratic Probing (QP) • Double Hashing (DH)
Open Addressing Mechanism • Each time a collision occur, • Count the number of collision • “ColCount” • Instead of using Hash1(x) as the room index • Use instead “Hash1(x) + F(ColCount)” • F(ColCount) is the open addressing function
Linear Probing • Use next room • F(ColCount) = ColCount;
Add Adam in LP Adam Collide!! 1 Hash Function
Add Adam in LP Adam 2 Hash Function Update!!
Add Adam in LP Adam 2 Collide!! Again Hash Function
Add Adam in LP Adam 3 Hash Function Update!!
Add Adam in LP Adam 3 Hash Function Free Space
Add Able in LP Able Collide!! 1 Hash Function
Add Able in LP Able 2 Hash Function Update!!
Add Able in LP Able 2 Collide!! Again Hash Function
Add Able in LP Able 3 Hash Function Update!!
Add Able in LP Able 3 Hash Function Collide Again
Add Able in LP Able 4 Hash Function Free Space Update!!
Quadratic Probing • Jump by square • F(ColCount) = ColCount * ColCount;
Add Adam in QP Adam Collide!! 1 Hash Function
Add Adam in QP Adam 2 Hash Function Update!!
Add Adam in QP Adam 2 Collide!! Again Hash Function
Add Adam in QP Adam 5 Hash Function Update!!
Add Adam in QP Adam 5 Hash Function Free Space
Add Able in QP Able Collide!! 1 Hash Function
Add Able in QP Able 2 Hash Function Update!!
Add Able in QP Able 2 Collide!! Again Hash Function
Add Able in QP Able 5 Hash Function Update!!
Add Able in QP Able 5 Hash Function Collide Again!!
Add Able in QP Able 10 Hash Function Update!!
Add Able in QP Able 5 Hash Function Free Space
Double Hashing • Jump by another hash function • F(ColCount) = ColCount * Hash2(x);