Question

In: Computer Science

Program in C++ **********Write a program to compute the number of collisions required in a long...

Program in C++

**********Write a program to compute the number of collisions required in a long random sequence of insertions using linear probing, quadratic probing and double hashing. For simplicity, only integers will be hashed and the hash function h(x) = x % D where D is the size of the table (fixed size of 1001). The simulation should continue until the quadratic hashing fails.*********

Solutions

Expert Solution

1. Linear Probing

#include<iostream>

using namespace std;

int main()

{
const int MAX_SIZE = 11;
int collisions = 0;
int array[MAX_SIZE];
bool flags[MAX_SIZE];

for (int i = 0; i < MAX_SIZE; i++)
{
array[i] = 0;
flags[i] = false;

}

//<Inserting numbers in hash table using linear probing
Linear_Probing(array, flags, MAX_SIZE, 67, collisions);
Linear_Probing(array, flags, MAX_SIZE, 19, collisions);
Linear_Probing(array, flags, MAX_SIZE, 3, collisions);
Linear_Probing(array, flags, MAX_SIZE, 808, collisions);
Linear_Probing(array, flags, MAX_SIZE, 337, collisions);
Linear_Probing(array, flags, MAX_SIZE, 1, collisions);
Linear_Probing(array, flags, MAX_SIZE, 86, collisions);
Linear_Probing(array, flags, MAX_SIZE, 38, collisions);

return 0;
}

bool Linear_Probing(int array[], bool flags[], int MAX_SIZE, int number, int &collisions)
{
int dummy_collisions = 0;
int index = Index_Finder(number, flags, MAX_SIZE, dummy_collisions);

if (index == -1)
return false;

else
{
collisions += dummy_collisions;

array[index] = number;
flags[index] = true;
return true;
}
}

int Index_Finder(int number, bool flags[], int MAX_SIZE, int &collisions)
{
int index = Hash(number);

if (index < MAX_SIZE && !flags[index])
return index;

if (index >= MAX_SIZE)
{
index = 0;

if (!flags[index])
return index;

else
collisions++;
}

else
{
collisions++;
}

int limit = index;

return Index_Finder(flags, MAX_SIZE, ++index, limit, collisions);

}

int Index_Finder(bool flags[], int MAX_SIZE, int index, int limit, int &collisions)
{
if (limit == index)
return -1;

if (index == MAX_SIZE)
index = 0;

if (!flags[index])
return index;

else
{
collisions++;
return Index_Finder(flags, MAX_SIZE, ++index, limit, collisions);
}
}


int Hash(int number)
{
int first_digit = 0, last_digit = 0;
bool first_time = true;

while (number > 0)
{
if (first_time)
{
first_digit = number % 10;
number -= (number % 10);
number /= 10;
first_time = false;
}

else
{
last_digit = number % 10;
number -= (number % 10);
number /= 10;
}
}

return (first_digit + last_digit);
}

2. Quadratic Probing

#include <iostream>
#include <cstdlib>
#define MIN_TABLE_SIZE 10
using namespace std;

enum EntryType {Legitimate, Empty, Deleted};

struct HashNode
{
int element;
enum EntryType info;
};

struct HashTable
{
int size;
HashNode *table;
};

bool isPrime (int n)
{
if (n == 2 || n == 3)
return true;
if (n == 1 || n % 2 == 0)
return false;
for (int i = 3; i * i <= n; i += 2)
if (n % i == 0)
return false;
return true;
}

int nextPrime(int n)
{
if (n <= 0)
n == 3;
if (n % 2 == 0)
n++;
for (; !isPrime( n ); n += 2);
return n;
}
int HashFunc(int key, int size)
{
return key % size;
}

HashTable *initializeTable(int size)
{
HashTable *htable;
if (size < MIN_TABLE_SIZE)
{
cout<<"Table Size Too Small"<<endl;
return NULL;
}
htable = new HashTable;
if (htable == NULL)
{
cout<<"Out of Space"<<endl;
return NULL;
}
htable->size = nextPrime(size);
htable->table = new HashNode [htable->size];
if (htable->table == NULL)
{
cout<<"Table Size Too Small"<<endl;
return NULL;
}
for (int i = 0; i < htable->size; i++)
{
htable->table[i].info = Empty;
htable->table[i].element = NULL;
}
return htable;
}
int Find(int key, HashTable *htable)
{
int pos = HashFunc(key, htable->size);
int collisions = 0;
while (htable->table[pos].info != Empty &&
htable->table[pos].element != key)
{
pos = pos + 2 * ++collisions -1;
if (pos >= htable->size)
pos = pos - htable->size;
}
return pos;
}
void Insert(int key, HashTable *htable)
{
int pos = Find(key, htable);
if (htable->table[pos].info != Legitimate)
{
htable->table[pos].info = Legitimate;
htable->table[pos].element = key;
}
}

HashTable *Rehash(HashTable *htable)
{
int size = htable->size;
HashNode *table = htable->table;
htable = initializeTable(2 * size);
for (int i = 0; i < size; i++)
{
if (table[i].info == Legitimate)
Insert(table[i].element, htable);
}
free(table);
return htable;
}
void Retrieve(HashTable *htable)
{
for (int i = 0; i < htable->size; i++)
{
int value = htable->table[i].element;
if (!value)
cout<<"Position: "<<i + 1<<" Element: Null"<<endl;
else
cout<<"Position: "<<i + 1<<" Element: "<<value<<endl;
}

}

int main()
{
int value, size, pos, i = 1;
int choice;
HashTable *htable;
while(1)
{
cout<<"Operations on Quadratic Probing"<<endl;
cout<<"1.Initialize size of the table"<<endl;
cout<<"2.Insert element into the table"<<endl;
cout<<"3.Display Hash Table"<<endl;
cout<<"4.Rehash The Table"<<endl;
cout<<"5.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter size of the Hash Table: ";
cin>>size;
htable = initializeTable(size);
cout<<"Size of Hash Table: "<<nextPrime(size);
break;
case 2:
if (i > htable->size)
{
cout<<"Table is Full, Rehash the table"<<endl;
continue;
}
   cout<<"Enter element to be inserted: ";
   cin>>value;
Insert(value, htable);
i++;
break;
case 3:
Retrieve(htable);
break;
case 4:
htable = Rehash(htable);
break;
case 5:
exit(1);
default:
cout<<"\nEnter correct option\n";
}
}
return 0;
}

3. Double Hashing

#include <iostream>
#define MIN_TABLE_SIZE 11
using namespace std;

enum EntryType {Legitimate, Empty, Deleted};

struct HashNode
{
int element;
enum EntryType info;
};

struct HashTable
{
int size;
HashNode *table;
};

int HashFunc1(int key, int size)
{
return key % size;
}

int HashFunc2(int key, int size)
{
return (key * size - 1) % size;
}

HashTable *initializeTable(int size)
{
HashTable *htable;
if (size < MIN_TABLE_SIZE)
{
cout<<"Table Size Too Small"<<endl;
return NULL;
}
htable = new HashTable;
if (htable == NULL)
{
cout<<"Out of Space"<<endl;
return NULL;
}
htable->size = size;
htable->table = new HashNode [htable->size];
if (htable->table == NULL)
{
cout<<"Table Size Too Small"<<endl;
return NULL;
}
for (int i = 0; i < htable->size; i++)
{
htable->table[i].info = Empty;
htable->table[i].element = NULL;
}
return htable;
}

int Find(int key, HashTable *htable)
{
int hashVal= HashFunc1(key, htable->size);
int stepSize= HashFunc2(key, htable->size);
while (htable->table[hashVal].info != Empty &&
htable->table[hashVal].element != key)
{
hashVal = hashVal + stepSize;
hashVal = hashVal % htable->size;
}
return hashVal;
}

void Insert(int key, HashTable *htable)
{
int pos = Find(key, htable);
if (htable->table[pos].info != Legitimate )
{
htable->table[pos].info = Legitimate;
htable->table[pos].element = key;
}
}

HashTable *Rehash(HashTable *htable)
{
int size = htable->size;
HashNode *table = htable->table;
htable = initializeTable(2 * size);
for (int i = 0; i < size; i++)
{
if (table[i].info == Legitimate)
Insert(table[i].element, htable);
}
free(table);
return htable;
}

void Retrieve(HashTable *htable)
{
for (int i = 0; i < htable->size; i++)
{
int value = htable->table[i].element;
if (!value)
cout<<"Position: "<<i + 1<<" Element: Null"<<endl;
else
cout<<"Position: "<<i + 1<<" Element: "<<value<<endl;
}

}

int main()
{
int value, size, pos, i = 1;
int choice;
HashTable *htable;
while(1)
{
cout<<"Operations on Double Hashing"<<endl;
cout<<"1.Initialize size of the table"<<endl;
cout<<"2.Insert element into the table"<<endl;
cout<<"3.Display Hash Table"<<endl;
cout<<"4.Rehash The Table"<<endl;
cout<<"5.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter size of the Hash Table: ";
cin>>size;
htable = initializeTable(size);
break;
case 2:
if (i > htable->size)
{
cout<<"Table is Full, Rehash the table"<<endl;
continue;
}
   cout<<"Enter element to be inserted: ";
   cin>>value;
Insert(value, htable);
i++;
break;
case 3:
Retrieve(htable);
break;
case 4:
htable = Rehash(htable);
break;
case 5:
exit(1);
default:
cout<<"\nEnter correct option\n";
}
}
return 0;
}


Related Solutions

Using C++ Write a program to compute the number of collisions required in a long random...
Using C++ Write a program to compute the number of collisions required in a long random sequence of insertions using linear probing, quadratic probing, and double hashing.
in C++ Write a program to compute the current and the power dissipation in an AC...
in C++ Write a program to compute the current and the power dissipation in an AC circuit that has four resistors R1, R2, R3, and R4 in parallel. The voltage source is V. Test your solution with various voltage levels and resistor values. Execute and submit the program and the results in screen captures. Please note that the equivalent resistor is given by 1/Rtotal = 1/R1 + 1/R2 + 1/R3 + 1/R4 and the current I is given by I...
Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
C program help 1. Write a program to compute the Mileage given by a vehicle. Mileage...
C program help 1. Write a program to compute the Mileage given by a vehicle. Mileage = (new_odometer – old_odometer)/(gallons_gas) // illustrating how ‘for’ loop works. 2. How to initialize an array of size 5 using an initializer list and to compute it’s sum How to initialize an array of size 5 with even numbers starting from 2 using ‘for’ loop and to compute it’s sum 3. Program to compute the car insurance premium for a person based on their...
write c++ program that takes the depth ( in kilometer) inside the earth to compute and...
write c++ program that takes the depth ( in kilometer) inside the earth to compute and display the temperature at the depth in degrees celsius and fahrenheit. the relevant formulas are: celsius+ 10 x depth + 20 fahrenheit = 9/5 celsius + 23
Write a program in C that prompts the user for a number of seconds and then...
Write a program in C that prompts the user for a number of seconds and then converts it to h:m:s format. Example: 5000 seconds should display as 1:23:20 (1 hour, 23 minutes, 20 seconds.) Test with several values between about 100 seconds and 10,000 seconds. use unint and remainders for this and keep it as simple as possible.
Write a program In C to compute internet charges according to a rate schedule. The rate...
Write a program In C to compute internet charges according to a rate schedule. The rate schedule is as follows: $0.08 per GB for usage between 0 and 40 GB, inclusive $0.07 per GB for usage between 41 GB and 70 GB, inclusive $0.05 per GB for usage between 71 GB and 110 GB, inclusive $0.04 per GB for usage greater than 110 GB Learning Objectives In this assignment, you will: Use a selection control structure Use a repetition control...
Subject is C++ Write a program to compute internet charges according to a rate schedule. The...
Subject is C++ Write a program to compute internet charges according to a rate schedule. The rate schedule is as follows: $0.08 per GB for usage between 0 and 40 GB, inclusive $0.07 per GB for usage between 41 GB and 70 GB, inclusive $0.05 per GB for usage between 71 GB and 110 GB, inclusive $0.04 per GB for usage greater than 110 GB code must use these four functions, using these names (in addition to main): getData computeCharges...
2.c++ if and loop statement Write a program that will count the number of even number...
2.c++ if and loop statement Write a program that will count the number of even number and odd numbers between two inputted numbers. Display the numbers and compute the sum and average of all the even numbers and the sum and average all the odd numbers. Sample outputs: Enter starting number:3 Enter starting number:4 Enter ending number:10 Enter ending number:10 odd numbers Even number 3 4 5 6 7 8 9 10 number of even numbers=4 number of even numbers=4...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT