CS70 L23 Hashing (1)Dan Garcia © UCB 2003-10-22 Dan Garcia (www.cs.berkeley.edu/~ddgarcia) inst.eecs.berkeley.edu/~cs70/ 1 Handout: notes Computer Science.

Slides:



Advertisements
Similar presentations
4/16/2015 MATH 224 – Discrete Mathematics Counting Basic counting techniques are important in many aspects of computer science. For example, consider the.
Advertisements

Recursively Defined Functions
Integer Tic Tac Toe Let’s Begin Rules: 1.Erase all x’s and o’s from the previous game before you begin. 2.Decide which player will be x’s and which will.
Python Programming: An Introduction to Computer Science
3.5 Paradox  1.Russell’s paradox  A  A, A  A 。  Russell’s paradox: Let S={A|A  A}. The question is, does S  S?  i.e. S  S or S  S?  If S  S,
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Recursion. n! (n factorial)  The number of ways n objects can be permuted (arranged).  For example, consider 3 things, A, B, and C.  3! = 6 1.ABC 2.ACB.
Chapter 3b Counting Rules. Permutations  How many ways can 5 students in a class of 30 be assigned to the front row in the seating chart?  There are.
Using arrays – Example 2: names as keys How do we map strings to integers? One way is to convert each letter to a number, either by mapping them to 0-25.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
CPSC 311, Fall 2009: Dynamic Programming 1 CPSC 311 Analysis of Algorithms Dynamic Programming Prof. Jennifer Welch Fall 2009.
Counting II: Recurring Problems And Correspondences Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2004 Lecture 10Feb 12, 2004Carnegie.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Chapter 5: Hashing Hash Tables
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Hash Tables1 Part E Hash Tables  
CPSC 411 Design and Analysis of Algorithms Set 5: Dynamic Programming Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 5 1.
Discrete Mathematics Recursion and Sequences
Hashing General idea: Get a large array
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CS 206 Introduction to Computer Science II 02 / 25 / 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.
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Game Trees: MiniMax strategy, Tree Evaluation, Pruning, Utility evaluation Adapted from slides of Yoonsuck Choe.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
Hashing CS 105. Hashing Slide 2 Hashing - Introduction In a dictionary, if it can be arranged such that the key is also the index to the array that stores.
Basic Counting. This Lecture We will study some basic rules for counting. Sum rule, product rule, generalized product rule Permutations, combinations.
1 Problem Solving using computers Data.. Representation & storage Representation of Numeric data The Binary System.
Discrete Mathematical Structures (Counting Principles)
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Mark Dunlop, Computer and Information Sciences, Strathclyde University 1 Algorithms & Complexity 5 Games Mark D Dunlop.
Fall 2002CMSC Discrete Structures1 One, two, three, we’re… Counting.
COMPSCI 102 Discrete Mathematics for Computer Science.
Discrete Mathematics for Computer Science. + + ( ) + ( ) = ? Counting II: Recurring Problems and Correspondences Chapter 9 slides 1-54.
Great Theoretical Ideas in Computer Science.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
CS201: Data Structures and Discrete Mathematics I Hash Table.
March 23 & 28, Csci 2111: Data and File Structures Week 10, Lectures 1 & 2 Hashing.
CRYPTOGRAPHY. WHAT IS PUBLIC-KEY ENCRYPTION? Encryption is the key to information security The main idea- by using only public information, a sender can.
Hashing CS 110: Data Structures and Algorithms First Semester,
Today’s Topics Playing Deterministic (no Dice, etc) Games –Mini-max –  -  pruning –ML and games? 1997: Computer Chess Player (IBM’s Deep Blue) Beat Human.
Dynamic Programming. Many problem can be solved by D&C – (in fact, D&C is a very powerful approach if you generalize it since MOST problems can be solved.
CS1101S Lecture 15: Memoization 12 Oct Using Tables Problem: determine word frequency in a text message. e.g. given ’(mary had a little lamb, little.
Foundations of Algorithms, Fourth Edition
Counting II: Recurring Problems And Correspondences Great Theoretical Ideas In Computer Science John LaffertyCS Fall 2005 Lecture 7Sept 20, 2005Carnegie.
Dynamic Programming David Kauchak cs161 Summer 2009.
CMSC 341 Hashing Readings: Chapter 5. Announcements Midterm II on Nov 7 Review out Oct 29 HW 5 due Thursday CMSC 341 Hashing 2.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
2/24/20161 One, two, three, we’re… Counting. 2/24/20162 Basic Counting Principles Counting problems are of the following kind: “How many different 8-letter.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
Fundamental Data Structures and Algorithms Ananda Guna March 18, 2003 Dynamic Programming Part 1.
Welcome to... A Game of X’s and O’s
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
More Mathematical Techniques
Discrete Structures for Computer Science
Hash table CSC317 We have elements with key and satellite data
Dynamic Programming.
CS 240 – Lecture 9 Bit Shift Operations, Assignment Expressions, Modulo Operator, Converting Numeric Types to Strings.
Basic Counting.
Counting Discrete Mathematics.
Counting II: Recurring Problems And Correspondences
Data Structures Introduction
Counting II: Recurring Problems And Correspondences
Presentation transcript:

CS70 L23 Hashing (1)Dan Garcia © UCB Dan Garcia ( inst.eecs.berkeley.edu/~cs70/ 1 Handout: notes Computer Science 70 Discrete Mathematics and Probability Theory Hashing Lecture 23

CS70 L23 Hashing (2)Dan Garcia © UCB Big Idea: memoization General principle: store rather than recompute. Context is a tree-recursive algorithm with lots of repeated computation, e.g. Fibonacci: int Fib (int n) { if (n==0 || n==1) { return n; } else if ( we’ve computed n 's value already ) { return that value ; } else { int value = Fib(n-1) + Fib(n-2); store (n, value); return value; } } Pairs ( n, value of Fib(n) ) are stored in the table.

CS70 L23 Hashing (3)Dan Garcia © UCB Hash Function If what we want to memoize isn’t a simple number, how do we convert it to a number to easily store it into a table? We need something that can help us map this data into an integer, to serve as an index into an array (used to store the table). This mapping function is called a hash function

CS70 L23 Hashing (4)Dan Garcia © UCB Writing hash functions - TTT (1) Let's consider Tic-Tac-Toe: –One player chooses X, the other chooses O –They take turns placing their piece on the board –Assume X goes first –Once a piece is placed, it isn't moved –The player who first gets 3-in-a-row wins –If the board gets filled up and nobody wins, it's a tie

CS70 L23 Hashing (5)Dan Garcia © UCB Writing hash functions - TTT (2) Writing a Tic-Tac-Toe hash function: One idea is to ignore the 2D nature of the game and make it a 1D array of slots h= 13,

CS70 L23 Hashing (6)Dan Garcia © UCB Writing hash functions - TTT (3) Think of each of the 9 slots as 1 of 3 values –Blank, O and X –Let's assign values 0, 1 and 2 to these How can we create a single number from this? –Let's think about this as a ternary number: –S S … + S S – = 13, 205 This is known as a "polynomial hash code"

CS70 L23 Hashing (7)Dan Garcia © UCB Writing hash functions - TTT (4) Analysis of ternary polynomial hashcode: –What's the smallest #? –What's the biggest #? –Is this as optimal (I.e., tightly-packed) as possible? –Any suggestions for making this more optimal? No!

CS70 L23 Hashing (8)Dan Garcia © UCB Writing hash functions - TTT (5) Optimizing the Tic-Tac-Toe hash function –This involves understanding the rules of placement »The players take turns & X goes first! –Let's consider some small 1D boards (S = # of slots) »S=1: 2 boards (- | X) We'll use "|" to separate groups »S=2: 5 boards (-- | -X, X- | XO, OX) »S=3: 13 boards (--- | --X, -X-, X-- | -OX, -XO, O-X, OX-, X-O, XO- | OXX, XOX, XXO) = ( ) »S=4: __ boards ( __ + __ + __ + __ + __ ) »…pattern?

CS70 L23 Hashing (9)Dan Garcia © UCB Remember your Combinatorics! Let's figure out numBoards(s), s = # slots For n=5, we had: # ways to rearrange 0 Xs, 0 Os in 4 slots + # ways to rearrange 1 Xs, 0 Os in 4 slots + # ways to rearrange 1 Xs, 1 Os in 4 slots + # ways to rearrange 2 Xs, 1 Os in 4 slots + # ways to rearrange 2 Xs, 2 Os in 4 slots Generalizing from this example (p=# pieces): But what is rearrange(x,o,s)? –# of ways to rearrange x Xs, o Os in s slots?

CS70 L23 Hashing (10)Dan Garcia © UCB Recall Pascal's Triangle ( )= This table describes how to calculate combinations. I.e., "N choose K". That is, the number of ways to rearrange 2 pieces in 5 slots is "5 choose 2", which is the expression at the top. 10 ways. NKNK = N! K! (N-K)! N K

CS70 L23 Hashing (11)Dan Garcia © UCB rearrange(x,o,s) = r(x,o,s) How many ways to rearrange x Xs, o Os in s slots? Blur method –First, blur eyes, how many ways to rearrange ALL (x+o) pieces in s slots? [stop blurring now] –For EACH, how many ways to rearrange Xs in pieces? –Answer is product of these Overcount method –Think of permuting all the elements; how many? –How many were overcounted? Xs, Os, spaces –Answer is quotient of these s x+o x+o x s! o!x!(s-x-o)! s! o!x!(s-x-o)! s x+o x+o x s! (s-x-o)!(x+o)! (x+o)! o! x! =

CS70 L23 Hashing (12)Dan Garcia © UCB Now we know our Hash Table size Now we know numBoards(s) –numBoards(4)  ( ) = 35 –numBoards(9)  ( ) = 6,046 < 19,683 = 3 9 Plotting rearrange(x,o,4) o x Note zig-zag pattern as a result of the alternating moves of each player! numBoards just sums 'em! s=4

CS70 L23 Hashing (13)Dan Garcia © UCB But what about the hash function? How do we write the combinatorially optimal hash() ? –This take our board and generates a # between 0 and ( numBoards - 1) Two steps (sum the following numbers) 1.Finding out how many numbers there were in the zigzag up to our box (this is the BIAS, or OFFSET) 2.Finding out our number REARRANGEMENT within our box »Exactly same idea as the ternary polynomial hash code: X counts as 2, i.e., 23 i, O counts as 1, I.e., 13 i, – = 0 »Here, we consider the leftmost slot & how much it's worth X counts for all ways to rearrange if it were O & – O counts for all ways to rearrange if it were – – counts for 0 (Shortcut when a board has all the same piece, counts for 0)

CS70 L23 Hashing (14)Dan Garcia © UCB Example TicTacToe hash function Let's hash XO-X = X 3 O X 0 –Must be a # between 0 and ( numBoards(4) - 1) = 34 Two steps: BIAS + REARRANGEMENT # –BIAS: X=2,O=1,S=4; Count buckets up to us:1+4+12=17 –REARRANGEMENT #: [R(X,O,S)] »X 3 = r(2,1,3) + r(2,0,3) = »O 2 = r(1,1,2) = 2 »– 1 = 0 »X 0 = 0 (from shortcut) »REARRANGEMENT # = = 8 Thus, combinatorially optimal hash (XO-X) = = o x s=4

CS70 L23 Hashing (15)Dan Garcia © UCB Summary We showed how to calculate combinatorially optimal hash functions for a game –In real-world applications, we often find this useful –If it's too expensive, we usu. settle for sub-optimal A good hash function spreads out values evenly Sometimes hard to write good hash function –In 8 real applications, 2 had written poor hash funs Java has a great hash function for Strings –Strings are commonly used as the keys (the things you hash upon for a data structure)