From 8c4d3957227b141a6e93460908221407a1358255 Mon Sep 17 00:00:00 2001 From: Ankit Jaiswal Date: Wed, 28 Oct 2020 22:12:19 +0530 Subject: [PATCH 1/3] Create Hashing_CheatSheet.md --- DSA/Hashing_CheatSheet.md | 94 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 DSA/Hashing_CheatSheet.md diff --git a/DSA/Hashing_CheatSheet.md b/DSA/Hashing_CheatSheet.md new file mode 100644 index 0000000..f715e29 --- /dev/null +++ b/DSA/Hashing_CheatSheet.md @@ -0,0 +1,94 @@ +Hashing: Finding exact value in a faster way for present or future use. + +-> Direct Address Table Hashing: Simply it uses the array indices to match the value asked and check whether it is hit or not. + ~ Disadvantage: This technique fails for the larger value, floating-point values, string ,and other non-integer values. + + +-> Hashing Using Hash Function: We use a hash function that maps the universe of the key with some finite length table using some hash function. + +~ Few points to be kept in mind while selecting hash function : + + 1. It should always map a large Key to some small key. + 2. It should generate value from 0 to n-1, where n is the size of the table. + 3. It should make the operations like insert, delete and search faster, O(1) for integers and O(size) for a string of length 'size' on an average. + 4. It should uniformly distribute the keys into the Hash Table Slots. (MOST IMPORTANT) + +~ Few Hash Functions: + + 1. Using Modulo Property that modulo with any number M generates a number from 0 to M-1. + -> H(large_key) = large_key % M, where M is a prime number greater than hash table size. + + 2. We use weighted sum for the hash function of string. + -> str="abcd" => H(str) = str[0]*x^0 + str[1]*x^1 + str[2]*x^2 +str[3]*x^3. + + 3. Universal Hashing - It's a mixture of several hash functions. + + +@ Since the hashtable size is smaller than the set of universal, the collision of keys will take place. + +:) Following are the techniques to tackle collision problems:- + + ~ If the keys are known in advance we can use a technique called Perfect Hashing. (Wikipedia says: In computer science, a perfect hash function for a set S is a hash function that maps distinct elements in S to a set of integers, with no collisions). + + ~ But if we do not know the keys in advance then the following are the techniques which will help us :) + +1. Chaining -> It links the keys with same hash value as chain which can be implemented using a dynamic array or LinkedList or even using self-balancing BST. + + Keys = { 21, 58, 17, 54, 50, 102, 8, 159, 205,506, 711, 544, 989 } + H(key) = key%5 + ------ + 0 | 50 | -> 205 + ------ + 1 | 21 | -> 506 -> 711 + ------ + 2 | 17 | -> 102 + ------ + 3 | 58 | -> 8 + ------ + 4 | 54 | -> 159 -> 544 -> 989 + ------ + +2. Open Addressing -> When there is a collision it uses the next free slot of the Hash table for insertion. + Condition for Open addressing : - Number of slots in the hash table must be greater than or equal to the number of keys to be inserted. + Advantage: i) Cache Friendly, since it uses the hash table only. + + Keys = { 50, 51, 49, 16, 56, 15, 19 } + H(key) = key%7 + ------ + 0 | 49 | + ------ + 1 | 50 | + ------ + 2 | 51 | + ------ + 3 | 16 | + ------ + 4 | 56 | + ------ + 0 | 15 | + ------ + 1 | 19 | + ------ + + Types of Open Addressing Based on Efficiency: + i. Linear Probing - It searches linearly from the next empty slot when a collision happens. + + H(key,i)=[H(key)+i]%M , where H(key) is a hash function. + + -> Disadvantage: i) When there is a large number of collision clustering takes place which makes the searching costly. + + ii. Quadratic Probing - It searches in a Quadratic manner for The next empty slot when a collision happens. + + H(key,i)=[H(key)+i^2]%M , where H(key) is a hash function. + + -> Disadvantage: i) Clustering is reduced to some extent as secondary clustering takes place instead of primary. + ii) It may fail to find an empty slot even when the empty slot present. + - This problem can be removed by making the number of slots in the hash table more than twice of the number of keys to be inserted and taking M as a + prime number. + + iii. Double Hashing - To avoid primary and secondary clustering we use two hash functions instead of one. + + H(key,i)=[H1(key)+i*H2(key)]%M , where H1(key)and H2(key) are the hash function. + + +For Detailed Info: https://www.geeksforgeeks.org/hashing-data-structure/ From 78c73cc81ecefbda3f05a0e4a17fcdd58b724e6f Mon Sep 17 00:00:00 2001 From: Ankit Jaiswal Date: Wed, 28 Oct 2020 22:15:23 +0530 Subject: [PATCH 2/3] Update Hashing_CheatSheet.md --- DSA/Hashing_CheatSheet.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/DSA/Hashing_CheatSheet.md b/DSA/Hashing_CheatSheet.md index f715e29..4520084 100644 --- a/DSA/Hashing_CheatSheet.md +++ b/DSA/Hashing_CheatSheet.md @@ -38,15 +38,15 @@ Hashing: Finding exact value in a faster way for present or future use. H(key) = key%5 ------ 0 | 50 | -> 205 - ------ + ------ 1 | 21 | -> 506 -> 711 - ------ + ------ 2 | 17 | -> 102 - ------ + ------ 3 | 58 | -> 8 - ------ + ------ 4 | 54 | -> 159 -> 544 -> 989 - ------ + ------ 2. Open Addressing -> When there is a collision it uses the next free slot of the Hash table for insertion. Condition for Open addressing : - Number of slots in the hash table must be greater than or equal to the number of keys to be inserted. @@ -56,19 +56,19 @@ Hashing: Finding exact value in a faster way for present or future use. H(key) = key%7 ------ 0 | 49 | - ------ + ------ 1 | 50 | - ------ + ------ 2 | 51 | - ------ + ------ 3 | 16 | - ------ + ------ 4 | 56 | ------ 0 | 15 | - ------ + ------ 1 | 19 | - ------ + ------ Types of Open Addressing Based on Efficiency: i. Linear Probing - It searches linearly from the next empty slot when a collision happens. From b192fa4fc32f89b93c9515dec04b8f8bbd8010ac Mon Sep 17 00:00:00 2001 From: Ankit Jaiswal Date: Wed, 28 Oct 2020 22:33:21 +0530 Subject: [PATCH 3/3] Update Hashing_CheatSheet.md Indentation Fixed --- DSA/Hashing_CheatSheet.md | 187 ++++++++++++++++++++------------------ 1 file changed, 97 insertions(+), 90 deletions(-) diff --git a/DSA/Hashing_CheatSheet.md b/DSA/Hashing_CheatSheet.md index 4520084..3ed4193 100644 --- a/DSA/Hashing_CheatSheet.md +++ b/DSA/Hashing_CheatSheet.md @@ -1,94 +1,101 @@ -Hashing: Finding exact value in a faster way for present or future use. - --> Direct Address Table Hashing: Simply it uses the array indices to match the value asked and check whether it is hit or not. - ~ Disadvantage: This technique fails for the larger value, floating-point values, string ,and other non-integer values. - - --> Hashing Using Hash Function: We use a hash function that maps the universe of the key with some finite length table using some hash function. - -~ Few points to be kept in mind while selecting hash function : - - 1. It should always map a large Key to some small key. - 2. It should generate value from 0 to n-1, where n is the size of the table. - 3. It should make the operations like insert, delete and search faster, O(1) for integers and O(size) for a string of length 'size' on an average. - 4. It should uniformly distribute the keys into the Hash Table Slots. (MOST IMPORTANT) - -~ Few Hash Functions: - - 1. Using Modulo Property that modulo with any number M generates a number from 0 to M-1. - -> H(large_key) = large_key % M, where M is a prime number greater than hash table size. - - 2. We use weighted sum for the hash function of string. - -> str="abcd" => H(str) = str[0]*x^0 + str[1]*x^1 + str[2]*x^2 +str[3]*x^3. - - 3. Universal Hashing - It's a mixture of several hash functions. - - -@ Since the hashtable size is smaller than the set of universal, the collision of keys will take place. - -:) Following are the techniques to tackle collision problems:- - - ~ If the keys are known in advance we can use a technique called Perfect Hashing. (Wikipedia says: In computer science, a perfect hash function for a set S is a hash function that maps distinct elements in S to a set of integers, with no collisions). - - ~ But if we do not know the keys in advance then the following are the techniques which will help us :) - -1. Chaining -> It links the keys with same hash value as chain which can be implemented using a dynamic array or LinkedList or even using self-balancing BST. - - Keys = { 21, 58, 17, 54, 50, 102, 8, 159, 205,506, 711, 544, 989 } - H(key) = key%5 - ------ - 0 | 50 | -> 205 - ------ - 1 | 21 | -> 506 -> 711 - ------ - 2 | 17 | -> 102 - ------ - 3 | 58 | -> 8 - ------ - 4 | 54 | -> 159 -> 544 -> 989 - ------ - -2. Open Addressing -> When there is a collision it uses the next free slot of the Hash table for insertion. - Condition for Open addressing : - Number of slots in the hash table must be greater than or equal to the number of keys to be inserted. - Advantage: i) Cache Friendly, since it uses the hash table only. - - Keys = { 50, 51, 49, 16, 56, 15, 19 } - H(key) = key%7 - ------ - 0 | 49 | - ------ - 1 | 50 | - ------ - 2 | 51 | - ------ - 3 | 16 | - ------ - 4 | 56 | - ------ - 0 | 15 | - ------ - 1 | 19 | - ------ - - Types of Open Addressing Based on Efficiency: - i. Linear Probing - It searches linearly from the next empty slot when a collision happens. - - H(key,i)=[H(key)+i]%M , where H(key) is a hash function. - - -> Disadvantage: i) When there is a large number of collision clustering takes place which makes the searching costly. - - ii. Quadratic Probing - It searches in a Quadratic manner for The next empty slot when a collision happens. - - H(key,i)=[H(key)+i^2]%M , where H(key) is a hash function. - - -> Disadvantage: i) Clustering is reduced to some extent as secondary clustering takes place instead of primary. - ii) It may fail to find an empty slot even when the empty slot present. - - This problem can be removed by making the number of slots in the hash table more than twice of the number of keys to be inserted and taking M as a - prime number. - - iii. Double Hashing - To avoid primary and secondary clustering we use two hash functions instead of one. - +# Hashing: + Short Intro -> Finding exact value in a faster way for present or future use. + + -> Direct Address Table Hashing: Simply it uses the array indices to match the value asked and check whether it is hit or not. + ~ Disadvantage: This technique fails for the larger value, floating-point values, string ,and other non-integer values. + + + -> Hashing Using Hash Function: We use a hash function that maps the universe of the key with some finite length table using some hash function. + + ~ Few points to be kept in mind while selecting hash function : + + 1. It should always map a large Key to some small key. + 2. It should generate value from 0 to n-1, where n is the size of the table. + 3. It should make the operations like insert, delete and search faster, O(1) for integers and O(size) for a string of length 'size' on an average. + 4. It should uniformly distribute the keys into the Hash Table Slots. (MOST IMPORTANT) + + ~ Few Hash Functions: + + 1. Using Modulo Property that modulo with any number M generates a number from 0 to M-1. + -> H(large_key) = large_key % M, where M is a prime number greater than hash table size. + + 2. We use weighted sum for the hash function of string. + -> str="abcd" => H(str) = str[0]*x^0 + str[1]*x^1 + str[2]*x^2 +str[3]*x^3. + + 3. Universal Hashing - It's a mixture of several hash functions. + + +# Since the hashtable size is smaller than the set of universal, the collision of keys will take place. + + :) Following are the techniques to tackle collision problems:- + + ~ If the keys are known in advance we can use a technique called Perfect Hashing. (Wikipedia says: In computer science, a perfect hash function for a set S is a hash function that maps distinct elements in S to a set of integers, with no collisions). + + ~ But if we do not know the keys in advance then the following are the techniques which will help us :) + +# Chaining + Short Intro -> It links the keys with same hash value as chain which can be implemented using a dynamic array or LinkedList or even using self-balancing BST. + + Keys = { 21, 58, 17, 54, 50, 102, 8, 159, 205,506, 711, 544, 989 } + H(key) = key%5 + ------ + 0 | 50 | -> 205 + ------ + 1 | 21 | -> 506 -> 711 + ------ + 2 | 17 | -> 102 + ------ + 3 | 58 | -> 8 + ------ + 4 | 54 | -> 159 -> 544 -> 989 + ------ + +# Open Addressing + + Short Intro -> When there is a collision it uses the next free slot of the Hash table for insertion.    + Condition for Open addressing : - Number of slots in the hash table must be greater than or equal to the number of keys to be inserted.    + Advantage: i) Cache Friendly, since it uses the hash table only. + + Keys = { 50, 51, 49, 16, 56, 15, 19 } + H(key) = key%7 + ------ + 0 | 49 | + ------ + 1 | 50 | + ------ + 2 | 51 | + ------ + 3 | 16 | + ------ + 4 | 56 | + ------ + 0 | 15 | + ------ + 1 | 19 | + ------ + + Types of Open Addressing Based on Efficiency:    + i. Linear Probing - It searches linearly from the next empty slot when a collision happens. + + H(key,i)=[H(key)+i]%M , where H(key) is a hash function. + + -> Disadvantage: i) When there is a large number of collision clustering takes place which makes the searching costly. + + ii. Quadratic Probing - It searches in a Quadratic manner for The next empty slot when a collision happens. + + H(key,i)=[H(key)+i^2]%M , where H(key) is a hash function. + + -> Disadvantage: i) Clustering is reduced to some extent as secondary clustering takes place instead of primary.         + ii) It may fail to find an empty slot even when the empty slot present.     + - This problem can be removed by making the number of slots in the hash table more than twice of the number of keys to be inserted and taking M as a + prime number. + +# Double Hashing + + Short Intro-> To avoid primary and secondary clustering we use two hash functions instead of one. + H(key,i)=[H1(key)+i*H2(key)]%M , where H1(key)and H2(key) are the hash function. For Detailed Info: https://www.geeksforgeeks.org/hashing-data-structure/ +