Module 3 Hash Algorithms: User documentation: 1. Introduction: One of the major concerns of the theory of data structures and algorithms is the efficient storage and retrieval of data. If data is not stored in a structured way, the only method of retrieval is scanning through the pile. The time to retrieve an individual item will then increase proportionately to the size of the pile. Hashing algorithms are used to overcome the dependence of retrieval time on the data size and tries to keep it constant. In a hash algorithm, the address where the data is stored is calculated and stored in a table along with a key value. The table that stores the addresses of the data items along with their key values is called the hash table. Assuming the address could be calculated independently of the number of items in the data structure, the retrieval or testing for an item's presence can be done in constant time. The function that calculates the address where the data is to be stored in the data structure and maps them to key values is called as hash function and the process is called as hashing. Different has functions have been developed that are used for varying degrees of data complexity and efficiency required. Some of the hashing functions that have been used in this applet are. 1) Taking the remainder (Modulus) of the number and table size. 2) Adding the digits of the number and taking modulus with the table size. 3) Reversing the digits of the number and taking modulus with the table size. 4) Taking the middle bits of the square of the number entered, and taking the modulus on the value of the middle bits with the table size. In this applet, linear probe collision resolution policy has been used for all the hash functions. Collision occurs when two data items map to the same address index. The collision resolution amechanims have been explained in detail in the later sections. This applet animates the hash functions mentioned before. The user interface is shown below. Figure shows the user interface. User interacts with the system using 2 combo-boxes, an input field and push buttons. The controls provided are: a) Function combo-box: This combo box allows you to select the hash function to compute the key value for the input. b) Table Size combo-box: This allows the user to select the table size. c) Input field: Allows user to enter the number. d) HashKey: The user needs to press this button to calculate the hash value for the number entered. e) Next: This button allows users to go to the next step in the algorithm. The speed of the animation depends on its use. f) Restart: This button allows users to restart the animation. g) Help: This button explains to users the interface of the system in brief. User feedback is given, using the display and the text field. 2) Applet Animation and User Tasks: Figure shows the combo-box and the various hash function available to the users. When a user starts the animation system, the text display as shown above, instructs the user to select the hash function that the user wants to implement using the combo box. Then the users select the size of the table to be implemented. Figure shows, the table is drawn once the user selects the table size. The table is drawn once the user selects the table size. The user then needs to input a number using the text input field, to enter into the table. The user can then compute the hash key by pressing the hash key button. As shown in the figure below, the user is given feedback on the display regarding the hash value computed. In addition to displaying the value that was calculated, the mechanism/formula used to calculate the hash value is also shown on the display. The formula shows the use of the hash function in it. Figure shows the display of the hash value on the display and the formula used to calculate it. The user then steps through the algorithm using the next button. The slots in the table are highlighted depending on the values that are generated using the hash function as shown in the figure below. Figure shows that the position calculated by the hash value is highlighted. In case collision resolution policy is used to determine an empty space to fill up with a new data item, the user is given feedback on how the hash value or the table position using the collision resolution is recalculated. Figure shows the display of the collision resolution policy on the display and the function used to calculate it. The restart button restarts the system and allows the user to start from the beginning. Figure shows the restarting of the system. The users are given appropriate instructions on the text field. In addition to the instructions in the text area and the feedback messages, the user can also refer to the help manual if there is any confusion with the interface and the graphical display. The help manual is displayed when the help button is clicked. Figure shows the help manual for the system. Software documentation: 3) Code Explanation: The following classes are used to implement this applet. The explanation for each of the classes is given below. a) HashMain: The HashMain class is written in the HashMain.java file. This class contains the main () function. To compile the system the user needs to type in the following command: “javac HashMain.java”. To run the system the user needs to type in the following command: “java HashMain” The class “HashMain” calls the setUp class. The control flow is shown below. Calls HashMain setUp The HashMain has only one function main () where the program execution begins b) setUp: The setUp function written in HashMain.java file, controls the user interface and interaction of the entire system. The following Java swing classes are used to create the user interface. - JFrame - JPanel - JTextPane - JScrollPane - JButton - JComboBox - JtextField - Container Other java classes used are as follows: - String - Style - StyleConstants Besides the above java classes the following classes written as a part of this thesis are also imported to create the user interface and show graph animation. - drawTable - AddDigits - ModeFunctions - ReverseDigits - MidSquare - InstructionsHash Each of the above classes is explained in detail below. Apart from these classes, Java Action Listeners have been used for creating the buttons, text field and the combo box. Document interface has been used for the JTextPane class. The constructor of the class i.e. setUp () creates the display area that the text area and the buttons text field and combo box that allows the user to interact with the system. The method summary of the setUp class: a) actionPerformed () This function is called when the user presses any of the buttons, combo box or enters a number in the text field. Depending on the button pressed or the algorithm selected in the combo box or the page entered in the text field, appropriate action is taken. b) ComputeHashKey () This function is called when the hashKey button is pressed. The function calls appropriate function depending on the hash function that the user has selected. c) ModeOfFunction () This function is called if the user has selected Mod of Function as the hash fucntion to be implemented. It passes the user input to the ModeFunction class and gets the hash value that is computed using this approach. d) ReverseTheDigits () This function is called if the user has selected reverse the digits as the function to be implemented. It passes the user input to the ReverseDigits class and gets the hash value that is computed using this approach. e) AddTheDigits () This function is called if the user has selected add the digits as the function to be implemented. It passes the user input to the AddTheDigits class and gets the hash value that is computed using this approach. f) MidSquareMethod () This function is called if the user has selected mid square method as the function to be implemented. It passes the user input to the MidSquare class and gets the hash value that is computed using this approach. g) RunDisplay () This method passes the appropriate parameters to the drawTable class to perform graphics operation. h) Message () This method is called to display a message in the text field. The control flow for the setUp class is given below: setUp ModeFunction MidSquare AddTheDigits ReverseDigits drawTable InstructionsHash c) AddDigits: This class is called to compute the hash value of the number that is entered if the user has selected hash function to be implemented add digits. This function sums the digits of the number entered, the sum being the same as the number itself in case of a onedigit number. A modulus operation is performed with the sum and the table size. The table positions are explored depending on the remainder, which is taken as the hash value. The method summary for this class is as follows. a) UpdateValues () This function receives the element to be inserted in the table. b) HashValue () This function returns the hash value computed for the number to be entered in the table. c) P () This function is used to implement the collision resolution policy. The policy implemented is Linear Probing. d) ReturnTotalNumber () This function is used to return the sum of the digits of the number whose hash value is required. e) ComputePosition () This function is used to store the table positions accessed to store the number entered for performing the animation operation. f) KeyPosition () This function is to determine the position in the table where the current value is inserted. g) Duplicates () This function is used to check if the number entered by the user is already present in the table. h) IsCollisionUsed () This function is used to determine if the collision resolution policy was used to insert the current value in the table. i) ResetCollisionUsed () This function is used to reset the variables so that the next number entered is not affected by the results of the previous ones. j) ReturnPArray () This function is used to return the array that stores the positions of the table that were accessed to store the current value. These positions are used to perform the animations. The control flow for this class is as follows: Calls HashMain AddDigits d) ModeFunctions: This class is called to compute the hash value of the number that is entered if the user has selected hash function to be implemented mod function. A modulus operation is performed with the number entered and the table size. The table positions are explored depending on the remainder, which is taken as the hash value. a) UpdateValues () This function receives the element to be inserted in the table. b) HashValue () This function returns the hash value computed for the number to be entered in the table. c) P () This function is used to implement the collision resolution policy. The policy implemented is Linear Probing. d) ComputePosition () This function is used to store the table positions accessed to store the number entered for performing the animation operation. e) KeyPosition () This function is to determine the position in the table where the current value is inserted. f) Duplicates () This function is used to check if the number entered by the user is already present in the table. g) IsCollisionUsed () This function is used to determine if the collision resolution policy was used to insert the current value in the table. h) ResetCollisionUsed () This function is used to reset the variables so that the next number entered is not affected by the results of the previous ones. i) ReturnPArray () This function is used to return the array that stores the positions of the table that were accessed to store the current value. These positions are used to perform the animations. The control flow for this class is shown as below: Calls HashMain ModeFunction e) ReverseDigits: This class is called to compute the hash value of the number that is entered if the user has selected hash function to be implemented reverse digits. This function reverses the digits of the number entered, the number remains same in case of one digit number. A modulus operation is performed with the reverse of the number and the table size. The table positions are explored depending on the remainder, which is taken as the hash value. a) UpdateValues () This function receives the element to be inserted in the table. b) HashValue () This function returns the hash value computed for the number to be entered in the table. c) P () This function is used to implement the collision resolution policy. The policy implemented is Linear Probing. d) ComputePosition () This function is used to store the table positions accessed to store the number entered for performing the animation operation. e) KeyPosition () This function is to determine the position in the table where the current value is inserted. f) ReturnReverseNumber () This function is used to return the reverse of the number that is entered. g) Duplicates () This function is used to check if the number entered by the user is already present in the table. h) IsCollisionUsed () This function is used to determine if the collision resolution policy was used to insert the current value in the table. i) ResetCollisionUsed () This function is used to reset the variables so that the next number entered is not affected by the results of the previous ones. j) ReturnPArray () This function is used to return the array that stores the positions of the table that were accessed to store the current value. These positions are used to perform the animations. The control flow for this class is as follows: Calls HashMain ReverseDigits f) MidSquare: This class is called if the user has selected hash function to be implemented as Mid Square method. The function removes the middle 8 bits of the square of the number entered, and then takes the remainder of the value formed by the middle 8 bits with the table size. If the square of the number has less than 8 bits then it takes remainder of the square of the number and the table size. a) UpdateValues () This function receives the element to be inserted in the table. b) HashValue () This function returns the hash value computed for the number to be entered in the table. c) P () This function is used to implement the collision resolution policy. The policy implemented is Linear Probing. d) ComputePosition () This function is used to store the table positions accessed to store the number entered for performing the animation operation. e) KeyPosition () This function is to determine the position in the table where the current value is inserted. f) ReturnBinaryNumber () This function is used to return a string that represents the square of the number entered in its binary form. g) ReturnMiddleValue () This function is used to return the middle 8 bits of the square of the number entered, if the square has less than 8 bits then the square of the number is returned. h) Duplicates () This function is used to check if the number entered by the user is already present in the table. h) IsCollisionUsed () This function is used to determine if the collision resolution policy was used to insert the current value in the table. i) ResetCollisionUsed () This function is used to reset the variables so that the next number entered is not affected by the results of the previous ones. j) ReturnPArray () This function is used to return the array that stores the positions of the table that were accessed to store the current value. These positions are used to perform the animations. The control flow for this class is as follows: Calls HashMain MidSquare g) drawTable: This class implements the graphics operation for this class. The method summary for this class is as follows: a) Paint () This function implements the graphics routines for this class. b) DrawKeyMidSquare () This funtion is used to display the binary representations of the square of the number and the middle 8 bits of the square of the number in case of the Mid square method. c) DrawKey () This function is used to draw the hash key, when the ‘Hash Key’ button is pressed. d) NoKey () This function is used to turn off the display of the hash value on the display area. e) DrawAlgoSelected () This function is used to display the hash function used on the display area. f) HighLightColumn () This function is used to highlight the columns of the table to perform animation operation. g) NoColumn () This function is used to stop the column highlighting, after the element has been inserted in its place in the table. h) DrawElement () This function is used to draw the element that is entered on the display area. i) NoElement () This function is used to stop the display of the element on the display area. j) SetTableSize () This function is used to pass the table size selected. k) DrawCollision () This function is used to draw the collision value and mechanism on the display area. l) NodrawCollision () This function is used to stop displaying the collision mechanism on the display area. The control flow of the class is shown below Calls HashMain drawTable h) InstructionsHash: This class is used to display the help manual. It is called when the user clicks the help button. The constructor of the class creates the interface for the help manual and adds text to it. The method summary for the Instructions class: a) Message () This function is used to display a string in the text area. b) getrid () This function disables more than one help manual open at a time. It will destroy the old manual window before displaying the new one. The control flow of the class is as follows: Calls HashMain InstructionsHash The control flow for the entire system is shown below: HashMain setUp ModeFunction AddTheDigits drawTable MidSquare ReverseDigits InstructionsHash