Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12 Hash Table.

Similar presentations


Presentation on theme: "Chapter 12 Hash Table."— Presentation transcript:

1 Chapter 12 Hash Table

2 Hash Table So far, the best worst-case time for searching is O(log n).
average search time of O(1). worst case search time of O(n).

3 Learning Objectives Develop the motivation for hashing.
Study hash functions. Understand collision resolution and compare and contrast various collision resolution schemes. Summarize the average running times for hashing under various collision resolution schemes. Explore the java.util.HashMap class.

4 12.1 Motivation Let's design a data structure using an array for which the indices could be the keys of entries. Suppose we wanted to store the keys 1, 3, 5, 8, 10, with a guaranteed one-step access to any of these.

5 12.1 Motivation The space consumption does not depend on the actual number of entries stored. It depends on the range of keys. What if we wanted to store strings? For each string, we would first have to compute a numeric key that is equivalent to it. java.lang.String.hashCode() computes the numeric equivalent (or hashcode) of a string by an arithmetic manipulation involving its individual characters.

6 12.1 Motivation Using numeric keys directly as indices is out of the question for most applications. There isn't enough space

7 12.1 Motivation

8 12.2 Hashing A simple hash function table size of 10 h(k) = k mod 10

9 12.2 Hashing ear collides with cat at position 4.
There is empty space in the table, and it is up to the collision resolution scheme to find an appropriate position for this string. A better mapping function For any hash function one could devise, there are always hashcodes that could force the mapping function to be ineffective by generating lots of collisions.

10 12.2 Hashing

11 12.3 Collision Resolution There are two ways to resolve collisions.
open addressing Find another location for the colliding key within the hash table. closed addressing store all keys that hash to the same location in a data structure that “hangs off” that location.

12 Linear Probing

13 Linear Probing As more and more entries are hashed into the table, they tend to form clusters that get bigger and bigger. The number of probes on collisions gradually increases, thus slowing down the hash time to a crawl.

14 Linear Probing Insert "cat", "ear", "sad", and "aid"

15 Linear Probing Clustering is the downfall of linear probing, so we need to look to another method of collision resolution that avoids clustering.

16 Quadratic Probing

17 12.3.2 Quadratic Probing Avoids Clustering
When the probing stops with a failure to find an empty spot, as many as half the locations of the table may still be unoccupied. A hash to 2,3,6,0,7, and 5 are endlessly repeated, and an insertion is not done, even though half the table is empty.

18 Quadratic Probing For any given prime N, once a location is examined twice, all locations that are examined thereafter are also ones that have been already examined.

19 Chaining If a collision occurs at location i of the hash table, it simply adds the colliding entry to a linked list that is built at that location.

20 Example Store the following elements into a hash table with size = 10, using hash function f(k) = k % table size where k is the hash code.: Using linear probing method for resolving collisions. Using Chaining method for resolving collisions. element Hash code “Rami” 93 “Samer” 246 “Majd” 22 “Lina” 103 “Farah” 316

21 Running times We assume that the hashing process itself (hashcode and mapping) takes O(1). Running time of insertion is determined by the collision resolution scheme.

22 12.4 The java.util.HashMap Class
Consider a university-wide database that stores student records. Every student is assigned a unique id (key), with which is associated several pieces of information such as name, address, credits, gpa, etc. These pieces of information constitute the value.

23 12.4 The java.util.HashMap Class
A StudentInfo dictionary that stores (id, info) pairs for all the students enrolled in the university. The operations corresponding to this relationship can be found in hava.util.Map<K,V> Other Map methods: Clear() isEmpty() Size()

24 12.4 The java.util.HashMap Class
The Map interface also provides operations to enumerate all the keys, enumerate all the values, get the size of the dictionary, check whether the dictionary is empty, and so on. The java.util.HashMap implements the dictionary abstraction as specified by the java.util.Map interface. It resolves collisions using chaining.

25 12.4.1 Table and Load Factor When the no-arg constructor is used
Default initial capacity 16 The table size is defined as the actual number of key-value mappings in the has table.

26 12.4.1 Table and Load Factor We can choose an initial capacity
Only uses capacities that are powers of 2. 101 becomes 128

27 Table and Load Factor An initial capacity of 128.

28 12.4.3 Adding an Entry Example
Name serves as a key to the phone number value.

29 Code Example Input file:
ahmad sami albagdadi salem saeed fahmawee rana sami malkawi leena fahmi alkhateeb

30 package javaapplication4; import java. io. File; import java. util
package javaapplication4; import java.io.File; import java.util.*; public class JavaApplication4 { public static void main(String[] args) { String name=""; String id=""; String sid=""; HashMap<String,String> student = new HashMap<String,String>(); Scanner sin = new Scanner(System.in); try { Scanner s = new Scanner(new File("input.txt")); while(s.hasNext()) id = s.next(); name = s.next()+" "+s.next()+" "+s.next(); student.put(id, name); } catch(Exception e) System.out.println("file not found"); System.out.println("Enter student id"); sid = sin.next(); if(student.containsKey(sid)) System.out.println("Student name is: "+student.get(sid)); else System.out.println(" student id not found");


Download ppt "Chapter 12 Hash Table."

Similar presentations


Ads by Google