Vous êtes sur la page 1sur 12

CS 374: Algorithms and Models of Computation, Spring 2015

Final Exam May 8, 2015


Name:
NetID:

# 1 2 3 4 5 6 7 Total
Score
Max 20 10 10 10 10 10 10 80
Grader

Please clearly PRINT your name and your NetID in the boxes above.

This is a closed-book, closed-notes, closed-electronics exam. If you brought anything except your
writing implements, put it away for the duration of the exam. In particular, you may not use any
electronic devices.

Please read the entire exam before writing anything. Please ask for clarification if any question
is unclear.

There are seven problems, on eight numbered pages, plus a ninth (blank) page should you need it.
A list of NP-Complete problems is available on the final page. The first problem is worth 20 points,
the others are worth 10 each.

You have 180 minutes (3 hours).

If you run out of space for an answer, continue on the back of the page, or on the blank page at
the end of this booklet, but please tell us where to look.

Proofs of correctness and justifications for running time are required only if we specifically
ask for them. However, giving a brief explanation may be helpful to us in understanding your
algorithm and thought process.

We do not have an I dont know policy as in some previous semesters. If you write I dont
know, you will earn nothing more than our sympathy. If you dont know please dont waste your
time, and ours, by writing irrelevant or clearly wrong responses.
CS 374 Final Exam Spring 2015

1 Multiple Choice
Clearly mark exactly one answer for each question. Ambiguously marked answers will be marked
incorrect. There is no penalty for incorrect answers. There are 20 questions each worth 1 point.
Pn
1. What is the tightest asymptotic upper bound for the sum i=1 i
3
= 1 + 23 + 33 . . . + n3

(a) O(1)
(b) O(n)
(c) O(n2 )
(d) O(n3 )
(e) O(n4 )
(f) O(n6 )
(g) O(2n )

2. Suppose A is a polynomial-time algorithm for solving 3-SAT. What does the existence of A imply?
i. P = NP
ii. 3-coloring can be solved in polynomial time

(a) i and ii
(b) i only
(c) ii only
(d) neither i nor ii

3. Suppose L is a NP-Complete problem. For each of the following circle True if the statement is
necessarily true and False if there are cases where the statement is false.

(a) True False L is in NP


(b) True False L is NP-Hard
(c) True False there is a polynomial time reduction from every NP-Complete language L 0 to L
(d) True False there is a polynomial time reduction from every NP-Hard language L 0 to L
(e) True False there is a polynomial-time reduction from L to 3SAT.

4. For each of the following, circle rec if the language is recursive, r.e. if it is recursively
enumerable but not recursive, or neither if it is not recursively enumerable.

(a) rec r.e. neither {M #w | M accepts w in |w|10 steps}


(b) rec r.e. neither {M | M does not halt on any input}
(c) rec r.e. neither {M | M halts on every input}
(d) rec r.e. neither {M | M halts on at least 374 different inputs}

1
CS 374 Final Exam Spring 2015

5. For each of the following, circle True if it must be true, or circle False if there are some cases
where the statement is false.

(a) True False The set of languages over alphabet = {0, 1} is countably infinite
(b) True False If L1 and L2 are both recursive, then L1 L2 is recursive
(c) True False If L1 is recursive and L2 is r.e., then L1 L2 is r.e.
(d) True False If L1 is r.e. and L2 is recursive, then L1 L2 is r.e.
/ P then L1
(e) True False If L1 p L2 and L2 / P.
(f) True False If L1 P and L2 is regular then L1 L2 is in P
(g) True False If L1 p L2 and L1 P then L2 P.
(h) True False If L is a context free language then L N P.
(i) True False If there is a nondeterministic TM accepting L in time T (n), then there is a
deterministic TM accepting L in time c T (n) for some constant c.

2
CS 374 Final Exam Spring 2015

2 Regularity
Let L be any language over a finite alphabet and let L374 = {w L : |w| 374} be the set of strings in
L that are of length 374 or more. Prove that if L is regular, then L374 is regular.

Solution: There are several ways to solve this:

By closure properties.
Let <374 = {w : |w| < 374}. Then <374 is finite, hence regular. And, so its complement, 374
is also regular. Then L374 = L 374 is regular because it is the intersection of two regular
languages.

By describing a machine M374 to accept L374 .


Since L is regular, let M = (Q, , , q0 , F ) be a DFA accepting L.
Basically, well add a finite counter to M s states, so that while behaving like an acceptor for L, it
additionally counts characters and only accepts if the count has exceeded 373.
Define M374 = (Q 374 , , 374 , start, F374 ) as follows.

Q 374 = Q {0, 1, . . . , 374}


374 (q, i, a) = (q, a), i + 1 if i 373, and (q, a), 374 otherwise.
start = q0 , 0
F374 = { f , 374 | f F }

It should be clear by construction that the machine simulates M in its first component, and keeps
track of the character count up to 374 in the second component.

3
CS 374 Final Exam Spring 2015

Rubric: 6 points for recognizing that intersection of L and 374 is a regular


language if the latter is a regular language. (Partial credit 3 points for recognizing
that you can count whether the number of characters read is 374 using another
automaton, but not stating what that automaton is.)

3 points for proving that 374 is a regular language by either constructing an


automaton, or by observing that its complement is a finite language and hence
regular.

1 point for outlining/describing the product construction.

And

3 points for recognizing that you need to count the number of characters seen
so far, 3 points for outlining the layered construction of the DFA with 374 copies
of each state.

3 points for the description of states, initial states, accepting states, and high
level transition function.

1 point if transition function is written explicitly and correctly.

8 points for recognizing that the finite set difference (because intersection
and set complement are) is a closure property of regular languages, and L -
{w | |w| < 374} is the set we want. item 2 points for proving that the latter is
regular because it is finite.

1 point taken off for not recognizing that the complement of L374 is the set of
words in L whose length is less than 374, AND the set of words not in L.

And

1 point for constructing NFA.

And

0 points for solutions which do not differentiate between "any language" in


and "any string" in . Regular languages are not closed under inclusion.

0 points if you have confused decidable and regular. Using turing machines to prove
anything regardless of whether your construction of a turing machine is correct is not
given any credit.

4
CS 374 Final Exam Spring 2015

3 Knights Journey
Consider a n n chess board where (i, j) refers to the position on the board in row i and column j. The
bottom left corner is position (1, 1). A knight in chess can move as shown in the figure below. Formally,
given a position (i, j) it can move to position (i 0 , j 0 ) if (i 0 , j 0 ) is a valid position on the board and one
of the following conditions holds true (i) |i i 0 | = 2 and | j j 0 | = 1 or (ii) |i i 0 | = 1 and | j j 0 | = 2.
The board also has a few positions that the knight cannot land on because they are occupied by other
pieces. Occupied positions are specified by a n n boolean array O[1..n, 1..n] where O[a, b] = 1 implies
that position (a, b) is occupied and O[a, b] = 0 means that it is not occupied. Describe an algorithm
that given n, a starting position (i, j), an ending position (i 0 , j 0 ), and the matrix of occupied positions
O[1..n, 1..n], determines the fewest number of moves a knight can move to get from (i, j) to (i 0 , j 0 )
without landing on any of the occupied positions, or outputs not possible if there is no such sequence
of moves. State and justify the running time of your algorithm.

Solution: This is a simple graph modeling problem. We model the board and allowable move as a
directed graph G = (V, E) as follows.

For each board position (a, b) that is not occupied we create a vertex va,b . We assume for simplicity
that (i, j) and (i 0 , j 0 ) are not occupied by other pieces, otherwise there is no feasible solution for
the given problem.

Suppose va,b and va0 ,b0 are two vertices that are created in the preceding step. We add an edge
(va,b , va0 ,b0 ) if a knight can move from (a, b) to (a0 , b0 ) in one step.

Once the graph G is constructed we run BFS on G to check if there is a path from vi, j to vi 0 , j 0 , and if
there is, to find a shortest path. We output not possible if there is no path, otherwise we ouput the
length of the shortest path.
The running time consists of two parts. First, to create the graph, and second to run BFS on it. The
first part takes O(n2 + N + M ) time where where N is number of nodes in G and M is number of edges
in G. Running BFS takes O(N + M ) time. N can be at most n2 and M is O(N ) since the number of edges
out of any node can be at most 8. Thus the total running time is O(n2 ).

Rubric: 8 points for any correct polynomial-time solution with correct running-time
analysis. 10 points if running time is O(n2 ) and correctly analyzed. Partial credit as
follows. Identifying as graph modeling problem (3 points). Correct construction of
graph (2 points). Correct algorithm on graph (3 points). Proper run time analysis (2
points).

5
CS 374 Final Exam Spring 2015

4 Burgers
The McKing chain wants to open several restaurants along Red street in Shampoo-Banana. The possible
locations are given in a sorted array L[1, . . . , n] where L[i] is the distance from the start of Red street in
meters. (We assume that the street is a straight line.) McKing has collected some data indicating that
opening a restaurant at location L[i] will yield a yearly profit of profit[i] independent of where the
other restaurants are located. However, the city of Shampoo-Banana has a zoning law which requires
that any two McKing locations should be D or more meters apart. Describe an efficient algorithm that
McKing can use to figure out the maximum yearly profit possible by placing its restaurants in compliance
with the citys zoning law. State and justify the running time of your algorithm.

Solution: We use dynamic programming.


Let OP T (i) denote the optimum yearly profit that McKing can obtain by opening restaurants in
locations from 1 to i. For location i we let prev(i) denote the nearest location to the left of i that is at
least D meters away from i. We define prev(i) = 0 if there is no such location. We write the following
recurrence relation for OP T (i).

OP T (i) = max{OP T (i 1), profit(i) + OP T (prev(i))}


with base case
OP T (0) = 0
What we want to compute is OP T (n).
A simple iterative algorithm based on the above recurrence can compute OP T (n) in O(n) time
assume that prev(i) for 1 i n is pre-computed and stored in an array. Given the sorted array
L[1, . . . , n] and D, prev(i) for each i can be computed in O(n) time (simple exercise). Space requirement
is also O(n).

Rubric: Standard DP rubric: 2 pts: recognize it is a DP. 4 pts: recursive function. 1 pt:
base case. 3 pts: Memorization & running time.
Comments: 1. Generally students did much better than they did in MT2. The only
trick is lots of them didnt notice the running time can be reduced to O(n) by prior
computing of previous store locations, even though they give out a correct memoization
structure. Most of these students received 9/10. 2. There are some students using
graph modeling which is also correct but less efficient. Creating all edges requires
O(n2 ). They received scores as high as 7/10. They were told that there is a better DP
solution. 3. People using Greedy methods typically received 3/10. 4. All other incorrect
answers received 1-2 for effort.

6
CS 374 Final Exam Spring 2015

5 Zoo Habitat
Suppose we have n animal species A1 , . . . , An that we want to house in some number of shared zoo
habitats. Each species will inhabit exactly one of the habitats. However, some pairs of species are natural
enemies or share a predator-prey relationship, so should not share the same habitat (e.g., lions and
gazelles). What is the fewest number of habitats needed to house all of the animals? We formulate this
as a decision problem as follows. Given the integers n and h and a two-dimensional array M [1..n, 1..n]
where M [i, j] = M [ j, i] = 1 if and only if Ai and A j cannot share a habitat, are h habitats sufficient to
house all of the animal species? Prove that this problem is NP-Complete.

Solution: This is just Coloring problem in disguise.


First we show that the Zoo Habitat problem is in NP by describing a polynomial-time non-deterministic
algorithm for it.

1. Guess an assignment of habitats from {1, 2, . . . , h} for each species. That is, for each species Ai we
guess a number `i from {1, 2, . . . , h}.

2. For each pair (i, j) with 1 i < j n do

(a) If (M [i, j] = 1) and (`i = ` j ) then Output NO

3. Output YES

The guessing takes polynomial-time in the length of the input since the length of the string to guess
is O(n log h) (one number between 1 and h per species). The verification takes O(n2 ) time to go over
each entry in the matrix.
Now we describe a reduction from Graph Coloring to Zoo Habitat. Given a graph G = (V, E) and
integer k the reduction creates n species A1 , . . . , An where n = |V |. The reduction creates a matrix M
where M [i, j] = M [ j, i] = 1 if (vi , v j ) is an edge in G, otherwise M [i, j] = M [ j, i] = 0. The reduction
sets h = k. The reduction can be seen to run in polynomial time. Now we argue about the correctness
of the reduction. We want to show that G can be colored with k colors if and only if the Zoo Habitat
instance produced by the reduction admits a solution with at most k habitats.
Suppose G can be colored with k colors. Let `i be the color of vi . We assign to Ai habitat `i . We
clearly use at most k habitats. M [i, j] = 1 only if vi and v j are connected by an edge in G in which case
`i 6= ` j . Thus the habitat assignment respects the property that two species Ai and A j are not assigned
the same habitat if M [i, j] = 1.
Suppose there is a valid habitat assignment for the ZooHabitat instance generated from the reduction.
Let `i be the habitat assigned to Ai . We assign a color `i to vertex vi . Since h = k the number of colors
used is at most k. We argue that this is a valid coloring of G. Suppose (vi , v j ) is an edge in G. Then
`i 6= ` j since M [i, j] = 1 and the habitat assignment would not have assigned the same habitat to Ai and
Aj.

Rubric: 3 points for Identifying reduction from Coloring. 3 points for a correct reduc-
tion. 1 point for proof of correctness of each direction. 2 points for proof in NP.

7
CS 374 Final Exam Spring 2015

6 Zero-length Path
Zero-length Path is the following problem: given a directed graph G = (V, E) with edge lengths that can
be positive or negative, is there a path containing at least one edge in G of total length exactly zero?
Describe a polynomial time algorithm for the Hamilton Cycle problem assuming black box access to a
polynomial time algorithm for Zero-length Path. Hint: The algorithm for Hamilton Cycle problem may
use the algorithm for Zero-length Path more than once.

Solution: We describe two solutions. The first one develops an algorithm for Hamiltonian Cycle using
an algorithm for ZP (short for zero-length path) several times.

Given a directed graph G = (V, E), well try to determine, for each edge (u, v), whether there is a
Hamiltonian cycle that uses the edge. Set weights on all of the edges to 1. Now, there is a Hamiltonian
cycle using (u, v) iff there is a path from v to u of weight n 1 (the rest of the cycle). Now consider
adding two new nodes s and t, and the edges (s, v) and (u, t). Clearly if there was a HC containing
(u, v), there is a path from s to t visiting each vertex once. (s v u t). Now set the weight
of (s, v) to 1, and the weight of (u, t) to n, and this forms a zero-length path.
Formally, let Gu,v = (V {s, t}, E {(s, v), (u, t)}). The weights of all edges in E are 1, the weight of
(s, v) is 1, and the weight of (u, t) is n. The following claim is the key observation and we leave the
proof as an exercise.

Claim: Gu,v has a zero-length path if and only if G has a Hamiltonian Path from s to t. Moreover,
if (u, v) is an edge of G then G has a Hamiltonian Cycle containing (u, v) if and only if Gu,v has a
zero-length path.
Assuming the claim, to check if there is a Hamiltonian cycle in G that contains the edge (u, v)
we construct Gu,v and use the ZP algorithm to check if it has zero length path. To check if G has a
Hamiltonian cycle we can run the preceding algorithm for each edge (u, v) in G. Thus m calls to the ZP
algorithm would suffice to check if G has a Hamiltonian Cycle.
One can use the above idea to make just one call to ZP algorithm as follows. Given graph G, pick
an arbitrary vertex v and obtain a new graph G 0 by splitting v. Replace v by v and v + and add
edge (v , v + ). Replace edges of the form (u, v) by (u, v ) and edges of the form (v, w) by (v + , w). We
claim that G has a Hamiltonian Cycle if and only if G 0 has a Hamiltonian cycle that contains the edge
(v , v + ). So, to check if G 0 has a Hamiltonian Cycle, equivalently, one containing (v , v + ), we run the
ZP algorithm on the graph G v0 ,v + as above.

Rubric: 1 point if the reduction is in the right direction. 2 points for adding both
positive and negative edge lengths to the input. 2 points if the algorithm does not
always output yes or always output no, and is either always correct when it outputs yes
or always correct when it outputs no. 4 points if the rest of the algorithm is correct. 1
point for stating the running time of the algorithm. Maximum of 8 points for a solution
that reduces the Hamiltonian path problem to the zero-length path problem, or reduces
the Hamiltonian cycle problem to the zero-length cycle problem.
Comment: Some students claimed that if for every node v in a graph G there is a
Hamiltonian path that starts at v, then G has a Hamiltonian cycle. This is not true; a
counterexample is the Petersen graph.

8
CS 374 Final Exam Spring 2015

7 Generators
Define a TM generator G as a TM with a single work tape, but also a special write-only output tape that
starts with # at the left most cell, and the write head one cell to the right of that. From time to time,
depending on its computation on the work tape, the generator may write a character on the output tape
and move the output head to the right. It is said to generate a word w if at some point in time, #w#
is on the output tape. The language generated by G, denoted by L(G), is the set of all strings that G
ever generates. Prove that a language L is recursively enumerable if and only if L = L(G) for some TM
generator G. Note that you need to show both directions separately.

Solution:

L is r.e. implies that L = L(G) for some TM generator G


Let M be a TM accepting L. The generator G for L simply dovetails the computations of M (w)
for all words w, and whenever one of the computations M (w) accepts, then G writes w# on its
output tape. Now, if w L(M ) then after some finite time, M (w) will accept, and G will generate
w, so L(M ) L(G). Conversely, if w L(G), it must be that at some time, the computation of
M (w) halted and accepted, so that w L(M ) also, so L(G) L(M ). Hence, L = L(M ) = L(G) as
desired.

L = L(G) for some TM generator G implies that L is r.e.


Given G, we construct a TM M that accepts L(G) as follows. On input w, M simulates G and if
ever #w# appears on the output tape of G, then M accepts w. Clearly M accepts all, and only,
the strings in L(G), so L(G) = L(M ) is r.e.

Rubric:
5 points for each direction
First direction:
4 points for construction, 1 point for showing/claiming it correct. For construction, need
to say either dovetail, or explain a dovetailing process. 0 points if the process clearly
relies on M (w) halting before moving on to the next w to generate. For the correctness,
no need to prove L(G) = L(M ) in such detail, but still need to observe, or at least
assert, that G generates all and only the words of L(M )
Second direction:
4 points for construction, 1 point for showing/claiming it correct.

9
CS 374 Final Exam Spring 2015

This page for extra work.

10
CS 374 Final Exam Spring 2015

8 List of some useful NP-Complete Problems/Languages


SAT { | is a boolean formula in CNF form and is satisfiable}

3SAT { | is a boolean formula in CNF form with exactly 3 literals per clause and is satisfiable}

Independent Set {G, k | Graph G = (V, E) has a subset of vertices V 0 V of size at least k such
that no two vertices in V 0 are connected by an edge}

Vertex Cover {G, k | Graph G = (V, E) has a subset of vertices V 0 V of size at most k such that
every edge in E has at least one of its endpoints in V 0 }

Clique {G, k | Graph G has a complete subgraph of size at least k}

3Color {G | The vertices of graph G can be colored with 3 colors so that no two adjacent vertices
share a color}

Coloring {G, k | The vertices of graph G can be colored with k colors so that no two adjacent
vertices share a color}

Hamiltonian Cycle {G | Directed graph G contains a directed cycle visiting each vertex exactly
once}

Undir Hamiltonian Cycle {G | Undirected graph G contains a cycle visiting each vertex exactly
once}

11

Vous aimerez peut-être aussi