19 Sep C++ data structure 5
Hashing Strings
Perform a comparative analysis regarding collision rates for various hash codes of character strings by
using a polynomial hash algorithm that allows different values of the parameter ๐. It is known that for
English words using values of: 33, 37, 39, or 41 for ๐ is preferred and reduces collisions. Use an open-
addressing scheme of a hash table and determine collisions using these four different values of ๐. Count
collisions when a hashed location for any given string maps to an occupied location in the hash table.
Use linear probing for collision resolution for one trial and quadratic probing for another trial.
Letโs understand how the polynomial hash algorithm computes a hash for a given character string.
Words in English are variable-length strings that can be viewed as a tuple of (x0, x1, …, xk-1). Choose a nonzero constant, ๐ โ 1, and calculate (x0 ๐ k-1+ x1 ๐ k-2+…+ xk-2 ๐ + xk-1) as the hash code, ignoring overflows. Mathematically speaking, this is simply a polynomial in ๐ that takes the components
(x0, x1,…, xk-1) of a string as its coefficients.
Here is the code for the polynomial hash algorithm: int hash = 0;
int n = s.length();
for (int i = 0; i < n; i ++)
hash = a * hash + s.charAt(i);
Test the hash codes of the following text files provided in Extra Files under Content in D2L:
hashText1.txt, hashText2.txt, hashText3.txt, hashText4.txt.
The input files come in the form:
[prime number greater than or equal to number of words]
[file contents as words]
For example, hashText1.txt:
2347
The quick brown fox jumped over
the lazy riverโฆ
Give a report of at least 250 words that summarizes the number of collisions for each of the files
respective to each of the four values of ๐ given above. Which value of ๐ gives the best results for each
file and why do you think so? Which probing technique yielded more clustering over the other? Where
did clustering occur? Why? Are there other probing techniques that prove to be more effective than the
two techniques tested?
Resource: https://www.cpp.edu/~ftang/courses/CS240/lectures/hashing.htm
Use the following function to parse the words from the input files and build the hash table:
void buildHashTable(ifstream& inFile, HashType<string>& hashTable, int type){ string word; if (inFile.is_open()){ while(inFile >> word){ // This for loop was taken from: // https://www.geeksforgeeks.org/removing-punctuations-given-string/ for (int i = 0, len = word.size(); i < len; i++) { // check whether parsing character is punctuation or not if (ispunct(word[i])) { word.erase(i–, 1); len = word.size(); } } if(type == 1) hashTable.InsertItemLinear(word); else hashTable.InsertItemQuadratic(word); } } }
Here is the header for the HashType class.
template <class ItemType> class HashType { public: HashType(); // Constructor HashType(int s, int factor); //Dynamic size constructor void MakeEmpty(); // Function: Returns the list to the empty state. // Post: List is empty. bool IsFull() const; // Function: Determines whether list is full. // Pre: List has been initialized. // Post: Function value = (list is full) int GetNumItems() const; // Function: Determines the number of elements in list. // Pre: List has been initialized. // Post: Function value = number of elements in list void RetrieveItem(ItemType& item, bool& found); // Function: Retrieves list element whose key matches item’s key (if // present). // Pre: List has been initialized.
// Key member of item is initialized. // Post: If there is an element someItem whose value matches // item’s value, then found = true and item contains the contents // of the item if it is found. // otherwise found = false and item is returned unchanged. // List is unchanged. void InsertItemLinear(ItemType item); // Function: Adds item to list and uses a linear probing technique to // resolve collisions. // Pre: List has been initialized. // List is not full. // item is not in list. // Post: item is in list. void InsertItemQuadratic(ItemType item); // Function: Adds item to list and uses a quadratic probing technique to // resolve collisions. // Pre: List has been initialized. // List is not full. // item is not in list. // Post: item is in list. void DeleteItem(ItemType item); // Function: Deletes the element whose key matches item’s key. // Pre: List has been initialized. // Key member of item is initialized. // One and only one element in list has a key matching item’s key. // Post: No element in list has a key matching item’s key. int Hash(string item) const; //This is the hash function for this class unsigned long int GetCollisions() const; //return the number of collisions that occured during the build of // the hash table template <class ItemHash> friend ostream& operator<<(ostream& out, const HashType<ItemHash>& items); private: int a; //the value used for a polynomial hash function int numItems; int size; ItemType* info; ItemType emptyItem = “”; //The empty string unsigned long int numCollisions; };
Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.
About Writedemy
We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.
How It Works
To make an Order you only need to clickย on โOrder Nowโย and we will direct you to ourย Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Are there Discounts?
All new clients are eligible forย 20% offย in their first Order. Our payment method is safe and secure.
