Intro to Computer Algorithms Lecture 16 - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Intro to Computer Algorithms Lecture 16

Description:

Let K be the key to hash and h is the hash function ... These functions all resemble linear-congruential pseudo-random number generators! ... Inserting a key K ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 21
Provided by: pgb8
Category:

less

Transcript and Presenter's Notes

Title: Intro to Computer Algorithms Lecture 16


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

2
Announcements
  • Advisory Boards Industrial Talk Series
  • http//www.cs.ua.edu/9IndustrialSeries.shtm
  • Next Research Colloquia
  • Prof. Nael Abu-Ghazaleh
  • 10-Nov _at_ 1100am
  • Active Routing in Mobile Ad Hoc Networks

3
CS Story Time
  • Prof. Jones research group
  • See http//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
  • Pi 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 mp a prime
  • Let Kc0c1cn, characters
  • h(x)
  • R?0 // and appropriate C
  • For i?1 to n do
  • R?(RC ord(ci)) 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 Ki and Kj
  • A hash collision is when h(Ki) h(Kj)
  • 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 a n/m
  • In an average chain, when might we expect to find
    an element?
  • Successful Searches about 1 a/2 comparisons
  • Unsuccessful searches about a comparisons

11
Uniform Inputs Balanced Chains
  • Chains

NULL
NULL
NULL
NULL
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 (11/(1-a)2)/2
  • S is about (11/(1-a))/2

13
Hash Collision Resolution Methods
  • Double Hashing
  • Two pseudo-random functions
  • h1(x) and h2(x)
  • Use h1(x) to find the starting place
  • If done, then stop
  • Use h2(x) to determine how much to hop from
    there
  • L? h1(x) try position HL first
  • Q ? h2(x)
  • Otherwise, try positions HL iQ mod m
  • For i?1 to floor( m/n )

14
Hash Collision Resolution Methods
  • U is about 11/(1-a)
  • S is about 1/(1-a)

15
A Basic Fact
  • CLRS2001 IA 1 if A happens
  • And IA 0 if A does not happen
  • Coin Example XH IX H.
  • XH 1 if coin is heads
  • XH 0 if coin is tails
  • PXH ½
  • But also, EXH ½

16
A Basic Fact
  • Lemma See CLRS2001 Take A in sample space S, if
    XAIA,
  • then EXA PA.
  • Proof
  • EXA 1PA 0PNot(A)
  • PA.

17
A Look Under the Hood
  • Hashing by chaining has average case O(1a)
    probes for a successful search.
  • Proof Sketch (full details at the board)
  • Assume Pih(K)1/m
  • Therefore by the last Lemma Eih(K)1/m

18
A Look Under the Hood
  • The number of probes in a successful search
  • E1/n S(1SXi,j)
  • Outer sum i?1 to n
  • Inner sum j?i1 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
  • Pascals Triangle
  • (ab)n
  • Classic Relation
  • C(n,k) C(n-1,k-1) C(n-1,k)
  • Base conditions C(n,0)C(n,n)1.
Write a Comment
User Comments (0)
About PowerShow.com