Download - Backpack

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
Transcript
Data Structures and Algorithms (CSE-102)
Midterm Exam
Time: 2 Hrs.
Marks: 40
Please provide explanations for each of your answers. If you do not explain your answer, you will not be awarded
any marks.
1. a) Find the complexities of the following recurrence relations.
You can assume T(1) = 1
i) T(n) = 4T(n/2) + n/log(n)
ii) T(n) = T(n-1) + T(n-2)
2+2
b) Consider the following code segment
int f1 (int a, int b)
{
while (a != b)
if (a > b)
a = a - b;
else
b = b - a;
return a;
}
i) Determine and explain the functionality of f1 (hint: try out with a=24, b=16 etc).
ii) What is the worst case complexity of f1. To represent a, log(a) bits are required, thus you may assume that
the input size is log(a) and log(b), you may also use a and b to find out the complexity.
2+2
c) Consider the following code segment,
int f2 (int a[], int n, int x)
{
int i, j, k;
i = 0; j = n -1;
while (i <= j) {
k = (i + j)/2;
if (x == a[k])
return k;
if (x > a[k])
i = k + 1;
else
j = k - 1;
}
return -1;
}
Find out the worst case complexity of f2.
2
2.
a) Mergesort divides a list of numbers into two halves and calls itself recursively on both of them. Instead
can you perform quicksort on the left half and mergesort on the right half? If yes, show how it will sort the
following list of numbers by showing every step. If no, explain why you cannot.
5 12 8 4 9 1 3 11 6 7 10 2
Assume that a sort base case sorts a single value and quick sort always chooses first element as a pivot.
3
b) You are to show how BuildHeap works on the following 10 keys (for min-heap):
3 8 9 2 5 6 10 1 4 7
Show how you will delete the minimum element.
3
c) For each of the following, give an example and a brief explanation.
(i) When might insertion sort be better than quicksort?
(ii) When might quicksort be better than insertion sort?
2
d) You have an array containing n keys, which are in heap order. Suppose that the value of exactly one
key is changed (either increased or decreased). As a function of n, how long does it take to restore the heap order.
(Do NOT give an algorithm, just explain the asymptotic running time.)
2
3.
a) Show by induction that the number of leaves of a ternary tree of height h is at most 3h.
Definitions:
i. In a ternary tree a node has at most 3 children.
ii. The height of a tree is the longest number of edges on any path from the root to a leaf.
3
b) You are given an array A of n integers, some positive some negative. Design an O(n log n) algorithm
which determines whether A contains two elements A[i] and A[j] such that A[i] = −A[j] (If A contains the element
0, then the answer is always yes.) Briefly explain your algorithm and derive its running time.
3
c) Write a C code or a pseudocode that will turn the "before" picture into the "after" picture by modifying links
between the nodes shown. There may be more than one way to write the code, but you are NOT allowed to change
any existing node's data field value. You also should not create new list nodes, but you may create a single list
node variable to refer to any existing node if you like. If a variable does not appear in the "after" picture, it doesn't
matter what value it has after the changes are made. To help maximize partial credit in case you make mistakes, we
suggest that you include optional comments with your code that describe the links you are trying to change.
4
4. a) We have an undirected simple graph G (V,E) with |V|=15 and |E|=8.
Answer the following:
i) Is G a connected graph?
ii) What is the maximum possible degree of a node in this graph?
iii) If there is a cycle of length 5 in G, what can be the minimum number of single node subgraphs of G?
iv) If there is a tree of 5 nodes, what can be the maximum number of single node subgraphs of G?
1+1+1.5+1.5
b) For a weighted undirected graph G(V,E) (each edge has a positive weight)
i) Write an algorithm to find out the shortest cost path between two nodes start and end. You need to present
the algorithm as a pseudocode function (or in other words write the pseudocode for your algorithm).
ii) Find the worst-case time complexity of algorithm in terms of |V| and |E|.
3+2
Bonus Question
5. You are given an array of n keys, each with one of the values red, white, and blue. Give an O(n) algorithm for
rearranging the keys so that all the reds come before all the whites, and all the whites come before all the blues.
The only operations permitted are examination of a key to find out what color it is, and swap of two keys
(specified by their indices). You cannot use any extra arrays. Explain your algorithm and derive its running time.
4