Menu

(Solved) : 0 Introduction Involves Designing Perfect Hash Function Small Set Strings Demonstrates Set Q37561783 . . .

0. Introduction.

This involves designing a perfect hash function for a small setof strings. It demonstrates that if the set of possible keys issmall, then a perfect hash function need not be hard to design, orhard to understand.

1. Theory.

A hash table is an array that associates keyswith values. A hash function takes a key as itsargument, and returns an index in the array. The object thatappears at the index is the key’s value. A hash function may returnthe same index for two different keys. This is called acollision. A collision is undesirable if two keys with thesame index have different values. A hash function that has nocollisions for a set of keys is said to be perfect forthat set. It may be perfect for some sets of keys, but not forothers.
      Most modern programming languagesuse a small set of reserved names as operators,punctuation, and syntactic markers. Reserved names are alsosometimes called reserved words or keywords. Forexample, Java is said to use 55 keywords, like if, private, while,etc. A compiler for a programming language must be able to test ifeach name in a program is reserved. The program may be hundreds orthousands of pages long, and may include thousands or millions ofnames, so the test must be done efficiently. It might beimplemented using a hash table and a perfect hash function.
      Here’s how the test works. Supposethat the hash table T is an array of strings. Each timethe compiler reads a name N, it calls a perfect hashfunction h to compute an index h(N). Ifh(N) is a legal index for T, andT[h(N)] = N, then the name isreserved, otherwise it is not. If there are unused elements inT, then they are empty strings “”, because an empty stringcan’t be a keyword. If we measure the efficiency of a test by thenumber of string comparisons it performs, then the test requiresonly O(1) comparisons. Of course this works only ifh is perfect for the set of reserved names.

2. Example.

Imagine that there is a very simple programming language whichuses the following twelve reserved names.

and

else    

or

begin

end

return

define  

if

then

do

not

while

We might define a perfect hash function for these reserved nameslike this. We get one or more characters from each name. Then weconvert each character to an integer—this is easy, becausecharacters are already represented as small nonnegative integers.In the ASCII and Unicode character sets, the characters ‘a’ through’z’ are represented as the integers 97 through 122, without gaps.Finally, we do arithmetic on the integers to obtain an index intothe hash table. We can choose the characters, and the arithmeticoperations, so that no two reserved names result in the same index.For example, if we define the hash function h to add thefirst and second characters of each name, then we get the followingindexes.

h(“and”)

  =  

207

h(“begin”)

  =  

199

h(“define”)

  =  

201

h(“do”)

  =  

211

h(“else”)

  =  

209

h(“end”)

  =  

211

h(“if”)

  =  

207

h(“not”)

  =  

221

h(“or”)

  =  

225

h(“return”)

  =  

215

h(“then”)

  =  

220

h(“while”)

  =  

223

This definition for h does not result in a perfect hashfunction, because it has collisions. For example, the strings “and”and “if” result in the index 207. Similarly, the strings “do” and”end” result in the index 211. We either didn’t choose the rightcharacters from each string, or we didn’t choose the rightoperations, or both. Unfortunately, there is no good theory abouthow to define h. The best we can do is try variousdefinitions, by trial and error, until we find one that isperfect.

3. Implementation.

For this project, you must design a perfect hash function forthe reserved names shown in the previous section. To do that, writea small test class Hasher, and run it with various definitions forthe function hash. The class’s main method calls hash for eachreserved name, and writes indexes to standard output. For fullcredit, no two reserved names can have the same index.

class Hasher  
{  
  private static final String [] reserved=  
   { “and”,  
     “begin”,  
     “define”,  
     “do”,  
     “else”,  
     “end”,  
     “if”,  
     “not”,  
     “or”,  
     “return”,  
     “then”,  
     “while” };  
  
  private static int hash(String name)  
  {  
    //  Your code goeshere.  
  }  
  
  public static void main(String []args)  
  {  
    for (int index = 0; index <reserved.length ; index += 1)  
    {  
      System.out.print(“h(“” +reserved[index] + “”) = “);  
      System.out.print(hash(reserved[index]));  
      System.out.println();  
    }  
  }  
}

Your method hash must work in O(1) time, without loopsor recursions. It must not use if’s, switch’es, or extra arrays. Itmust not call the Java method hashCode, because that uses a loop.It must not return negative integers, because they can’t be arrayindexes.
      Here are some some things to trywhen defining hash. Try using characters at the same indexes fromeach name. Try linear combinations of the characters: that is,multiplying them by small constants, then adding or subtracting theresults. Try the operator ‘%’. Try a mixture of these. Reject anydefinition of hash that is not perfect: one that returns the sameindex for two different names.
      Here are some hints about how towrite the code for hash. Characters in Java String’s are indexedstarting from 0, and ending with the length of the string minus 1.The character at index k in name is obtained byname.charAt(k). For example, the first character from nameis returned by name.charAt(0), the second character byname.charAt(1), and the last character byname.charAt(name.length()- 1). If you use a character where an integer is expected, then thecharacter turns into its integer code. For example, name.charAt(0)+ 1 adds one to the code for the first character in name, andreturns an integer.
      Don’t worry if there are gapsbetween the indexes: your hash function need not beminimal. Try to keep the returned indexes small: theyshouldn’t exceed 2000. For example, there is a perfect hashfunction for the reserved words in this assignment whose indexesrange from 1177 to 1413. It was found after about ten minutes oftrial-and-error search.

Expert Answer


Answer to 0 Introduction Involves Designing Perfect Hash Function Small Set Strings Demonstrates Set Q37561783 . . .

OR