Fall 2005 COP 3530 Exam #1 Date: 9/22/05

advertisement
Fall 2005 COP 3530 Exam #1
Date: 9/22/05
1) (8 pts) What is the running time of the following method below that calculates the n th
Fibonacci number in terms of n? Assume that the addition of two k-bit numbers takes
O(k) time and that Fk is represented using k bits.
public BigInteger Fibonacci(int n) {
BigInteger one = new BigInteger(“1”);
fib1 = one;
fib2 = one;
for (int i=2; i<=n; i++) {
fib2 = fib1.add(fib2);
fib1 = fib2.subtract(fib1);
}
return fib2;
}
2) (6 pts) What is the run-time of the following segment of code in terms of n?
int i=1;
while (i <= n) {
int j = i;
while (j > 0)
j = j/2;
i = 2*i;
}
3) (6 pts) What is the run-time of the following segment of code in terms of n?
int i=n, k=0;
while (i > 0) {
for (j=0; j<i; j++)
k++;
i = i/4;
}
4) (10 pts) Determine a closed form solution for the following recurrence relation using
the method shown in class:
T(n) = 4T(n-2), for n > 2, T(1) = 6, T(2) = 4
5) (3 pts) Determine a loop invariant for the loop below right before the first statement
inside of the loop is evaluated during each iteration. (Your invariant should be about the
value of the variable sum in terms of cnt.)
int sum = 0, i = 1, cnt = 1;
while (cnt <= n) {
/ ******* /
sum = sum + i;
i = i + 2;
cnt++;
}
6) (12 pts) Using induction, prove your loop invariant from question 7.
7) (4 pts) Create an undirected unweighted graph G out of a complete binary tree of
height 3 as follows: label the root node of the tree 1, and its children 2 and 3 respectively,
with 2 being assigned to the left child and 3 to the right child. Continue in this fashion,
labeling each left child of a node labeled k with the label 2k and each right child of a
node labeled k with the label 2k+1. G should have 15 vertices total. Draw G below.
8) (8 pts) Run a depth first search on G starting at vertex 15. When you have a choice of
which vertex to visit next, always visit the vertex with the smallest possible number. In
what order are the nodes visited in this depth first search?
9) (8 pts) Run a breadth first search on G starting at vertex 12. When you have a choice
of which vertex to visit next, always visit the vertex with the smallest possible number. In
what order are the nodes visited in this breadth first search?
10) (10 pts) Run a topological sort on the list 1, 2, 3, 4, 5, 6, 7, 8, 9 , 10 using the
following constraints: (2, 6), (5, 3), (4, 7), (5, 1), (10, 8), (9, 1), (1, 4), (4, 8), (10, 7), (2,
3), (6, 5). (Note: a constraint (a,b) means that a must come before b in the overall
ordering of the values.) First, draw the corresponding directed acyclic graph to this set of
constraints. Run the algorithm considering the nodes in numerical order. When you have
a choice of nodes to visit, always visit the one that is numerically minimal. What is the
ordering of the items in the topological sort?
11) (20 pts) Consider attempting to find the kth permutation of the list 1, 2, 3, …, n when
we list these permutations in lexicographical order. (For example, 2, 1, 3, 4 is the 7th
permutation of the list 1, 2, 3, 4.) One algorithm to solve this problem would simply be to
call the nextPermutation method shown in class k-1 times on the original permutation 1,
2, 3, …, n. However, the run time of this method would at least be O(k). Since k could be
close to n!, this method is not necessarily desirable for this specific task. Design a more
efficient algorithm (in terms of time) to solve this problem. Give an example of your
algorithm to find the 315th permutation of 1, 2, 3, 4, 5, 6.
12) (5 pts) What is the name of current president George Bush's father? ______________
Scratch Page - Please clearly mark any work on this page that you would like
graded.
Download