Docsity

Separate Chaining and Open Addressing Hashing Techniques, Slides of Data Structures and Algorithms

Partial preview of the text

Download Separate Chaining and Open Addressing Hashing Techniques and more Slides Data Structures and Algorithms in PDF only on Docsity!

7/7/2012 Hashing 5-

Chapter 5

Hashing

EE 232 Data Structures Session-05 , Spring-

7/7/2012 Hashing 5-

Motivation

 Let us assume that we want to search for a particular item in a database of 20,000,000 data items  How long would it take to find for a successful search?  How long would it take for an unsuccessful search?

 It depends on the data structure

7/7/2012 Hashing 5-

Chapter 5: Hashing

5.1 General Idea

5.2 Hash Function

5.3 Separate Chaining

5.4 Open Addressing

5.5 Rehashing

5.6 Extendible Hashing

7/7/2012 Hashing 5-

Chapter 5: Hashing

Our goals: We will

 See several methods of implementing the hash table  Compare these methods analytically  Show numerous applications of hashing  Compare hash tables with binary search trees

7/7/2012 Hashing 5-

Chapter 5: Hashing

5.1 General Idea

5.2 Hash Function

5.3 Separate Chaining

5.4 Open Addressing

5.5 Rehashing

5.6 Extendible Hashing

7/7/2012 Hashing 5-

General Idea

 The general idea behind hashing is to directly map each data item into an address in memory using some function  key  hash function  index to an array

 Components of hashing  A hash table is an array of some fixed size ‘m’  A hash function h(k) that maps a search key k to some location in the range [0...m-1]

  • h(k): S  {0, 1, …, m-1}

7/7/2012 Hashing 5-

General Idea…

 Desired Properties of h(k)  simple to compute  uniform distribution of keys over {0, 1, …, m-1}

  • when h(k 1 ) = h(k 2 ) for two distinct keys k 1 , k 2 , we have a collision

7/7/2012 Hashing 5-

General Idea…

m-

Name: Kashif Javed University: UET Office: room 4 EED Mobile Number: Email: etc

Data Items

h(Javed) = 1

Name: Hassan Hamid University: UET Office: room 8 EED Mobile Number: Email: etc

h(Hamid) = 1

A collision has occurred

array

7/7/2012 Hashing 5-

General Idea…

 Hashing revisited  A hash table data structure is an array  Each data element contains a key  Each key is mapped to some number in the range from 0 to TableSize-1, with the help of a hash function

  • The hash function should be efficient to compute and should ensure that different data items get mapped to different numbers  The key and the hashing function are used both to insert the data into the table and to later find that data

7/7/2012 Hashing 5-

General Idea…

 Example  PTCL is a large telephone company, and they want to maintain a database that provides the caller ID capability

  • given a phone number, return the caller’s name
  • phone numbers range from 0 to r = 10^7 -
  • want to do this as efficiently as possible

7/7/2012 Hashing 5-

General Idea…

 Solution 2  Linked list

  • takes O (r) time,
  • O (r) space (only as much space as is needed )

Umer Hamid 6829227

Hassan Hamid 6829229

7/7/2012 Hashing 5-

General Idea…

 Solution 3  Hash table

  • O (1) expected time, O (n+m) space, where m is table size  Like an array, but come up with a function to map the large range into one which we can manage
  • e.g. take the original key, modulo the (relatively small) size of the array, and use that as an index
  • 6829229 mod 5 = 4

(null) (null) (null) (null) Hassan Hamid 0 1 2 3 4

7/7/2012 Hashing 5-

Hash Function

 A simple hash function  If input keys (k) are integers  hash function, h( k ) = k mod m

  • where m is the table size

 Example

  • Suppose m = 10,
  • k = 10, 20, 30, 40
  • h(k) = 0, 0, 0, 0
    • A bad choice if the keys end in zeros

7/7/2012 Hashing 5-

Hash Function…

 Another simple hash function  If input keys (k) are integers  hash function, h( k ) = k mod m

  • where m is the table size and is a prime number

 Example

  • Suppose m = 11,
  • k = 10, 20, 30, 40
  • h(k) = 10, 9, 8, 7
    • Distributes the keys more uniformly