lec_sept_21

lec_sept_21 - CS 2800 Discrete Math Sept 21 2011 Lecture...

This preview shows pages 1–2. Sign up to view the full content.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 2800: Discrete Math Sept. 21, 2011 Lecture Lecturer: John Hopcroft Scribe: June Andrews Review Last class we covered several applications: Hashing Let us say our application is to store the netids of students going to a concert. We could store the netids in an array. Now to check whether or not Emily went to the concert we have to check every cell of the array and see if that cell has Emily’s netid. This could take a very long time. What’s the worst case? Instead of searching the array from left to right, let us consider hashing. In accessing elements of an array, it takes the same amount of time to access the first cell as it takes to access 10 th , 100 th , or even last cell. So if we have a function that tells us: if Emily’s netid is in the array, it is stored in the y th cell. Now by checking if Emily’s netid is in the y th cell, we can check in constant time, whether or not Emily attended the concert. This function is the hash function. Given a netid, i , the hash function, H , will map a netid to a cell of the array, H ( i ). Now to store or find i , we can access H ( i ) in constant time. The tricky part comes when for two different netids, i 6 = j , map to the same cell, ie, H ( i ) = H ( j ). This is known as a collision. Unfortunately, due to the birthday problem hash collisions are likely. So our implementation of hashing and arrays must include collision resolution. Ie, if two netids map to the same cell, we can’t store them both in the same cell. How about the cell next to the one we mapped to? This is known as linear probing. When inserting a netid, i , we use H ( i ) to find the first cell we try to store i in. If cell H ( i ) is full, we move to the cell H ( i ) + 1, if that cell is empty we store i there. If H ( i ) + 1 is not empty we continue marching along the array until we find an empty cell to store i . Looking up i in the array is done in the same manner. We start looking at H ( i ), if H ( i ) is full, but does not contain i we keep checking cells until we either find i or find an empty cell, in which case we know i has not been stored in the array. A small note: Unlike storing in a linked list, we can not tack new netids onto the end of the hash array. The values are inserted into the array, hence to insert new netids at least a majority of the array has to be empty. As the array fills up, we may have to resize. A resize is when we create a new array that is much larger, pick a new hash function and insert all values from our older smaller array into the new array using the new hash function....
View Full Document

{[ snackBarMessage ]}

Page1 / 4

lec_sept_21 - CS 2800 Discrete Math Sept 21 2011 Lecture...

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

View Full Document
Ask a homework question - tutors are online