Professional Documents
Culture Documents
INSTRUCTIONS
Instructions
Please enter your answers on the bubble sheet with your name.
Use pencil only for bubble sheets..
YOU CAN GUESS. MARKS WILL NOT BE DEDUCTED FOR FALSE ANSWERS.
Questions
2. (3 marks) Which is the least desirable complexity for an algorithm (i.e. which of these
complexities is guaranteed to be the slowest)?
A. O(n2)
B. θ(n2)
C. Ω(n)
D. o(n)
3. (2 marks) A problem with n elements is solved with a divide and conquer algorithm in
which the problem is divided in 3 equal parts which are solved and recombined. A
recurrence relation for the cost of this solution is C(n) =6C(n/3) + n2. What is the cost of
this algorithm?
A. θ(n log n)
B. θ(n2)
C. θ(n2 log n)
D. θ(n3)
4. (2 marks) What is a tight lower bound for the element uniqueness problem in an array of n
elements?
A. Ω(log n)
B. Ω(n)
C. Ω(nlog n)
D. Ω(n2)
5. (3 marks) If you know that the cost C(A) of an algorithm A is such that C(A) ∈ O(n logn),
which of the following statements must be false
A. A ∈ P
B. A ∈ NP
C. C(A) ∈ θ(n2)
D. C(A) ∈ o(n)
E. C(A) ∈ Ω(n)
b c
A. a b g e f c d h
a d B. e f g c h a d b
C. h f g c a e b d
e f
D.None of the above
g h
7. (2 marks) Which of these sets of vertices is not a minimum vertex cover of the graph on the
left?
b c A. { a, d, f }
B. { b, c, g }
a d
C. { b, f, g }
e f D. All of the above: none are
minimum vertex covers
g h
8. (2 marks) Which one of these sequences of vertices is a topological sort of the graph on the
left?
b c
A. dabcfhge
B. abcdefgh
a e f d
C. abecfhgd
D. None of the above
g h
9. (2 marks) Which of these graphs is the transitive closure of the graph in the previous
question?
b c b c b c
a f a f a f
g h g h g h
d d d
1 2 2 3 3 4
3 5 1 6 3 7 1
8 6 9 10 6 11
10. (5 marks) Build a minimum spanning tree of G using Prim’s algorithm starting at vertex 1.
Show each step of the algorithm by labelling each edge of the spanning tree that you are
adding in the order in which it is being added. In other words, label the first edge you are
adding as “1”, the second as “2” and so on. You do not need to show the weights of the
edges in your MST because they can be inferred from the graph of G above.
1 2 3 4
5 6 7
8 9 10 11
11. (5 marks) Build a minimum spanning tree of G using Kruskal’s algorithm. Just like in the
previous question, show each step of the algorithm by labelling each edge of the spanning
tree that you are adding in the order in which it is being added.
1 2 3 4
5 6 7
8 9 10 11
Here is a NTM with 3 tapes called T1,T2,T3. T2 and T3 are initially blank.
The symbol is the blank symbol.
The actions for each tape head have the format: <new symbol> <move>
where the possible moves are: R=move right, L=move left, S=stay
The wildcard symbol * is used as input to match any symbol and as output to specify that the
tape symbol should not be overwritten.
12. (1 mark) Circle all the non-deterministic states in the first column of the table (i.e. where
non-deterministic transitions originate)
13. (5 marks) Explain in English in the last column of the table what the states q0 to q4 do. Do
not simply translate the actions in English but instead explain what the whole state does.
15. (2 marks) If this NTM accepts an input string on T1 which has altogether n +s and -s, how
many times will it have read a + or - before it accepted the string? The answer should be a
function of n. In this question "reading a + or -" means that one of the tape heads was over a
+ or - and moved left or right away from it.
16. (3 marks) Is the problem that this NTM solves in NP class? Explain your answer.
17. (2 marks) Is the problem that this NTM solves in P class? Explain your answer.
Main()
// Description of values is stored in an array of n elements indexed from 1 to n.
// The values are all non-negative integers
Integer price[1..n] // price [i] = price of a piece of cable of length i
Print(“Best price for “,n, “is”, BP(n))
// This function returns the best possible selling price for a cable of length n
Integer BP (Integer n)
result = price[n]
// Try cutting off pieces of increasing length
// and selling them and the remainder at the best price
length=1
while length < n
try = price[length] + BP(n-length)
if try>result result = try
length++
return result
For the analysis that follows on the next pages, you will be assuming that the basic operation
is retrieving a price from the price array. You are asked to count the number of times an
expression of the form price[i] (where i can be anything) is executed.
BP(5)
p
21. (5 marks) Modify the function BP so that it solves exactly the same problem by recursing
exactly the same way, but using a dynamic programming approach, i.e: all the solutions to
subproblems are stored in a global array called best the first time they are calculated and
they are never calculated again. This array has already been declared in your code below,
but has not been initialized.
Main()
// Description of values is stored in an array of n elements indexed from 1 to n.
// The values are all non-negative integers
Integer price[1..n] // price [i] = price of a piece of cable of length i
Integer best[1..n] // best[i] = best price that can be fetched
// for a cable of length i
// This function returns the best possible selling price for a cable of length n
Integer BP (Integer n)
result = price[n]
length++
return result
23. (3 marks) Use your new execution tree and the new BP algorithm to give a recursive
definition of the cost Bn of your dynamic programming version of BP(n). You need to
provide the initial condition and recurrence relation.
24. (3 marks) What is the big-O complexity of the dynamic programming version of the BP
algorithm as a function of n? Explain your answer.