Hashing-Datastructues and Algorithms-Solutions, Exercises of Data Structures and Algorithms

This is solution manual provided by Muhammad Uzair at University of Engineering and Technology Lahore. It related to Data Structures and Algorithms course. Its main points are: Hashing, Collisions, Separate, Chaining, Table, Inserted, Alternative, Locations, Rehashing, Occupied

Typology: Exercises

2011/2012

Uploaded on 07/16/2012

sangodkar
sangodkar 🇵🇰

5

(2)

12 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures & Algorithm Analysis in C
(second edition)
MarkAllenWeiss
FloridaInternationalUniversity
SolutionsManual
By:
Muhammad Uzair
05-E-147
U.E.T. Lahore
Pakistan
pf3
pf4
pf5

Partial preview of the text

Download Hashing-Datastructues and Algorithms-Solutions and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Data Structures & Algorithm Analysis in C

(second edition)

MarkAllenWeiss

FloridaInternationalUniversity

SolutionsManual

By:

Muhammad Uzair

05-E-

U.E.T. Lahore

Pakistan

[email protected]

Preface

Included in this manual are answers to most of the exercises in the textbook Data Structures and Algorithm Analysis in C, second edition, published by Addison-Wesley. These answers reflect the state of the book in the first printing.

Specifically omitted are likely programming assignments and any question whose solu- tion is pointed to by a reference at the end of the chapter. Solutions vary in degree of complete- ness; generally, minor details are left to the reader. For clarity, programs are meant to be pseudo-C rather than completely perfect code.

Errors can be reported to [email protected]. Thanks to Grigori Schwarz and Brian Harvey for pointing out errors in previous incarnations of this manual.

Chapter 5: Hashing

5.1 (a) On the assumption that we add collisions to the end of the list (which is the easier way if a hash table is being built by hand), the separate chaining hash table that results is shown here.

(b)

(c)

(d) 1989 cannot be inserted into the table because hash O 2 (1989) = 6, and the alternative locations 5, 1, 7, and 3 are already taken. The table at this point is as follows:

5.2 When rehashing, we choose a table size that is roughly twice as large and prime. In our case, the appropriate new table size is 19, with hash function h O( x O) = x O( mod O 19). (a) Scanning down the separate chaining hash table, the new locations are 4371 in list 1, 1323 in list 12, 6173 in list 17, 4344 in list 12, 4199 in list 0, 9679 in list 8, and 1989 in list

(b) The new locations are 9679 in bucket 8, 4371 in bucket 1, 1989 in bucket 13, 1323 in bucket 12, 6173 in bucket 17, 4344 in bucket 14 because both 12 and 13 are already occu- pied, and 4199 in bucket 0.

O O( M O + N O) space. Another method of implementing these operations is to use a search tree instead of a hash table; a balanced tree is required because elements are inserted in the tree with too much order. A splay tree might be particularly well suited for this type of a problem because it does well with sequential accesses. Comparing the different ways of solving the problem is a good programming assignment.

5.8 The table size would be roughly 60,000 entries. Each entry holds 8 bytes, for a total of 480,000 bytes.

5.9 (a) This statement is true.

(b) If a word hashes to a location with value 1, there is no guarantee that the word is in the dictionary. It is possible that it just hashes to the same value as some other word in the dic- tionary. In our case, the table is approximately 10% full (30,000 words in a table of 300,007), so there is a 10% chance that a word that is not in the dictionary happens to hash out to a location with value 1. (c) 300,007 bits is 37,501 bytes on most machines. (d) As discussed in part (b), the algorithm will fail to detect one in ten misspellings on aver- age. (e) A 20-page document would have about 60 misspellings. This algorithm would be expected to detect 54. A table three times as large would still fit in about 100K bytes and reduce the expected number of errors to two. This is good enough for many applications, especially since spelling detection is a very inexact science. Many misspelled words (espe- cially short ones) are still words. For instance, typing them O instead of then O is a misspelling that won’t be detected by any algorithm.

5.10 To each hash table slot, we can add an extra field that we’ll call WhereOnStack, O and we can keep an extra stack. When an insertion is first performed into a slot, we push the address (or number) of the slot onto the stack and set the WhereOnStack O field to point to the top of the stack. When we access a hash table slot, we check that WhereOnStack O points to a valid part of the stack and that the entry in the (middle of the) stack that is pointed to by the WhereOn- Stack O field has that hash table slot as an address.

(2) 00000010 00001011 00101011

(2) 01010001 01100001 01101111 01111111

(3) 10010110 10011011 10011110

(3) 10111101 10111110

(2) 11001111 11011011 11110000

000 001 010 011 100 101 110 111