Lecture 9 - Hashing 4 slides per page

Lecture 9 - Hashing 4 slides per page - Readings Hashing...

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

View Full Document Right Arrow Icon
1 Hashing 2 Readings ± Required ² [Carrano and Prichard] ch12 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 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. 2 Direct Addressing Table - a simplified version of hash table 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 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 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 3 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? 10 Direct addressing table operations insert (key, data) a[key] = data delete (key) a[key] = null find (key) return a[key] 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. Hash Table Hash Table is a generalization of direct addressing table to remove these restrictions 4 13 Idea ± Map large integers to smaller integers ± Map non-integer keys to integers 14 Hash table : 17 66752378, data 68744483, data 66752378 68744483 h 974 h is a hash function 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! 16 Problem Two keys can have the same hash value COLLISION occurs
Background image of page 1

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

View Full DocumentRight Arrow Icon
5 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 ”. 18 Two important issues ± How to hash? ± How to resolve collision? Hash Functions 20 Good Hash Functions ± Fast to compute ± Scatter keys evenly throughout the hash table ± Less collisions ± Less space 6 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 ± What happen
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/02/2009 for the course ECE CS1102C taught by Professor Dr.tansuntuck during the Spring '07 term at National University of Singapore.

Page1 / 5

Lecture 9 - Hashing 4 slides per page - Readings Hashing...

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

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