0% found this document useful (0 votes)
27 views

5-Hash Table Datastructure

The document discusses hash tables and techniques for resolving collisions in hash tables. It defines key terms like hashing, hash functions, and hash tables. It explains that a hash table uses a hash function to map keys to indexes in an array, and collisions occur when two keys map to the same index. Two collision resolution techniques are open hashing, which uses linked lists at each index, and closed hashing, which uses techniques like linear or quadratic probing to find the next open index.

Uploaded by

Hai Kim Sreng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

5-Hash Table Datastructure

The document discusses hash tables and techniques for resolving collisions in hash tables. It defines key terms like hashing, hash functions, and hash tables. It explains that a hash table uses a hash function to map keys to indexes in an array, and collisions occur when two keys map to the same index. Two collision resolution techniques are open hashing, which uses linked lists at each index, and closed hashing, which uses techniques like linear or quadratic probing to find the next open index.

Uploaded by

Hai Kim Sreng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Algorithm and

Programming
(C++)
Hash Table

1
Outline

▪ Time complexity

▪ What is Hashing? Hash table? Hash function?

▪ Collision

▪ Collision resolution techniques


▪ Open hashing

▪ Close hashing

▪ Examples
2
Time complexity

Time complexity is a time required for an algorithm to run.


It is denoted by a big-O notation: O(n) ▪ Best case
▪ Average case
▪ Worst case
▪ Linked list : O(n)

▪ Array (unsorted) : O(n) Searching time


complexity
▪ Array (sorted) : O(log2 n)

3
Big-O Notation

▪ YouTube video: bit.ly/312x9ou

4
Other Data structure
❑ Searching operation

▪ Linked list

▪ Array

▪ Stack What is the time required


to search ?
▪ Queue

O(n)
5
Hash Table
❑ Searching operation: Time Complexity

Hash Table : O(1)

6
Terms
❑ Hash Table Vs. Hash Function Vs. Hashing

▪ Hash Table
▪ Is a data structure which is used to store key-value pairs. It is implemented as array

▪ Hash Function
▪ Is a function used by hash table to compute an index of an array in which an element will
be inserted to or be searched at

▪ Hashing
▪ In hashing, large keys are converted into smaller ones using hash function AND then the
values are stored in hash table

7
Hash Functions

▪ A hash function usually means a function that compresses.


▪ The output is shorter than the input

8
An example of a hash function
❑ Definition

int hash(int key){ Size of array is


return key%7; taken.

9
#include<iostream> Example: Implementation of hash table
using namespace std;
void initializeArray(){ 1 main(){
for(int i=0; i<SIZE; i++){
const int SIZE=7; ht[i] = -999 main(){ insertData(7); 2
int ht[SIZE]; }
} insertData(7); insertData(8);
int hashFunction(int n){ insertData(8); insertData(15);
return n%SIZE; insertData(25); insertData(22);
} displayHT(); insertData(25);
void insertData(int value){ } displayHT();
int index; }
index = hashFunction(value);
ht[index] = value; What is the output?
}
void displayHT(){
for(int i=0; i<SIZE; i++){
cout<<i<<"\t --> ";
cout<<ht[i]<<"\n“’

}
} Ooop! Data is overridden?
This is known as collision!
10
Collision
❑ Definition

▪ A situation when two or more data hash to be stored in the same


location in the table, is called a hash collision.

• Suppose we have a hash table with 7 elements


• Hash function is to modulo with 7
• How to insert these numbers?
• 15, 11, 27, 8

11
Factors of a good hash function

✓ Easy to compute

✓ Minimize collision

12
Perfect Hashing
❑ Definition

▪ Perfect hashing maps each valid input to a different hash value

(no collision)

13
Collision Resolution Techniques

(Array-based implementation)
(An array of linked list implementation)

An example of using chaining


14
Solution for Hash Collision
❑ Open Hashing

▪ Open hashing defines each slot in the hash table to be the head of a
linked list

▪ All records that hash to a particular slot (collision cases) are


placed on that slot’s linked list

15
Hash Table with Chaining resolution technique
struct Element{ 1 void addEnd(List *ls, int value){ 4
int value; Element *e; int hashFunction(int n){ int main(){ 7
Element *next; e= new Element;
}; 3 return n%SIZE;
createEmptyAllLists();
e->next=NULL; }
struct List{
e->value=value; insertData(7);
int n;
Element *head, *tail;
}; if(ls->n==0){ 5 insertData(8);
const int SIZE=7; ls->head = e; void insertData(int value){ insertData(15);
List *ht[SIZE]; ls->tail = e;
int index; insertData(22);
ls->n = ls->n + 1;
}else{ index = hashFunction(value);
List* createAnEmptylist(){ 2 addEnd(ht[index], value); insertData(25);
ls->tail->next = e;
List *L1; ls->tail = e; } displayHT();
L1 = new List(); ls->n = ls->n + 1;
L1->n = 0; }
}
L1->head=NULL; void displayHT(){
}
L1->tail=NULL; Element *e;
return L1; for(int i=0; i<SIZE; i++){ 6 Output?
} cout<<i<<"\t -->";
void createEmptyAllLists(){ if(ht[i]!=NULL){
for(int i=0; i<SIZE; i++){ e = ht[i]->head;
ht[t] = createEmptyList() while(e!=0){
} cout<<e->value<<" ";
e=e->next;
}
Display data }
}
in hash table cout<<endl;
}
}
16
Solution for Hash Collision
❑ Closed Hashing

1. Linear probing

2. Quadratic probing

3. Double hashing
Case Study
-Teamwork (4-5 person/team) –using project team
-Period: 1 week
-Submit a report with code testing to G Classroom
12th December 2021

17
Q&A
18
Discussion

You might also like