Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Database Applications Hashing and Hashtables in C#

Similar presentations


Presentation on theme: "Data Structures and Database Applications Hashing and Hashtables in C#"— Presentation transcript:

1 Data Structures and Database Applications Hashing and Hashtables in C#

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

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

4 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)

5 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

6 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

7 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

8 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 Separate chaining is simple, but requires additional memory outside the table

9 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

10 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.

11 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);

12 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

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

14 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 s. So in this implementation, on entry of a name, phone and will point to another entry of a name, phone and .

15 Hashtable Database Implementation
The Entries Table:

16 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. .Length; ++i) value += (int)(entry. [i]); return value %= 1000; }


Download ppt "Data Structures and Database Applications Hashing and Hashtables in C#"

Similar presentations


Ads by Google