chap11-solutions

# chap11-solutions - Selected Solutions for Chapter 11 Hash...

This preview shows pages 1–3. Sign up to view the full content.

Selected Solutions for Chapter 11: Hash Tables Solution to Exercise 11.2-1 For each pair of keys k; l , where k ¤ l , define the indicator random variable X kl D I f h.k/ D h.l/ g . Since we assume simple uniform hashing, Pr f X kl D 1 g D Pr f h.k/ D h.l/ g D 1=m , and so E ŒX kl ± D 1=m . Now define the random variable Y to be the total number of collisions, so that Y D P k ¤ l X kl . The expected number of collisions is E ŒY ± D E ±X k ¤ l X kl ² D X k ¤ l E ŒX kl ± (linearity of expectation) D n 2 ! 1 m D n.n N 1/ 2 ± 1 m D n.n N 1/ 2m : Solution to Exercise 11.2-4 The flag in each slot will indicate whether the slot is free. ± A free slot is in the free list, a doubly linked list of all free slots in the table. The slot thus contains two pointers. ± A used slot contains an element and a pointer (possibly NIL) to the next element that hashes to this slot. (Of course, that pointer points to another slot in the table.)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
11-2 Selected Solutions for Chapter 11: Hash Tables Operations ± Insertion: ± If the element hashes to a free slot, just remove the slot from the free list and store the element there (with a NIL pointer). The free list must be doubly linked in order for this deletion to run in O.1/ time. ± If the element hashes to a used slot j , check whether the element x already there “belongs” there (its key also hashes to slot j ). ± If so, add the new element to the chain of elements in this slot. To do so, allocate a free slot (e.g., take the head of the free list) for the new element and put this new slot at the head of the list pointed to by the hashed-to slot ( j ).
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 04/20/2010 for the course IE ie200 taught by Professor . during the Spring '10 term at 카이스트, 한국과학기술원.

### Page1 / 5

chap11-solutions - Selected Solutions for Chapter 11 Hash...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online