searching
play

Searching Binary search (log n ) Better Can we achieve (1) - PDF document

4/18/2013 Constant time access Linear search ( n ) OK Searching Binary search (log n ) Better Can we achieve (1) search time? Chapter 9 CPTR 318 1 2 Use an array? Hash function Map a key to an array index


  1. 4/18/2013 Constant time access  Linear search Θ ( n ) OK Searching  Binary search Θ (log n ) Better  Can we achieve Θ (1) search time? Chapter 9 CPTR 318 1 2 Use an array? Hash function  Map a key to an array index  Use random access on a key such as a string?   { 0 , 1 , 2 ,..., n 1 }  h ( k ) = i , where i  h (“ mary ”) = 7 3 4 Better hash function? Hash function  Keys has at least three characters  English is not random  Let n be 10,007 (a prime number)  Only 2,851 combinations  Is this a good hash function? 5 6 1

  2. 4/18/2013 Better hash function Another Hash Function int hashpjw(char *s) { char *p; unsigned h = 0, g; for ( p = s; *p; p++ ) { h = (h << 4) + (*p); if ( g = h & 0xf0000000 ) { h ^= g >> 24; h ^= g; } } Aho, Sethi, and Ullman return h % PRIME; } 7 What about collisions? Perfect Hash Functions  What if two keys map to the same  Hash function is injective (1-1) location?  Possible only when values to store are fixed  Strategies: and known in advance  Open hashing (separate chaining)  To find a perfect hash function for n keys: Θ ( n )  Closed hashing (open addressing, or  Such a function can be evaluated in Θ (1) time probing)  Minimal perfect hash function  Linear probing  Range is n  Quadratic probing  Rehashing 10 Separate chaining Load factor  α (some authors use λ )  hash( x ) = x % 10  The ratio of the number of elements in the hash table to the table size  Average cost 1 + α /2 11 12 2

  3. 4/18/2013 Probing hash tables Disadvantages of separate chaining  Uses linked lists, so time to • Given a collision, find an empty cell via a dynamically allocate and collision resolution strategy deallocate nodes • Try cells h 0 ( x ), h 1 ( x ), h 2 ( x ),... until an empty  Requires implementing a cell is found second data structure (the • h i (x) = ( h ( x ) + f ( i )) % n linked list) • f (0) = 0  Space required to store the pointers • Load factor should be α < 0.5 14 Linear probing Linear probing  hash( x ) = x % 10  Add 89, 18, 49, 58, 69 • f is a linear function • Commonly, f ( i ) = i • Try sequential cells until an open cell is found 15 16 Problem with linear probing  Primary clustering  Number of probes  Insertion/unsuccessful search:   1 1    1    α 2   2 ( 1 )  Successful search:   1 1     1   α   2 ( 1 ) 17 3

  4. 4/18/2013 Quadratic probing Other strategies • f is a quadratic function  Double hashing • Commonly, f ( i ) = i 2  Apply second hash function • Eliminates the primary clustering problem  f ( i ) = i ∙ hash 2 ( x )  Rehashing  When the load factor gets high create a new table twice the size with new hash function  Rehash the existing values into the new table  Can be used with quadratic probing  Expensive to rehash, but amortized cost is low CPTR 314 20 How hard is it to … ? How hard is it to … ? Data Structure Find an List Find maximum Data Structure Find an List Find maximum element elements in (or minimum) element elements in (or minimum) order element order element Θ ( n ) Θ ( n 2 ) Θ ( n ) Θ ( n ) Θ ( n 2 ) Θ ( n ) Unordered list Unordered list Θ (log n ) Θ ( n ) Θ (log n ) Θ (log n ) Θ ( n ) Θ (log n ) Binary search tree Binary search tree Ordered list Θ (log n ) Θ ( n ) Θ ( 1 ) Ordered list Θ (log n ) Θ ( n ) Θ ( 1 ) Θ (1) Θ (?) Θ (?) Θ (1) Θ (?) Θ (?) Hash table Hash table How hard is it to … ? How hard is it to … ? Data Structure Find an List Find maximum Data Structure Find an List Find maximum element elements in (or minimum) element elements in (or minimum) order element order element Θ ( n ) Θ ( n 2 ) Θ ( n ) Θ ( n ) Θ ( n 2 ) Θ ( n ) Unordered list Unordered list Θ (log n ) Θ ( n ) Θ (log n ) Θ (log n ) Θ ( n ) Θ (log n ) Binary search tree Binary search tree Θ (log n ) Θ ( n ) Θ ( 1 ) Θ (log n ) Θ ( n ) Θ ( 1 ) Ordered list Ordered list Θ (1) Θ (?) Θ (?) Θ (1) Θ (?) Θ (?) Hash table Hash table 4

  5. 4/18/2013 How hard is it to … ? How hard is it to … ? Data Structure Find an List Find maximum Data Structure Find an List Find maximum element elements in (or minimum) element elements in (or minimum) order element order element Unordered list Θ ( n ) Θ ( n 2 ) Θ ( n ) Unordered list Θ ( n ) Θ ( n 2 ) Θ ( n ) Θ (log n ) Θ ( n ) Θ (log n ) Θ (log n ) Θ ( n ) Θ (log n ) Binary search tree Binary search tree Θ (log n ) Θ ( n ) Θ ( 1 ) Θ (log n ) Θ ( n ) Θ ( 1 ) Ordered list Ordered list Θ (1) Θ (?) Θ (?) Θ (1) Θ (?) Θ (?) Hash table Hash table How hard is it to … ? How hard is it to … ? Data Structure Find an List Find maximum Data Structure Find an List Find maximum element elements in (or minimum) element elements in (or minimum) order element order element Θ ( n ) Θ ( n 2 ) Θ ( n ) Θ ( n ) Θ ( n 2 ) Θ ( n ) Unordered list Unordered list Θ (log n ) Θ ( n ) Θ (log n ) Θ (log n ) Θ ( n ) Θ (log n ) Binary search tree Binary search tree Ordered list Θ (log n ) Θ ( n ) Θ ( 1 ) Ordered list Θ (log n ) Θ ( n ) Θ ( 1 ) Θ (1) Θ (?) Θ (?) Θ (1) Θ (?) Θ (?) Hash table Hash table How hard is it to … ? How hard is it to … ? Data Structure Find an List Find maximum Data Structure Find an List Find maximum element elements in (or minimum) element elements in (or minimum) order element order element Θ ( n ) Θ ( n 2 ) Θ ( n ) Θ ( n ) Θ ( n 2 ) Θ ( n ) Unordered list Unordered list Θ (log n ) Θ ( n ) Θ (log n ) Θ (log n ) Θ ( n ) Θ (log n ) Binary search tree Binary search tree Θ (log n ) Θ ( n ) Θ ( 1 ) Θ (log n ) Θ ( n ) Θ ( 1 ) Ordered list Ordered list Θ (1) Θ (?) Θ (?) Θ (1) Θ (?) Θ (?) Hash table Hash table 5

  6. 4/18/2013 How hard is it to … ? How hard is it to … ? Data Structure Find an List Find maximum Data Structure Find an List Find maximum element elements in (or minimum) element elements in (or minimum) order element order element Unordered list Θ ( n ) Θ ( n 2 ) Θ ( n ) Unordered list Θ ( n ) Θ ( n 2 ) Θ ( n ) Θ (log n ) Θ ( n ) Θ (log n ) Θ (log n ) Θ ( n ) Θ (log n ) Binary search tree Binary search tree Θ (log n ) Θ ( n ) Θ ( 1 ) Θ (log n ) Θ ( n ) Θ ( 1 ) Ordered list Ordered list Θ (1) Θ (?) Θ (?) Θ (1) Θ (?) Θ (?) Hash table Hash table How hard is it to … ? How hard is it to … ? Data Structure Find an List Find maximum Data Structure Find an List Find maximum element elements in (or minimum) element elements in (or minimum) order element order element Θ ( n ) Θ ( n 2 ) Θ ( n ) Θ ( n ) Θ ( n 2 ) Θ ( n ) Unordered list Unordered list Θ (log n ) Θ ( n ) Θ (log n ) Θ (log n ) Θ ( n ) Θ (log n ) Binary search tree Binary search tree Ordered list Θ (log n ) Θ ( n ) Θ ( 1 ) Ordered list Θ (log n ) Θ ( n ) Θ ( 1 ) Θ (1) Θ (?) Θ (?) Θ (1) Θ ( n 2 ) Θ ( n ) Hash table Hash table 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend