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

Slides:



Advertisements
Similar presentations
Hash Tables.
Advertisements

Hashing.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
CSCE 3400 Data Structures & Algorithm Analysis
Lecture 11 oct 6 Goals: hashing hash functions chaining closed hashing application of hashing.
1.1 Data Structure and Algorithm Lecture 9 Hashing Topics Reference: Introduction to Algorithm by Cormen Chapter 12: Hash Tables.
Hash Tables How well do hash tables support dynamic set operations? Implementations –Direct address –Hash functions Collision resolution methods –Universal.
E.G.M. PetrakisHashing1  Data organization in main memory or disk  sequential, binary trees, …  The location of a key depends on other keys => unnecessary.
11.Hash Tables Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Directed-address tables Direct addressing is a simple technique that works well.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
Hashing Text Read Weiss, §5.1 – 5.5 Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Design and Analysis of Algorithms - Chapter 71 Hashing b A very efficient method for implementing a dictionary, i.e., a set with the operations: – insert.
Tirgul 9 Hash Tables (continued) Reminder Examples.
Introduction to Hashing CS 311 Winter, Dictionary Structure A dictionary structure has the form: (Key, Data) Dictionary structures are organized.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CSE 326 Hashing Richard Anderson (instead of Martin Tompa)
Tirgul 8 Hash Tables (continued) Reminder Examples.
Lecture 10: Search Structures and Hashing
Lecture 11 oct 7 Goals: hashing hash functions chaining closed hashing application of hashing.
Hashing General idea: Get a large array
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
1. 2 Problem RT&T is a large phone company, and they want to provide enhanced caller ID capability: –given a phone number, return the caller’s name –phone.
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
IT 60101: Lecture #151 Foundation of Computing Systems Lecture 15 Searching Algorithms.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
Implementing Dictionaries Many applications require a dynamic set that supports dictionary-type operations such as Insert, Delete, and Search. E.g., a.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
Comp 335 File Structures Hashing.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 23: Review.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Searching Given distinct keys k 1, k 2, …, k n and a collection of n records of the form »(k 1,I 1 ), (k 2,I 2 ), …, (k n, I n ) Search Problem - For key.
CS201: Data Structures and Discrete Mathematics I Hash Table.
David Luebke 1 11/26/2015 Hash Tables. David Luebke 2 11/26/2015 Hash Tables ● Motivation: Dictionaries ■ Set of key/value pairs ■ We care about search,
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Hashing Chapter 7 Section 3. What is hashing? Hashing is using a 1-D array to implement a dictionary o This implementation is called a "hash table" Items.
Introduction to Algorithms 6.046J/18.401J LECTURE7 Hashing I Direct-access tables Resolving collisions by chaining Choosing hash functions Open addressing.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Midterm Midterm is Wednesday next week ! The quiz contains 5 problems = 50 min + 0 min more –Master Theorem/ Examples –Quicksort/ Mergesort –Binary Heaps.
1 Chapter 9 Searching And Table. 2 OBJECTIVE Introduces: Basic searching concept Type of searching Hash function Collision problems.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Hashing Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision handling Separate chaining.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
CMSC 341 Hashing Readings: Chapter 5. Announcements Midterm II on Nov 7 Review out Oct 29 HW 5 due Thursday CMSC 341 Hashing 2.
CSC 413/513: Intro to Algorithms Hash Tables. ● Hash table: ■ Given a table T and a record x, with key (= symbol) and satellite data, we need to support:
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Hash table CSC317 We have elements with key and satellite data
CSCI 210 Data Structures and Algorithms
Hashing - resolving collisions
Hashing Alexandra Stefan.
Hash Tables (Chapter 13) Part 2.
EEE2108: Programming for Engineers Chapter 8. Hashing
Hashing Alexandra Stefan.
Richard Anderson (instead of Martin Tompa)
Hashing and Hash Tables
Collision Resolution Neil Tang 02/18/2010
Introduction to Algorithms 6.046J/18.401J
Resolving collisions: Open addressing
CS202 - Fundamental Structures of Computer Science II
Introduction to Algorithms
A Hash Table with Chaining
Chapter 11: Hash Tables.
Collision Resolution Neil Tang 02/21/2008
Chapter 11: Hash Tables.
Ch Hash Tables Array or linked list Binary search trees
Presentation transcript:

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

Announcements Advisory Board’s Industrial Talk Series Next Research Colloquia: Prof. Nael Abu-Ghazaleh 11:00am “Active Routing in Mobile Ad Hoc Networks”

CS Story Time Prof. Jones’ research group See

Next Midterm Tuesday before Thanksgiving !

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

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

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

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.

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

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

Uniform Inputs: Balanced Chains Chains:

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

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 )

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

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 ] = ½

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].

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

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…

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

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.