Hash Table (2024)

Highlights of This Lab:

  • Definition of a Hash Table
  • Application: Looking up Passwords

Lab Exercise:


Click the little computer above for a detailed description.
For this excercise you will be asked to implement a password checking system to authenticate a users password. 7

1. Definition of a Hash Table

Before we get into the definition of Hash Tables, it is good to introduce WHY to use Hash tables.

Hash tables are good for doing a quick search on things.

For instance if we have an array full of data (say 100 items). If we knew the position that a specific item is stored in an array, then we could quickly access it. For instance, we just happen to know thatthe item we want it is at position 3;I can apply:
myitem=myarray[3];

With this, we don't have to search through each element in the array, we just access position 3.

The question is, how do we know that position 3 stores the data that we are interested in?

This is where hashing comes in handy. Given some key, we can apply a hash function to it to find an index or position that we want to access.

Hash Table (2)

1.1 What is the hash function?

There are many different hash functions. Some hash functions will take an integer key and turn it into an index.A common one is the division method.

Let's learn through an example:

1.2 Division method (one hash method for integers)

Let's say you had the following numbers or keys that you wanted to map into anarray of 10 elements:

123456
123467
123450

To apply the division method, you could divide the number by 10 (or the maximum number of elements in the array)and use the remainder (the modulo) as an index. The following would result:

123456 % 10 = 6 (the remainder is 6 when dividing by 10)
123467 % 10 = 7 (the remainder is 7)
123450 % 10 = 0 (the remainder is 0)

These numbers would be inserted into the array at positions 6, 7, and 0 respectively. It might look something like this:

Hash Table (3)

The important thing with the division method is that the keys are integers.

1.3 What happens when the keys aren't integers?

You have to apply another hash function to turn them into integers. Effectively, you get two hash functions in one:

  1. function to get an integer
  2. function to apply a hash method from above to get an index to an array

Hash Table (4)

What do we mean that the keys aren't integers? Well, let's say that the keys are people's names. Such as:

Sarah Jones
Tony Balognie
Tom Katz

The goal is to type in one of these names and get an index to an array inorder to access that information. How do we do this?
The hash function has to do two things:

  1. Convert the names into integers
      For instance, we have a function which turns a string intoan integer. The results will be as follows:
      Sarah Jones --> 1038
      Tony Balognie --> 1259
      Tom Katz --> 746
  2. Apply a hash method to get an index
      We can now apply the division method to get an index foran array of 10 elements
      Sarah Jones --> 1038 % 10 --> 8
      Tony Balognie --> 1259 % 10 --> 9
      Tom Katz --> 746 % 10 --> 6

1.4 What would that look like in the array?

The array is known as a hash table. It stores the key (used to find theindex) along with associated values. In the above example, we might have ahash table that looked something like this:

Hash Table (5)

Again, the idea is that we will insert items into the hash table using the key and applying the hash function(s) to get the index.

A problem occurs when two keys yield the same index. For Instance, say we wanted to include:
John Smith --> 948 % 10 --> 8

We have a collision because Sarah Jones is already stored at array index 8.

We need a method to resolve this. The resolution comes in how you create your hash table. There two major approaches given in the book:

  1. Linear Probing
  2. Chaining
The approach used in this lab is referred to as chaining.

The details are left as class material, but recognize that in chaining you have an array of linked lists. All the data in the "same link", have colliding index values.

Consider a diagram of the above example. Remember, there was a collision with Sarah Jones and John Smith.Notice that John Smith is "chained" or "linked" after Sarah Jones.

Hash Table (6)

1.5 Applications of a Hash Table

Hash tables are good in situations where you have enormous amounts of data from which you would like to quickly search and retrieve information. A few typical hash table implementations would be in the following situations:
  • For driver's license record's. With a hash table, you could quicklyget information about the driver (ie. name, address, age)given the licence number.
  • For compiler symbol tables. The compiler uses a symbol table tokeep track of the user-defined symbols in a C++ program.This allows the compiler to quickly look up attributesassociated with symbols (for example, variable names)
  • For internet search engines. For more information, click here
  • For telephone book databases. You could make use of a hash table implementatationto quickly look up John Smith's telephone number.
  • For electronic library catalogs. Hash Table implementationsallow for a fast find among the millions of materials stored in the library.
  • For implementing passwords for systems with multiple users. Hash Tables allow for a fast retrieval of the password which corresponds to a given username.

1.6 Typical Operations of a Hash Table

The functions associated with our implementation of the hash table are the following:
  • bool isEmpty()
    Returns true if the hash table is empty. Otherwise, returns false
  • bool isFull()
    Returns true if the hash table is full. Otherwise, returns false
  • void insert (const DT &newDataItem)
    Inserts newDataItem into the appropriate list in the hash table. The location (index) in the hash table is determined by the key and the hash function.
  • bool remove (KF searchkey)
    Searches the hash table for the data item with the key searchKey. If the data item is found, then removes the data item and returns true. Otherwise, returns false.
  • bool retrieve (KF searchkey, DT &dataItem)
    Searches the hash table for the data item with the key searchKey. If the data item is found, then copies the data item to dataItem and returns true. Otherwise, returns false.
  • void clear()
    Removes all data items in the hash table.
  • void showStructure()
    Outputs the data items in a hash table. If the hash table is empty, outputs, "Empty hash table". This is meant for testing/debugging purposes.

2. Application: Looking up Passwords

The following section outlines an algorithm for authenticating a user's password. Later, in the lab exercise, you will be given the skeleton code and asked to add lines to make it work.

One possible use for a hash table is to store computer user login usernames and passwords.

There are two major steps to this program:
  1. The program will load username/password sets from the file password.dat and insert them into the hash table until the end of file is reached on password.dat. The password.dat file will look something like this with one username/password set per line:
    jackbroken.crownjilltumblin'downmarycontrarybopeepsheep!lost
  2. The program will then present a login prompt, read one username, present a password prompt, and after looking up the username's password in the hash table, will print either "Authentication successful" or "Authentication failure". The output might look something like this:
    Login: maryPassword: contraryAuthentication successfulLogin: jim Password: contraryAuthentication failureLogin: bopeepPassword: sheeplostAuthentication failure
Step 2 will be repeated until the end of the input data (EOF) is reached on the console input stream (cin). The EOF character on the PC's is the CTRL Z character.

Let's fill in some of the details:

To convert from a string to an integer, we can add the ascii value of each character together. For instance, mary's conversion from string to integer might look something like this:
109('m') + 97('a') + 114('r') + 121('y')=441
The code will look like this:
 int hash(const string str) const { int val = 0; for (int i=0; i<str.length(); i++) val += str[i]; return val; }
We've converted the string to an integer, but we still need to convert theinteger to an index. For an array of 10 elements we can divide by 10 and usethe remainder as an index. Combining these two hash functions, we will get some code that looks like this:
 int index = dataItem.hash ( searchKey ) % 10;
Therefore mary's index will be:
 441 % 10 = 1.

3. Lab Exercise


  • Get the files:
    hashtbl.cpphashtbl.hlistlnk.cpplistlnk.hlogin.cpppassword.datAll of the above files zipped in a folder
  • There are six files used in this program:
    • hashtbl.cpp and hashtble.h --contain the implementation of the hashtable class
    • listlnk.cpp and listlnk.h --contain the implementation of linked lists class
    • login.cpp -- the main program. This contains thePassword structure, which is inserted intothe hashtable.
    • password.dat -- contains all the users and corresponding passwords.

    Your primary tasks for this exercise are to edit the login.cpp toadd in lines so that it does the following:

    1. insert passwords into the Hash Table
    2. retrieve one user's Password structure from the Hash Table
    3. check to see if the user is in the table and compare retrieved user password to input password, print "Authentication failure" or "Authentication successful"

    Steps include:

  • Try to run this program. You should find that it will prompt you for "Login:" and "Password:" (type in random words at these prompts). You will notice that it continuely cycles around asking you for this information.
    To stop the program from running, at the "Login:" prompt, type CTRL and z (simultaneously) and then the Enter key .
  • Add in a line to insert passwords into the table. Hint: notice that the name of the hashtable is passwords and that you want to insert a Password structure called tempPass into the hashtable.
  • Add in a line to print out the hash table. Hint: the hashtable is passwords and there is a member function called showStructure.
  • Build and Run this program. If all is working well, you should get some output that looks like this:
    The Hash Table has the following entries0: _1: mary2: _3: _4: _5: bopeep6: _7: jill8: _9: jackLogin:
    This shows the hash table that has resulted from inserting data from the password.dat file (mentioned in Section 2). Notice that mary is at index 1, just as we predicted (in Section 2).
  • Add lines to compare the true password to the input password and print "Authentication failure" or "Authentication successful". Hint: Compare the input password (pass) to the password within the tempPass object (which has been retrieved).
  • Build and Run your program. You should get results like the following:
    Login: maryPassword: contraryAuthentication successfulLogin: jim Password: contraryAuthentication failureLogin: bopeepPassword: sheeplostAuthentication failure

    Test Plan for "Login" Simulation Program

    Login: Password: Authentication
    Predicted
    Result
    mary contrary successful
    jim contrary failure
    bopeep sheeplost failure
    bopeep sheep!lost successful

    When you are finished, your program should:

    1. Read the list of username and password combinations from the data file and insert them into the hash table
    2. Print out all of the names in the hash table
    3. Take a username and password input from the user
    4. Check if the username is in the table, and if the password matches the passsword associated with that username
    5. Print out a success message if everything matches, print out a failure message if anything does not match

    A sample output could look like this:

    Hash Table (7)

    4. Postlab Exercises

    For postlab exercices, click here.
    CS Dept Home Page
    Back to 210 Lab Outline

    © Copyright: Department of Computer Science, University of Regina.
Hash Table (2024)

FAQs

Hash Table? ›

A hash table is a data structure that is used to store keys/value pairs. It uses a hash function to compute an index into an array in which an element will be inserted or searched.

What is the difference between HashMap and HashTable? ›

HashMap and HashTable are both key-value storage classes in Java. HashMap is non-synchronized, making it faster for single-threaded tasks, while HashTable is inherently synchronized, providing thread safety. HashTable doesn't allow any null keys or values, but HashMap lets you have one null key and several null values.

Why is it called a hash table? ›

In French, a hash table is called "table de hachage", the related verb "hacher" means to chop/to mince (food mostly). The verb to hash has the same meaning in English. So as other have pointed out it is called hash, because you chop your input that you put in pieces in different places (your table entries).

What is hashing with an example? ›

Hashing is the process of transforming any given key or a string of characters into another value. This is usually represented by a shorter, fixed-length value or key that represents and makes it easier to find or employ the original string. The most popular use for hashing is the implementation of hash tables.

What is the difference between hash table and list? ›

A list is typically ordered whereas a hashtable is not. So when you add elements into a list and expect the ordering to remain consistent then an array retains the ordering while a hashtable gives no guarantee as to the order you get back out.

Is Hashtable obsolete? ›

The new Hashtable object has an initial capacity equal to the number of elements copied, and uses the default load factor, and the specified hash code provider and comparer. This API is obsolete.

Why Hashtable is faster than HashMap? ›

However, since synchronization checks are unnecessary, HashMap is quicker than Hashtable. Hashtable is thread-safe because it is synchronized. This indicates that concurrent access by several threads to a hashtable is not problematic. However, since synchronization checks are required, Hashtable is slower than HashMap.

What is a real life example of hashing function? ›

Every time you attempt to log in to your email account, your email provider hashes the password YOU enter and compares this hash to the hash it has saved. Only when the two hashes match are you authorized to access your email.

What are the three types of hashing? ›

Types of Hash functions
  • Division Method.
  • Mid Square Method.
  • Folding Method.
  • Multiplication Method.

What is hashing mostly used for? ›

Hashing is a function used to map data to a fixed-length value. Businesses use hashing in authentication systems and to validate different types of data, such as files and documents. Understanding what hashing is and how it's used is important because it can help to prevent data breaches and protect stored information.

When shouldn't you use a hash table and why? ›

Items in a hash table are ordered by their hash value, which isn't an order that makes any sense to people. If you want to traverse the contents in order, then a hash table isn't a good choice.

When should you use a hash table? ›

Use Hash tables when we need to store key-value pairs and perform frequent lookup, insert, or delete operations. Use Sets when we need to store unique elements, don't care about duplicates, and just need to perform operations like checking if an element is in the set or not.

Why use a hash table instead of an array? ›

Hash tables are more flexible than arrays, as they allow for dynamic resizing to accommodate new data, but they also have more overhead in terms of memory usage.

What is the difference between HashMap and Hashset and Hashtable? ›

Hashtable and HashMap both implement Map , HashSet implements Set , and they all use hash codes for keys/objects contained in the sets to improve performance. Hashtable is a legacy class that almost always should be avoided in favor of HashMap .

Does HashMap allow duplicate keys? ›

A HashMap does not allow duplicate keys, but you can have duplicate values, mapped to different keys.

What is the difference between HashMap and Hashtree? ›

HashMap implements Map interface while TreeMap implements SortedMap interface. A Sorted Map interface is a child of Map. HashMap implements Hashing, while TreeMap implements Red-Black Tree(a Self Balancing Binary Search Tree). Therefore all differences between Hashing and Balanced Binary Search Tree apply here.

What is the difference between HashMap and ConcurrentHashMap? ›

HashMap isn't thread-safe at all. Thus, it is non-synchronized in nature. The ConcurrentHashMap, on the other hand, is thread-safe. Due to non-synchronization, the performance of HashMap is relatively higher, and various threads are capable of performing simultaneously.

Top Articles
Best Banks in India: 25 Reasons That Make Us a Preferred Choice
How to Open Bitlocker Encrypted USB Drive on Another Computer
Mchoul Funeral Home Of Fishkill Inc. Services
35105N Sap 5 50 W Nit
Tripadvisor Near Me
Available Training - Acadis® Portal
Simplify: r^4+r^3-7r^2-r+6=0 Tiger Algebra Solver
Skyward Login Jennings County
Uky Linkblue Login
Honda cb750 cbx z1 Kawasaki kz900 h2 kz 900 Harley Davidson BMW Indian - wanted - by dealer - sale - craigslist
Spider-Man: Across The Spider-Verse Showtimes Near Marcus Bay Park Cinema
How to Create Your Very Own Crossword Puzzle
Forum Phun Extra
Joann Ally Employee Portal
FDA Approves Arcutis’ ZORYVE® (roflumilast) Topical Foam, 0.3% for the Treatment of Seborrheic Dermatitis in Individuals Aged 9 Years and Older - Arcutis Biotherapeutics
Hood County Buy Sell And Trade
1145 Barnett Drive
14 Top-Rated Attractions & Things to Do in Medford, OR
Bleacher Report Philadelphia Flyers
Black Lion Backpack And Glider Voucher
101 Lewman Way Jeffersonville In
Sacramento Craigslist Cars And Trucks - By Owner
Darktide Terrifying Barrage
Wake County Court Records | NorthCarolinaCourtRecords.us
Haley Gifts :: Stardew Valley
Craigslist In Myrtle Beach
Skip The Games Ventura
Final Exam Schedule Liberty University
Jefferson Parish Dump Wall Blvd
Umiami Sorority Rankings
20+ Best Things To Do In Oceanside California
Pokemon Reborn Locations
Ashoke K Maitra. Adviser to CMD&#39;s. Received Lifetime Achievement Award in HRD on LinkedIn: #hr #hrd #coaching #mentoring #career #jobs #mba #mbafreshers #sales…
Sam's Club Gas Prices Florence Sc
Janaki Kalaganaledu Serial Today Episode Written Update
Karen Wilson Facebook
888-822-3743
Despacito Justin Bieber Lyrics
Fedex Passport Locations Near Me
Chase Bank Zip Code
Garland County Mugshots Today
Headlining Hip Hopper Crossword Clue
Canonnier Beachcomber Golf Resort & Spa (Pointe aux Canonniers): Alle Infos zum Hotel
Haunted Mansion Showtimes Near Millstone 14
Okta Login Nordstrom
El Patron Menu Bardstown Ky
Oak Hill, Blue Owl Lead Record Finastra Private Credit Loan
View From My Seat Madison Square Garden
Okta Hendrick Login
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated:

Views: 6229

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.