Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Hashing: Collision Resolution Schemes Collision Resolution Techniques Introduction to Separate Chaining Collision Resolution using Separate Chaining.

Similar presentations


Presentation on theme: "1 Hashing: Collision Resolution Schemes Collision Resolution Techniques Introduction to Separate Chaining Collision Resolution using Separate Chaining."— Presentation transcript:

1 1 Hashing: Collision Resolution Schemes Collision Resolution Techniques Introduction to Separate Chaining Collision Resolution using Separate Chaining Introduction to Collision Resolution using Open Addressing

2 2 Collision Resolution Techniques There are three broad ways of collision resolution: 1. Separate Chaining: A linked list-based implementation. 2. Open Addressing: Array-based implementation. (i) Linear probing (linear search) (ii) Quadratic probing (nonlinear search) (iii) Random increments/decrements (iv) Rehashing (double hashing) 3. Buckets methods: Usually a combination of (1) & (2)

3 3 Introduction to Separate Chaining The hash table is implemented as an array of linked lists. Inserting an item, r, at index i is simply insertion into the linked list at position i. Synonyms are chained in the same linked list. Retrieval of an item, r, with hash address, i, is simply retrieval from the linked list at position i. Deletion of an item, r, with hash address, i, is simply deleting r from the linked list at position i.

4 4 Separate Chaining with String Keys Recall that search keys can be numbers, strings or some other object. The following Java method implements such technique public static int hash(String key, int tableSize) { int hashVal = 0; for (int i = 0; i < key.length(); i++) { hashVal += key.charAt(i); } return hashVal % tableSize; } The following class which describes commodity items class CommodityItem { String name;// commodity name int quantity;// commodity quantity needed double price;// commodity price }

5 5 Example 1: Separate Chaining Devise an appropriate hash function and use it to load the information about the following commodity items into a hash table of size 13 using separate chaining. onion1 10.0 tomato1 8.50 cabbage3 3.50 carrot1 5.50 okra1 6.50 melon2 10.0 potato2 7.50 Banana 3 4.0 olive2 15.0 salt2 2.50 cucumber3 4.50 mushroom3 5.50 orange2 3.00

6 6 Example 1: Separate Chaining (cont'd) 0 1 2 3 4 5 6 7 8 9 10 11 12 onion okra melon banana tomatoolive cucumber mushroom salt cabbage carrot potato orange

7 7 Introduction to Open Addressing In this method the entries are placed inside the array itself. The probe sequence is essentially a sequence of functions {h 0, h 1, h 2, …, h n-1 } where, h i : K -> {0, 1, …, n-1 } To insert item r, we examine array locations h 0 (r), h 1 (r), h 2 (r),..., Similarly, to find item r, we examine the same sequence of locations in the same order.

8 8 Introduction to Open Addressing (cont'd) The most common probe sequences are of the form h i (r) = (h(r) + c(i)) mod n, i = 0, 1, …, n-1. The function c(i) is required to have the following two properties: Property 1: c(0) = 0. Property 2: The set of values {c(0) mod n, c(1) mod n, c(2) mod n, …, c(n-1) mod n} must contain every integer between 0 and n-1 inclusive.

9 9 Open Addressing: Linear Probing Linear Probe: Here the function c(i ) is a linear function in i : c(i) = ai + b Property 1 requires that c(0) = 0. Therefore, b must be zero. For c(i) = ai to satisfy Property 2, a and n must be relatively prime. The linear probing sequence that is usually used is h i (r)= (h(r) + i) mod n, i=0,1,2,…, n-1 Insert record at first empty slot and if no empty slot is found then the hash table is full and insertion fails.

10 10 Example 2: Linear Probing Use the hash function h(r) = r.id % 13 to load the following records into an array of size 13. Al-Otaibi Ziyad1.73985926 Al-Turki, Musab Ahmad Bakeer1.60970876 Al-Saegh, Radha Mahdi1.58980962 Al-Shahrani, Adel Saad1.80986074 Al-Awami, Louai Adnan Muhammad1.73970728 Al-Amer, Yousuf Jauwad1.66994593 Al-Helal, Husain Ali AbdulMohsen1.70996321 Then insert the following records using linear probing to resolve collisions, if any. Al-Najjar, Khaled Ziyad1.69987615 Al-Ali, Amr Ali Zaid1.79987630 Al-Ramadi, Husam Yahya1.58987602

11 11 Example 2: Introduction to Hashing (cont'd) 0 1 2 3 4 5 6 7 8 9 10 11 12 HusainYousufKhalidRadhaAmrMusabAdelHusamLouaiZiyad

12 12 Linear Probing: Some Notes Notice from this table that a large cluster has already been formed. In general, empty cells following the cluster have higher chance of being hashed into. The probability of taking longer probe sequences is much higher with clusters. This is one disadvantage of linear probing. Other methods attempt to improve on this.

13 13 Introduction to Retrieval & Deletion Retrieval: To search for a record we: Calculate its hash value. Check that location of the array for the record. · If found, return the record. · If not, keep searching until you find the record or you reach an empty table location. Attempting to retrieve a non-existent record is very expensive. Deletion: In open addressing, where a record is stored is not necessarily its home position. We cannot just set the location of a deleted record to empty. A special flag or key value is needed to mark deleted records locations.

14 14 Example 3: Retrieval & Deletion Consider the following hash table constructed in Example 2: 0 1 2 3 4 5 6 7 8 9 10 11 12 HusainYousufKhalidRadhaAmrMusabAdelHusamLouaiZiyad Delete Khalid's record (id 987615) and then retrieve the records for Amr and then that of Husam.

15 15 Example 3: Retrieval & Deletion 0 1 2 3 4 5 6 7 8 9 10 11 12 HusainYousufRadhaAmrMusabAdelHusamLouaiZiyad ?

16 16 Exercises 1.Given that, c(i) = a*i, for c(i) in linear probing, we discussed that this equation satisfies Property 2 only when a and n are relatively prime. Explain what the requirement of being relatively prime means in simple plain language. 2.Consider the general probe sequence, h i (r) = (h(r) + c(i))mod n. Are we sure that if c(i) satisfies Property 2, then h i (r) will cover all n hash table locations, 0,1,...,n-1 ? Explain. 3.Suppose you are given k records to be loaded into a hash table of size n, with k < n using linear probing. Does the order in which these records are loaded matter for retrieval and insertion? Explain. 4.A prime number is always the best choice of a hash table size. Is this statement true of false? Justify your answer either way.


Download ppt "1 Hashing: Collision Resolution Schemes Collision Resolution Techniques Introduction to Separate Chaining Collision Resolution using Separate Chaining."

Similar presentations


Ads by Google