How to use hash tables to solve olympiad problems

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Hash Tables1  
Advertisements

1.1 Data Structure and Algorithm Lecture 9 Hashing Topics Reference: Introduction to Algorithm by Cormen Chapter 12: Hash Tables.
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.
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)
Hash Tables1 Part E Hash Tables  
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
Hashing General idea: Get a large array
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
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 Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
1 5. Abstract Data Structures & Algorithms 5.2 Static Data Structures.
March 23 & 28, Csci 2111: Data and File Structures Week 10, Lectures 1 & 2 Hashing.
March 23 & 28, Hashing. 2 What is Hashing? A Hash function is a function h(K) which transforms a key K into an address. Hashing is like indexing.
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.
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.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
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 CSCD 326 Data Structures I Hashing. 2 Hashing Background Goal: provide a constant time complexity method of searching for stored data The best traditional.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
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.
1 Resolving Collision Although collisions should be avoided as much as possible, they are inevitable Need a strategy for resolving collisions. We look.
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
Sets and Maps Chapter 9.
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.
Lecture 6 of Computer Science II
Data Structures Using C++ 2E
Hash table CSC317 We have elements with key and satellite data
CSCI 210 Data Structures and Algorithms
Open Addressing: Quadratic Probing
LEARNING OBJECTIVES O(1), O(N) and O(LogN) access times. Hashing:
Hashing CSE 2011 Winter July 2018.
Subject Name: File Structures
Hashing Alexandra Stefan.
Data Structures Using C++ 2E
Review Graph Directed Graph Undirected Graph Sub-Graph
Dictionaries 9/14/ :35 AM Hash Tables   4
Hash functions Open addressing
Hash Table.
Hash In-Class Quiz.
Computer Science 2 Hashing
Hash Tables.
Resolving collisions: Open addressing
Data Structures – Week #7
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hashing Alexandra Stefan.
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
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Space-for-time tradeoffs
Sets and Maps Chapter 9.
EE 312 Software Design and Implementation I
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Data Structures – Week #7
Collision Handling Collisions occur when different elements are mapped to the same cell.
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Hashing.
EE 312 Software Design and Implementation I
Collision Resolution: Open Addressing Extendible Hashing
Chapter 5: Hashing Hash Tables
Lecture-Hashing.
Presentation transcript:

How to use hash tables to solve olympiad problems H a s h i n g How to use hash tables to solve olympiad problems

What is hashing Hashing is used to allow elements of a set to be accessed directly Hashing is used when possible data sets are too big for arrays, but the actual data sets are quite small

When is hashing used? Hashing is used most often to encode a string so that it can be easily stored or processed. It is often necessary to keep track of whether strings exist, or some other fact about them. Hashing allows this data to be retrieved almost in O(1) time.

How does hashing work? Hashing follows a number of steps: First, the data items are converted to a natural number This number is then converted, using a hash function, to a different number which falls within the hash table The original data is then stored in the hash table, where it can be quickly retrieved

Converting to natural numbers If numerical data is being hashed, it can easily be converted to natural numbers If text is being converted, it is easiest to use the ASCII values and compute a “value” for the string. This can be done in a number of ways:

Primes are used to reduce patterns The string can be taken as a number base 256, which can be converted to decimal A simpler method is to multiply letters by prime numbers and add them together. Eg “hello” - take the value of h, add to it the value of the e multiplied by a prime, eg 29, then add the value of the l, multiplied by 29 and 31, etc. Primes are used to reduce patterns

Using a hash function In the previous step, each element was given a key, which had a reasonalbe chance of being unique. These keys are too big to be used directly in an array, so they are hashed first The simplest hash function involves modding the key by the table size.

A hash function If a given element has a key k, and is being inserted into a hash table of size m, the following is a simple and reasonably effective hash function: h(k) = k mod m If this hash function is used, m should be prime to increase the spread of data

Inserting data into a hash table Before data can be added, the table must be created. The size is reasonably important Unless using linked lists, the table should be 1.5 to 2 times the size of the data being stored in it, and its size should be a prime (or space is wasted)

Inserting data Once the key has been hashed, it gives some value h(k). The original data can then be inserted into the array with this as index If this space is full, keep checking the next space till an open space is found. Wrapping around may be necessary

Finding an element in the table To find an element, first compute its key and then pass this through the hash table. Go to this entry in the table. If it doesn’t contain the correct entry, check the next one. Repeat this till the entry is found or and empty space occurs

Deleting an item from a hash table To delete an item, first locate it using the search procedure The block should be marked with some code or flag which indicates it is “deleted”, but not in its original state

Using linked lists in hash tables Instead of inserting items below the index in the table, they can form a linked list starting at the index When using this data structure, which needs pointers, the table does not need to be larger than the number of entries

Problems using hashing A very simple problem which can be solved using hashing is the problem of counting the frequencies of words in a piece of text These are then stored in a hash table, with the word itself as the key

Problems using hashing Decrypting a piece of text with a simple code where a letter is replaced by a letter k further in the alphabet For various values of k, the text can be checked to find which gives the best match.

Problems using hashing Finding which words are synonyms given a list of pairs of words with the same meaning This becomes graph theory, and is solved with a flood fill algorithm

Problems using Hashing Although these all relate to text, hashing can be used to replace any akward data with a direct access table This can be used for polygons, long sequences of numbers, etc

Conclusion Hashing is used to allow data like text to be stored so that it can be accessed in almost O(1) time. It is often only part of the solution, and is used to simplify a problem so that an method like dynamic programming or a graph theory algorithm can be used