Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees.

Similar presentations


Presentation on theme: "Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees."— Presentation transcript:

1 Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees

2 What is Collision? Collisions occur when different elements are mapped to the same cell

3 Linear Probing (§2.5.5) Open addressing: the colliding item is placed in a different cell of the table Linear probing handles collisions by placing the colliding item in the next (circularly) available table cell Each table cell inspected is referred to as a “probe” Colliding items lump together, causing future collisions to cause a longer sequence of probes Example: h(x)  x mod 13 Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order 0123456789101112 41 18445932223173 0123456789101112

4 Search with Linear Probing Consider a hash table A that uses linear probing findElement (k) We start at cell h(k) We probe consecutive locations until one of the following occurs An item with key k is found, or An empty cell is found, or N cells have been unsuccessfully probed Algorithm findElement(k) i  h(k) p  0 repeat c  A[i] if c   return NO_SUCH_KEY else if c.key ()  k return c.element() else i  (i  1) mod N p  p  1 until p  N return NO_SUCH_KEY

5 Updates with Linear Probing To handle insertions and deletions, we introduce a special object, called AVAILABLE, which replaces deleted elements removeElement (k) We search for an item with key k If such an item (k, o) is found, we replace it with the special item AVAILABLE and we return element o Else, we return NO_SUCH_KEY insert Item (k, o) We throw an exception if the table is full We start at cell h(k) We probe consecutive cells until one of the following occurs A cell i is found that is either empty or stores AVAILABLE, or N cells have been unsuccessfully probed We store item (k, o) in cell i

6 Double Hashing Double hashing uses a secondary hash function d(k) and handles collisions by placing an item in the first available cell of the series (i  jd(k)) mod N for j  0, 1, …, N  1 The secondary hash function d ( k ) cannot have zero values The table size N must be a prime to allow probing of all the cells Common choice of compression map for the secondary hash function: d 2 ( k )  q  k mod q where q  N q is a prime The possible values for d 2 ( k ) are 1, 2, …, q

7 Consider a hash table storing integer keys that handles collision with double hashing N  13 h(k)  k mod 13 d(k)  7  k mod 7 Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order Example of Double Hashing 0123456789101112 31 41 183259732244 0123456789101112

8 What else can we do for Collision? Chaining: let each cell in the table point to a linked list of elements that map there Chaining is simple, but requires additional memory outside the table    0 1 2 3 4 451-229-0004981-101-0004 025-612-0001

9 Performance of Hashing In the worst case, searches, insertions and removals on a hash table take O(n) time The worst case occurs when all the keys inserted into the dictionary collide The load factor   n  N affects the performance of a hash table The expected running time of all the dictionary ADT operations in a hash table is O(1) In practice, hashing is very fast provided the load factor is not close to 100% Applications of hash tables: small databases compilers browser caches

10 Binary Search Maintain two variables low and high Initialize low=0 and high=n-1 Compute mid= [(low+high/2) 1.If key (i)==key(mid), YOU FOUND IT! 2.If key (i)< key(mid), high=mid-1 3.If key (i)> key(mid), low=mid+1 4.Compute mid again and repeat from Step 1. 13457 8 91114161819 1 3 457891114161819 134 5 7891114161819 1345 7 891114161819 0 0 0 0 m l h m l h m l h l  m  h

11 Binary Search This operation is called findElement(k) on a dictionary implemented by means of an array-based sequence, sorted by key similar to the high-low game at each step, the number of candidate items is halved terminates after a logarithmic number of steps Example: findElement(7) 13457 8 91114161819 1 3 457891114161819 134 5 7891114161819 1345 7 891114161819 0 0 0 0 m l h m l h m l h l  m  h

12 Binary Search Tree A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying the following property: Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u)  key(v)  key(w) External nodes do not store items, only internal nodes store items An inorder traversal of a binary search trees visits the keys in increasing order 6 92 418

13 How to search a Binary Tree To search for a key k, we trace a downward path starting at the root The next node visited depends on the outcome of the comparison of k with the key of the current node If we reach a leaf, the key is not found and we return NO_SUCH_KEY Example: findElement(4) Algorithm findElement(k, v) if T.isExternal (v) return NO_SUCH_KEY if k  key(v) #SEARCH THE FIRST HALF!! return findElement(k, T.leftChild(v)) else if k  key(v) #FOUND THE ITEM! return element(v) else { k  key(v) } #SEARCH THE SECOND HALF!! return findElement(k, T.rightChild(v)) 6 9 2 4 1 8   

14 Performance of a Binary Tree Search Consider a dictionary with n items implemented by means of a binary search tree of height h the space used is O(n) methods findElement, insertItem and removeElement take O(h) time The height h is O(n) in the worst case and O(log n) in the best case

15 Insertion to a Binary Search Tree To perform operation insertItem(k, o), we search for key k until leaf Assume k is not already in the tree, and let let w be the leaf reached by the search We insert k at node w and expand w into an internal node Example: insert 5 6 92 418 6 9 2 4 18 5    w w

16 Removal in a Binary Search Tree –Simple case ; it has an External node!! To perform operation removeElement( k ), first we search for key k Assume key k is in the tree, and let v be the node storing k If node v has a leaf child w, we remove v and w from the tree with operation removeAboveExternal( w ) Example: remove 4 6 9 2 4 18 5 v w 6 9 2 5 18  

17 Not simple, if no external child!

18 Remember the 3 types of tree traversals Pre-order 1.Visit the root. 2.Traverse the left subtree. 3.Traverse the right subtree. In-order (symmetric) 1.Traverse the left subtree. 2.Visit the root. 3.Traverse the right subtree. Post-order 1.Traverse the left subtree. 2.Traverse the right subtree. 3.Visit the root.

19 Removal from a Binary Search Tree –no external leaf child We consider the case where the key k to be removed is stored at a node v whose children are both internal we find the internal node w that follows v in an inorder traversal we copy key(w) into node v we remove node w and its left child z (which must be a leaf) by means of operation removeAboveExternal( z ) Example: remove 3 3 1 8 6 9 5 v w z 2 5 1 8 6 9 v 2

20 Full vs Complete Binary Tree

21

22 Ordered Dictionaries (or Lookup tables) Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k) closestKeyAfter(k) closestElemAfter(k)

23 Lookup Table A lookup table is a dictionary implemented by means of a sorted sequence We store the items of the dictionary in an array-based sequence, sorted by key We use an external comparator for the keys Performance: findElement takes O(log n) time, using binary search insertItem takes O(n) time since in the worst case we have to shift n/2 items to make room for the new item –Why? removeElement take O(n) time since in the worst case we have to shift n/2 items to compact the items after the removal –Why ? The lookup table is effective only for dictionaries of small size or for dictionaries on which searches are the most common operations, while insertions and removals are rarely performed (e.g., credit card authorizations)

24 Comparison of Performance of Log File and Lookup table


Download ppt "Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees."

Similar presentations


Ads by Google