Hash Tables and Constant Access Time CS-2303, C-Term 20101 Hash Tables and Constant Access Time CS-2303 System Programming Concepts (Slides include materials.

Slides:



Advertisements
Similar presentations
Information Security of Embedded Systems : Design of Secure Systems Prof. Dr. Holger Schlingloff Institut für Informatik und Fraunhofer FIRST.
Advertisements

Summary Overview of Vireo Student Submission of ETDs
Under the Hood of Polymorphism CS-2303, C-Term Under the Hood of Polymorphism CS-2303 System Programming Concepts (Slides include materials from.
Pointers.
® Microsoft Office 2010 Excel Tutorial 3: Working with Formulas and Functions.
® Microsoft Office 2010 Managing Your Files. XP Objectives Develop file management strategies Explore files, folders, and libraries Create, name, copy,
August 4, The following PEIMS reporting changes have been made to the PEIMS Collection in order to collect the Classroom Link information.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Stacks, Queues, and Linked Lists
Hash Tables CSC220 Winter What is strength of b-tree? Can we make an array to be as fast search and insert as B-tree and LL?
A useful testing technique and more…
SYMBOL TABLES &CODE GENERATION FOR EXECUTABLES. SYMBOL TABLES Compilers that produce an executable (or the representation of an executable in object module.
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Hashing.
Tutorial 5: Working with Excel Tables, PivotTables, and PivotCharts
CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
Tutorial 1 Creating a Database
ACOT Intro/Copyright Succeeding in Business with Microsoft Excel 2010: Chapter1.
Collaboration Works! 10/20/20101 Planning Research Institutional Effectiveness.
Quick Training Guide New SpringerLink, August 2010.
Chapter 13 – Aggregate Planning
Tutorial 8 Sharing, Integrating, and Analyzing Data
Introduction to Database Systems1 Records and Files Storage Technology: Topic 3.
Binary TreesCS-2303, C-Term Binary Trees (and Big “O” notation) CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
Event-drive SimulationCS-2303, C-Term Project #3 – Event-driven Simulation CS-2303 System Programming Concepts (Slides include materials from The.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 18: Hash Tables.
1 Hash Tables Gordon College CS Hash Tables Recall order of magnitude of searches –Linear search O(n) –Binary search O(log 2 n) –Balanced binary.
Lists and TreesCS-2301 D-term Data Structures — Lists and Trees CS-2301 System Programming D-term 2009 (Slides include materials from The C Programming.
Lists and Trees (continued) CS-2301, B-Term Lists and Trees (continued) CS-2301, System Programming for Non-Majors (Slides include materials from.
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.
More on Data Structures in C CS-2301 B-term More on Lists and Trees Introduction to Hash Tables CS-2301, System Programming for Non-majors (Slides.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
Review of Exam #2CS-2301, B-Term Review of Exam #2 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language,
Hash TablesCS-2301, B-Term Hash Tables and Constant Access Time CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
More on Data Structures in C CS-2301 D-term More on Data Structures in C CS-2301 System Programming D-term 2009 (Slides include materials from The.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
Data Structures, Lists, and Trees CS-2301 B-term Data Structures — Lists and Trees CS-2301, System Programming for Non-majors (Slides include materials.
Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
1 Chapter 5 Hashing General ideas Methods of implementing the hash table Comparison among these methods Applications of hashing Compare hash tables with.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
1 Symbol Tables The symbol table contains information about –variables –functions –class names –type names –temporary variables –etc.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Indexing and hashing Azita Keshmiri CS 157B. Basic concept An index for a file in a database system works the same way as the index in text book. For.
Hashing Hashing is another method for sorting and searching data.
Hashing as a Dictionary Implementation Chapter 19.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
Linked Lists in C and C++
Binary Trees (and Big “O” notation)
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Hash Tables and Constant Access Time CS-2303, C-Term Hash Tables and Constant Access Time CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Hash Tables and Constant Access Time CS-2303, C-Term New Challenge What if we require a data structure that has to be accessed by value in constant time? I.e., O(log n) is not good enough! Need to be able to add or delete items Total number of items unknown But an approximate maximum might be known

Hash Tables and Constant Access Time CS-2303, C-Term Examples Anti-virus scanner Symbol table of compiler Virtual memory tables in operating system Bank or credit card account for a person

Hash Tables and Constant Access Time CS-2303, C-Term Example – Validate a Credit Card 16-digit credit card numbers possible card numbers Sparsely populated space E.g., 10 8 MasterCard holders, similar for Visa Not random enough for a binary tree Too many single branches really deep searches Need to respond to customer in 1-2 seconds 1000s or tens of 1000s of customers per second! Same is true for ATM card numbers Bank account numbers Etc.

Hash Tables and Constant Access Time CS-2303, C-Term Example Anti-Virus Scanner Look at each sequence of bytes in a file See if it matches against library of virus patterns How many possible patterns? If so, flag it as a possible problem Tens of Thousands!

Hash Tables and Constant Access Time CS-2303, C-Term Anti-Virus Scanner (continued) Time to scan a file? O(length) O(# of patterns) Can we do better? Store patterns in a tree O(length) O(log (# of patterns)) Can we do even better? Yes a Hash Table. Todays topic.

Hash Tables and Constant Access Time CS-2303, C-Term Requirement In these applications (and many like them), need constant time access I.e., O(1) Need to access by value!

Hash Tables and Constant Access Time CS-2303, C-Term Observation Arrays provide constant time access … … but you have to know which element you want! We only know the contents of the item we want! Also Not easy to grow or shrink Not open-ended Can we do better?

Hash Tables and Constant Access Time CS-2303, C-Term Definition – Hash Table A data structure comprising an array for constant time access A set of linked lists one list for each array element A hashing function to convert search key to array index a randomizing function to assure uniform distribution of values across array indices Also known as a hash function

Hash Tables and Constant Access Time CS-2303, C-Term Definition – Search Key A value stored as (part of) the payload of the item you are looking for E.g., your credit card number Your account number at Amazon A pattern characteristic of a virus Need to find the item containing that value (i.e., that key)

Hash Tables and Constant Access Time CS-2303, C-Term Definition – Hash Function A function that randomizes the search key it to produce an index into the array Always returns the same value for the same key So that non-random keys dont concentrate around a subset of the indices in the array See §6.6 in Kernighan & Ritchie

Hash Tables and Constant Access Time CS-2303, C-Term data next Hash Table Structure item... data next data next data next data next data next data next data next data next data next data next data next data next The array The lists

Hash Tables and Constant Access Time CS-2303, C-Term data next Hash Table Structure (continued) item... data next data next data next data next data next data next data next data next data next data next data next data next The array Note that some of the lists are empty Average length of list should be in single digits

Hash Tables and Constant Access Time CS-2303, C-Term Guidelines for Hash Tables Lists from each item should be short I.e., with short search time (approximately constant) Size of array should be based on expected # of entries Err on large side if possible Hashing function Should spread out the values relatively uniformly Multiplication and division by prime numbers usually works well

Hash Tables and Constant Access Time CS-2303, C-Term Example Hashing Function P. 144 of K & R #define HASHSIZE 101 unsigned int hash(char *s) { unsigned int hashval; for (hashval = 0; *s != '\0'; s++) hashval = *s + 31 * hashval; return hashval % HASHSIZE } Note prime numbers to mix it up

Hash Tables and Constant Access Time CS-2303, C-Term Using a Hash Table struct item *lookup(char *s) { struct item *np; for (np = hashtab[hash(s)]; np != NULL; np = np -> next) if (strcmp(s, np->data) == 0) return np; /*found*/ return NULL;/* not found */ }

Hash Tables and Constant Access Time CS-2303, C-Term Using a Hash Table struct item *lookup(char *s) { struct item *np; for (np = hashtab[hash(s)]; np != NULL; np = np -> next) if (strcmp(s, np->data) == 0) return np; /*found*/ return NULL;/* not found */ } Hash table is indexed by hash value of s

Hash Tables and Constant Access Time CS-2303, C-Term Using a Hash Table struct item *lookup(char *s) { struct item *np; for (np = hashtab[hash(s)]; np != NULL; np = np -> next) if (strcmp(s, np->data) == 0) return np; /*found*/ return NULL;/* not found */ } Traverse the linked list to find item s

Hash Tables and Constant Access Time CS-2303, C-Term Using a Hash Table (continued) struct item *addItem(char *s, …) { struct item *np; unsigned int hv; if ((np = lookup(s)) == NULL) { np = malloc(item); /* fill in s and data */ np -> next = hashtab[hv = hash(s)]; hashtab[hv] = np; }; return np; }

Hash Tables and Constant Access Time CS-2303, C-Term Using a Hash Table (continued) struct item *addItem(char *s, …) { struct item *np; unsigned int hv; if ((np = lookup(s)) == NULL) { np = malloc(item); /* fill in s and data */ np -> next = hashtab[hv = hash(s)]; hashtab[hv] = np; }; return np; } Inserts new item at head of the list indexed by hash value

Hash Tables and Constant Access Time CS-2303, C-Term Challenge What kinds of situations in your field might you need a hash table?

Hash Tables and Constant Access Time CS-2303, C-Term Example Source Code Control System System stores every version of every file since creation Storage for one file comprises two parts: Hash table of lines of the file List of lines for each version of that file Easy to reconstruct any version of the file Easy to do an intelligent diff of two files I.e., each line that has ever been part of that file!

Hash Tables and Constant Access Time CS-2303, C-Term Hash Table Summary Widely used for constant time access Easy to build and maintain There is an art and science regarding the choice of hashing functions Consult textbooks, web, etc.

Hash Tables and Constant Access Time CS-2303, C-Term Questions?