Docsity

Sample Final Exam for Algorithm Design and Analysis | ECS 122A, Exams of Computer Science

Toggle sidebar
bg1
ECS 122A: Algorithm Design and Analysis Handout Z
UC Davis — Charles Martel June 3, 2008
Sample Final
1 Algorithm Usage [60 points]
1.1. (5 points) )You are given a computer network where some pairs of computers (but not all) are
connected by a 2-way communication line. You know which computers have links directly connecting them,
and you want to find out, for a specific computer, its shortest route to each other computer in the network.
Distance is defined in terms of how many hops the message must travel (thus if computer A can send a
message directly to B, and B can send directly to C and C can send directly to D, the cost from A is 1 to
B, 2 to C and 3 to D). There are ccomputers and Llinks.
Solution: Do a BFS starting at the source computer. As you do the DFS label each computer
reached with its predecessor and distance. You can then backtrack to get the path for any given
destination. This takes O(c+L) assuming an adjacency list representation.
1.2. (5 points) You want to build a railroad line between Sacramento and New York City. There are n
possible intermediate cities which could be used, and you know the cost of laying track between any pair of
these cities. Your goal is to spend the least total amount to construct all the track needed.
Solution: Just find a shortest path from Sac. to NY using Dijkstra’s algorithm. Uses Θ(n2) time
since there are n2arcs.
1.3. (8 points) Same as 1.2 except now you want to lay track which will connect all ncities (thus for any
pair of cities, you will end up with track connecting them. Note that two routes may share the same track,
track laid between say Sacramento and San Francisco can be used for travel in both directions). Again, your
goal is to have the total construction cost be as small as possible.
Solution: Now we want to find a minimum spanning tree. Its best to use Prim’s algorithm since
the graph is dense. Again Θ(n2) time.
1.4. (8 points) You want to store nfiles where fiis the number of bytes in the ith file. You have
two discs each of which can hold up to Dbytes of data. Each file is to be stored on one of the two
discs (and you can’t split a file: all of it must be on Disc 1 or on Disc 2).
Your goal is to keep Disc 2 as empty as possible, so you want to assign files so that Disc 1 is as full
as possible and the total number of bytes for files assigned to Disc2 is as small as possible.
Solution: This is basically a subset sum problem with the files being the numbers and D the target
sum (we want to find files which sum as close to D as possible without going over). Using DP we
get a Θ(nD) solution using D space.
1.5. (5 points) For video conferencing you sometimes need to find a route in a computer network which
has enough capacity for the connection. Suppose you have a network with nnodes and bi,j is the bandwidth
of the link connecting nodes iand j(assume bandwidth is symmetric, so bi,j =bj,i). The bandwidth of a
connection depends on the smallest link in the route. Thus if you find a route, say i−j−kconnecting nodes
iand j, the bandwidth of this route is the minimum of bi,j and bk,j.
pf3
pf4
pf5

Partial preview of the text

Download Sample Final Exam for Algorithm Design and Analysis | 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 3, 2008

Sample Final

1 Algorithm Usage [60 points]

1.1. (5 points) ) You are given a computer network where some pairs of computers (but not all) are connected by a 2-way communication line. You know which computers have links directly connecting them, and you want to find out, for a specific computer, its shortest route to each other computer in the network. Distance is defined in terms of how many hops the message must travel (thus if computer A can send a message directly to B, and B can send directly to C and C can send directly to D, the cost from A is 1 to B, 2 to C and 3 to D). There are c computers and L links.

Solution: Do a BFS starting at the source computer. As you do the DFS label each computer reached with its predecessor and distance. You can then backtrack to get the path for any given destination. This takes O(c + L) assuming an adjacency list representation.

1.2. (5 points) You want to build a railroad line between Sacramento and New York City. There are n possible intermediate cities which could be used, and you know the cost of laying track between any pair of these cities. Your goal is to spend the least total amount to construct all the track needed.

Solution: Just find a shortest path from Sac. to NY using Dijkstra’s algorithm. Uses Θ(n^2 ) time since there are n^2 arcs.

1.3. (8 points) Same as 1.2 except now you want to lay track which will connect all n cities (thus for any pair of cities, you will end up with track connecting them. Note that two routes may share the same track, track laid between say Sacramento and San Francisco can be used for travel in both directions). Again, your goal is to have the total construction cost be as small as possible.

Solution: Now we want to find a minimum spanning tree. Its best to use Prim’s algorithm since the graph is dense. Again Θ(n^2 ) time.

1.4. (8 points) You want to store n files where fi is the number of bytes in the ith file. You have two discs each of which can hold up to D bytes of data. Each file is to be stored on one of the two discs (and you can’t split a file: all of it must be on Disc 1 or on Disc 2).

Your goal is to keep Disc 2 as empty as possible, so you want to assign files so that Disc 1 is as full as possible and the total number of bytes for files assigned to Disc2 is as small as possible.

Solution: This is basically a subset sum problem with the files being the numbers and D the target sum (we want to find files which sum as close to D as possible without going over). Using DP we get a Θ(nD) solution using D space.

1.5. (5 points) For video conferencing you sometimes need to find a route in a computer network which has enough capacity for the connection. Suppose you have a network with n nodes and bi,j is the bandwidth of the link connecting nodes i and j (assume bandwidth is symmetric, so bi,j = bj,i). The bandwidth of a connection depends on the smallest link in the route. Thus if you find a route, say i − j − k connecting nodes i and j, the bandwidth of this route is the minimum of bi,j and bk,j.

a) Suppose you are given two nodes i and j and a needed bandwidth B, how would you efficiently find a route from i to j which has bandwidth ≥ B?

Solution: Just ignore all arcs with bandwidth less than B since they aren’t useful. Then just do DFS starting at i until you reach j. This takes time linear in the size of the network.

b) Now suppose you want a route of maximum bandwidth from i to j. How would you efficiently find such a route?

Solution: Now you need to find a bottleneck longest path from i to j as we did in the homework. Modify Dijkstra’s to label i infinity and all others 0. Now you repeatedly make the largest temp. label u permanent and relax on its neighbors v use:

If L(v) ¡ MINL(u), bu,v then L(v) ¡- MINL(u), bu,v

This takes the same time as Dijkstra’s algorithm.

2 Short answers [60 points]

1.6. (5 points) An Independent Set in a graph is a set S of vertices such that no two vertices in S are connected by an edge. The Independent Set problem is defined as follows: given an undirected graph G = (V,E), and an integer k, can you find an Independent Set of size k in G?

Argue that the Independent Set Problem is in N P. NOTE: you are NOT being asked to show it is N P -complete.

Solution: Given a proposed solution S we need only check that S consists of vertices in G, that S is of size k, and that for every two elements i, j in S, (i, j) is NOT in E. We can easily do this in n^2 time using an adjacency matrix representation of G, so we can check a solution in polynomial time. Thus IS is in NP.

1.7. (15 points) For each of the following problems, write true or false. Briefly justify your answer. For the purpose of answering this question you may assume that P is not equal to N P.

(a) Every Problem in N P is hard.

Solution: False. All the easy problems are in NP.

(b) There are instances of the Hamilton Path Problem which can be solved quickly.

Solution: True. for example a graph with three nodes and arcs A-¿B, B-¿C and C-¿A (also any graph which is not connected has no hamilton path, so it is easy to say no).

(c) A polynomial time algorithm for the decision version of the Traveling Salesman Problem (TSP) would imply a polynomial time algorithm for the optimization version of the TSP problem (where we want the cost of the best tour).

Solution: True. We can do a binary search over the possible optimal path lengths using the decision procedure to tell us whether our current guess is too high or not (if we get a no answer we need to try a lower cost, if we get a yes, a higher one).

We can represent this structure with an array A[1..n] such that A[i] = parent of i if i is not a root, and A[i] = n + k where k is the size of the set containing i if i is the root of its tree (and thus the name of the set).

Suppose we are running Kruskal’s Minimum Spanning Tree Algorithm, and we are using the above structure to help check if adding the next edge forms a cycle.

If (i, j) is the next edge considered by Kruskal’s algorithm, to see if we should add this edge:

(g) (5 points) We should start by doing a FIND on i and j.

Solution: True. We need to know the set names to see if they are in the same CC.

(h) (5 points) FIND(i) takes Θ(log n) time.

Solution: True. The true can have height up to be no greater than log n.

(i) (5 points) Let s and t be roots of trees. UNION(s, t) takes Θ(log n) time.

Solution: False. If you have the roots it takes O(1) time.

4 Half-3-CNF-SAT

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

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

This boolean formula is satisfiable because when x 1 = true and x 2 = false, and the other variables arbitrarily set, every clause is true. The decision problem, 3SAT is defined as follows:

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

The Half-3-CNF-SAT decision problem is defined as follows:

Given a boolean formula in 3-CNF form, does there exists a truth assignment to the variables such that exactly half of the clauses are satisfied?

Given that 3-SAT is NP-Complete, show that Half-3-CNF-SAT is also NP-Complete.

Solution: In order to prove that the Half 3-CNF problem is NP-Complete, we need to prove two things:

  1. Half 3-CNF ∈ N P
  2. ∀L ∈ NP, L ≤p Half 3-CNF, or Half 3-CNF ∈ NP-Hard

In order to prove that Half 3-CNF is in NP, we need to show that a solution to a particular instance of the problem can be verified in polynomial time. Consider the following “witness” algo- rithm which takes an instance of Half 3-CNF and a “certificate” as parameters. Our algorithm takes a boolean formula in conjunctive normal form, and the “certificate” or “proposed solution” is a list of the boolean variables and their proposed truth assignments. The algorithm walks the formula, evaluating each clause with the proposed truth assignments and returns true if exactly half the clauses are true, and false if not. This is clearly polynomial time because the boolean formula is only walked once.

In order to prove that Half 3-CNF is NP-Hard, we can consider a reduction from a known NP-Complete problem to Half 3-CNF (or prove it directly, which we won’t even consider). We choose to reduce from from 3-CNF-SAT to Half 3-CNF. Because Circuit-SAT ≤p SAT ≤p 3- CNF-SAT, if we are able to show 3-CNF-SAT ≤p Half 3-CNF, then this implies that Circuit-SAT ≤p Half 3-CNF. And since L ≤p Circuit-SAT ∀L ∈ NP, then this implies that L ≤p Half 3-CNF ∀L ∈ NP or that Half 3-CNF ∈ NP-hard.

Consider the following reduction. First, we need to convert an instance of 3-CNF-SAT to Half 3- CNF. Our approach is create a φ′^ which contains 4 times as many clauses as φ. Suppose φ contains m clauses. When creating φ′, first we take all of the clauses from φ. Next, we create m clauses of the form:

(p ∨ ¬p ∨ q) (1)

Clearly, these clauses are always true, regardless of the individual truth assignments for p and q. Next, we create 2m clauses of the form:

(p ∨ q ∨ r) (2)

These clauses are always true or always false. Therefore, we have created a boolean formula φ′ which contains all of the clauses φ and m clauses which are always true and 2m clauses which are either all true or all false.

We also need to show that there exists a ”yes” instance of 3-CNF-SAT if and only if there exists a ”yes” instance of Half 3-CNF. In order for Half 3-CNF to return true, exactly half of the clauses must be true and half false. But m clauses are always true, and 2m clauses have the same truth value. Therefore, if Half 3-CNF returns true, the 2m clauses must be false and there must a truth assignment which causes the m clauses of φ contained within φ′^ to be true. Therefore, if there exists a truth assignment which causes φ to be true, than Half 3-CNF will return true when run on φ′. If Half 3-CNF returns false when run on φ′, then there does not exist any truth assignment which makes φ true. This concludes our proof. 2