You are on page 1of 5

Final Exam Paper Part 1

1. Consider the graph with V = {a,b,c,d,e,f} and E = {(a,b), (a,d), (b,c), (20points) (b,e), (c,e), (c,f), (d,e), (e,f)} A. B. C. D. Draw the Graph Find all paths from a to f What is the length of the shortest path from a to f Draw the Matrix of this graph

ANS: V = {a, b, c, d, e, f} and E = {(a, b), (a, d), (b, c),(b, e), (c, e), (c, f), (d, e), (e, f)} Graph:

Paths from a - f: 1. 2. 3. 4. Abcf Abef Abcef Adef

Shortest Path from a - f: A b c f or a b e f or a d e f Length is 4 Matrix:

2. Using figure 1: shown below as a model, illustrate the operation of Merge sort on the array A= {15, 41, 4, 26, 7, 57, 9, 60}. (10 Points)

Figure 1

ANS: Illustrate the operation of Merge sort on the array A= {15, 41, 4, 26, 7, 57, 9, 60} Given A = (15,41,4,26,7,57,9,60)

15,41,4,26

7,57,9,60

(15,41) (4,26 )

( 7,57) ( 9,60)

4,15,26,41

7,9,57,60

Finally A = 4,7,9,15,26,41,57,60

The Merge sort algorithm can be used to sort a collection of objects. Merge sort is so called divide and conquer algorithm. Divide and conquer algorithms divide the original data into smaller sets of data to solve the problem. During the Merge sort process the object in the collection are divided into two collections. To split a collection, Merge sort will take the middle of the collection and split the collection into its left and its right part. The resulting collections are again recursively sorted via the Merge sort algorithm. Once the sorting process of the two collections is finished, the result of the two collections is combined. To combine both collections Merge sort start at each collection at the beginning. It pick the object which is smaller and inserts this object into the new collection. For this collection it now selects the next elements and selects the smaller element from both collections. Once all elements from both collections have been inserted in the new collection, Merge sort has successfully sorted the collection. To avoid the creation of too many collections, typically one new collection is created and the left and right side are treated as different collections.

3. Write at least a page but not more than 2 pages on Hash table as relates to data structures. (10 Points) ANS: Hash Table A hash table, or a hash map, is a data structure that associates keys with values. The primary operation it supports efficiently is a lookup: given a key (e.g. a person's name), find the corresponding value (e.g. that person's telephone number). It works by transforming the key using a hash function into a hash, a number that the hash table uses to locate the desired value. Hash tables are often used to implement associative arrays, sets and caches. Like arrays, hash tables provide constant-time O(1) lookup on average, regardless of the number of items in the table. However, the rare worst-case lookup time can be as bad as O(n). Compared to other associative array data structures, hash tables are most useful when large numbers of records of data are to be stored. A good hash function should be quick to compute, produce all possible indices from 0 to prime - 1, and should minimize collisions. For integer keys, hash(key) = key % prime is typically used. For string keys, some method of converting the string to an integer is usually used, followed by taking result % prime to cut the integer value down to size. One also needs to worry about integer overflow. The hash value should ideally depend on every bit of the key. It is not good to ignore part of the key as that may lead to several keys that hash to the same location. One method for strings: - Let's suppose that the strings only contain the letters of the alphabet, in upper case. In other words, we have the letters A through Z. We can translate the individual letters to the integers 0 through 25. Then we treat the string as a base 26 number. For example, CPU would be the base 26 number with digits 2, 15, 20. The decimal value of this number is 2(26^2) + 15(26) + 20. All computations are done % prime, both to avoid overflow, and because the final hash value must fit the range 0 to prime - 1. Hash tables may be used as in-memory data structures. Hash tables may also be adopted for use with persistent data structures; database indexes commonly use disk-based data structures based on hash tables. Hash tables are used to speed-up string searching in many implementations of data compression. In computer Chess, a hash table is generally used to implement the transposition table.

Hash table is an implementation of a key-value pair data structure in java. You can store and retrieve a value using a key and it is an identifier of the value stored. It is obvious that the key should be unique.

You might also like