From 12473b72ccfad5c95a0b372be1fb8734bb700d1b Mon Sep 17 00:00:00 2001 From: "Rafael J. Rodriguez" Date: Sat, 28 May 2016 13:22:38 -0400 Subject: [PATCH] Markdown fixes for hash tables --- Hash-Tables-And-Hashing-Functions.md | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/Hash-Tables-And-Hashing-Functions.md b/Hash-Tables-And-Hashing-Functions.md index a277d1cd6e..d9b1547426 100644 --- a/Hash-Tables-And-Hashing-Functions.md +++ b/Hash-Tables-And-Hashing-Functions.md @@ -1,6 +1,6 @@ # Hash Tables and Hashing Functions -## Introduction to hashing +### Introduction to hashing Hashing is designed to solve the problem of needing to efficiently find or store an item in a collection. For example, if we have a list of 10,000 words of English and we want to check if a given word is in the list, it would be inefficient to successively compare the word with all 10,000 items until we find a match. Even if the list of words are lexographically sorted, like in a dictionary, you will still need some time to find the word you are looking for. @@ -17,6 +17,7 @@ Generally, these hash codes are used to generate an index, at which the value is In hash tables, you store data in forms of key and value pairs. The key, which is used to identify the data, is given as an input to the hashing function. The hash code, which is an integer, is then mapped to the fixed size we have. Hash tables have to support 3 functions. + - insert (key, value) - get (key) - delete (key) @@ -54,20 +55,22 @@ Position | Keys array | Values array 11 | Switzerland | Berne -Now, in this specific example things work quite well. -Our array needs to be big enough to accommodate the longest string, but in this case that’s only 11 slots. -And we do waste a bit of space because, for example, there are no 1-letter keys in our data, nor keys between 8 and 10 letters. But in this case, the wasted space isn’t so bad either. Taking the length of a string is nice and fast, and so is the process of finding the value associated with a given key (certainly faster than doing up to five string comparisons). +Now, in this specific example things work quite well. +Our array needs to be big enough to accommodate the longest string, but in this case that’s only 11 slots. +We do waste a bit of space because, for example, there are no 1-letter keys in our data, nor keys between 8 and 10 letters. But in this case, the wasted space isn’t so bad either. Taking the length of a string is nice and fast, and so is the process of finding the value associated with a given key (certainly faster than doing up to five string comparisons). But, what do we do if our dataset has a string which has more than 11 characters? -What if we have one another word with 5 characters, "India", and try assigning it to an index using our hash function. Since the index 5 is already occupied, we have to make a call on what to do with it. This is called a collision. +What if we have one another word with 5 characters, "India", and try assigning it to an index using our hash function. Since the index 5 is already occupied, we have to make a call on what to do with it. This is called a collision. + If our dataset had a string with thousand characters, and you make an array of thousand indices to store the data, it would result in a wastage of space. If our keys were random words from English, where there are so many words with same length, using length as a hashing function would be fairly useless. ## Collision Handling Two basic methods are used to handle collisions. - 1. Separate Chaining - 2. Open Addressing + +1. Separate Chaining +2. Open Addressing #### Separate Chaining @@ -96,9 +99,10 @@ The problem with separate chaining is that the data structure can grow with out Open addressing does not introduce any new data 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 restricted, i.e. embedded processors. Open addressing not necessarily faster then separate chaining. Methods for Open Addressing - - [Linear Probing](https://en.wikipedia.org/wiki/Linear_probing) - - [Quadratic Probing](https://en.wikipedia.org/wiki/Quadratic_probing) - - [Double Hashing](https://en.wikipedia.org/wiki/Double_hashing) + +- [Linear Probing](https://en.wikipedia.org/wiki/Linear_probing) +- [Quadratic Probing](https://en.wikipedia.org/wiki/Quadratic_probing) +- [Double Hashing](https://en.wikipedia.org/wiki/Double_hashing) ## How to use hashing in your code. @@ -164,4 +168,4 @@ Methods for Open Addressing - [Bloom Filters](https://www.youtube.com/watch?v=-SuTGoFYjZs) - [Hashing Strategies](https://www.youtube.com/watch?v=D65JQ0qQwZk) - [Password Hashing](https://crackstation.net/hashing-security.htm) -- [Difference between Hashing and encrypting](http://stackoverflow.com/questions/326699/difference-between-hashing-a-password-and-encrypting-it) \ No newline at end of file +- [Difference between Hashing and encrypting](http://stackoverflow.com/questions/326699/difference-between-hashing-a-password-and-encrypting-it)