Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution.

Similar presentations


Presentation on theme: "Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution."— Presentation transcript:

1 Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution

2 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining

3 3 Hashing key hash function 0 1 2 3 TABLESIZE - 1 : : hash table pos

4 4 “Kruse” 0 1 2 3 6 4 5 hash table Example: 5 Kruse hash function

5 5Hashing Each item has a unique key. Uses a large array called a Hash Table. Uses a Hash Function. Hash Function Maps keys to positions in the Hash Table. Be easy to calculate. Use all of the key. Spread the keys uniformly.

6 6 Hash Table Operations Initialize –all locations in Hash Table are empty. Insert Search Delete

7 7 Example: Hash Function #3 “collisions” value = (s[i] + 3*value) % 7; Hash KeyValue Aho 0 Kruse5 Standish1 Horowitz5 Langsam5 Sedgewick2 Knuth1

8 8Collision When two keys are mapped to the same position. Very likely. 10 20 30 40 50 60 70 0.1169 0.4114 0.7063 0.8912 0.9704 0.9941 0.9992 Number of PeopleProbabilityBirthdays

9 9 Collision Resolution Two methods are commonly used: –Linear Probing. –Chaining.

10 10 Linear Probing Linear search in the array from the position where collision occurred.

11 11 Insert with Linear Probing Apply hash function to get a position. Try to insert key at this position. Deal with collision. –Must also deal with a full table!

12 12 Aho Hash Function 0 1 2 3 6 4 5 hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 0 Example: Insert with Linear Probing Aho

13 13 Kruse 0 1 2 3 6 4 5 hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 5 Example: Insert with Linear Probing Aho Kruse Hash Function

14 14 Standish 0 1 2 3 6 4 5 hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 1 Example: Insert with Linear Probing Aho Kruse Standish Hash Function

15 15 Horowitz 0 1 2 3 6 4 5 hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 5 Example: Insert with Linear Probing Aho Kruse Standish Horowitz Hash Function

16 16 Langsam 0 1 2 3 6 4 5 hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 5 Example: Insert with Linear Probing Aho Kruse Standish Horowitz Hash Function Langsam

17 17 module linearProbe(item) { position = hash(key of item) count = 0 loop { if (count == hashTableSize) then { output “Table is full” exit loop } if (hashTable[position] is empty) then { hashTable[position] = item exit loop } position = (position + 1) % hashTableSize count++ }

18 18 Search with Linear Probing Apply hash function to get a position. Look in that position. Deal with collision.

19 19 Kruse Langsam 0 1 2 3 6 4 5 hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 5 Example: Search with Linear Probing Aho Standish Horowitz Hash Function Langsam found.

20 20 Kruse Knuth 0 1 2 3 6 4 5 hash table 1 Example: Search with Linear Probing Aho Standish Horowitz Hash Function Langsam not found. Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth

21 21 module search(target) { count = 0 position = hash(key of target) loop { if (count == hashTableSize) then { output “Target is not in Hash Table” return -1. } else if (hashTable[position] is empty) then { output “Item is not in Hash Table” return -1. } else if (hashTable[position].key == target) then { return position. } position = (position + 1) % hashTableSize count++ }

22 22 Delete with Linear Probing Use the search function to find the item If found check that items after that also don’t hash to the item’s position If items after do hash to that position, move them back in the hash table and delete the item. Very difficult and time/resource consuming!

23 23 Linear Probing: Problems Speed. Tendency for clustering to occur as the table becomes half full. Deletion of records is very difficult. If implemented in arrays – table may become full fairly quickly, resizing is time and resource consuming

24 24 Chaining Uses a Linked List at each position in the Hash Table. –Linked list at a position contains all the items that ‘hash’ to that position. –May keep linked lists sorted or not.

25 25 hash table 0 1 2 3 : :

26 26 0 1 2 3 6 4 5 Aho, Kruse, Standish, Horowiz, Langsam, Sedgwick, Knuth Example: Chaining 0, 5, 1, 5, 5, 2, 1 3 1 2 KruseHorowitz Knuth 1 Standish Aho Sedgewick Langsam 0 0 0

27 27 Hashtable with Chaining At each position in the array you have a list: List hashTable[MAXTABLE]; You must initialise each list in the table. 0 1 2 1 2 1 :

28 28 Insert with Chaining Apply hash function to get a position in the array. Insert key into the Linked List at this position in the array.

29 29 module InsertChaining(item) { posHash = hash(key of item) insert (hashTable[posHash], item); } 0 1 2 1 2 Knuth 1 Standish Aho Sedgewick :

30 30 Search with Chaining Apply hash function to get a position in the array. Search the Linked List at this position in the array.

31 31 /* module returns NULL if not found, or the address of the * node if found */ module SearchChaining(item) { posHash = hash(key of item) Node* found; found = searchList (hashTable[posHash], item); return found; } 0 1 2 1 2 Knuth 1 Standish Aho Sedgewick :

32 32 Delete with Chaining Apply hash function to get a position in the array. Delete the node in the Linked List at this position in the array.

33 33 /* module uses the Linked list delete function to delete an item *inside that list, it does nothing if that item isn’t there. */ module DeleteChaining(item) { posHash = hash(key of item) deleteList (hashTable[posHash], item); } 0 1 2 1 2 Knuth 1 Standish Aho Sedgewick :

34 34 Disadvantages of Chaining Uses more space. More complex to implement. –Contains a linked list at every element in the array. –Requires linear searching. –May be time consuming.

35 35 Advantages of Chaining Insertions and Deletions are easy and quick. Allows more records to be stored. Naturally resizable, allows a varying number of records to be stored.

36 36Revision Hash Tables –Hash Functions –Insert, Search Collision Resolution –Linear Probing, Chaining Revision: Reading Kruse 8 Standish 11 Langsam 7.4 Sedgewick 16 Preparation Next lecture: Advanced Sorting Read Chapter 7.6-7.8 in Kruse et al.


Download ppt "Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution."

Similar presentations


Ads by Google