-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cam
committed
Jun 7, 2017
0 parents
commit ae93c4b
Showing
3 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Hashing-Cuckoo-Algorithm- | ||
|
||
CPSC 335 Project 4: Hashing | ||
Prof. Doina Bein, CSU Fullerton | ||
[email protected] | ||
Introduction | ||
In this project you will design, implement, and analyze one algorithm for the hashing problem. The algorithm is called Cuckoo Hashing, presented in the class. | ||
For this problem, you will design and implement one algorithm in C/C++/Java/Python, test it on various inputs and complete a hash table with a given input. No algorithm analysis is needed for this project. | ||
|
||
The Cuckoo Hashing Algorithm | ||
|
||
There are several versions of cuckoo hashing. The version we learned in class is the simplest, where there are two hash functions, and thus only two places where any given item could be stored in the table. Let us consider the set of keys to be printable ASCII strings of length no more than 80. Let us consider the hash table size to be 17 . | ||
|
||
If key is the string representing the key, then let keysize be the size of the string and key[i] be the ASCII code of the (i+1)th character in the string key read from left to right: | ||
Java uses the following hash function on strings: | ||
|
||
|
||
Let us consider two hash functions, f1 and f2. Function f2 will compute the hash value using Java’s hash function formula, while the function f1 computes a different hash value using a different hash function. Function f1 computes first a large number then it brings the result into the proper range using the formulas below: | ||
|
||
|
||
if then =+ tablesize | ||
|
||
Function f2 computes first a large number then it brings the result into the proper range using the formulas below: | ||
|
||
|
||
|
||
if then =+ tablesize | ||
Both functions f1 and f2 compute first a large number then it brings the result into the proper range 0..tablesize-1. But we bring the intermediate results into the proper range after each calculation, we do not need to wait until we compute the final result. Also, we can ring the power term into the proper range before multiplying it with | ||
|
||
You need to insert the strings below (also given in the input file in6.txt) into the hash table provided next. Please put an empty line at the end of the file. | ||
|
||
Algorithm Engineering | ||
California State University | ||
Fullerton | ||
College of Engineering | ||
and Computer Science | ||
Department of Computer | ||
Science | ||
Dynamic Programming | ||
Monge Properties | ||
String Matching | ||
Matrix Searching | ||
Optimal Tree Construction | ||
Online algorithms | ||
emphasis on | ||
Server Problem | ||
Some related problem | ||
Self-Stabilization | ||
One of the greatest | ||
mysteries in science | ||
Quantum Nature of Universe | ||
In physics and | ||
are known | ||
Cuckoo hashing is fun | ||
|
||
into the hash table (next page) using f1 for the first table and f2 for the second table. Show the result of the insertion in the table shown on next page. | ||
|
||
Hint: consider a two-dimensional table of strings t, where t[0] is T1 and t[1] is T2. Consider a variable index that oscillates between 0 and 1as it would have oscillated between T1 and T2. In C++, the value of index could be changed using the tertiary operator: index = index? 0:1. Depending on the value of index, either apply hash function f1 (index == 0) or f2 (index == 1). | ||
|
||
What to do | ||
1. Write clear pseudocode for the algorithm. | ||
2. Type these notes (electronically or on paper) and submit it as a PDF report. | ||
3. Implement your algorithm in C/C++/Java/Python. You may use the templates provided at the end of this file. | ||
4. Compile and execute the program. | ||
5. Complete the table using the strings from the file in6.txt as the input and insert it into the PDF report. | ||
6. Create a file with the output of the program for an input value and submit it together with the program. Note, the output can be redirected to a file (for easy printing). For example, the following command line will create an output file in Linux-based operating system called a1out.txt by re-directing the output from the screen (display) to the file a1out.txt: | ||
K:\cpscs335> a.out > a4out.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Algorithm Engineering | ||
California State University | ||
Fullerton | ||
College of Engineering | ||
and Computer Science | ||
Department of Computer | ||
Science | ||
Dynamic Programming | ||
Monge Properties | ||
String Matching | ||
Matrix Searching | ||
Optimal Tree Construction | ||
Online algorithms | ||
emphasis on | ||
Server Problem | ||
Some related problem | ||
Self-Stabilization | ||
One of the greatest | ||
mysteries in science | ||
Quantum Nature of Universe | ||
In physics and | ||
are known | ||
Cuckoo hashing is fun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
// Assignment 4: Cuckoo Hashing algorithm | ||
// XX YY ( YOU NEED TO COMPLETE YOUR NAME ) | ||
// An open addressing method called Cuckoo Hashing | ||
// INPUT: an input file containing strings of characters, one string per line | ||
// OUTPUT: a detailed list of where the strings are inserted. | ||
|
||
#include <iostream> | ||
#include <cstring> | ||
#include <stdio.h> | ||
|
||
using namespace std; | ||
|
||
// cuckoo tables' size | ||
const int tablesize = 17; | ||
// combine the two 1-dimensional table into one 2-dimensional table | ||
char t[tablesize][2][255]; | ||
|
||
// compute the hash functions | ||
size_t f( char*, size_t ); | ||
|
||
// place a string in one of the hash tables | ||
bool place_in_hash_tables ( char * ); | ||
|
||
int main () { | ||
|
||
// the strings to be stored in the hash tables | ||
char s[255]=""; | ||
char null_st[] =""; | ||
size_t i; | ||
size_t len; | ||
bool placed; | ||
|
||
// clear the tables | ||
for ( i = 0; i < tablesize; i++ ) { | ||
strcpy ( t[i][0], null_st ); | ||
strcpy ( t[i][1], null_st ); | ||
} | ||
|
||
char filename[255] = ""; | ||
|
||
// display the header | ||
cout << endl << "CPSC 335-x - Programming Assignment #4: "; | ||
cout << "Cuckoo Hashing algorithm" << endl; | ||
|
||
// read the strings from a file | ||
cout << "Input the file name (no spaces)!" << endl; | ||
cin >> filename; | ||
|
||
// open the file for reading | ||
FILE *file = fopen ( filename, "r" ); | ||
if ( file != NULL ) { | ||
/* read line by line from the file */ | ||
while ( fgets ( s, 255, file ) != NULL ) { | ||
// place null character at the end of the line instead of <return> | ||
len = strlen ( s ); | ||
s[ len - 2 ] = '\0'; | ||
// insert the string in the cuckoo table | ||
placed = place_in_hash_tables( s ); | ||
// check whether the placement was successful | ||
if ( !placed ) { | ||
cout << "Placement has failed" << endl; | ||
return -1; | ||
} | ||
} | ||
fclose ( file ); | ||
} else { | ||
perror ( filename ); /* why didn't the file open? */ | ||
} | ||
return 0; | ||
} | ||
|
||
|
||
bool place_in_hash_tables ( char *s ) { | ||
|
||
bool placed; | ||
size_t pos; | ||
int index; | ||
char temp_s[255]; | ||
char temp[255]; | ||
|
||
strcpy( temp_s, s ); | ||
|
||
// use a counter to detect loops | ||
int counter = 0; | ||
|
||
// start with table T1 | ||
index = 0; | ||
|
||
placed = false; | ||
|
||
pos = f ( temp_s, index ); | ||
|
||
while ( ( !placed ) && ( counter < 2 * tablesize) ) { | ||
if ( strcmp ( t[pos][index], "" ) == 0 ) { | ||
// the entry at index <pos> in the <index> hash table is available so store the string <temp_s> there | ||
cout << "String <" << temp_s << "> will be placed at"; | ||
cout << " t[" << pos <<"][" << index << "]" << endl; | ||
strcpy ( t[pos][index], temp_s ); | ||
placed = true; | ||
return placed; | ||
} else { | ||
// the entry at index <pos> in the <index> hash table is not available so | ||
// obtain the string stored over there in variable <temp> and store the string <temp_s> there | ||
// now the string <temp> needs to be placed in the other table | ||
cout << "String <" << temp_s << "> will be placed at" << " t[" << pos; | ||
cout <<"][" << index << "]" << " replacing <" << t[pos][index] << ">"; | ||
cout << endl; | ||
// YOU NEED TO WRITE THE CODE TO STORE IN temp THE STRING STORED AT | ||
// t[pos][index] AND STORE IN t[pos][index] THE STRING temp_s | ||
strcpy ( temp, t[pos][index] ); | ||
strcpy ( t[pos][index], temp_s ); | ||
strcpy ( temp_s, temp ); | ||
// NOW temp_s CONTAINING THE EVICTED STRING NEEDS TO BE STORED | ||
// IN THE OTHER TABLE | ||
// WRITE THE CODE TO SET index TO INDICATE THE OTHER TABLE | ||
// WRITE THE CODE TO CALCULATE IN pos THE HASH VALUE FOR temp_s | ||
index = (index + 1) % 2; | ||
pos = f ( temp_s, index ); | ||
counter++; | ||
} | ||
} | ||
return placed; | ||
}; | ||
|
||
size_t f ( char *s, | ||
size_t index ) { | ||
// compute the hash functions | ||
// s is the string (the key) to which we apply the hash function | ||
// index indicates which hash function will be used | ||
// index == 0 means the first hash function | ||
// index == 1 means the second hash function | ||
size_t po; | ||
size_t len; | ||
int i; | ||
int val; | ||
int temp; | ||
|
||
po = 1; | ||
len = strlen ( s ); | ||
|
||
if ( index == 0 ) { | ||
val = s[0]; | ||
val = val % tablesize; | ||
if ( val < 0 ) { | ||
val += tablesize; | ||
} | ||
|
||
if ( len == 1 ) { | ||
return val; | ||
} | ||
|
||
for ( i = 1; i < len; i++ ) { | ||
temp = s[i]; | ||
po *= 31; | ||
|
||
po = po % tablesize; | ||
if ( po < 0 ) { | ||
po += tablesize; | ||
} | ||
|
||
val += temp * po; | ||
val = val % tablesize; | ||
|
||
if ( val < 0 ) { | ||
val += tablesize; | ||
} | ||
} | ||
return val; | ||
} else { | ||
// YOU NEED TO IMPLEMENT THE STEPS TO CALCULATE THE SECOND | ||
// HASH FUNCTION | ||
val = s[len - 1]; | ||
val = val % tablesize; | ||
if ( val < 0 ) { | ||
val += tablesize; | ||
} | ||
|
||
if ( len == 1 ) { | ||
return val; | ||
} | ||
|
||
for (i = 1; i < len; ++i) { | ||
temp = s[len - i - 1]; | ||
po *= 31; | ||
|
||
po = po % tablesize; | ||
if ( po < 0 ) { | ||
po += tablesize; | ||
} | ||
|
||
val += temp * po; | ||
val = val % tablesize; | ||
|
||
if ( val < 0 ) { | ||
val += tablesize; | ||
} | ||
} | ||
return val; | ||
} | ||
} | ||
|