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

searching
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

4/18/2013 1

1

Searching

Chapter 9

CPTR 318

2

Constant time access

 Linear search Θ(n) OK  Binary search Θ(log n) Better  Can we achieve Θ(1) search time?

3

Use an array?

 Use random access on

a key such as a string?

4

Hash function

 Map a key to an array index  h(k) = i, where i  h(“mary”) = 7

} 1 ,..., 2 , 1 , {   n

5

Hash function

 Let n be 10,007 (a prime number)  Is this a good hash function?

6

Better hash function?

 Keys has at least three characters  English is not random  Only 2,851 combinations

slide-2
SLIDE 2

4/18/2013 2

7

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; } } return h % PRIME; }

Aho, Sethi, and Ullman

Perfect Hash Functions

 Hash function is injective (1-1)  Possible only when values to store are fixed

and known in advance

 To find a perfect hash function for n keys:

Θ(n)

 Such a function can be evaluated in Θ(1) time

 Minimal perfect hash function

 Range is n

10

What about collisions?

 What if two keys map to the same

location?

 Strategies:  Open hashing (separate chaining)  Closed hashing (open addressing, or

probing)

 Linear probing  Quadratic probing  Rehashing

11

Separate chaining

 hash(x) = x % 10

12

Load factor

 α (some authors use λ)  The ratio of the number

  • f elements in the hash

table to the table size

 Average cost 1 + α/2

slide-3
SLIDE 3

4/18/2013 3

Disadvantages of separate chaining

 Uses linked lists, so time to

dynamically allocate and deallocate nodes

 Requires implementing a

second data structure (the linked list)

 Space required to store the

pointers

14

Probing hash tables

  • Given a collision, find an empty cell via a

collision resolution strategy

  • Try cells h0(x), h1(x), h2(x),... until an empty

cell is found

  • hi(x) = (h(x) + f(i)) % n
  • f(0) = 0
  • Load factor should be α < 0.5

15

Linear probing

  • f is a linear function
  • Commonly, f(i) = i
  • Try sequential cells until an open cell is

found

16

Linear probing

 hash(x) = x % 10  Add 89, 18, 49, 58, 69

17

Problem with linear probing

 Primary clustering  Number of probes  Insertion/unsuccessful search:  Successful search:

         

2

) α 1 ( 1 1 2 1           ) α 1 ( 1 1 2 1

slide-4
SLIDE 4

4/18/2013 4

20

CPTR 314

Quadratic probing

  • f is a quadratic function
  • Commonly, f(i) = i 2
  • Eliminates the primary clustering problem

Other strategies

 Double hashing

 Apply second hash function  f(i) = i ∙ hash2(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

How hard is it to … ?

Data Structure Find an element List elements in

  • rder

Find maximum (or minimum) element Unordered list Θ(n) Θ(n2) Θ(n) Binary search tree Θ(log n) Θ(n) Θ(log n) Ordered list Θ(log n) Θ(n) Θ(1) Hash table Θ(1) Θ(?) Θ(?)

How hard is it to … ?

Data Structure Find an element List elements in

  • rder

Find maximum (or minimum) element Unordered list Θ(n) Θ(n2) Θ(n) Binary search tree Θ(log n) Θ(n) Θ(log n) Ordered list Θ(log n) Θ(n) Θ(1) Hash table Θ(1) Θ(?) Θ(?)

How hard is it to … ?

Data Structure Find an element List elements in

  • rder

Find maximum (or minimum) element Unordered list Θ(n) Θ(n2) Θ(n) Binary search tree Θ(log n) Θ(n) Θ(log n) Ordered list Θ(log n) Θ(n) Θ(1) Hash table Θ(1) Θ(?) Θ(?)

How hard is it to … ?

Data Structure Find an element List elements in

  • rder

Find maximum (or minimum) element Unordered list Θ(n) Θ(n2) Θ(n) Binary search tree Θ(log n) Θ(n) Θ(log n) Ordered list Θ(log n) Θ(n) Θ(1) Hash table Θ(1) Θ(?) Θ(?)

slide-5
SLIDE 5

4/18/2013 5 How hard is it to … ?

Data Structure Find an element List elements in

  • rder

Find maximum (or minimum) element Unordered list Θ(n) Θ(n2) Θ(n) Binary search tree Θ(log n) Θ(n) Θ(log n) Ordered list Θ(log n) Θ(n) Θ(1) Hash table Θ(1) Θ(?) Θ(?)

How hard is it to … ?

Data Structure Find an element List elements in

  • rder

Find maximum (or minimum) element Unordered list Θ(n) Θ(n2) Θ(n) Binary search tree Θ(log n) Θ(n) Θ(log n) Ordered list Θ(log n) Θ(n) Θ(1) Hash table Θ(1) Θ(?) Θ(?)

How hard is it to … ?

Data Structure Find an element List elements in

  • rder

Find maximum (or minimum) element Unordered list Θ(n) Θ(n2) Θ(n) Binary search tree Θ(log n) Θ(n) Θ(log n) Ordered list Θ(log n) Θ(n) Θ(1) Hash table Θ(1) Θ(?) Θ(?)

How hard is it to … ?

Data Structure Find an element List elements in

  • rder

Find maximum (or minimum) element Unordered list Θ(n) Θ(n2) Θ(n) Binary search tree Θ(log n) Θ(n) Θ(log n) Ordered list Θ(log n) Θ(n) Θ(1) Hash table Θ(1) Θ(?) Θ(?)

How hard is it to … ?

Data Structure Find an element List elements in

  • rder

Find maximum (or minimum) element Unordered list Θ(n) Θ(n2) Θ(n) Binary search tree Θ(log n) Θ(n) Θ(log n) Ordered list Θ(log n) Θ(n) Θ(1) Hash table Θ(1) Θ(?) Θ(?)

How hard is it to … ?

Data Structure Find an element List elements in

  • rder

Find maximum (or minimum) element Unordered list Θ(n) Θ(n2) Θ(n) Binary search tree Θ(log n) Θ(n) Θ(log n) Ordered list Θ(log n) Θ(n) Θ(1) Hash table Θ(1) Θ(?) Θ(?)

slide-6
SLIDE 6

4/18/2013 6 How hard is it to … ?

Data Structure Find an element List elements in

  • rder

Find maximum (or minimum) element Unordered list Θ(n) Θ(n2) Θ(n) Binary search tree Θ(log n) Θ(n) Θ(log n) Ordered list Θ(log n) Θ(n) Θ(1) Hash table Θ(1) Θ(?) Θ(?)

How hard is it to … ?

Data Structure Find an element List elements in

  • rder

Find maximum (or minimum) element Unordered list Θ(n) Θ(n2) Θ(n) Binary search tree Θ(log n) Θ(n) Θ(log n) Ordered list Θ(log n) Θ(n) Θ(1) Hash table Θ(1) Θ(?) Θ(?)

How hard is it to … ?

Data Structure Find an element List elements in

  • rder

Find maximum (or minimum) element Unordered list Θ(n) Θ(n2) Θ(n) Binary search tree Θ(log n) Θ(n) Θ(log n) Ordered list Θ(log n) Θ(n) Θ(1) Hash table Θ(1) Θ(?) Θ(?)

How hard is it to … ?

Data Structure Find an element List elements in

  • rder

Find maximum (or minimum) element Unordered list Θ(n) Θ(n2) Θ(n) Binary search tree Θ(log n) Θ(n) Θ(log n) Ordered list Θ(log n) Θ(n) Θ(1) Hash table Θ(1) Θ(n2) Θ(n)