Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records.

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

An Introduction to Hashing. By: Sara Kennedy Presented: November 1, 2002.
Hashing as a Dictionary Implementation
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hashing Techniques.
Hashing CS 3358 Data Structures.
CSE 250: Data Structures Week 12 March 31 – April 4, 2008.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
Hash Tables1 Part E Hash Tables  
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
Introduction to Hashing CS 311 Winter, Dictionary Structure A dictionary structure has the form: (Key, Data) Dictionary structures are organized.
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.
Hash Tables. Container of elements where each element has an associated key Each key is mapped to a value that determines the table cell where element.
Hashing 1. Def. Hash Table an array in which items are inserted according to a key value (i.e. the key value is used to determine the index of the item).
COSC 2007 Data Structures II
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 7 Prepared by İnanç TAHRALI.
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.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Hashing Hashing is another method for sorting and searching data.
HASHING PROJECT 1. SEARCHING DATA STRUCTURES Consider a set of data with N data items stored in some data structure We must be able to insert, delete.
Robbie CSCI2100A Data Structures Tutorial
Hashing as a Dictionary Implementation Chapter 19.
CS201: Data Structures and Discrete Mathematics I Hash Table.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
Hashing is a method to store data in an array so that sorting, searching, inserting and deleting data is fast. For this every record needs unique key.
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
Hash Tables. 2 Exercise 2 /* Exercise 1 */ void mystery(int n) { int i, j, k; for (i = 1; i
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.
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.
COSC 1030 Lecture 10 Hash Table. Topics Table Hash Concept Hash Function Resolve collision Complexity Analysis.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Chapter 9 Hashing Dr. Youssef Harrath
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
CSC2100B Tutorial 6 Hashing Hao Ma Yi LIU Mar 4, 2004.
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Hashing Problem: store and retrieving an item using its key (for example, ID number, name) Linked List takes O(N) time Binary Search Tree take O(logN)
Hashing CSE 2011 Winter July 2018.
Slides by Steve Armstrong LeTourneau University Longview, TX
Hash Table.
Hash Tables.
BBM 204 Algorithms Lab Recitation 5 Hash functions Sequential Chaining
Searching Tables Table: sequence of (key,information) pairs
CS202 - Fundamental Structures of Computer Science II
Tree traversal preorder, postorder: applies to any kind of tree
EE 312 Software Design and Implementation I
Ch Hash Tables Array or linked list Binary search trees
Collision Handling Collisions occur when different elements are mapped to the same cell.
Ch. 13 Hash Tables  .
EE 312 Software Design and Implementation I
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records Sometimes the key is the whole record Searching a table Given a key k and a table T = (k 1,i 1 ),…, (k n,i n ), find the pair (k j,i j ) in T such that k=k j (if it exists) Possible approaches: –Sequential search: simple, but only effective for small tables –Binary search: fast, but table must be sorted –Hashing 1COSC 2P03 Week 11

Hash Tables Idea: use a function f such that for every possible key k, f(k) = index of record with key k: O(1) time Hash Function: maps keys to addresses Build the table using the hash function: if f(k)=1 then put record (k,i) in cell 1 of table (etc) Hash functions should ideally be: 1.Easy to compute, and 2.Ensure different keys are always mapped to different cells Perfect hash functions are not always possible COSC 2P03 Week 112

Hashing and Collisions Collision: the effect of more than one key being mapped to the same cell –Given k x ≠k y, we have f(k x ) = f(k y ) Approaches to dealing with collisions: 1.Allow >1 record to be stored in each table index –Separate chaining: each index has a linked list of records –Buckets: each index is a fixed-size bucket of records 2.Open addressing: allow only 1 record at each index –When a collision occurs, use a collision resolution policy to find a new index for the item, e.g. linear probing etc. COSC 2P03 Week 113

Hash table findPos – Linear Probing If a record is hashed to index j, which is already occupied, then look in index j+1, j+2, … and put the record in the next available index (each attempt is called a probe) int findPos(int k) // search for index that should store // record with key k { current = hash(k, tableSize); while(array[current] != null && array[currentPos].record.key != k) { current = (current+1) % tableSize; } return current; } 4COSC 2P03 Week 11

Hash Table search int find(int k) // search for record with key k { current = findPos(k); if(isActive(current)) return current; else return -1; // not found } 5COSC 2P03 Week 11

Hash Table – insertion void insert(record R) // insert R if not already in table { current = findPos(R.key); if(isActive(current)) // already in hash table return; else { array[current].record = R; array[current].isActive = true; } 6COSC 2P03 Week 11

Hash table – deletion void remove(int k) // delete record with key k { current = findPos(R.key); if(isActive(currentPos)) array[current].isActive = false; } 7COSC 2P03 Week 11

Open addressing – collision resolution policies Linear probing: If a record is hashed to index j, which is already occupied, then look in index j+1, j+2, … and put the record in the next available index Quadratic probing: If a record is hashed to index j, which is already occupied, then look in index j+1, j+4, j+9, …, and put the record in the next available index. Double hashing: If h 1 (key) hashes a record to index j, which is already occupied, then use h 2 (key) as a step size COSC 2P03 Week 118