Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC220 Fall 2009 James Atlas Dec 04: Hashing and Maps K+W Chapter 9.

Similar presentations


Presentation on theme: "CISC220 Fall 2009 James Atlas Dec 04: Hashing and Maps K+W Chapter 9."— Presentation transcript:

1 CISC220 Fall 2009 James Atlas Dec 04: Hashing and Maps K+W Chapter 9

2 Map Picture

3 Map usage (STL) The map can be used like an array, except that the Key_Type is the index. Example: map a_map; a_map["J"] = "Jane"; a_map["B"] = "Bill"; a_map["S"] = "Sam"; a_map["B1"] = "Bob"; a_map["B2"] = "Bill"; –If a mapping exists, assignment will replace it. –If a mapping does not exist, a reference will create one with a default value.

4 Hash Tables Goal: access item given its key (not its position) –we wish to avoid much searching Hash tables provide this capability –Constant time in the average case!O(1) –Linear time in the worst caseO(n) Searching an array: O(n) Searching BST: O(log n)

5 911 call center example Key: phone # Value: address

6 Address calculator 0 1........ n-1 Array table Address calculator Search key

7 Address calculator 0 1 101 Smith Hall Newark, DE n-1 Array table Address calculator 302-831-2712

8 Address calculator 0 1 101 Smith Hall Newark, DE n-1 Array table Address calculator 302-831-2712 If we had 9999999999 table entries, this would be easy!

9 Hash Function Hash function IntegerInteger in the range [0,n-1] Any ideas for our phone number?

10 Hash Function Hash function IntegerInteger in the range [0,n-1] Any ideas for our phone number? mod(x,n) -> y such that y is in the range [0,n-1]

11 Address calculator 0 1 101 Smith Hall Newark, DE n-1 Array table Hash function mod(10000) 302-831-2712 302-737-2712 ?

12 Collision 0 1 101 Smith Hall Newark, DE n-1 Array table Hash function mod(10000) 302-831-2712 302-737-2712 ? Two keys map to the same location

13 How could we resolve collisions? Goal is to still be able to insert, delete, and search based on key 0 1 101 Smith Hall Newark, DE n-1 Array table Hash function mod(10000) 302-831-2712 302-737-2712 ?

14 Open Addressing Data for a key can be at multiple locations Probe sequence 0 1 302-831-2712 101 Smith Hall Newark, DE 302-737-2712 136 Main St. n-1 Array table Hash function mod(10000) 302-831-2712 302-737-2712 Linear Probing

15 s = starting point (based on hash function) i = 0 do –check position s + i –i = i + 1; while not found s, s+1, s+2, s+3, s+4, etc.

16 Quadratic Probing s = starting point (based on hash function) i = 0 do –check position s + i*i –i = i + 1; while not found s, s+1, s+4, s+9, s+16, etc.

17 Another collision resolution: Chaining Each entry in table is actually a linked list 0 1 302-831-2712 101 Smith Hall Newark, DE n-1 Array table Hash function mod(10000) 302-831-2712 302-737-2712 136 Main St.

18 Performance of Hash Tables Load factor = # filled cells / table size –Between 0 and 1 Load factor has greatest effect on performance Lower load factor  better performance –Reduce collisions in sparsely populated tables Average expected # probes for open addressing: –linear probing = ½(1 + 1/(1-L)) –quadratic probing = ½(1 + 1/(1-L)) 2 For chaining = 1 + (L/2) –Note: Here L can be greater than 1

19 Performance of Hash Tables (2)

20 Performance of Hash Tables (3) Hash table: –Insert: average O(1) –Search: average O(1) Sorted array: –Insert: average O(n) –Search: average O(log n) Binary Search Tree: –Insert: average O(log n) –Search: average O(log n) But balanced trees can guarantee worst case O(log n)


Download ppt "CISC220 Fall 2009 James Atlas Dec 04: Hashing and Maps K+W Chapter 9."

Similar presentations


Ads by Google