Improving search performance: Hash tables - PowerPoint PPT Presentation

About This Presentation
Title:

Improving search performance: Hash tables

Description:

Phone directory. public String SearchForNumber (String name) ... Phone directory. class DirectoryBuilder { public static void main (String a ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 21
Provided by: iit1
Category:

less

Transcript and Presenter's Notes

Title: Improving search performance: Hash tables


1
Improving search performance Hash tables
  • Instructor Mainak Chaudhuri
  • mainakc_at_cse.iitk.ac.in

2
Announcements
  • No class on Wednesday (8th November)
  • Please send your project demo preference to me by
    mail
  • 21, 22, 23, 24 November
  • May require up to one hour
  • Come with your final project report (no more than
    8 pages) to the demo
  • Project carries 20 points broken into
  • Quality of demo
  • Question-answer during demo
  • Presentation in report

3
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

4
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

5
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 IntegerListsize
  • for (i0i
  • // next slide

6
Hash tables
  • public void Insert (int x)
  • // Apply mod hash function
  • int entry x numEntries
  • if (tableentry ! null)
  • tableentry.Enqueue(x)
  • else
  • tableentry new IntegerList(x)
  • // next slide

7
Hash tables
  • public void SearchAndDelete (int x)
  • // Find out the bucket
  • int entry x numEntries
  • if (tableentry ! null)
  • tableentry tableentry.SearchAndDe
    lete(x)
  • // next slide

8
Hash tables
  • public void Print ()
  • int i
  • for (i0 i
  • if (tablei ! null)
  • System.out.println(Entry i
    )
  • tablei.Print()
  • // end class
  • // next slide

9
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.Print()
  • ht.SearchAndDelete(5)
  • ht.SearchAndDelete(64)
  • ht.Print()

10
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

11
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

12
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

13
Phone directory
  • public void Enqueue (String name, String ph)
  • if (nextnull)
  • next new DirectoryEntry (name, ph)
  • else
  • next.Enqueue(name, ph)
  • // next slide

14
Phone directory
  • public String SearchForNumber (String name)
  • if (this.name.equalsIgnoreCase (name))
  • return phoneNumber
  • if (nextnull)
  • return 0000000000
  • return next.SearchForNumber (name)

15
Phone directory
  • public void Print ()
  • if (nextnull)
  • System.out.println ( phoneNumber )
  • else
  • System.out.print (phoneNumber , )
  • next.Print()
  • // end class

16
Phone directory
  • class PhoneDirectory
  • // Array of directory entry lists
  • DirectoryEntry directory
  • int numEntries
  • public PhoneDirectory (int size)
  • int i
  • numEntries size
  • directory new DirectoryEntrysize
  • for (i0 i
  • directoryi null
  • // next slide

17
Phone directory
  • public void Insert (String name, String ph)
  • // Sort by first letter
  • int entry (int)(name.charAt(0))
    numEntries
  • if (directoryentry ! null)
  • directoryentry.Enqueue (name, ph)
  • else
  • directoryentry new DirectoryEntry
    (name, ph)
  • // next slide

18
Phone directory
  • public String SearchForNumber (String name)
  • // Find out bucket
  • int entry (int)(name.charAt(0))
    numEntries
  • if (directoryentry ! null)
  • return directoryentry.SearchForNumber
    (name)
  • else
  • return 0000000000

19
Phone directory
  • public void Print ()
  • int i
  • for (i0 i
  • if (directoryi ! null)
  • System.out.println (Entry i
    )
  • directoryi.Print()
  • // end class

20
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))
Write a Comment
User Comments (0)
About PowerShow.com