Вы находитесь на странице: 1из 10

York University

CSE 3101 Summer 2012 Exam


Instructor: Jeff Edmonds

Family Name:

Given Name:

Student #:

Email:

0) Art

1) Loop Invariants

14

2) Greedy

27 = 7+14+6

3) Recursion

24 = 14+6+4

4) Dynamic Programming

26 = 6+3+3+14

5) NP

9=3+3+3

Total

102 marks

This test is closed book and was designed to last 3 hours.


Keep your answers short and clear.
0 Art therapy question: When you are half done, draw on the front a picture of how you are feeling.

1. (14 marks) Loop Invariants Selection Sort: Describe the algorithm Selection Sort using the loop
invariant method. Give the key steps as described in class.

2. (27 = 7+14+6 marks) Greedy (Independent sets in trees): The input consists of a rooted tree T . (See
example below.) A solution consists of a subset S of the nodes of tree T . (See black nodes below.)
Such a solution is valid if no two of the nodes in S have an edge between them (i.e. one is not the
parent of the other.) Said another way S is valid if each edge includes at most one of its two nodes in
the solution. The cost/value of S is the number of nodes |S| included. Our goal, given a tree T is to
find a valid solution whose value is maximized. We will consider a number of different algorithms for
it.

(a) An easy algorithm is to layer the nodes of the graph by their distance from the root. Colour the
nodes on the evenly indexed levels red and on the odd ones blue. Take the bigger of these two
sets as the solution to independent set.
- Does this algorithm always return a valid solution? Prove.
- Does this algorithm always return an optimal solution? Prove.
- What does this say about the number of nodes in the optimal solution to this problem?

(b) Consider the following greedy algorithm, given a tree T . The greedy criteria sorts the nodes in
reverse order by their distance from s, i.e. The algorithm starts with the bottom leaves and works
its way up the tree level by level until it reaches the root. The key point in this order is that
when the algorithm considers node u, it has already considered all of its children. The algorithm
then commits to include this node u in S unless u has a child that has already been included, in
which case it excludes u.
Use the technique given in class to prove that this produces an optimal solution.

(c) Which of the following are true. Give a one sentence proof for each.
i. trees T , optimal solutions S of T , leaf nodes u of T , u S.
ii. trees T , optimal solutions S of T , leaf nodes u of T , u S.
iii. trees T , optimal solutions S of T , leaf nodes u of T , u S.

3. (24 = 14+6+4) Recursive (Independent sets in trees): Reconsider the exact same problem given
above and the exact same greedy algorithm for it. Let ST denote the optimal solution returned by this
greedy algorithm for tree T .
(a) Give a recursive algorithm that takes as input a tree T and returns the set ST .
Hint: You may have to redefine the post condition so that your algorithm returns some extra
information. If so clearly document the change.

(b) Give a quick paragraph arguing why your code gives the correct answer.

(c) Give a quick paragraph explaining the running time of your algorithm.

4. (26 = 6+3+3+14) Dynamic Programing (Weighted Independent sets in trees): The problem considered is the weighted version of the previous problem. The input consists of a rooted tree T in which
each node u has a weight wu . A solution consists of a subset S of the nodes of tree T . Such a solution
is valid if no two of the nodes in S have an edge between them (i.e. one isPnot the parent of the other
in T .) The cost/value of S is the sum of its node weights, i.e. cost(S) = uS wu . Our goal, given a
tree T is to find a valid solution whose value is maximized.
(a) Suppose you have a instance T , a little bird, and as many recursive friends as you like. Give a
good bird/friend algorithm solving this problem.
Hint: Ask the bird the whether or not to take the root.
For both this question and the coding question feel free to say things like
For each subtree Tu rooted at children and/or the grandchildren u of the root of T do . . ..

(b) What is the set of subinstances that needs to be solved?


In what order should these subinstances be solved.

(c) What is the running time? (Remember there may be more than one friend per bird answer.)

(d) Give the dynamic programming code.

5. (9=3+3+3) Nondeterministic Polynomial Time:


(a) How do you convince your boss that the computational problem he wants you to solve is likely
too hard to solve in general?

(b) How do you prove that one computational problem is at least as hard as another? P1 P2 .

(c) What does it mean for a problem to be in NP (Non-Deterministic Polynomial Time)?


What does it mean for a problem to be NP-Complete.

10