1 Microsoft Imagine Cup

Slides:



Advertisements
Similar presentations
Hash Tables CSC220 Winter What is strength of b-tree? Can we make an array to be as fast search and insert as B-tree and LL?
Advertisements

The ADT Hash Table What is a table?
CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
Hashing as a Dictionary Implementation
What we learn with pleasure we never forget. Alfred Mercier Smitha N Pai.
Hashing Chapters What is Hashing? A technique that determines an index or location for storage of an item in a data structure The hash function.
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
Hashing Techniques.
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.
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.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
1 Hash table. 2 Objective To learn: Hash function Linear probing Quadratic probing Chained hash table.
Hashing Hashing is another method for sorting and searching data.
Hashing as a Dictionary Implementation Chapter 19.
Searching Given distinct keys k 1, k 2, …, k n and a collection of n records of the form »(k 1,I 1 ), (k 2,I 2 ), …, (k n, I n ) Search Problem - For key.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
Chapter 12 Hash Table. ● So far, the best worst-case time for searching is O(log n). ● Hash tables  average search time of O(1).  worst case search.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
“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 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.
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.
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.
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Appendix I Hashing.
Chapter 12 A Tables.
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.
COMP 53 – Week Eleven Hashtables.
Data Abstraction & Problem Solving with C++
School of Computer Science and Engineering
Slides by Steve Armstrong LeTourneau University Longview, TX
Efficiency add remove find unsorted array O(1) O(n) sorted array
Hash functions Open addressing
Hash tables Hash table: a list of some fixed size, that positions elements according to an algorithm called a hash function … hash function h(element)
Advanced Associative Structures
Hash Table.
Hash Table.
Searching.
Hash Tables.
Dictionaries and Their Implementations
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Hash Tables Chapter 12.7 Wherein we throw all the data into random array slots and somehow obtain O(1) retrieval time Nyhoff, ADTs, Data Structures and.
Searching CLRS, Sections 9.1 – 9.3.
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
Advanced Implementation of Tables
Sets and Maps Chapter 9.
Data Structures – Week #7
Hash Tables Dr. Cory Budischak
Ch Hash Tables Array or linked list Binary search trees
slides created by Marty Stepp
What we learn with pleasure we never forget. Alfred Mercier
Hash Maps Introduction
Hash Maps Implementation and Applications
Hash Tables Dr. Cory Budischak
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
CSE 373: Data Structures and Algorithms
Presentation transcript:

1 Microsoft Imagine Cup

2 CompSci 105 SS 2005 Principles of Computer Science Lecture 24: Tables and Hashing

3 Tables What is a table??

4 List ADT createTable() isEmpty() tableLength() tableInsert(item) tablDelete(searchKey) tableRetrieve(searchKey) tableTraverse()

5 Search Key It is important that the search key remain the same as long as the item is stored in the table. public abstract class KeyedItem { private Comparable searchKey; Public KeyItem(comparable key) { searchKey = key; } // end constructor public Comparable getKey() { return searchKey; } // end getKey } // end KeyedItem

6 Implementation?? Implementations for the ADT Table –Linear approaches Unsorted, array based Unsorted, reference based Sorted (by search key), array based Sorted (by search key), reference based –Non-linear approach Binary Search Tree The requirements of a particular application influence the selection of an implementation –What operations and how often they are used

7 Which to use??

8 ADT Table Unsorted Array Binary Search Tree ADT Table Program that uses a table Textbook, p

9 Databases Relational databases are simply a set of tables filled with data Use a variety of methods to store/retrieve that data

10 Hash Tables

11 Wouldn’t it be nice? Table ADT Key can be used as an array index IDSurnameFirst Name 1EksepshenCatchda 3BaseeksBeegoh 0HeadnodeDummy 2GettingsoonAyplus

12 Wouldn’t it be nice? Table ADT Key can be used as an array index IDSurnameFirst Name 1EksepshenCatchda 3BaseeksBeegoh 0HeadnodeDummy 2GettingsoonAyplus 0Headnode, Dummy 1Eksepshen, Catchda 2 3Baseeks, Beegoh

13 The Problem IDSurnameFirst Name EksepshenCatchda BaseeksBeegoh HeadnodeDummy GettingsoonAyplus ID range is far greater than can or should be stored...

14 The Problem General case is where we have a large range of possible keys/values, but are only storing a small number of items How do we distribute items in a smaller space?

15 Naive Solution If we have N possible search key values and M locations Simply divide N into M lots: e.g. N=1-1000, M=

16 What about collisions? If we want to store two items with search key 150 and 160, they will collide in the same array point

17 Hash Functions ? Hash Table Hash Function

18 A Hash Function ID % 10 IDSurname Eksepshen Baseeks Headnode Gettingsoon Hash Function Hash Table

19 Collision 0 Headnode Baseeks 8 9 key % 10 IDSurname Eksepshen Baseeks Headnode Gettingsoon Hash Function Hash Table Eksepshen Gettingsoon

20 Hash Function Tricks ?

21 Requirements of Hash Functions Don’t produce values outside of array Distribute items as evenly as possible Use all available space in array to minimise collision

22 Selecting Digits Digits 3 and 5 Hash Function How big do we need?

23 Folding Digits Sum of all digits Hash Function How big do we need? = 24

24 Folding Digits Group and add digits Hash Function How big do we need? = 735

25 Handling Characters Sum of Unicodes Hash Function “Catchda” Fold these as well?

26 Are these any good?? Do they even distribute values? No mention of array size?

27 Modulo Arithmetic % tablesize Hash Function

28 Modulo Arithmetic % tablesize Hash Function

29 Hash Functions Can combine multiple Hash functions into one Combine folding with modulus

30 Solutions to Collision?? All methods will result in collision There are many solutions....

31 Separate Chaining key % 10 IDSurname Eksepshen Baseeks Headnode Gettingsoon Hash Function Hash Table GettingsoonEksepshen Headnode Baseeks

32 Separate Chaining Could use ANY of the data structures so far Search time is reduced, but extra data structures required Can’t we just use array??

33 Linear Probing key % 10 IDSurname Eksepshen Baseeks Headnode Gettingsoon Hash Function Hash Table 0 Headnode 1 Eksepshen Baseeks 8 9 Clustering

34 Linear Probing key % 10 IDSurname Eksepshen Baseeks Headnode Gettingsoon Elizabeth II Hash Function Hash Table 0 Headnode 1 Eksepshen 2 Gettingsoon Baseeks 8 9 Clustering

35 Linear Probing key % 10 IDSurname Eksepshen Baseeks Headnode Gettingsoon Elizabeth II Hash Function Hash Table 0 Headnode 1 Eksepshen 2 Gettingsoon 3 Elizabeth II Baseeks 8 9 Clustering

36 Finding a Node

37 key % 10 Hash Function Hash Table 0 Headnode 1 Eksepshen 2 Gettingsoon 3 Elizabeth II Baseeks 8 9 Problem: Find item with key Solution: Search as if we were ADDING the item, checking each place we come across Stop if found or reach null Finding a Node

38 Efficiency?? What is the efficiency of these operations?? What is it dependant upon?? When is it best/worst??

39 Efficiency