Hash Tables


Problems

Problem : Give the best, average, and worst case efficiencies of both the brute-force string search and Rabin-Karp string search.


Problem : How does Rabin-Karp achieve an efficiency of O(M + N) ?


Problem : Using the hash() and hash_update() functions given in this section, give an example of a pattern string and text string that will reduce Rabin-Karp back to brute-force search, decreasing its efficiency back to O(MN) .


Problem : Challenge problem: Create a hash_update() function to go along with this hash() function:


long hash_str(hash_table_t *hashtable, int hash_len, char *start)
{
	long hval;
	int i; 
	
	/* If the string passed in is NULL, return 0 */
	if (start == NULL) return 0; 
	
	/* Multiply the old hash value by 257 and add the current character
	 * for as long as the string
	 */
	hval = 0L;
	for(i=0; i < hash_len; i++) {
		hval =  ((257 * hval) + start[i]) % hashtable->size;
	} 

	/* Return the hash value */
	return hval;
}
Use the function prototype:

long hash_update(
	long hval, 	/* old hash value */
	char start,	/* character to be removed */
	char end,	/* character to be added */
	int hash_len,	/* length of the string */
	hash_table_t *hashtable );	/* the hash table */


Problem : Give a hash function and a hash update function that will always reduce Rabin-Karp to O(MN) efficiency.


Take a Study Break

SparkLife

Star Trek gets SEXY

Chris Pine and Zoe Saldana heat up the red carpet!

SparkLife

Are you afraid of relationships?

Auntie SparkNotes can help!

SparkLife

Wanna get JLaw's gorgeous glow?

Click here for simple, sexy makeup tricks!

SparkLife

Sexy starlet style

See every single look from the Met Gala!

SparkLife

Who'd be on your zombie-apocalypse crew?

We already dib'sed Genghis Khan.

Geek out!

The MindHut

Geeky Actors: Then and Now

Travel back in time!

The MindHut

Villains We Want These Actresses to Play

From super cute to super bad!

The MindHut

10 Movies Better Than Their Books

What do you think?

The MindHut

How To Look Like J-Law...

When you don't look like J-Law.

The MindHut

12 Scientific Inaccuracies in Into Darkness

What did Star Trek get wrong?

The Book

Cover image

Read What You Love, Anywhere You Like

Get Our FREE NOOK Reading Apps