Lecture_9_-_Hashing - Hashing 1 Readings Required [Carrano...

Info iconThis preview shows pages 1–22. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Hashing
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 2 Readings ± Required ² [Carrano and Prichard] ch12
Background image of page 2
3 3 ADT table operations O(log n) O(log n) O(log n) Balanced BST O(log n) Retrieval O(n) Deletion O(n) Insertion Sorted Array Q : What is the meaning of O(1)? Hashing O(1) avg O(1) avg O(1) avg
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 4 outline ± What is hashing? ± How to hash? ± What is collison? ± How to resolve collision ? ² separate chaining ² linear probing ² quadratic probing ² double hashing ± Load factor ± Primary clustering and secondary clustering.
Background image of page 4
5 Direct Addressing Table - a simplified version of hash table
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 6 SBS bus problem ± Retrieval find(N) ² Does bus service no. N exist? ± Insertion insert(N) ² Introduce a new bus service no. N ± Deletion delete(N) ² Remove bus service no. N
Background image of page 6
7 7 SBS bus problem Note that bus numbers are integers between 0 – 999 , we can create an array with 1000 booleans If bus service N exists, just set position N to true : : false true 998 true 2 false 0 1 999 false
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 8 Direct addressing table If we want to maintain additional data about a bus, each slot can reference to an Object. : 998 2 0 1 2, data 998, data 999
Background image of page 8
9 9 Direct addressing table We can store the record (key value and other data) directly in the table slots also. : 998, data 998 2, data 2 0 1 999 Q: What are the advantages and disadvantages of these 2 approaches?
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 10 Direct addressing table operations insert (key, data) a[key] = data delete (key) a[key] = null find (key) return a[key]
Background image of page 10
11 11 Restrictions of direct addressing table ± Keys must be integer ² What happens for key values 151A, NR10? ± Range of keys must be small ± Keys must be dense , i.e. not many gaps in the key values.
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 Hash Table Hash Table is a generalization of direct addressing table to remove these restrictions
Background image of page 12
13 13 Idea ± Map large integers to smaller integers ± Map non-integer keys to integers
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
14 14 Hash table : 17 66752378, data 68744483, data 66752378 68744483 h 974 h is a hash function
Background image of page 14
15 15 Hash table operations h, a hash function h(key): the home address of key insert (key, data) a[ h(key) ] = data delete (key) a[ h(key) ] = null find (key) return a[ h(key) ] This does not work for all cases!
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
16 16 Problem Two keys can have the same hash value COLLISION occurs
Background image of page 16
17 17 Hash table : 66752378, data 68744483, data h 67774385 Let h(key) = even pos. digits E.g. h( 6 7 7 7 4 3 8 5) = 6748 and h( 6 8 7 4 4 4 8 3 ) = 6748 This is called a “ collision ”.
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
18 18 Two important issues ± How to hash? ± How to resolve collision?
Background image of page 18
19 Hash Functions
Background image of page 19

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
20 20 Good Hash Functions ± Fast to compute ± Scatter keys evenly throughout the hash table ± Less collisions ± Less space
Background image of page 20
21 21 Bad Hashing Functions ± Select Digits e.g. Given d 0 d 1 d 2 …d 7 , choose d 2 and d 7 ² hash(67 5 437 8 ) = 58 ² hash(34 9 782 0 ) = 90 ±
Background image of page 21

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 22
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/02/2011 for the course CS 3211 taught by Professor Dunnowho during the Spring '11 term at National University of Singapore.

Page1 / 65

Lecture_9_-_Hashing - Hashing 1 Readings Required [Carrano...

This preview shows document pages 1 - 22. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online