Hashing - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Hashing

Description:

example: simple hash method with 'baab' and 'abba' There are several ways to handle collisions ... BankAccount a1 = new BankAccount('abba', 100.00) ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 19
Provided by: rickm1
Category:
Tags: abba | hashing

less

Transcript and Presenter's Notes

Title: Hashing


1
Hash Tables Rick Mercer
2

Hash TablesAn implementation of a Map
  • Outline
  • Discuss what a hash method does
  • translates a string key into an integer
  • Discuss a few strategies for implementing a hash
    table
  • linear probing
  • quadratic probing
  • separate chaining hashing

3
The Map ADT
  • Map describes a type that stores a collection of
    elements that consists of a key and a value
  • A Map associates (maps) a key the it's value
  • The keys must be unique
  • the values need not be unique

4
Java's HashMap Class
  • HashMap accountCollection
    new
  • HashMapBankAccount()
  • accountCollection.put("456C", new
    BankAccount("Ted", 2.00))
  • accountCollection.put("123C", new
    BankAccount("Bob", 1.00))
  • accountCollection.put("678C", new
    BankAccount("Kim", 3.00))
  • // remove return value associated with key (or
    return null)
  • accountCollection.remove("456C")
  • // Send a deposit message to the object in the
    collection
  • BankAccount ref accountCollection.get("123C")
  • ref.deposit(567.89)
  • java.util.Collection
    tableAsCollection

  • accountCollection.values()
  • System.out.println(tableAsCollection.toString())
  • // Bob 568.89, Kim 3.00

5
Hash Tables
  • A Map is often implemented with a hash table
  • Hash tables provide virtually direct access to
    objects based on a key (String or Integer)
  • key could be your SID, your telephone number,
    social security number, account number,
  • The direct access is made possible by converting
    a String or Integer key to an array index

6
Hashing
  • A key such as "555-1234" results in an integer
    index from 0 to an array upper bound index
  • Elements can be found, inserted, and removed
    using the integer index as an array index
  • A hash table uses the same "address calculator"
    to (insert), get (find), and remove
  • Could convert a String key to 4 like this
  • "able" 97 98 108 101 404
  • array index 404 array capacity

7
Hash Function
  • Ideally, every key has a unique hash value
  • Then the hash value could be used as an array
    index, however ....
  • Cannot rely on every key "hashing" to a unique
    array index
  • can get "close"
  • need a way to handle "collisions"
  • "abc" may hash to the same integer as "cba"

8
Hash function works something like this
Convert a String key into an integer that will be
in the range of 0 through the maximum capacity-1
Assume the array
capacity is 9997
hash(key)
AAAAAAAA
8482
1273
zzzzzzzz
hash(key)
Domain "!" .. "zzzzzzzz"
Range 0 ... 9996
9
Hash Function
  • What if the ASCII value of individual chars of
    the string key added up to a number from ("A") 65
    to possibly 488 ("zzzz") 4 chars max
  • If the array has size 309, mod the sum
  • 390 TABLE_SIZE 81
  • 394 TABLE_SIZE 85
  • 404 TABLE_SIZE 95
  • Keys converted to array indices could be

81 85 95
"abba" "abcd" "able"
10
A "too simple" hash function
  • // Return an int in the range of 0..TABLE_SIZE-1
  • public int hash(String key)
  • int result 0
  • int n key.length()
  • for (int j 0 j
  • result key.charAt(j) // add up the chars
  • return result TABLE_SIZE
  • public static final int TABLE_SIZE 309
  • public void testHashFunction()
  • assertEquals(81, hash("abba"))
  • assertEquals(81, hash("baab"))
  • assertEquals(85, hash("abcd"))
  • assertEquals(86, hash("abce"))
  • assertEquals(308, hash("IKLT"))
  • assertEquals(308, hash("KLMP"))

11
Collisions
  • A good hash method
  • executes quickly
  • distributes keys equitably
  • But you still have to handle collisions when two
    keys have the same hash value
  • the hash method is not guaranteed to return a
    unique integer for each key
  • example simple hash method with "baab" and
    "abba"
  • There are several ways to handle collisions
  • Linear Probing, Quadratic Linear Probing, and
    separate chaining hashing (discussed next)

12
An Array of LinkedList Objects Implementation
  • An array of linked lists

0
321
365
1
2
13
Insert is Easy
  • public void put(K key, V x)
  • int pos hash(key)
  • // Place the element in the correct list
  • tablepos.add (x) // is addFirst faster?
  • public void display()
  • for(int j 0 j
  • System.out.print(j ". ")
  • // Print an entire linked list
  • System.out.println(tablej)

14
put 6 and Display the Data Structure as an array
of lists
  • MyHashTable h
  • new MyHashTableBankAccount()
  • BankAccount a1 new BankAccount("abba",
    100.00)
  • BankAccount a2 new BankAccount("abcd",
    200.00)
  • BankAccount a3 new BankAccount("abce",
    300.00)
  • BankAccount a4 new BankAccount("baab",
    400.00)
  • BankAccount a5 new BankAccount("KLMP",
    500.00)
  • BankAccount a6 new BankAccount("IKLT",
    600.00)
  • // Insert BankAccount objects using ID as the
    key
  • h.put(a1.getID(), a1)
  • h.put(a2.getID(), a2)
  • h.put(a3.getID(), a3)
  • h.put(a4.getID(), a4)
  • h.put(a5.getID(), a5)
  • h.put(a6.getID(), a6)
  • h.display()

15
The output when TABLE_SIZE10
  • 0. abbaabba 100.00, baabbaab 400.00
  • 1.
  • 2.
  • 3.
  • 4. abcdabcd 200.00
  • 5. abceabce 300.00
  • 6.
  • 7.
  • 8. KLMPKLMP 500.00, IKLTIKLT 600.00
  • 9.
  • public void display()
  • for (int j 0 j
  • System.out.print(j ". ")
  • // Print an entire linked list
  • System.out.println(tablej)

16
Array of LinkedLists
  • import java.util.LinkedList
  • public class MyHashTable
  • private final static int TABLE_SIZE 10
  • private LinkedList table
  • private class HashNode
  • private K key
  • private V data
  • private HashNode(K key, V data)
  • this.key key
  • this.data data
  • _at_Override
  • public String toString()
  • return key.toString() ""
    data.toString()

17
  • public MyHashTable()
  • table new LinkedListTABLE_SIZE
  • for (int j 0 j
  • tablej new LinkedList()
  • // return an int in the range of 0..TABLE_SIZE-1
  • public int hash(K key)
  • return key.hashCode() TABLE_SIZE
  • public void put(K key, V x)
  • HashNode hn new HashNode(key, x)
  • int pos hash(key)
  • tablepos.add(hn)

18
A Better Hash methodFrom Mark Alan Weiss
  • Java requires the key to override hashCode
  • the hash function then uses the key's hashCode
  • // Return an int in the range of 0..TABLE_SIZE-1
  • public int hash(K key)
  • return key.hashCode() TABLE_SIZE
  • String and integer already do
  • Check out String's hashCode
  • http//java.sun.com/j2se/1.5.0/docs/api/java/lang/
    String.htmlhashCode()
Write a Comment
User Comments (0)
About PowerShow.com