“never doubt that a small group of thoughtful, committed people can change the world. indeed, it...

21
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought for the Day

Upload: arline-stanley

Post on 13-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.”– Margaret Meade

Thought for the Day

Page 2: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

Hash Functions

• Essential ingredient for a successful hash table

• Numeric keys– simple

• Textual (alphanumeric) keys– e.g. 609A1234– Not so simple!

key

Hash Function

index

Page 3: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

Collisions

• Prevented by using perfect hashing functions

• But this is not always possible

• Other solutions?

• Two types of hash tables:– Internal hashing– External hashing

Page 4: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

Internal Hashing with Open Addressing

• Hash the key to find location

• If it is already occupied: start “probing”– simple approach: try immediately following

positions

programming 59

......

5958 60

programming

words 59 Collision!Collision!

words

Probe

Page 5: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

Probing

• Example shows linear probing

• Other possibilities– add constant amount to hash value– add variable amount to hash value– apply second hash function to get probe

“distance”

• Must be consistent

Page 6: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

Important Side Effect

• Must be very careful when removing items

......

5958 60

programming words

• Must mark deleted entries

• One of three states:– empty– occupied– deleted

XX

Page 7: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

Clustering and Efficiency

• As the hash table fills up collisions become more frequent

• Clustering– primary clustering– secondary clustering

1 2 3 4 5 6

• Efficiency decreases

Page 8: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

Clustering: Solutions?

• Make the table bigger than required

• Use external hashing

• Use more sophisticated probing techniques– helps decrease secondary clustering

Page 9: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

Deletions

• Decrease efficiency of searching

• If deletions are frequent, need to consider rebuilding the table periodically

Page 10: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

External Hashing

• Using the hash function identifies a bucket into which the key should be placed

programming 59

programming

words 59 Collision!

words......

5958 60

Page 11: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

External Hashing

• Buckets hold several values

• As the buckets fill up, efficiency decreases– no worse than probing– usually better (no “secondary clustering”)

• Big advantage:– space is not limited by table size

Page 12: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

Implementing the Buckets

• Many possible approaches– linked lists– binary search trees– secondary hash tables!

• We will use a simple unordered linked list

Page 13: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

The ExternalHashTable Classpublic class ExternalHashTable<K, V> implements Dictionary<K, V> { private static final int DEF_SIZE = 101; private class EntryNode<K, V> extends DictionaryPair<K, V> { EntryNode<K, V> next; } // class EntryNode

private EntryNode<K, V>[] table;

private int hash (K aKey) // Scale hash value for table size { . . . } // hash ... } // class ExternalHashTable

Thebuckets

Page 14: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

Hashing Function

• How do we handle the hashing function?

• Java to the rescue!– Object class has hashCode method

public int hashCode ()

Page 15: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

Using the hashCode Method

private int hash (K aKey)// Scale hash value for table size { return ((aKey.hashCode() & 0x7FFFFFFF) % table.length); } // hash

• Need to ensure it is positive and in correct range:

Page 16: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

The Buckets

table ...

key

value

key

value

key

value

Page 17: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

The insert Method

public void insert (K aKey, V aValue)// Insert new element or update existing one { int index = hash(aKey); // Look for aKey in linked list EntryNode<K, V> c; for (c = table[index]; c != null && ! c.getKey().equals(aKey); c = c.next) ; ... } // insert

Page 18: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

The insert Method (cont.)

...if (c == null) // Insert new node { EntryNode<K, V> n = new EntryNode<K, V>(aKey, aValue); n.next = table[index]; table[index] = n; }else // Update existing entry c.setValue(aValue);

Page 19: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

Other Methods• Quite simple

• Remember structure:– array of linked lists

• Example:

public boolean isEmpty ()// Tell whether hash table is empty { for (int k = 0; k < table.length; k++) if (table[k] != null) return false; // Found at least one entry return true; // Found no entries } // isEmpty

Page 20: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought

External Hash Table: Iterators

• Slightly more complicated:– need to work through array– and work through linked

lists

table ...

key

value

key

value

key

value

Page 21: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought