Hash Tables. Group Members: Syed Husnain Bukhari SP10-BSCS-92 Ahmad Inam SP10-BSCS-06 M.Umair Sharif SP10-BSCS-38.

Slides:



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

Hash Tables COT4810 Ken Pritchard 2 Sep 04.
CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
HASH TABLE. HASH TABLE a group of people could be arranged in a database like this: Hashing is the transformation of a string of characters into a.
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.
Part II Chapter 8 Hashing Introduction Consider we may perform insertion, searching and deletion on a dictionary (symbol table). Array Linked list Tree.
CSCE 3400 Data Structures & Algorithm Analysis
Data Structures Using C++ 2E
Hashing as a Dictionary Implementation
File Processing - Indirect Address Translation MVNC1 Hashing Indirect Address Translation Chapter 11.
What we learn with pleasure we never forget. Alfred Mercier Smitha N Pai.
Appendix I Hashing. Chapter Scope Hashing, conceptually Using hashes to solve problems Hash implementations Java Foundations, 3rd Edition, Lewis/DePasquale/Chase21.
Hashing Chapters What is Hashing? A technique that determines an index or location for storage of an item in a data structure The hash function.
Hashing21 Hashing II: The leftovers. hashing22 Hash functions Choice of hash function can be important factor in reducing the likelihood of collisions.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Nov 12, 2009IAT 8001 Hash Table Bucket Sort. Nov 12, 2009IAT 8002  An array in which items are not stored consecutively - their place of storage is calculated.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Hash Tables and Associative Containers CS-212 Dick Steflik.
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.
FALL 2004CENG 3511 Hashing Reference: Chapters: 11,12.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Hashing General idea: Get a large array
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Hashing Lesson Plan - 8.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Searching Chapter 2.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Chapter 13 File Structures. Understand the file access methods. Describe the characteristics of a sequential file. After reading this chapter, the reader.
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
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.
Appendix E-A Hashing Modified. Chapter Scope Concept of hashing Hashing functions Collision handling – Open addressing – Buckets – Chaining Deletions.
Comp 335 File Structures Hashing.
1 5. Abstract Data Structures & Algorithms 5.2 Static Data Structures.
1 HASHING Course teacher: Moona Kanwal. 2 Hashing Mathematical concept –To define any number as set of numbers in given interval –To cut down part of.
Hashing Hashing is another method for sorting and searching data.
Hashing as a Dictionary Implementation Chapter 19.
CS201: Data Structures and Discrete Mathematics I Hash Table.
Hash Tables. Introduction A hash table is a data structure that stores things and allows insertions, lookups, and deletions to be performed in O(1) time.
Data Structures and Algorithms Hashing First Year M. B. Fayek CUFE 2010.
Lecture 12COMPSCI.220.FS.T Symbol Table and Hashing A ( symbol) table is a set of table entries, ( K,V) Each entry contains: –a unique key, K,
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
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.
Hashing Basis Ideas A data structure that allows insertion, deletion and search in O(1) in average. A data structure that allows insertion, deletion and.
Hash Tables. Group Members: Syed Husnain Bukhari SP10-BSCS-92 Ahmad Inam SP10-BSCS-06 M.Umair Sharif SP10-BSCS-38.
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.
Hashed Files Text Versus Binary Meghan Cavanagh. Hashed Files a file that is searched using one of the hashing methods User gives the key, the function.
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.
1 Hashing by Adlane Habed School of Computer Science University of Windsor May 6, 2005.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
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. Hashing is the transformation of a string of characters into a usually shorter fixed-length value or key that represents the original string.
Hashing O(1) data access (almost) -access, insertion, deletion, updating in constant time (on average) but at a price… references: Weiss, Goodrich & Tamassia,
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.
Data Structures Using C++ 2E
Hashing, Hash Function, Collision & Deletion
Data Structures Using C++ 2E
Review Graph Directed Graph Undirected Graph Sub-Graph
Hash Table.
Hash Tables.
Data Structures Hashing 1.
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
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
What we learn with pleasure we never forget. Alfred Mercier
Collision Resolution.
Presentation transcript:

Hash Tables

Group Members: Syed Husnain Bukhari SP10-BSCS-92 Ahmad Inam SP10-BSCS-06 M.Umair Sharif SP10-BSCS-38

Description A hash table is a data structure that stores things and allows insertions, lookups, and deletions to be performed in O(1) time. An algorithm converts an object, typically a string, to a number. Then the number is compressed according to the size of the table and used as an index. There is the possibility of distinct items being mapped to the same key. This is called a collision and must be resolved.

Key  Hash Code Generator  Number  Compression  Index Smith  Bob Smith 123 Main St. Orlando, FL

Definition: Hashing is a key-to-address mapping process. Terms must be familiarized. Synonyms: The set of keys that hash to the same location Collision: A collision occurs when a hashing algorithm produces an address for an insertion key and that address is already occupied. Home address: The address produced by the hashing algorithm is known as the home address. Prime area: The memory that contains all of the home addresses is known as the prime area. Probe: Each calculation of an address and test for success is known as a probe.

Collision Resolution There are two kinds of collision resolution: 1 – Chaining makes each entry a linked list so that when a collision occurs the new entry is added to the end of the list. 2 – Open Addressing uses probing to discover an empty spot. With chaining, the table does not have to be resized. With open addressing, the table must be resized when the number of elements is larger than the capacity.

Collision Resolution - Chaining With chaining, each entry is the head of a (possibly empty) linked list. When a new object hashes to an entry, it is added to the end of the list. A particularly bad hash function could create a table with only one non-empty list that contained all the elements. Uniform hashing is very important with chaining. The load factor of a chained hash table indicates how many objects should be found at each location, provided reasonably uniform hashing. The load factor LF = n/c where n is the number of objects stored and c is the capacity of the table. With uniform hashing, a load factor of 2 means we expect to find no more than two objects in one slot. A load factor less than 1 means that we are wasting space.

Smith  Bob Smith 123 Main St. Orlando, FL Jim Smith 123 Elm St. Orlando, FL Chaining

Collision Resolution – Open Addressing With open addressing, the table is probed for an open slot when the first one already has an element. There are different types of probing, some more complicated than others. The simplest type is to keep increasing the index by one until an open slot is found. The load factor of an open addressed hash table can never be more than 1. A load factor of.5 indicates that the table is half full. With uniform hashing, the number of positions that we can expect to probe is 1/(1 – LF). For a table that is half full, LF =.5, we can expect to probe 1/(1 -.5) = 2 positions. Note that for a table that is 95% full, we can expect to probe 20 positions.

Smith  Bob Smith 123 Main St. Orlando, FL Jim Smith 123 Elm St. Orlando, FL Probing

Hashing Methods There are eight hashing methods they are: 1: Direct method 2: Subtraction method 3: Modulo-division 4: Mid square 5: Digit extraction 6: Rotation 7: Folding 8: Pseudorandom generation

Hashing Methods Direct Method In direct hashing the key is the address without any algorithmic manipulation. Direct hashing is limited, but it can be very powerful because it guarantees that there are no synonyms and therefore no collision.

Modulo-division Method This is also known as division remainder method. This algorithm works with any list size, but a list size that is a prime number produces fewer collisions than other list sizes. The formula to calculate the address is: Address = key MODULO listsize + 1 Where listsize is the number of elements in the array.

Example: Given data : Keys are : % = % = % = 2

Digit-extraction Method Using digit extraction selected digits are extracted from the key and used as the address. Example : Using six-digit employee number to hash to a three digit address ( ), we could select the first, third, and fourth digits( from the left) and use them as the address. The keys are: > > > 388

Folding Method Two folding methods are used they are: 1: Fold shift 2: Fold boundary 1: Fold Shift In fold shift the key value is divided into parts whose size matches the size of the required address. Then the left and right parts are shifted and added with the middle part.

Fold boundary In fold boundary the left and right numbers are folded on a fixed boundary between them and the center number. The two outside values are thus reversed.

Midsquare Method In midsquare hashing the key is squared and the address is selected from the middle of the square number. Limitation is the size of the key. Example: = : address is 3403

Rotation Method Rotation method is generally not used by itself but rather is incorporated in combination with other hashing methods. It is most useful when keys are assigned serially.

Pseudorandom Hashing A common random-number generator is shown below. y= ax + c To use the pseudorandom-number generator as a hashing method, we set x to the key, multiply it by the coefficient a, and then add the constant c. The result is then divided by the list size, with the remainder being the hashed address. Example: Y= ((17 * ) + 7) modulo 307 Y= ( ) modulo 307 Y= Y=41

Hash Table Uses Compilers use hash tables for symbol storage. The Linux Kernel uses hash tables to manage memory pages and buffers. High speed routing tables use hash tables. Database systems use hash tables.

Summary A hash table is a convenient data structure for storing items that provides O(1) access time. The concepts that drive selection of the key generation and compression functions can be complicated, but there is a lot of research information available. There are many areas where hash tables are used. Modern programming languages typically provide a hash table implementation ready for use in applications.

References Knuth, Donald A. The Art of Computer Programming. Philippines: Addison-Wesley Publishing Company, Loudon, Kyle. Mastering Algorithms with C. Sebastopol: O’Reilly & Associates, 1999 Watt, David A., and Deryck F. Brown. Java Collections. West Sussex: John Wiley & Sons, 2001 Dewdney, A. K. The New Turing Omnibus. New York: Henry Holt and Company, 2001

Any Question