12
RYERSON UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE CPS 616 FINAL EXAM WINTER 2018 INSTRUCTIONS This exam is 3 hours long. This exam is out of 70 and is worth 55% of the course mark. This is a closed book exam. However, two double-sided letter-sized crib sheets are allowed. This exam is double-sided and has 12 pages including this front page. The last three pages are blank. Therefore there are 8 pages of questions: pages 2 to 9 inclusive The first part of this exam is multiple choice. Please enter your answers to this part on the bubble sheet provided. The second part of this exam is short answer. Please answer all questions of this part directly on this exam. If you need extra space to finish answering questions, please do so on pages 10 to 12 and indicate very clearly on the original page of each question on which page the rest of your answer can be found

RYERSON UNIVERSITY DEPARTMENT OF …cps616.scs.ryerson.ca/OldExams/W18Final.pdfRYERSON UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE CPS 616 FINAL EXAM WINTER 2018 INSTRUCTIONS This exam

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

RYERSON UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE

CPS 616 FINAL EXAM

WINTER 2018

INSTRUCTIONS

This exam is 3 hours long.

This exam is out of 70 and is worth 55% of the course mark.

This is a closed book exam. However, two double-sided letter-sized crib sheets are

allowed.

This exam is double-sided and has 12 pages including this front page. The last three

pages are blank. Therefore there are 8 pages of questions: pages 2 to 9 inclusive

The first part of this exam is multiple choice. Please enter your answers to this part on

the bubble sheet provided.

The second part of this exam is short answer. Please answer all questions of this part

directly on this exam. If you need extra space to finish answering questions, please do so

on pages 10 to 12 and indicate very clearly on the original page of each question on

which page the rest of your answer can be found

CPS 616 W2018 FINAL EXAM 2

PART 1 – 20 MARKS - MULTIPLE CHOICE

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

1. (3 marks) Which of these sequences is not a Gray code:

A. 000,100, 110, 010, 011, 111, 101, 001

B. abc, bac, bca, cba, cab, acb

C. 11,10,12,02,22,20,21,01,00

D. None of the above: they are all Gray codes

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)

CPS 616 W2018 FINAL EXAM 3

6. (2 marks) Which one of these sequences of vertices was generated from a BFS (Breadth

First Search) traversal of the graph on the left? Assume ties are broken by the alphabetical

order of the vertices.

a

b c

d

e

g h

f

A. a b g e f c d h

B. e f g c h a d b

C. h f g c a e b d

D.None of the above

7. (2 marks) Which of these sets of vertices is not a minimum vertex cover of the graph on the

left?

a

b c

d

e

g h

f

A. a, d, f

B. b, c, g

C. b, f, g

D. All of the above: none are

minimum vertex covers

8. (2 marks) Which one of these sequences of vertices is a topological sort of the graph on the

left?

a

b c

de

g h

f

A. d a b c f h g e

B. a b c d e f g h

C. a b e c f h g d

D. None of the above

9. (2 marks) Which of these graphs is the transitive closure of the graph in the previous

question?

A. Graph A: B. Graph B: C. Graph C:

a

b c

d

e

g h

f

a

b c

d

e

g h

f

a

b c

d

e

g h

f

CPS 616 W2018 FINAL EXAM 4

PART 2 – SHORT ANSWERS – 50 MARKS - PLEASE WRITE YOUR ANSWERS DIRECTLY IN THIS EXAM

Greedy MST algorithms (10 marks)

Given the following weighted graph G:

2

3

3

1 3

6 6

165 7

8 9 10 11

2 3 41

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.

65

1 2 43

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.

65

1 2 43

7

8 9 10 11

CPS 616 W2018 FINAL EXAM 5

Non-Deterministic Turing Machine (NTM) (15 marks)

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.

Current Symbol Action New State Explanation of state

State T1 T2 T3 T1 T2 T3

q0 + R * S * S q1

+ R * S * S q2

- R * S * S q1

- R * S * S q3

* S * L * L q4

q1 0 R * S * S q1

* * * * S * S * S q0

q2 0 R 0 R * S q2

* * * * S * S * S q0

q3 0 R * S 0 R q3

* * * * S * S * S q0

q4 * 0 0 * S * L * L q4

* * S * S * S Accept

* * * * S * S * S Reject

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.

14. (3 marks) What problem does this NTM solve?

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.

CPS 616 W2018 FINAL EXAM 6

Dynamic Programming (25 marks)

In this exercise you will be modifying pseudocode to implement a dynamic programming

approach to solving the cable cutting problem which is described as follows:

Find the maximum total sale price that can be obtained by cutting an n meter long cable into full

meter pieces if the sale price of a piece of length i meters is pi for i=1, 2, …, n.

For example, if the prices of cables of length 1m to 5m are:

p1=$3, p2=$5, p3=$10, p4=$10, p5=$10,

The best price that can be fetched for a 5m cable is $16 by cutting the cable into three pieces of

length 3m, 1m, and 1m.

There are some good efficient algorithms to solve this problem when the prices are well

structured, for example in increasing order as is the case in the example above. However, the

problem is harder when the price structure is not predictable.

Here is pseudocode for a recursive program to deal with that situation. It solves the general

version of this problem without making any assumptions about the price structure of the cables.

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.

CPS 616 W2018 FINAL EXAM 7

18. (5 marks) Finish drawing the execution tree below of the function call BP(5). This tree is

defined as follows:

The tree nodes are the function calls to BP, with the root being BP(5).

For two numbers i and j, BP(i) is a parent of node BP(j) iff BP(i) calls BP(j) directly.

For two siblings B(i) and B(j), B(i) is to the left of B(j) iff B(i) was called before B(j).

The tree should be annotated with the letter p for each reference to the price array, using the

following convention:

If price was referenced during a function call to BP(i) the letter p should be below the

node BP(i)

Also if price was referenced right before a function call to BP(j), the letter p should be

to the left of the BP(j) node

and if price was referenced right after a function call to BP(j), the letter p should be to

the right of the BP(j) node

BP(5)

p

BP(1)pBP(2)pBP(3)pBP(4)p

CPS 616 W2018 FINAL EXAM 8

19. (3 marks) Use your execution tree and the BP algorithm to give a recursive definition of the

cost Bn of BP(n). You need to provide the initial condition and recurrence relation.

20. (2 marks) What is the big-O complexity of the BP algorithm as a function of n?

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

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

CPS 616 W2018 FINAL EXAM 9

22. (4 marks) Redraw the execution tree of BP(5) for your new dynamic programming

algorithm. This tree should follow exactly same convention as the execution tree for the

original algorithm, including annotating it with a p for each reference to the price array.

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.

THIS PAGE IS INTENTIONALLY LEFT BLANK AND CAN BE USED FOR ROUGH WORK OR TO CONTINUE ANSWERING AN EARLIER QUESTION. WORK ON THIS PAGE WILL ONLY BE GRADED IF SPECIFICALLY REQUESTED ON ONE OF PAGES 4 TO 9.

CPS 616 W2018 FINAL EXAM 10

THIS PAGE IS INTENTIONALLY LEFT BLANK AND CAN BE USED FOR ROUGH WORK OR TO CONTINUE ANSWERING AN EARLIER QUESTION. WORK ON THIS PAGE WILL ONLY BE GRADED IF SPECIFICALLY REQUESTED ON ONE OF PAGES 4 TO 9.

CPS 616 W2018 FINAL EXAM 11

THIS PAGE IS INTENTIONALLY LEFT BLANK AND CAN BE USED FOR ROUGH WORK OR TO CONTINUE ANSWERING AN EARLIER QUESTION. WORK ON THIS PAGE WILL ONLY BE GRADED IF SPECIFICALLY REQUESTED ON ONE OF PAGES 4 TO 9.

CPS 616 W2018 FINAL EXAM 12