Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Computer Algorithms Lecture 16 Phillip G. Bradford Computer Science University of Alabama.

Similar presentations


Presentation on theme: "Intro to Computer Algorithms Lecture 16 Phillip G. Bradford Computer Science University of Alabama."— Presentation transcript:

1 Intro to Computer Algorithms Lecture 16 Phillip G. Bradford Computer Science University of Alabama

2 Announcements Advisory Board’s Industrial Talk Series http://www.cs.ua.edu/9IndustrialSeries.shtm Next Research Colloquia: Prof. Nael Abu-Ghazaleh 10-Nov @ 11:00am “Active Routing in Mobile Ad Hoc Networks”

3 CS Story Time Prof. Jones’ research group See http://cs.ua.edu/StoryHourSlide.pdfhttp://cs.ua.edu/StoryHourSlide.pdf

4 Next Midterm Tuesday before Thanksgiving !

5 Outline The Dictionary Problem and Hashing Open Hashing with Chaining Hashing with Open Addressing Double Hashing Introduction to Dynamic Programming

6 The Dictionary Problem Motivation Fundamental Operations Insert Search Delete Desire Quick Lookup Large Records & We Focus on the Key

7 Hash Tables Two desired properties Uniform distribution in the table Let K be the key to hash and h is the hash function Table of size m and for any i in {1,…,m}, then P[i = h(K)] = 1/m The hash function h is easy to compute

8 Hash Tables What are some candidates for h? h(x) = (ax + b) mod m For appropriate (random?) a and b and m=p a prime Let K=c 0 c 1 …c n, characters h(x): R  0; // and appropriate C For i  1 to n do R  (R*C + ord(c i )) mod m Endfor Return R.

9 Hash Tables These functions all resemble linear- congruential pseudo-random number generators! Why is this not surprising? Consider two different keys K i and K j A hash collision is when h(K i ) = h(K j ) Two approaches to resolve this situation

10 Hash Collision Resolution Methods Separate Chaining Also called Open Hashing Given n keys, how long can chains be? Worst case? Expected length? What is the expectation computed over? Load Factor  = n/m In an average chain, when might we expect to find an element? Successful Searches: about  comparisons Unsuccessful searches: about  comparisons

11 Uniform Inputs: Balanced Chains Chains:

12 Hash Collision Resolution Methods Linear Probing Inserting a key K Start at h(K) and if it is full, then try h(K)+1, h(K)+2, etc., until one of these is empty How do we know that the hash table is full? What can we track? U is about (1+1/(1-  ) 2 )/2 S is about (1+1/(1-  ))/2

13 Hash Collision Resolution Methods Double Hashing Two pseudo-random functions h 1 (x) and h 2 (x) Use h 1 (x) to find the `starting place’ If done, then stop Use h 2 (x) to determine how much to `hop’ from there L  h 1 (x) ; try position H[L] first Q  h 2 (x) Otherwise, try positions H[L+ iQ mod m] For i  1 to floor( m/n )

14 Hash Collision Resolution Methods U is about 1+1/(1-  ) S is about 1/(1-  )

15 A Basic Fact CLRS2001: I{A} = 1 if A happens And I{A} = 0 if A does not happen Coin Example X H = I{X = H}. X H = 1 if coin is heads X H = 0 if coin is tails P[X H ] = ½ But also, E[X H ] = ½

16 A Basic Fact Lemma [See CLRS2001] Take A in sample space S, if X A =I{A}, then E[X A ] = P[A]. Proof: E[X A ] = 1*P[A] + 0*P[Not(A)] = P[A].

17 A Look “Under the Hood” Hashing by chaining has average case O(1+  ) probes for a successful search. Proof Sketch (full details at the board) Assume P[i=h(K)]=1/m Therefore by the last Lemma E[i=h(K)]=1/m

18 A Look “Under the Hood” The number of probes in a successful search E[1/n  (1+  X i,j )] Outer sum i  1 to n Inner sum j  i+1 to n Why? Rest at the board… Double Hashing on Thursday…

19 Introduction to Dynamic Programming Richard Bellman in the 1950s Optimizing multistage decision processes Problems generally have overlapping subproblems

20 Dynamic Programming Example Pascal’s Triangle (a+b) n Classic Relation C(n,k) = C(n-1,k-1) + C(n-1,k) Base conditions: C(n,0)=C(n,n)=1.


Download ppt "Intro to Computer Algorithms Lecture 16 Phillip G. Bradford Computer Science University of Alabama."

Similar presentations


Ads by Google