Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Improving search performance: Hash tables Instructor: Mainak Chaudhuri

Similar presentations


Presentation on theme: "1 Improving search performance: Hash tables Instructor: Mainak Chaudhuri"— Presentation transcript:

1 1 Improving search performance: Hash tables Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

2 2 Hash tables The idea is to divide the incoming data into different “buckets” depending on the value of the data –The mapping of data value to a bucket id is called a hash function –Need to search just one bucket for a particular data (example: marks to name) –If the values are well distributed, or, put in a different way, the hash function distributes the data evenly into the buckets, the search time naturally goes down –The data within a bucket are usually organized as a list (often called chaining) –The array of lists forms a hash table

3 3 Hash functions Given a data value, we want to compute an array index –Ideally, two different values should always map to two different indices: zero collision, O(1) search time –Possible if value range is small and known a priori (often this is not known) –Formally, the value used as the input to the hash function is called the key Any value is transformed to a natural number key (applies to strings also) –Simple mod function is often used as the hash map

4 4 Hash tables public class HashTable { // An array of integer lists private IntegerList table[]; // Number of table entries; private int numEntries; // Constructor public HashTable (int size) { int i; numEntries = size; table = new IntegerList[size]; for (i=0;i<size;i++) table[i] = null; } // next slide

5 5 Hash tables public void Insert (int x) { // Apply mod hash function int entry = x % numEntries; if (table[entry] != null) { table[entry].Enqueue(x); } else { table[entry] = new IntegerList(x); } // next slide

6 6 Hash tables public void SearchAndDelete (int x) { // Find out the bucket int entry = x % numEntries; if (table[entry] != null) { table[entry] = table[entry].SearchAndDelete(x); } // next slide

7 7 Hash tables public void Print () { int i; for (i=0; i<numEntries; i++) { if (table[i] != null) { System.out.println(“Entry ” + i + “ :”); table[i].Print(); } } // end class // next slide

8 8 Hash tables class HashTableBuilder { public static void main (String a[]) { HashTable ht = new HashTable (7); ht.Insert(10); ht.Insert(64); ht.Insert(20); ht.Insert(17); ht.Print(); ht.SearchAndDelete(5); ht.SearchAndDelete(64); ht.Print(); }

9 9 Phone directory How would you organize a phone directory? –Simple solution: a list of tuples; drawback: linear search time –Better solution: a hash table with name as the hash key; average search time reduced –Observation: hash key should be the one you want to use to search for something e.g., search the phone number of Rahul

10 10 Phone directory public class DirectoryEntry { private String name; private String phoneNumber; private DirectoryEntry next; public DirectoryEntry (String name, String ph) { this.name = name; phoneNumber = ph; next = null; } // next slide

11 11 Phone directory public void SetNext (DirectoryEntry de) { next = de; } public DirectoryEntry InsertAtFront (String name, String ph) { DirectoryEntry newHead = new DirectoryEntry (name, ph); // Note that “this” refers to the old head newHead.SetNext (this); return newHead; } // next slide

12 12 Phone directory public void Enqueue (String name, String ph) { if (next==null) { next = new DirectoryEntry (name, ph); } else { next.Enqueue(name, ph); } // next slide

13 13 Phone directory public String SearchForNumber (String name) { if (this.name.equalsIgnoreCase (name)) { return phoneNumber; } if (next==null) { return “0000000000”; } return next.SearchForNumber (name); }

14 14 Phone directory public void Print () { if (next==null) { System.out.println (“ ”); } else { System.out.print (“, ”); next.Print(); } } // end class

15 15 Phone directory class PhoneDirectory { // Array of directory entry lists private DirectoryEntry directory[]; private int numEntries; public PhoneDirectory (int size) { int i; numEntries = size; directory = new DirectoryEntry[size]; for (i=0; i<size; i++) { directory[i] = null; } // next slide

16 16 Phone directory public void Insert (String name, String ph) { // Sort by first letter int entry = (int)(name.charAt(0)) % numEntries; if (directory[entry] != null) { directory[entry].Enqueue (name, ph); } else { directory[entry] = new DirectoryEntry (name, ph); } } // next slide

17 17 Phone directory public String SearchForNumber (String name) { // Find out bucket int entry = (int)(name.charAt(0)) % numEntries; if (directory[entry] != null) { return directory[entry].SearchForNumber (name); } else { return “0000000000”; }

18 18 Phone directory public void Print () { int i; for (i=0; i<numEntries; i++) { if (directory[i] != null) { System.out.println (“Entry ” + i + “ :”); directory[i].Print(); } } // end class

19 19 Phone directory class DirectoryBuilder { public static void main (String a[]) { PhoneDirectory d = new PhoneDirectory (26); d.Insert (“Emily”, “1408999999”); d.Insert (“John”, “0123456789”); d.Insert (“Jack”, “9876543210”); d.Insert (“Abraham”, “2357111317”); d.Insert (“Christina”, “0246810121”); d.Insert (“Lucia”, “0112345011”); d.Print(); System.out.println (“Phone number of Jack: ” + d.SearchForNumber (“Jack”)); System.out.println (“Phone number of Mainak: ” + d.SearchForNumber (“Mainak”)); }


Download ppt "1 Improving search performance: Hash tables Instructor: Mainak Chaudhuri"

Similar presentations


Ads by Google