CSE 326: Data Structures Hashing Ben Lerner Summer 2007.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CSCE 3400 Data Structures & Algorithm Analysis
© 2004 Goodrich, Tamassia Hash Tables1  
Hashing CS 3358 Data Structures.
CSE 326: Data Structures Disjoint Union/Find Ben Lerner Summer 2007.
Disjoint Union / Find CSE 373 Data Structures Lecture 17.
CSE 326: Data Structures Disjoint Union/Find. Equivalence Relations Relation R : For every pair of elements (a, b) in a set S, a R b is either true or.
Dictionaries and Hash Tables1  
Lecture 10 Sept 29 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
Lecture 18 Nov 3 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
CSC 2300 Data Structures & Algorithms February 27, 2007 Chapter 5. Hashing.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
REPRESENTING SETS CSC 172 SPRING 2002 LECTURE 21.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Tirgul 9 Hash Tables (continued) Reminder Examples.
Hash Tables1 Part E Hash Tables  
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.
Tirgul 8 Hash Tables (continued) Reminder Examples.
Lecture 10: Search Structures and Hashing
Hashing. Hashing as a Data Structure Performs operations in O(c) –Insert –Delete –Find Is not suitable for –FindMin –FindMax –Sort or output as sorted.
Data Structures Hashing Uri Zwick January 2014.
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.
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 7 Prepared by İnanç TAHRALI.
Oct 29, 2001CSE 373, Autumn External Storage For large data sets, the computer will have to access the disk. Disk access can take 200,000 times longer.
1 22c:31 Algorithms Union-Find for Disjoint Sets.
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.
Hash Tables1   © 2010 Goodrich, Tamassia.
1 CSE 326: Data Structures: Hash Tables Lecture 12: Monday, Feb 3, 2003.
Hashing1 Hashing. hashing2 Observation: We can store a set very easily if we can use its keys as array indices: A: e.g. SEARCH(A,k) return A[k]
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
CSE 326: Data Structures Lecture #12
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,
Chapter 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
CSC 172 DATA STRUCTURES. SETS and HASHING  Unadvertised in-store special: SETS!  in JAVA, see Weiss 4.8  Simple Idea: Characteristic Vector  HASHING...The.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
CSE 373 Data Structures and Algorithms Lecture 17: Hashing II.
Hashing Fundamental Data Structures and Algorithms Margaret Reid-Miller 18 January 2005.
Hashing, Hashing Tables Chapter 8. Class Hierarchy.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
Hashing Suppose we want to search for a data item in a huge data record tables How long will it take? – It depends on the data structure – (unsorted) linked.
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.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
1 Hashing by Adlane Habed School of Computer Science University of Windsor May 6, 2005.
CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets and the Union-Find ADT Lauren Milne Summer 2015.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Week 9 - Monday.  What did we talk about last time?  Practiced with red-black trees  AVL trees  Balanced add.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
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:
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
Lecture 10 Hashing.
CSE373: Data Structures & Algorithms Lecture 6: Hash Tables
Hashing CSE 2011 Winter July 2018.
CS223 Advanced Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 10: Disjoint Sets and the Union-Find ADT Linda Shapiro Spring 2016.
CSE 332: Data Structures Disjoint Set Union/Find
CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets & Union-Find Dan Grossman Fall 2013.
CS223 Advanced Data Structures and Algorithms
Data Structures and Algorithm Analysis Hashing
CSE 373: Data Structures and Algorithms
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

CSE 326: Data Structures Hashing Ben Lerner Summer 2007

2 From last time – Cute Application Build a random maze by erasing edges.

3 Cute Application Pick Start and End Start End

4 Cute Application Repeatedly pick random edges to delete. Start End

5 Desired Properties None of the boundary is deleted Every cell is reachable from every other cell. There are no cycles – no cell can reach itself by a path unless it retraces some part of the path.

6 A Cycle Start End

7 A Good Solution Start End

8 A Hidden Tree Start End

9 Number the Cells Start End We have disjoint sets S ={ {1}, {2}, {3}, {4},… {36} } each cell is unto itself. We have all possible edges E ={ (1,2), (1,7), (2,8), (2,3), … } 60 edges total.

10 Basic Algorithm S = set of sets of connected cells E = set of edges Maze = set of maze edges initially empty While there is more than one set in S pick a random edge (x,y) and remove from E u := Find(x); v := Find(y); if u  v then Union(u,v) else add (x,y) to Maze All remaining members of E together with Maze form the maze

11 Example Step Start End S {1,2,7,8,9,13,19} {3} {4} {5} {6} {10} {11,17} {12} {14,20,26,27} {15,16,21}. {22,23,24,29,30,32 33,34,35,36} Pick (8,14)

12 Example S {1,2,7,8,9,13,19} {3} {4} {5} {6} {10} {11,17} {12} {14,20,26,27} {15,16,21}. {22,23,24,29,39,32 33,34,35,36} Find(8) = 7 Find(14) = 20 S {1,2,7,8,9,13,19,14,20 26,27} {3} {4} {5} {6} {10} {11,17} {12} {15,16,21}. {22,23,24,29,39,32 33,34,35,36} Union(7,20)

13 Example Start End S {1,2,7,8,9,13,19 14,20,26,27} {3} {4} {5} {6} {10} {11,17} {12} {15,16,21}. {22,23,24,29,39,32 33,34,35,36} Pick (19,20)

14 Example at the End Start End S {1,2,3,4,5,6,7,… 36} E Maze

15 Hashing

16 Implementations So Far Unsorted linked list Sorted Array BSTAVLSplay (amortized) Insert Find Delete Student Activity

17 Hash Tables Constant time accesses! A hash table is an array of some fixed size, usually a prime number. General idea: key space (e.g., integers, strings) 0 … TableSize –1 hash function: h(K) hash table

18 Example key space = integers TableSize = 10 h(K) = K mod 10 Insert: 7, 18, 41,

19 Another Example key space = integers TableSize = 6 h(K) = K mod 6 Insert: 7, 18, 41, Student Activity

20 Hash Functions 1.simple/fast to compute, 2.Avoid collisions 3.have keys distributed evenly among cells. Perfect Hash function:

21 Sample Hash Functions: key space = strings s = s 0 s 1 s 2 … s k-1 1.h(s) = s 0 mod TableSize 2.h(s) = mod TableSize 3.h(s) = mod TableSize

22 Designing a Hash Function for web URLs s = s 0 s 1 s 2 … s k-1 Issues to take into account: h(s) = Student Activity

23 Compare Hash Functions for web URLs h(s) = mod TableSize Pros: Cons: h(s) = mod TableSize Pros: Cons:

24 Good Hash Functions Integers: Division method –Choose Hsize to be a prime (why?) –h(n) = n mod Hsize –Example. Hsize = 23, h(50) = 4, h(1257) = 15 –When might this fail?

25 Good Hash Functions Character Strings –x = a 0 a 1 a 2 …a m is a character string. Define int(x) = a 0 +a a … +a m 128 m-1 h(x) = int(x) mod Hsize –Compute h(x) using Horner’s Rule h :=0 for i = m to 0 by -1 do h := (a i +128h) mod Hsize return h

26 A Bad Hash Function Keys able1, able2, able3, able4 –Hsize = 128 int(ableX) mod 128 = int(a) = 97 Thus, h(ableX) = h(ableY) for all X and Y What’s the main problem we’re trying to avoid? How can we fix it?