m2skills

dhash py

Apr 4th, 2017
4,795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.82 KB | None | 0 0
  1. # Program to implement Double Hashing
  2.  
  3. class doubleHashTable:
  4.     # initialize hash Table
  5.     def __init__(self):
  6.         self.size = int(input("Enter the Size of the hash table : "))
  7.         self.num = 5
  8.         # initialize table with all elements 0
  9.         self.table = list(0 for i in range(self.size))
  10.         self.elementCount = 0
  11.         self.comparisons = 0
  12.    
  13.    
  14.     # method that checks if the hash table is full or not
  15.     def isFull(self):
  16.         if self.elementCount == self.size:
  17.             return True
  18.         else:
  19.             return False
  20.    
  21.    
  22.     # method that returns position for a given element
  23.     # replace with your own hash function
  24.     def h1(self, element):
  25.         return element % self.size
  26.        
  27.     # method that returns position for a given element
  28.     def h2(self, element):
  29.         return element % self.num
  30.            
  31.    
  32.     # method to resolve collision by quadratic probing method
  33.     def doubleHashing(self, element, position):
  34.         posFound = False
  35.         # limit variable is used to restrict the function from going into infinite loop
  36.         # limit is useful when the table is 80% full
  37.         limit = 50
  38.         i = 2
  39.         # start a loop to find the position
  40.         while i <= limit:
  41.             # calculate new position by quadratic probing
  42.             newPosition = (i*self.h1(element) + self.h2(element)) % self.size
  43.             # if newPosition is empty then break out of loop and return new Position
  44.             if self.table[newPosition] == 0:
  45.                 posFound = True
  46.                 break
  47.             else:
  48.                 # as the position is not empty increase i
  49.                 i += 1
  50.         return posFound, newPosition
  51.  
  52.        
  53.     # method that inserts element inside the hash table
  54.     def insert(self, element):
  55.         # checking if the table is full
  56.         if self.isFull():
  57.             print("Hash Table Full")
  58.             return False
  59.            
  60.         posFound = False
  61.        
  62.         position = self.h1(element)
  63.            
  64.         # checking if the position is empty
  65.         if self.table[position] == 0:
  66.             # empty position found , store the element and print the message
  67.             self.table[position] = element
  68.             print("Element " + str(element) + " at position " + str(position))
  69.             isStored = True
  70.             self.elementCount += 1
  71.        
  72.         # collision occured hence we do linear probing
  73.         else:
  74.             while not posFound:
  75.                 print("Collision has occured for element " + str(element) + " at position " + str(position) + " finding new Position.")
  76.                 posFound, position = self.doubleHashing(element, position)
  77.                 if posFound:
  78.                     self.table[position] = element
  79.                     self.elementCount += 1
  80.  
  81.         return posFound
  82.        
  83.  
  84.     # method that searches for an element in the table
  85.     # returns position of element if found
  86.     # else returns False
  87.     def search(self, element):
  88.         found = False
  89.         position = self.h1(element)
  90.         self.comparisons += 1
  91.        
  92.         if(self.table[position] == element):
  93.             return position
  94.        
  95.         # if element is not found at position returned hash function
  96.         # then we search element using double hashing
  97.         else:
  98.             limit = 50
  99.             i = 2
  100.             newPosition = position
  101.             # start a loop to find the position
  102.             while i <= limit:
  103.                 # calculate new position by double Hashing
  104.                 position = (i*self.h1(element) + self.h2(element)) % self.size
  105.                 self.comparisons += 1
  106.                 # if element at newPosition is equal to the required element
  107.                
  108.                
  109.                 if self.table[position] == element:
  110.                
  111.                     found = True
  112.                     break
  113.                
  114.                 elif self.table[position] == 0:
  115.                     found = False
  116.                     break
  117.                    
  118.                 else:
  119.                     # as the position is not empty increase i
  120.                     i += 1
  121.             if found:
  122.                 return position
  123.             else:
  124.                 print("Element not Found")
  125.                 return found           
  126.  
  127.     # method to remove an element from the table       
  128.     def remove(self, element):
  129.         position = self.search(element)
  130.         if position is not False:
  131.             self.table[position] = 0
  132.             print("Element " + str(element) + " is Deleted")
  133.             self.elementCount -= 1
  134.         else:
  135.             print("Element is not present in the Hash Table")
  136.         return
  137.        
  138.    
  139.     # method to display the hash table
  140.     def display(self):
  141.         print("\n")
  142.         for i in range(self.size):
  143.             print(str(i) + " = " + str(self.table[i]))
  144.         print("The number of element is the Table are : " + str(self.elementCount))
  145.        
  146.            
  147. # main function
  148. table1 = doubleHashTable()
  149.  
  150. # storing elements in table
  151. table1.insert(12)
  152. table1.insert(26)
  153. table1.insert(31)
  154. table1.insert(17)
  155. table1.insert(90)
  156. table1.insert(28)
  157. table1.insert(88)
  158. table1.insert(40)
  159. table1.insert(77)       # element that causes collision at position 0
  160.  
  161. # displaying the Table
  162. table1.display()
  163. print()
  164.  
  165. # printing position of elements
  166. print("The position of element 31 is : " + str(table1.search(31)))
  167. print("The position of element 28 is : " + str(table1.search(28)))
  168. print("The position of element 90 is : " + str(table1.search(90)))
  169. print("The position of element 77 is : " + str(table1.search(77)))
  170. print("The position of element 1 is : " + str(table1.search(1)))
  171. print("\nTotal number of comaprisons done for searching = " + str(table1.comparisons))
  172. print()
  173.  
  174. table1.remove(90)
  175. table1.remove(12)
  176.  
  177. table1.display()
Add Comment
Please, Sign In to add comment