Skip to content

Commit ae93c4b

Browse files
author
cam
committed
Upload
0 parents  commit ae93c4b

File tree

3 files changed

+291
-0
lines changed

3 files changed

+291
-0
lines changed

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Hashing-Cuckoo-Algorithm-
2+
3+
CPSC 335 Project 4: Hashing
4+
Prof. Doina Bein, CSU Fullerton
5+
6+
Introduction
7+
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.
8+
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.
9+
10+
The Cuckoo Hashing Algorithm
11+
12+
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 .
13+
14+
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:
15+
Java uses the following hash function on strings:
16+
17+
18+
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:
19+
20+
21+
if then =+ tablesize
22+
23+
Function f2 computes first a large number then it brings the result into the proper range using the formulas below:
24+
25+
26+
27+
if then =+ tablesize
28+
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
29+
30+
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.
31+
32+
Algorithm Engineering
33+
California State University
34+
Fullerton
35+
College of Engineering
36+
and Computer Science
37+
Department of Computer
38+
Science
39+
Dynamic Programming
40+
Monge Properties
41+
String Matching
42+
Matrix Searching
43+
Optimal Tree Construction
44+
Online algorithms
45+
emphasis on
46+
Server Problem
47+
Some related problem
48+
Self-Stabilization
49+
One of the greatest
50+
mysteries in science
51+
Quantum Nature of Universe
52+
In physics and
53+
are known
54+
Cuckoo hashing is fun
55+
56+
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.
57+
58+
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).
59+
60+
What to do
61+
1. Write clear pseudocode for the algorithm.
62+
2. Type these notes (electronically or on paper) and submit it as a PDF report.
63+
3. Implement your algorithm in C/C++/Java/Python. You may use the templates provided at the end of this file.
64+
4. Compile and execute the program.
65+
5. Complete the table using the strings from the file in6.txt as the input and insert it into the PDF report.
66+
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:
67+
K:\cpscs335> a.out > a4out.txt

in4_31.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Algorithm Engineering
2+
California State University
3+
Fullerton
4+
College of Engineering
5+
and Computer Science
6+
Department of Computer
7+
Science
8+
Dynamic Programming
9+
Monge Properties
10+
String Matching
11+
Matrix Searching
12+
Optimal Tree Construction
13+
Online algorithms
14+
emphasis on
15+
Server Problem
16+
Some related problem
17+
Self-Stabilization
18+
One of the greatest
19+
mysteries in science
20+
Quantum Nature of Universe
21+
In physics and
22+
are known
23+
Cuckoo hashing is fun

main.cpp

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
// Assignment 4: Cuckoo Hashing algorithm
2+
// XX YY ( YOU NEED TO COMPLETE YOUR NAME )
3+
// An open addressing method called Cuckoo Hashing
4+
// INPUT: an input file containing strings of characters, one string per line
5+
// OUTPUT: a detailed list of where the strings are inserted.
6+
7+
#include <iostream>
8+
#include <cstring>
9+
#include <stdio.h>
10+
11+
using namespace std;
12+
13+
// cuckoo tables' size
14+
const int tablesize = 17;
15+
// combine the two 1-dimensional table into one 2-dimensional table
16+
char t[tablesize][2][255];
17+
18+
// compute the hash functions
19+
size_t f( char*, size_t );
20+
21+
// place a string in one of the hash tables
22+
bool place_in_hash_tables ( char * );
23+
24+
int main () {
25+
26+
// the strings to be stored in the hash tables
27+
char s[255]="";
28+
char null_st[] ="";
29+
size_t i;
30+
size_t len;
31+
bool placed;
32+
33+
// clear the tables
34+
for ( i = 0; i < tablesize; i++ ) {
35+
strcpy ( t[i][0], null_st );
36+
strcpy ( t[i][1], null_st );
37+
}
38+
39+
char filename[255] = "";
40+
41+
// display the header
42+
cout << endl << "CPSC 335-x - Programming Assignment #4: ";
43+
cout << "Cuckoo Hashing algorithm" << endl;
44+
45+
// read the strings from a file
46+
cout << "Input the file name (no spaces)!" << endl;
47+
cin >> filename;
48+
49+
// open the file for reading
50+
FILE *file = fopen ( filename, "r" );
51+
if ( file != NULL ) {
52+
/* read line by line from the file */
53+
while ( fgets ( s, 255, file ) != NULL ) {
54+
// place null character at the end of the line instead of <return>
55+
len = strlen ( s );
56+
s[ len - 2 ] = '\0';
57+
// insert the string in the cuckoo table
58+
placed = place_in_hash_tables( s );
59+
// check whether the placement was successful
60+
if ( !placed ) {
61+
cout << "Placement has failed" << endl;
62+
return -1;
63+
}
64+
}
65+
fclose ( file );
66+
} else {
67+
perror ( filename ); /* why didn't the file open? */
68+
}
69+
return 0;
70+
}
71+
72+
73+
bool place_in_hash_tables ( char *s ) {
74+
75+
bool placed;
76+
size_t pos;
77+
int index;
78+
char temp_s[255];
79+
char temp[255];
80+
81+
strcpy( temp_s, s );
82+
83+
// use a counter to detect loops
84+
int counter = 0;
85+
86+
// start with table T1
87+
index = 0;
88+
89+
placed = false;
90+
91+
pos = f ( temp_s, index );
92+
93+
while ( ( !placed ) && ( counter < 2 * tablesize) ) {
94+
if ( strcmp ( t[pos][index], "" ) == 0 ) {
95+
// the entry at index <pos> in the <index> hash table is available so store the string <temp_s> there
96+
cout << "String <" << temp_s << "> will be placed at";
97+
cout << " t[" << pos <<"][" << index << "]" << endl;
98+
strcpy ( t[pos][index], temp_s );
99+
placed = true;
100+
return placed;
101+
} else {
102+
// the entry at index <pos> in the <index> hash table is not available so
103+
// obtain the string stored over there in variable <temp> and store the string <temp_s> there
104+
// now the string <temp> needs to be placed in the other table
105+
cout << "String <" << temp_s << "> will be placed at" << " t[" << pos;
106+
cout <<"][" << index << "]" << " replacing <" << t[pos][index] << ">";
107+
cout << endl;
108+
// YOU NEED TO WRITE THE CODE TO STORE IN temp THE STRING STORED AT
109+
// t[pos][index] AND STORE IN t[pos][index] THE STRING temp_s
110+
strcpy ( temp, t[pos][index] );
111+
strcpy ( t[pos][index], temp_s );
112+
strcpy ( temp_s, temp );
113+
// NOW temp_s CONTAINING THE EVICTED STRING NEEDS TO BE STORED
114+
// IN THE OTHER TABLE
115+
// WRITE THE CODE TO SET index TO INDICATE THE OTHER TABLE
116+
// WRITE THE CODE TO CALCULATE IN pos THE HASH VALUE FOR temp_s
117+
index = (index + 1) % 2;
118+
pos = f ( temp_s, index );
119+
counter++;
120+
}
121+
}
122+
return placed;
123+
};
124+
125+
size_t f ( char *s,
126+
size_t index ) {
127+
// compute the hash functions
128+
// s is the string (the key) to which we apply the hash function
129+
// index indicates which hash function will be used
130+
// index == 0 means the first hash function
131+
// index == 1 means the second hash function
132+
size_t po;
133+
size_t len;
134+
int i;
135+
int val;
136+
int temp;
137+
138+
po = 1;
139+
len = strlen ( s );
140+
141+
if ( index == 0 ) {
142+
val = s[0];
143+
val = val % tablesize;
144+
if ( val < 0 ) {
145+
val += tablesize;
146+
}
147+
148+
if ( len == 1 ) {
149+
return val;
150+
}
151+
152+
for ( i = 1; i < len; i++ ) {
153+
temp = s[i];
154+
po *= 31;
155+
156+
po = po % tablesize;
157+
if ( po < 0 ) {
158+
po += tablesize;
159+
}
160+
161+
val += temp * po;
162+
val = val % tablesize;
163+
164+
if ( val < 0 ) {
165+
val += tablesize;
166+
}
167+
}
168+
return val;
169+
} else {
170+
// YOU NEED TO IMPLEMENT THE STEPS TO CALCULATE THE SECOND
171+
// HASH FUNCTION
172+
val = s[len - 1];
173+
val = val % tablesize;
174+
if ( val < 0 ) {
175+
val += tablesize;
176+
}
177+
178+
if ( len == 1 ) {
179+
return val;
180+
}
181+
182+
for (i = 1; i < len; ++i) {
183+
temp = s[len - i - 1];
184+
po *= 31;
185+
186+
po = po % tablesize;
187+
if ( po < 0 ) {
188+
po += tablesize;
189+
}
190+
191+
val += temp * po;
192+
val = val % tablesize;
193+
194+
if ( val < 0 ) {
195+
val += tablesize;
196+
}
197+
}
198+
return val;
199+
}
200+
}
201+

0 commit comments

Comments
 (0)