Asst. Prof. Dr. İlker Kocabaş Hash Tables. 2 Overview Information Retrieval Binary Search Trees Hashing. Applications. Example. Hash Functions. Hash Tables.

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

1 A B C
Simplifications of Context-Free Grammars
Variations of the Turing Machine
COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables III.
AP STUDY SESSION 2.
1
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
David Burdett May 11, 2004 Package Binding for WS CDL.
Create an Application Title 1Y - Youth Chapter 5.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
CALENDAR.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt BlendsDigraphsShort.
1 Chapter 12 File Management Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design Principles,
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
The 5S numbers game..
Media-Monitoring Final Report April - May 2010 News.
Chapter 7: Steady-State Errors 1 ©2000, John Wiley & Sons, Inc. Nise/Control Systems Engineering, 3/e Chapter 7 Steady-State Errors.
Break Time Remaining 10:00.
The basics for simulations
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
Chapter 10: Applications of Arrays and the class vector
Data structure is concerned with the various ways that data files can be organized and assembled. The structures of data files will strongly influence.
1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.
1 Hash Tables Saurav Karmakar. 2 Motivation What are the dictionary operations? What are the dictionary operations? (1) Insert (1) Insert (2) Delete (2)
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.
Briana B. Morrison Adapted from William Collins
Analysis of Algorithms CS 477/677
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.
Hash Tables.
Hash Tables Dr. Li Jiang School of Computer Science,
Operating Systems Operating Systems - Winter 2012 Chapter 4 – Memory Management Vrije Universiteit Amsterdam.
Operating Systems Operating Systems - Winter 2010 Chapter 3 – Input/Output Vrije Universiteit Amsterdam.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Biology 2 Plant Kingdom Identification Test Review.
Overview Hash Table Hash Function Hash table ADT operations
Adding Up In Chunks.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Artificial Intelligence
Before Between After.
Slide R - 1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Prentice Hall Active Learning Lecture Slides For use with Classroom Response.
Subtraction: Adding UP
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
Static Equilibrium; Elasticity and Fracture
Essential Cell Biology
Converting a Fraction to %
Numerical Analysis 1 EE, NCKU Tien-Hao Chang (Darby Chang)
CSE20 Lecture 15 Karnaugh Maps Professor CK Cheng CSE Dept. UC San Diego 1.
Clock will move after 1 minute
famous photographer Ara Guler famous photographer ARA GULER.
PSSA Preparation.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Physics for Scientists & Engineers, 3rd Edition
Energy Generation in Mitochondria and Chlorplasts
Select a time to count down from the clock above
Copyright Tim Morris/St Stephen's School
1.step PMIT start + initial project data input Concept Concept.
1 Dr. Scott Schaefer Least Squares Curves, Rational Representations, Splines and Continuity.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution.
Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.
Presentation transcript:

Asst. Prof. Dr. İlker Kocabaş Hash Tables

2 Overview Information Retrieval Binary Search Trees Hashing. Applications. Example. Hash Functions. Hash Tables Collisions Linear Probing Problems with Linear Prob Chaining

3 R. Kruse, C. Tondo, B. Leung, Data Structures and Program Design in C, 1991, Prentice Hall. E. Horowitz, S. Salini, S. Anderson-Freed, Fundamentals of Data Structures in C, 1993, Computer Science Press. R. Sedgewick, Algorithms in C, 1990, Addison-Wesley. A. Aho, J. Hopcroft, J. Ullman, Data Structures and Algorithms, 1983, Addison-Wesley. T.A. Standish, Data Structures, Algorithms & Software Principles in C, 1995, Addison-Wesley. D. Knuth, The Art of Computer Programming, 1975, Addison- Wesley. Y. Langsam, M. Augenstein, M. Fenenbaum, Data Structures using C and C++, 1996, Prentice Hall. Example: Bibliography

4 Insert the information into a Binary Search Tree, using the first authors surname as the key

5 Kruse Horowitz Sedgewick Aho Knuth Langsam Standish Insert the information into a Binary Search Tree, using the first authors surname as the key KruseHorowitzSedgewickAhoKnuthLangsamStandish

6 Complexity Inserting Balanced Trees O(log(n)) Unbalanced Trees O(n) Searching Balanced Trees O(log(n)) Unbalanced Trees O(n)

7 Hashing key hash function TABLESIZE - 1 : : hash table pos

8 Kruse hash table Example: 5 Kruse hash function

9 Hashing Each item has a unique key. Use a large array called a Hash Table. Use a Hash Function.

10 Applications Databases. Spell checkers. Computer chess games. Compilers.

11 Operations Initialize all locations in Hash Table are empty. Insert Search Delete

12 Hash Function Maps keys to positions in the Hash Table. Be easy to calculate. Use all of the key. Spread the keys uniformly.

13 unsigned hash(char* s) { int i = 0; unsigned value = 0; while (s[i] != \0) { value = (s[i] + 31*value) % 101; i++; } return value; } Example: Hash Function #1

14 A. Aho, J. Hopcroft, J. Ullman, Data Structures and Algorithms, 1983, Addison-Wesley. A = 65h = 104o = 111 value = ( * 0) % 101 = 65 value = ( * 65) % 101 = 99 value = ( * 99) % 101 = 49 Example: Hash Function #1 value = (s[i] + 31*value) % 101;

15 resulting table is sparse Example: Hash Function #1 value = (s[i] + 31*value) % 101; Hash KeyValue Aho 49 Kruse95 Standish60 Horowitz28 Langsam21 Sedgewick24 Knuth44

16 value = (s[i] *value) % 128; Example: Hash Function #2 likely to result in clustering Hash KeyValue Aho 111 Kruse101 Standish104 Horowitz122 Langsam109 Sedgewick107 Knuth104

17 Example: Hash Function #3 collisions value = (s[i] + 3*value) % 7; Hash KeyValue Aho 0 Kruse5 Standish1 Horowitz5 Langsam5 Sedgewick2 Knuth1

18 Insert Apply hash function to get a position. Try to insert key at this position. Deal with collision.

19 Aho Hash Function hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 0 Example: Insert Aho

20 Kruse hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 5 Example: Insert Aho Kruse Hash Function

21 Standish hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 1 Example: Insert Aho Kruse Standish Hash Function

22 Search Apply hash function to get a position. Look in that position. Deal with collision.

23 Kruse hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 5 Example: Search Aho Standish Hash Function found.

24 Kruse Sedgwick hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 2 Example: Search Aho Standish Hash Function Not found.

Hash Tables: Collision Resolution

26 Hashing key hash function TABLESIZE - 1 : : hash table pos

27 Kruse hash table Example: 5 Kruse hash function

28 Hashing Each item has a unique key. Uses a large array called a Hash Table. Uses a Hash Function. Hash Function Maps keys to positions in the Hash Table. Be easy to calculate. Use all of the key. Spread the keys uniformly.

29 Hash Table Operations Initialize all locations in Hash Table are empty. Insert Search Delete

30 Example: Hash Function #3 collisions value = (s[i] + 3*value) % 7; Hash KeyValue Aho 0 Kruse5 Standish1 Horowitz5 Langsam5 Sedgewick2 Knuth1

31 Collision When two keys are mapped to the same position. Very likely Number of PeopleProbability Birthdays

32 Collision Resolution Two methods are commonly used: Linear Probing. Chaining.

33 Linear Probing Linear search in the array from the position where collision occurred.

34 Insert with Linear Probing Apply hash function to get a position. Try to insert key at this position. Deal with collision. Must also deal with a full table!

35 Aho Hash Function hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 0 Example: Insert with Linear Probing Aho

36 Kruse hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 5 Example: Insert with Linear Probing Aho Kruse Hash Function

37 Standish hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 1 Example: Insert with Linear Probing Aho Kruse Standish Hash Function

38 Horowitz hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 5 Example: Insert with Linear Probing Aho Kruse Standish Horowitz Hash Function

39 Langsam hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 5 Example: Insert with Linear Probing Aho Kruse Standish Horowitz Hash Function Langsam

40 module linearProbe(item) { position = hash(key of item) count = 0 loop { if (count == hashTableSize) then { output Table is full exit loop } if (hashTable[position] is empty) then { hashTable[position] = item exit loop } position = (position + 1) % hashTableSize count++ }

41 Search with Linear Probing Apply hash function to get a position. Look in that position. Deal with collision.

42 Kruse Langsam hash table Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth 5 Example: Search with Linear Probing Aho Standish Horowitz Hash Function Langsam found.

43 Kruse Knuth hash table 1 Example: Search with Linear Probing Aho Standish Horowitz Hash Function Langsam not found. Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth

44 module search(target) { count = 0 position = hash(key of target) loop { if (count == hashTableSize) then { output Target is not in Hash Table return -1. } else if (hashTable[position] is empty) then { output Item is not in Hash Table return -1. } else if (hashTable[position].key == target) then { return position. } position = (position + 1) % hashTableSize count++ }

45 Delete with Linear Probing Use the search function to find the item If found check that items after that also dont hash to the items position If items after do hash to that position, move them back in the hash table and delete the item. Very difficult and time/resource consuming!

46 Linear Probing: Problems Speed. Tendency for clustering to occur as the table becomes half full. Deletion of records is very difficult. If implemented in arrays – table may become full fairly quickly, resizing is time and resource consuming

47 Chaining Uses a Linked List at each position in the Hash Table. Linked list at a position contains all the items that hash to that position. May keep linked lists sorted or not.

48 hash table : :

Aho, Kruse, Standish, Horowiz, Langsam, Sedgwick, Knuth Example: Chaining 0, 5, 1, 5, 5, 2, Kruse Horowitz Knuth 1 Standish Aho Sedgewick Langsam 0 0 0

50 Hashtable with Chaining At each position in the array you have a list: List hashTable[MAXTABLE]; You must initialise each list in the table :

51 Insert with Chaining Apply hash function to get a position in the array. Insert key into the Linked List at this position in the array.

52 module InsertChaining(item) { posHash = hash(key of item) insert (hashTable[posHash], item); } Knuth 1 Standish Aho Sedgewick :

53 Search with Chaining Apply hash function to get a position in the array. Search the Linked List at this position in the array.

54 /* module returns NULL if not found, or the address of the * node if found */ module SearchChaining(item) { posHash = hash(key of item) Node* found; found = searchList (hashTable[posHash], item); return found; } Knuth 1 Standish Aho Sedgewick :

55 Delete with Chaining Apply hash function to get a position in the array. Delete the node in the Linked List at this position in the array.

/* module uses the Linked list delete function to delete an item *inside that list, it does nothing if that item isnt there. */ module DeleteChaining(item) { posHash = hash(key of item) deleteList (hashTable[posHash], item); } Knuth 1 Standish Aho Sedgewick :

57 Disadvantages of Chaining Uses more space. More complex to implement. Contains a linked list at every element in the array. Requires linear searching. May be time consuming.

58 Advantages of Chaining Insertions and Deletions are easy and quick. Allows more records to be stored. Naturally resizable, allows a varying number of records to be stored.

Dictionaries and Hash Tables59 Double Hashing Double hashing uses a secondary hash function d(k) and handles collisions by placing an item in the first available cell of the series (i jd(k)) mod N for j 0, 1, …, N 1 The secondary hash function d ( k ) cannot have zero values The table size N must be a prime to allow probing of all the cells Common choice of compression map for the secondary hash function: d 2 ( k ) q k mod q where q N q is a prime The possible values for d 2 ( k ) are 1, 2, …, q

Dictionaries and Hash Tables60 Performance of Hashing In the worst case, searches, insertions and removals on a hash table take O(n) time The worst case occurs when all the keys inserted into the dictionary collide The load factor n N affects the performance of a hash table Assuming that the hash values are like random numbers, it can be shown that the expected number of probes for an insertion with open addressing is 1 (1 ) The expected running time of all the dictionary ADT operations in a hash table is O(1) In practice, hashing is very fast provided the load factor is not close to 100% Applications of hash tables: small databases compilers browser caches