Data Structures and Database Applications Hashing and Hashtables in C#

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Hash Tables
Advertisements

© 2004 Goodrich, Tamassia Hash Tables1  
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Data Structures Lecture 12 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Hashing Techniques.
Maps, Dictionaries, Hashtables
Dictionaries and Hash Tables1  
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.
Hash Tables1 Part E 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.
Dictionaries 4/17/2017 3:23 PM Hash Tables  
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Hash Tables1   © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Hash Tables1  
CS201: Data Structures and Discrete Mathematics I Hash Table.
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.
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 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
1 What is it? A side order for your eggs? A form of narcotic intake? A combination of the two?
Hash Maps Rem Collier Room A1.02 School of Computer Science and Informatics University College Dublin, Ireland.
Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees.
CS203 Lecture 14. Hashing An object may contain an arbitrary amount of data, and searching a data structure that contains many large objects is expensive.
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.
Hashing (part 2) CSE 2011 Winter March 2018.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Hashing.
COMP 53 – Week Eleven Hashtables.
CSCI 210 Data Structures and Algorithms
Hashing CSE 2011 Winter July 2018.
Cse 373 April 24th – Hashing.
Dictionaries and Hash Tables
Review Graph Directed Graph Undirected Graph Sub-Graph
Dictionaries Dictionaries 07/27/16 16:46 07/27/16 16:46 Hash Tables 
© 2013 Goodrich, Tamassia, Goldwasser
Dictionaries 9/14/ :35 AM Hash Tables   4
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Hash functions Open addressing
Data Structures and Database Applications Hashing and Hashtables in C#
Data Structures and Database Applications Dictionaries in C#
Data Structures and Database Applications Queues in C#
Hash Table.
Chapter 28 Hashing.
CS313D: Advanced Programming Language
Data Structures Maps and Hash.
Hash Tables 11/22/2018 3:15 AM Hash Tables  1 2  3  4
Dictionaries 11/23/2018 5:34 PM Hash Tables   Hash Tables.
Dictionaries and Hash Tables
Chapter 21 Hashing: Implementing Dictionaries and Sets
Copyright © Aiman Hanna All rights reserved
Hash Tables   Maps Dictionaries 12/7/2018 5:58 AM Hash Tables  
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Dictionaries and Hash Tables
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Dictionaries 1/17/2019 7:55 AM Hash Tables   4
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CS202 - Fundamental Structures of Computer Science II
Hash Tables Computer Science and Engineering
Hash Tables Computer Science and Engineering
Hash Tables Computer Science and Engineering
Dictionaries 4/5/2019 1:49 AM Hash Tables  
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
slides created by Marty Stepp
CS210- Lecture 17 July 12, 2005 Agenda Collision Handling
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Dictionaries and Hash Tables
Presentation transcript:

Data Structures and Database Applications Hashing and Hashtables in C#

Hashtable Overview System.Collections.Hashtable Automatic resizing and non-homogeneous, unordered list Represents a collection of key-value pairs

What is a Hash Function? Key: 506643548 The Hash Function converts the key to an index value within the table boundaries. [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700] . . .

Why Hashing? To find a certain entry in a list (or a table), you may have to search through the whole table if it’s the table’s last entry or not there. For big tables, this can be a long search, and the order of magnitude is called Order N, where N represents the number of entries in the list (or table). Order N is written: O(n) If the values are in order, you only have to search from the beginning until you find it or go over its value. Usually you don’t go through the whole table, its order of magnitude is: O(Log n) With a Hash Function, you can usually get the entry’s index in a constant amount of time, not related to n, so its written: O(1)

Hash Tables and Hash Values Hashing is a technique that retrieves the value using the index obtained from key without performing a search. A typical hash function first converts a search key to an integer value called a hash code, and then compresses the hash code into an index to the hash table. Example: h(x) = x mod N // The integer h(x) is called the hash value of key x Where the hash table for a given key type consists of: Hash function h Array (some type of table) of size N

Example If we have a hash table for a map storing entries as (SSN, Name), where SSN (social security number) is a nine-digit positive integer If we know we will have less than 10, 000 SSNs to store, our hash table might use an array of size N = 10,000 and the hash function might be: h(x) = last four digits of x  1 2 3 4 9997 9998 9999 … 451-229-0004 981-101-0002 200-751-9998 025-612-0001

Composite Hash Functions A hash function is usually specified as the composition of two functions: Hash code: h1: keys  integers Compression function: h2: integers  [0, N - 1] The hash code is applied first, and the compression function is applied next on the result, i.e., h(x) = h2(h1(x)) The goal of the hash function is to “disperse” the keys in an apparently random way

Collision Handling Collisions occur when different elements are mapped to the same cell Separate Chaining: lets each cell in the table point to a linked list of entries that map there  1 2 3 4 451-229-0004 981-101-0004 025-612-0001 Separate chaining is simple, but requires additional memory outside the table

Open Addressing and Linear Probing Open addressing: the colliding item is placed in a different cell of the table Linear probing: handles collisions by placing the colliding item in the next (circularly) available table cell Each table cell inspected is referred to as a “probe” Colliding items lump together, causing future collisions to cause a longer sequence of probes Example: h(x) = x mod 13 Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order 1 2 3 4 5 6 7 8 9 10 11 12 41 18 44 59 32 22 31 73 1 2 3 4 5 6 7 8 9 10 11 12

The Hashtable ADT A hashtable is a data structure that stores entries, like the dictionary, that contain two parts: a key and a value. The value can be any data type. The key is usually an integer, but it doesn’t have to be. It can be any type of value that has a Hash Function which can convert it to a number. x[key] = value; v[14] = "Another Thing“; w["first"] = "Something“; Unlike with dictionaries, Hashtables are non-homogeneous, meaning that a single Hashtable can store objects of different data types.

Creating and Traversing a Hashtable Example: Hashtable openWith = new Hashtable(); openWith.Add("txt", "notepad.exe"); openWith.Add("bmp", "paint.exe"); openWith.Add(987, "integerpad.exe"); openWith.Add(3.45, "doublepad.exe"); try { openWith.Add("txt", "winword.exe"); } catch (ArgumentException) { Console.WriteLine("An element with Key = \"txt\" already exists."); } if (openWith.ContainsKey("bmp")) { Console.WriteLine("\nbmp is opened with {0}\n", openWith["bmp"]); foreach (DictionaryEntry item in openWith) { Console.WriteLine("Key: {0}, Value: {1}", item.Key, item.Value);

Creating and Traversing a Hashtable The previous code prints out the following: An element with Key = "txt" already exists. bmp is opened with paint.exe Key: 3.45, Value: doublepad.exe Key: txt, Value: notepad.exe Key: bmp, Value: paint.exe Key: 987, Value: integerpad.exe

Common C# Dictionary Methods Add() ContainsKey() ContainsValue() Remove() Clear()

Hashtable Database Implementation To create an efficient Database implementation of a Hashtable we will continue using the Tables we created for Dictionaries, Stacks, Queues and LinkedLists operations. We shall also add a new table called the HLists Table which will have a KeyID field and an EntryID field for an HList entry. Although the KeyID field and the EntryID field in the HList entry could each point to an Entry of any type of data in theory, for now the both just point to Entries of Names, Phones and Emails. So in this implementation, on entry of a name, phone and email will point to another entry of a name, phone and email.

Hashtable Database Implementation The Entries Table:

Hashtable Database Implementation The ID of the table entries will be set by the following hash function: public int MyHashFunction(Entry entry) { int value = 0; for (int i = 0; i < entry.Name.Length; ++i) value += (int)(entry.Name[i]); for (int i = 0; i < entry.Phone.Length; ++i) value += (int)(entry.Phone[i]); for (int i = 0; i < entry.Email.Length; ++i) value += (int)(entry.Email[i]); return value %= 1000; }