Hash Tables: A basic O(1)verview

Slides:



Advertisements
Similar presentations
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Advertisements

Preliminaries Advantages –Hash tables can insert(), remove(), and find() with complexity close to O(1). –Relatively easy to program Disadvantages –There.
Hashing as a Dictionary Implementation
Data Management and File Organization
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.
Chapter 5: Hashing Hash Tables
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Chapter 5 Hashing General ideas Methods of implementing the hash table Comparison among these methods Applications of hashing Compare hash tables with.
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
Algorithms and Data Structures* Objective: To review the fundamental algorithms and data structures that are commonly used in programs. To see how to use.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
Comp 335 File Structures Hashing.
Hashing as a Dictionary Implementation Chapter 19.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
Hashing, Hashing Tables Chapter 8. Class Hierarchy.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
Hashing & Hash Tables. Sets/Dictionaries Set - Our best efforts to date:
TOPIC 5 ASSIGNMENT SORTING, HASH TABLES & LINKED LISTS Yerusha Nuh & Ivan Yu.
CSC2100B Tutorial 6 Hashing Hao Ma Yi LIU Mar 4, 2004.
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
DS.H.1 Hashing Chapter 5 Overview The General Idea Hash Functions Separate Chaining Open Addressing Rehashing Extendible Hashing Application Example: Geometric.
Hash Tables 1/28/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Hashing CSE 2011 Winter July 2018.
School of Computer Science and Engineering
Cse 373 April 24th – Hashing.
Review Graph Directed Graph Undirected Graph Sub-Graph
Introduction to Hashing - Hash Functions
External Methods Chapter 15 (continued)
Hash Tables.
Hash Functions Sections 5.1 and 5.2
Hash Tables in C James Goerke.
Hash tables Hash table: a list of some fixed size, that positions elements according to an algorithm called a hash function … hash function h(element)
Hashing CS2110 Spring 2018.
Hash Tables banana apple banana apple cantaloupe kiwi
CS223 Advanced Data Structures and Algorithms
Hash Tables in C Louis Manco.
Hash Table.
Chapter 28 Hashing.
Hashing CS2110.
Chapter 10 Hashing.
BBM 204 Algorithms Lab Recitation 5 Hash functions Sequential Chaining
Chapter 21 Hashing: Implementing Dictionaries and Sets
Searching Tables Table: sequence of (key,information) pairs
Growing Arrays in C Russell Stephens.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Hash Tables and Associative Containers
CSE 373 Data Structures and Algorithms
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
File Storage and Indexing
CS202 - Fundamental Structures of Computer Science II
A Hash Table with Chaining
By Yogesh Neopaney Assistant Professor Department of Computer Science
CS223 Advanced Data Structures and Algorithms
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Data Structures and Algorithm Analysis Hashing
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Hash Tables banana apple banana apple cantaloupe kiwi
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 5: Hashing Hash Tables
Dictionaries and Hash Tables
Presentation transcript:

Hash Tables: A basic O(1)verview Niteesh Prasad CS-265

Hash Tables in C Data structure used for mapping/ searching. Increased efficiency to $O(1)$- Constant Look up time. Disadvantage: Wastage of memory Used in database indexing and associate arrays. Commonly used applications like web-browsers use hash tables. Creates a table with operations such as Insert and Retrieve(look up).

Basic Idea Store records of data in the hash table, which is either an array (when main memory is used), or a file (if disk space is used). Each record has a key field and an associated data field. The record is stored in a location that is based on its key. The function that produces this location for each given key is called a hash function

More Background Each element of the array is a list that chains together items that share a hash value. Basic element type would look like this: typedef structNameval Nameval; struct Nameval { char *name; int value; Nameval *next; /* in chain */ }; Hash function is important in deciding the efficiency of the search.

It’s not a “perfect” world out there Finding the perfect hash function is difficult (a function that maps each input to a different has value) Consequences: Conflicts b/w keys (Hash Collision) Collision resolution :(1) Resolution by overflow (Creating a separate table) (2) Double Hashing (Two independent hash functions) (3) Rehashing (Rebuilding the entire table ) The Multiplication method is the most-widely used to find the indices. (1) Multiply the key by a constant A, 0 < A < 1 (2) Extract the fractional part of the product (3) Multiply this value by m

Hash Function Hash function /* hash: compute hash value for array of NPREF strings */ unsigned int hash(char *s[NPREF]) { unsigned int h; unsigned char *p; int i; h = 0; for (i = 0; i < NPREF; i++) // traversal through the array for (p = (unsigned char *) s[i]; *p != '\0'; p++) h = MULTIPLIER * h + *p; return h % NHASH; }

Insert and Look up /* lookup: find name in symtab, with optional create */ Nameval* lookup(char *name, int create, int value) { int h; Nameval *sym; h = hash(name); for (sym = symtab[h]; sym != NULL; sym = sym->next) if (strcmp(name, sym->name) == 0) return sym; // To avoid duplication if (create) { sym = (Nameval *) emalloc(sizeof(Nameval)); sym->name=name; /* assumed allocated elsewhere */ sym->value=value; sym->next=symtab[h]; symtab[h]=sym; } return sym;

Sources http://eternallyconfuzzled.com/tuts/datastructures/jsw_tut_hashtable.aspx http://www.cs.drexel.edu/~knowak/cs265_fall_2010/week_6.pdf Brian Kernighan and Rob Pike, The Practice of Programming, Addison Wesley, 1999