Docsity

6 Solved Problems on Algorithms Usage - Final Exam | ECS 122A, Exams of Computer Science

Partial preview of the text

Download 6 Solved Problems on Algorithms Usage - Final Exam | ECS 122A and more Exams Computer Science in PDF only on Docsity!

ECS 122A: Algorithm Design and Analysis Handout Z UC Davis — Charles Martel June 7, 2008

Final Exam ECS122A

Instructions:

Congratulations! You’ve made it through a tough quarter and there’s but one final task awaiting you. This is an open book, open notes exam.

Please write clearly and succinctly;

Your name:

On problem you got out of

1 Algorithm Usage [15 points]

For each of the following settings specify which of the algorithms we have studied you would use to solve it. You should use the most efficient algorithm (considering both time (possibly expected time) and space), and you may choose an algorithm which needs minor modifications to work. Briefly justify your choice, indicate how the problem would be solved, and give the running time of your algorithm.

a) You have n houses which you want to connect by fiber-optic cables. Your goal is to make sure that there is a route (possibly indirect, such as sending from house A to B then from B to C). For each pair of houses i, j, you know the cost c(i, j) for putting a cable between the two houses (cables allow two way traffic and c(i, j) = c(j, i)).

You want a minimum cost collection of cables (minimize the sum of the costs of the cables selected) that allows routes between all the cities.

b) You have a collection of 100 text files on your computer ranging in size from 100K bytes to several megabytes. The total size of all files is about 20 megabytes (so you can assume everything easily fits in main memory). Some files may be exact duplicates of each other. You want to identify any such duplicate files (so you can remove them). Describe how to find such files quickly.

3 Data Compression [10 points]

S = EF ABCCBCCAABCCBCCABCCB

(a) Give a huffman code for the string S (describe both your tree and the actual binary encoding of the first five characters of the above string).

(b) Give the ”high-level” Lempel-Ziv encoding of the above string where each match of length 3 or longer is represented by a length, pointer pair, and otherwise by the actual input character. Assume that your window goes all the way back to the start of the string.

(c) Give the two huffman trees used by GZIP to encode the LZ string you created in part b).

4 Vertex Cover [20 points]

For an undirected graph G = (V, E), the vertex cover problem finds the smallest set of vertices which cover (thus touch) all the edges in E. This problem is NP-hard, but in class we described a simple algorithm that always gives a vertex cover of size at most twice the optimal.

a) Now, suppose each vertex has a weight. The weighted vertex cover problem finds a vertex cover whose total weight is minimum. Give a small example graph which shows that our approximation algorithm for vertex cover may produce a cover whose total weight is much larger than the optimal cover.

b) Consider the weighted vertex cover problem on an n-node ring: our graph has n nodes where wi is the weight of node i. The undirected edges are (1, 2), (2, 3),... , (n − 1 , n), (n, 1). Give a dynamic programming formulation to find a vertex cover of minimum weight in an n node ring. Justify your solution and give its running time. You may find it useful to consider solutions to prefixes of the ring (e.g. nodes 1, 2 , ..., i). Solution: Consider the following data structures. Let

c^1 [i] = min cost vertex cover of nodes 1,... , i , c^2 [i] = min cost vertex cover of nodes 2,... , i.

Thus, we initialize the two data structures in the following way.

c^1 [1] = w 1 , c^1 [2] = min{w 1 , w 2 } , c^2 [1] = ∞ , c^2 [2] = w 2 , c^2 [3] = min{w 2 , w 3 }

Our recursive formula is the same for both data structures:

c^1 [i] = min{wi + c^1 [i − 1], wi− 1 + c^1 [i − 2]} , c^2 [i] = min{wi + c^2 [i − 1], wi− 1 + c^2 [i − 2]}.

This recursive formula insures that if the i-th node is taken, then the (i − 1)-th node may or may not be taken. However, if the i-th node is not taken, then the (i − 1)-th node must be taken (to insure that the edge (i − 1 , i) is covered). In order to find the minimum-cost vertex cover, we have

minimum cost = min{wn + c^1 [n − 1], w 1 + c^2 [n]}.

5 Network Flow [15 points]

As in the problem set, consider a computer network where there are p processors P 1 , P 2 , .., Pp, and c communication lines C 1 , C 2 , ..., Cc. Each processor i has the ability to test ti lines per day and there is a list Li which contains the communication lines that processor i is able to test.

Now, in addition, assume that communication line i needs to be tested di times, (for i = 1, 2 ,... c). Note that a given processor can test the same line multiple times. A testing schedule determines for each processor the lines it should test.

(a) Describe how to find a testing schedule to test each line enough times in a single day or determine that no schedule can test all lines in a single day.

(b) Same as a) but now line i needs to be tested by di different processors (e.g. if di = 3, now line i must be tested by three different processors. In a correct part a) solution we could instead have one processor test line i 3 times, or have it tested twice by one and once by another processor.)

6 4 SAT [22 points]

A boolean formula is in 4-conjunctive normal form (4-CNF form) if every clause contains 4 literals which are separated by an “or”, and the clauses are separated from one another by an “and”. For example,

(x 1 ∨ x 3 ∨ ¬x 4 ∨ ¬x 5 ) ∧ (¬x 2 ∨ x 4 ∨ x 5 ∨ ¬x 7 ) ∧ (¬x 1 ∨ ¬x 3 ∨ x 5 ∨ x 6 )

The decision problem, 4SAT is defined as follows:

Given a boolean formula in 4-CNF form, does there exists a truth assignment to the variables such that the formula is satisfiable?

  • a) Give a polynomial-time randomized approximation algorithm for 4-SAT and justify its approximation ratio. Your algorithm always be fast and should have a good expected ap- proximation bound. Solution: Consider the following algorithm: randomly assign a true/false value to every variable. We will now analyze the expected number of satisfied clauses, given this algorithm. Let Z be a random variable such that Z = total # of satisfied clauses. Let Zi be an indicator variable such that

Zi =

1 if the i-th clause is satisfied 0 otherwise

In order to calculate the probability that an clause is satisfied, we note that there is only one truth assignment such that the clause is not satisfied: this is the truth assignment when all of the variables are false. Since there are 4 variables per clause, the probability of an all-false truth assignment is 1/ 24 or 1/16. Thus,

Pr[Zi = 1] =

The different Zi variables are not independent, but that is not important for the following formula:

E[Z] = E[Z 1 ] + E[Z 2 ] +... + E[Zm]

=

m

Thus, the approximation bound is 15/16. Clearly, this algorithm runs in polynomial time, as it is linear in the number of variables and clauses to randomly assign and evalute the boolean formula.

  • b) Show that 4-SAT is NP-Complete. You may assume 3-SAT is NP-Hard. Solution: First, we note that 4-SAT is in NP because we can write a polynomial-time verifier which takes a 4-SAT instance and proposed truth assignment as input, and evaluates the boolean formula at that truth assignment. If the boolean formula evaluates to true the