Data Structures and Algorithms

advertisement
MAKERERE UNIVERSITY
COLLEGE OF COMPUTING AND INFORMATION SCIENCE
DEPARTMENT OF COMPUTER SCIENCE AND NETWORKS
DATA STRUCTURE AND ALGORITHMS (CSC 2100)
SECTION A
Question One
1. Which of the following is not true concerning hash tables?
(A) An advantage of using hash tables is constant search time.
(B) A disadvantage of hash tables implemented as arrays is the need to know the amount
of data in advance.
(C) An advantage of hash tables is that data can be accessed in order.
(D) The load factor of a hash table should never be more than 1/2 to 2/3 when open
addressing is used.
2. Which of the following statements concerning binary search trees is always true?
(A) The search time is O(log2 n).
(B) They are always balanced.
(C) The delete time is O(log2 n).
(D) The insert time depends on how well balanced they are.
3. What is the complexity of the following code expressed in O( ) notation? If more than
one answer is correct, choose the smallest one.
for (int j = n; j > 0; j--) {
for (int k = 1; k < j; k = k+k) {
cout << j+k << “ ”;
}
cout << endl;
}
(A) O(log n)
(B) O(n)
(C) CO(n log n)
(D) O(n2)
(E) O(2n)
4. A binary search tree is a binary tree in which the data in the left subtree of a node is
greater than the data in the node and the data in the right subtree is greater than the data
in the node.
(A) True
(B) False
5. A technique for direct search is
(A) Binary Search
(C) Tree Search
(B) Linear Search
(D) Hashing
6. In worst case Quick Sort has order
(A) O (n log n)
(C) O (log n)
(B) O (n2/2)
(D) O (n2/4)
7. A binary tree if all levels except possibly the last is completely filled and all nodes are
as far left as possible, is known as
(A) strict binary tree.
(C) perfect binary tree.
(B) AVL tree.
(D) complete binary tree.
8. A sort which relatively passes through a list to exchange the first element with any
element less than it and then repeats with a new first element is called
(A) insertion sort.
(C) heap sort.
(B) selection sort.
(D) quick sort.
9. Which of the following sorting algorithms does not have a worst case running time of
O(n2) ?
(A) Insertion sort
(C) Quick sort
(B) Merge sort
(D) Bubble sort
10. What is the expected time required to search for a value in a binary search tree
containing n nodes? (You should make reasonable assumptions about the structure of
the tree.)
(A) O(1)
(B) O(log n)
(C) O(n)
(D) O(n log n)
(E) O(n2)
11. An undirected graph G with n vertices and e edges is represented by adjacency list.
What is the time required to generate all the connected components?
(A) O (n)
(C) O (e+n)
(B) O (e)
(D) O (e2)
12. Consider a linked list of n elements. What is the time taken to insert an element after
an element pointed by some pointer?
(A) O (1)
(C) O (n)
(B) O (log2 n)
(D) O (n log2 n)
13. In a circular linked list
(A) components are all linked together in some sequential manner.
(B) there is no beginning and no end.
(C) components are arranged hierarchically.
(D) forward and backward traversal within the list is permitted.
14. The data structure required for Breadth First Traversal on a graph is
(A) queue
(C) array
(B) stack
(D) tree
15. One can convert a binary tree into its mirror image by traversing it in
(A) inorder
(C) postorder
(B) preorder
(D) any order
16. The data structure required to evaluate a postfix expression is
(A) queue
(C) array
(B) stack
(D) linked-list
17. What data structure would you mostly likely see in a nonrecursive implementation of a
recursive algorithm?
(A) Stack
(C) Queue
(B) Linked list
(D) Trees
18. The postfix form of A*B+C/D is
(A) *AB/CD+
(C) A*BC+/D
(B) AB*CD/+
(D) ABCD+/*
19. Representation of data structure in memory is known as:
(A) recursive
(C) storage structure
(B) abstract data type
(D) file structure
20. Which of the following operations is performed more efficiently by doubly linked list
than by singly linked list?
(A) Deleting a node whose location in given
(B) Searching of an unsorted list for a given item
(C) Inverting a node after the node with given location
(D) Traversing a list to process each node
Question Two
(A) Arrays and linked lists are used in the implementation of several data structures. State
two the difference between arrays and linked lists.
(4 Marks)
(B) Draw a binary expression tree that stores the expression: (((a - b) / c) + (d * e))
(2 Marks)
(C) The BFS and DFS are the traversal techniques used in traversing a graph. State two
differences between BFS and DFS.
(4 Marks)
(D) Explain why data structure is important in computing?
(2 Marks)
(E) An algorithm with processing time T(n) = cn2 spends T(N) seconds for processing N
data items. How much time will be spent for processing n = 10,000 data items, assuming
that N = 1000 and T(N) = 10ms?
(2 Marks)
(F) What are binary search trees and what is it mainly used for?
(2 Marks)
(G) Write the two advantage and two disadvantages of separate chaining.
(4 Marks)
SECTION B
Question Three
The government of uganda has embarked on a strategy to run internet cables (an integer number
designates the length of the cable to be installed) to the following districts;
(Pallisa,Budaka)=2,(Pallisa,Kibuku)=4,(Pallisa,Kaliro)=9,(Budaka,Kibuku)=1,(Kibuku,Nam
utumba)=7,(Kibuku,Bugiri)=7,(Kaliro,Iganga)=2,(Namutumba,Iganga)=5,(Namutumba,Bugi
ri)=3,(Namutumba,Kaliro)=1,(Budaka,Mbale)=2,(Bugiri,Iganga)=3,(Bugiri,Busia)=1,(Busia,
Tororo)=2,(Mbale,Tororo)=10, (Iganga, Jinja)=2, (Jinja, Tororo)=4 where the integer number
designates the length of the cable to be connected between districts.
The government with much emphasis tries as much as possible in finding a way to minimize
on the cost of installing the cables in these districts.
(a) Explain the data structure suitable for this kind of problem.
(2 Marks)
(b) Explain with an illustration one of the ways how such a data structure can be
implemented.
(5 Marks)
(c) Write an algorithms that determines the shortest path from Pallisa district to Tororo
District.
(5 Marks)
(d) If the government is to minimize the cost of cables,
(i) State two mechanisms that can be used and in each case write an algorithm to
implement these mechanisms.
(4 Marks)
(ii) Using the algorithms above, implement both mechanisms and show results in
each case.
(4 Marks)
Question Four
Sorting is a process that organizes a collection of data into either ascending or descending
order. This is done using a number of techniques.
(a) Explain briefly how any 3 of the algorithms used in sorting data works, stating the time
complexity in all cases (i.e. best, average or worst case) and also the stability of the
chosen algorithm.
(9 Marks)
(b) Given the following sequence 3 9 1 14 24 22 20 17.
(i) Write a partition algorithm that will partition the sequence.
(4 Marks)
(ii) Sort the sequence using a quicksort algorithm.
(3 Marks)
(c) Quicksort is claimed to have an expected running time of O(n log n), but it could be as
slow as O(n2).
(i) Briefly explain why Quicksort could use O(n2) time instead of always running in
time O(n log n).
(2 Marks)
(ii) How can you fix Quicksort so the expected time is O(n log n), if it can be done?
You should give a specific suggestion (don’t just say something like “be clever and
careful”). Explain why your solution will change the expected time to O(n log n).
(2 Marks)
Question Five
Algorithmic is a branch of computer science that consists of designing and analyzing
computer algorithms. The design pertains to the description of algorithm at an abstract level
by means of a pseudo language, and proof of correctness that, the algorithm solves the
given problem in all cases. The analysis deals with performance evaluation (complexity
analysis).
(a) State and explain five characteristics of a good algorithm.
(5 Marks)
(b) Explain the two approaches/method used in the analysis of the algorithm.
(3 Marks)
(c) Explain three challenges of experimental method of algorithm analysis.
(3 Marks)
(d) Arrange the following functions in an increasing order of growth rate,
showing your working.
(5 Marks)
3 𝑛
(2) , 264 - 1, nlog n, n 2n, n3 log n, 4n, log n3, nn
(e) A binary search algorithm with a time complexity of O(n log n) spends exactly 5
millisecond to search 10,000 data items. Assuming that time T(n) of searching n
items is directly proportional to n log n, that is, T(n) = cn log n, derive a formula
for T(n), given the time T(N) for searching N items, and estimate how long this
method will search 1,000,000 items.
(4 Marks)
Question Six
(a) Give three differences between a tree and a binary tree.
(3 Marks)
(b) You are given a binary tree below.
(i) Write an algorithm for preorder and postorder traversal
(3 Marks)
(ii) Determine the preorder, inorder and postorder tree traversal from the binary tree
above.
(3 Marks)
(iii)Implement the above binary tree using an array
(2 Marks)
(iv) Illustrate how someone can prove that the left and right subtree are children of the
parent node in the array in (iii) above.
(3 Marks)
(c) Make
a
BST
for
the
following
sequence
of
numbers.
45,32,90,34,68,72,15,24,30,66,11,50,10.
(2 Marks)
(d) Write an algorithm to insert a node on a binary tree.
(3 Marks)
Question Seven
(a) The Tower of Hanoi is a mathematical game. The game consists of 3 rods, and n disks
of different sizes which can slide onto any rod. The puzzle starts with the disks stack in
ascending order of size on rod number 1 (the smallest disk is on the top). The objective
of the game is to move the entire stack to rod number 3, obeying the following rules:
(i) State the rules of the tower of Hanoi algorithm
(3 Marks)
(ii) In this problem, please write an algorithm using the recursive technique to
output a series of moves which solve the Tower of Hanoi problem.
(5 Marks)
(b) Write a merge sort recursive algorithm for sorting a given array.
(6 Marks)
(c) Write a recursive algorithm that implements a factorial of a number n
(6 Marks)
Question Eight
(a) Explain the concept of hashing illustrating how it can be used to form a hash table.
(4 Marks)
(b) Describe with examples any four hashing techniques used in hash tables.
(12 Marks)
(c) Collision is one of the problems that any hashing technique cannot avoid.
(i) Under what two circumstances may collision occur in a hash table?
(2 Marks)
(ii) Describe two ways in which collision may be handled.
WISH YOU MERRY CHRISMAS AND SUCCESS
( 4 Marks)
Download