Uploaded by Khalid Qureshi

DSALab12

advertisement
Aim
To discuss various open addressing schemes in Hashing like Linear Probing, Quadratic
Probing, and Double Hashing, with a working example and diagrammatic representation of
each.
Theory
Open addressing, also known as closed hashing, is a collision resolution technique in hash
tables. With this method, a hash collision is resolved by probing, or searching through
alternative locations in the array (the probe sequence) until either the target record is found,
or an unused array slot is found, which indicates that there is no such key in the table.
Linear probing is the simplest open addressing scheme. In linear probing, the probe
sequence is simply the next available slot in the hash table. If that slot is occupied, the
algorithm continues searching for the next available slot until an empty slot is found. This
process is repeated until the target record is found or it is determined that the record does
not exist in the table.
Quadratic probing is another open addressing scheme. In quadratic probing, the probe
sequence is a quadratic function of the hash value. This means that the distance between
each probe is increasing. This helps to reduce clustering, which is a problem with linear
probing.
Double hashing is a more sophisticated open addressing scheme that uses a second hash
function to generate the probe sequence. This helps to avoid clustering and improve the
performance of the hash table.
Time Complexity
Conclusion
Open addressing is a simple and efficient collision resolution technique. It is well-suited for
applications where the load factor of the hash table is low. However, open addressing can
suffer from clustering, which can degrade performance. Linear probing is the simplest open
addressing scheme. It is also the most efficient when the load factor is low. However, linear
probing is susceptible to clustering. Quadratic probing helps to reduce clustering, but it is
less efficient than linear probing. Double hashing is the most sophisticated open addressing
scheme. It is also the most efficient and least susceptible to clustering. However, double
hashing is the most complex to implement. The choice of open addressing scheme depends
on the specific application requirements. For applications where the load factor is low and
simplicity is important, linear probing
Code
//Linear Probing
#include <stdio.h>
#include <stdlib.h>
int size;
int calculateHash(int num, int i) {
return (num%size + i%size)%size;
}
int main()
{
printf("Enter Set Size: ");
scanf("%d", &size);
int set [size];
for (int i = 0; i<size; i++) {
set[i] = -1;
}
int quit =0, inserted=0, index,k,newNum;
int choice;
while (quit == 0) {
printf("\nSelect An Option: \n1. Insert\n2. Search\n3. Display\n4. Quit\n");
scanf("%d",&choice);
switch(choice) {
case 1:
k = 0;
printf("\nEnter Number to Insert: \n");
scanf("%d", &newNum);
for (int i = 0; i<size; i++) {
index = calculateHash(newNum, k);
if (set[index] == -1) {
set[index] = newNum;
printf("\n%d inserted at index %d", newNum, index);
break;
}
else {
k++;
//printf("\nk = %d for num %d", k, newNum);
}
if (i == size-1) {
printf("\nError: Set Full");
}
}
break;
case 2:
k = 0;
printf("\nEnter Number to Search For: \n");
scanf("%d", &newNum);
for (int i = 0; i<size; i++) {
index = calculateHash(newNum, k);
index = index%size;
if (set[index] == newNum) {
printf("\n%d found at index %d", newNum, index);
break;
}
else {
k++;
}
if (i == size-1) {
printf("\nError: Element Not Found");
}
}
break;
case 3:
printf("Displaying Set\n-------");
for (int i = 0; i<size; i++) {
if (set[i] != -1)
printf("\n%d: %d", i, set[i]);
else
printf("\n%d: ", i);
}
printf("\n");
break;
case 4:
printf("\nQuitting Program Successfully");
quit = 1;
break;
default:
printf("\nPlease Enter Valid Option");
break;
}
}
}
//-------------------------------------------------------------------------------------------------------------------//-------------------------------------------------------------------------------------------------------------------//-------------------------------------------------------------------------------------------------------------------//-------------------------------------------------------------------------------------------------------------------//-------------------------------------------------------------------------------------------------------------------//-------------------------------------------------------------------------------------------------------------------//-------------------------------------------------------------------------------------------------------------------//--------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------//Quadratic Probing
#include <stdio.h>
#include <stdlib.h>
int size;
int calculateHash(int num, int i) {
return (num%size + (i*i)%size)%size;
}
int main()
{
printf("Enter Set Size: ");
scanf("%d", &size);
int set [size];
for (int i = 0; i<size; i++) {
set[i] = -1;
}
int quit =0, inserted=0, index,k,newNum;
int choice;
while (quit == 0) {
printf("\nSelect An Option: \n1. Insert\n2. Search\n3. Display\n4. Quit\n");
scanf("%d",&choice);
switch(choice) {
case 1:
k = 0;
printf("\nEnter Number to Insert: \n");
scanf("%d", &newNum);
for (int i = 0; i<size; i++) {
index = calculateHash(newNum, k);
if (set[index] == -1) {
set[index] = newNum;
printf("%d inserted at index %d\n", newNum, index);
break;
}
else {
k++;
//printf("\nk = %d for num %d", k, newNum);
}
if (i == size-1) {
printf("\nError: Set Full");
}
}
break;
case 2:
k = 0;
printf("\nEnter Number to Search For: \n");
scanf("%d", &newNum);
for (int i = 0; i<size; i++) {
index = calculateHash(newNum, k);
index = index%size;
if (set[index] == newNum) {
printf("\n%d found at index %d", newNum, index);
break;
}
else {
k++;
}
if (i == size-1) {
printf("\nError: Element Not Found");
}
}
break;
case 3:
printf("\nDisplaying Set\n-------");
for (int i = 0; i<size; i++) {
if (set[i] != -1)
printf("\n%d: %d", i, set[i]);
else
printf("\n%d: ", i);
}
printf("\n");
break;
case 4:
printf("\nQuitting Program Successfully");
quit = 1;
break;
default:
printf("\nPlease Enter Valid Option");
break;
}
}
}
//-------------------------------------------------------------------------------------------------------------------//-------------------------------------------------------------------------------------------------------------------//-------------------------------------------------------------------------------------------------------------------//-------------------------------------------------------------------------------------------------------------------//-------------------------------------------------------------------------------------------------------------------//-------------------------------------------------------------------------------------------------------------------//--------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------//Double Hashing
#include <stdio.h>
#include <stdlib.h>
int size;
int calculateHash(int num, int i) {
return (num%size + i*((num-1)%(size-1))%size);
}
int main()
{
printf("Enter Set Size: ");
scanf("%d", &size);
int set [size];
for (int i = 0; i<size; i++) {
set[i] = -1;
}
int quit =0, inserted=0, index,k,newNum;
int choice;
while (quit == 0) {
printf("\nSelect An Option: \n1. Insert\n2. Search\n3. Display\n4. Quit\n");
scanf("%d",&choice);
switch(choice) {
case 1:
k = 0;
printf("\nEnter Number to Insert: \n");
scanf("%d", &newNum);
for (int i = 0; i<size; i++) {
index = calculateHash(newNum, k);
if (set[index] == -1) {
set[index] = newNum;
printf("%d inserted at index %d\n", newNum, index);
break;
}
else {
k++;
//printf("\nk = %d for num %d", k, newNum);
}
if (i == size-1) {
printf("\nError: Set Full");
}
}
break;
case 2:
k = 0;
printf("\nEnter Number to Search For: \n");
scanf("%d", &newNum);
for (int i = 0; i<size; i++) {
index = calculateHash(newNum, k);
index = index%size;
if (set[index] == newNum) {
printf("\n%d found at index %d", newNum, index);
break;
}
else {
k++;
}
if (i == size-1) {
printf("\nError: Element Not Found");
}
}
break;
case 3:
printf("\nDisplaying Set\n-------");
for (int i = 0; i<size; i++) {
if (set[i] != -1)
printf("\n%d: %d", i, set[i]);
else
printf("\n%d: ", i);
}
printf("\n");
break;
case 4:
printf("\nQuitting Program Successfully");
quit = 1;
break;
default:
printf("\nPlease Enter Valid Option");
break;
}
}
}
Output
Linear Probing
Quadratic Probing
Double Hashing
Download