Docsity

UC Berkeley CS 170 Final Exam - Algorithms and Optimization, Exams of Algorithms and Programming

Toggle sidebar
bg1
UC Berkeley—CS 170 Final
Lecturer: Gene Myers December 16
Final for CS 170
Print your name:
Myers Gene
,
(last) (first)
Three pages of notes are permitted. Two from your prior midterms and a new one pre-
pared specifically for the final. No electronic devices, e.g. cell phones and calculators, are
permitted. Do all your work on the pages of this examination. If you need more space, you
may use the reverse side of the page, but try to use the reverse of the same page where the
problem is stated.
You have 3 hours. The questions are of varying difficulty, so avoid spending too long on
any one question.
In all algorithm design problems, you may use high-level pseudocode.
DO NOT TURN THE PAGE UNTIL YOU ARE TOLD TO DO SO.
Problem Score/Points
1 /10
2 /10
3 /10
4 /10
5 /10
6 /10
Total /60
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download UC Berkeley CS 170 Final Exam - Algorithms and Optimization and more Exams Algorithms and Programming in PDF only on Docsity!

UC Berkeley—CS 170 Final Lecturer: Gene Myers December 16

Final for CS 170

Print your name:

Myers Gene , (last) (first)

Three pages of notes are permitted. Two from your prior midterms and a new one pre- pared specifically for the final. No electronic devices, e.g. cell phones and calculators, are permitted. Do all your work on the pages of this examination. If you need more space, you may use the reverse side of the page, but try to use the reverse of the same page where the problem is stated.

You have 3 hours. The questions are of varying difficulty, so avoid spending too long on any one question.

In all algorithm design problems, you may use high-level pseudocode.

DO NOT TURN THE PAGE UNTIL YOU ARE TOLD TO DO SO.

Problem Score/Points 1 / 2 / 3 / 4 / 5 / 6 / Total /

1. Network Flow

Find the maximal flow in the following network. Give each augmenting path and the net flow gain obtained (we give the first one). Use the back sheet of the previous page for your work.

5

s 6

a

c

d

t

b e

8

5

10

4

2

3

5

7

12

2

Augmenting Path Net Flow Gain s - a - d - t 4 s - a - c - d - t 2 s - c - d - t 1 s - c - d - t 5 s - b - e - t 2 Total 14

Draw the residual graph at the conclusion of the algorithm, and draw or list a minimum cut of the graph.

Augmenting Path Net Flow Gain a - d 4 c - d 3 c - e 5 b - e 2 Total 14

7

s 6

a

c

d

t

b e

2

3

4

5

2

5

6

6

2 2

5

4

3

7

(c) Give the dual formulation of your LP problem. What is the intuitive meaning of each of the dual variables? We view the dual LP as a valuation problem and ask what the value per pound of cereal, yc, the value per pound of meat, ym, and the value per package of Frisky Pup packaging, yp. min 240, 000 yc + 180, 000 ym + 110, 000 yp (total value) subject to

1 yc + 1. 5 ym + 1yp ≥ 1.6 (value of a package of Frisky Pup) 2 yc + 1ym ≥ 1.4 (value of a package of Husky Hound) y 1 , y 2 , y 3 ≥ 0 (negative valuations make no sense)

3. Bottleneck Shortest Paths

Consider a weighted directed graph G = (V, E) where the weight w(u → v) on any edge u → v is a positive integer. Let the bottleneck length of a path be the weight of the edge in the path with the greatest weight. Let the bottleneck distance b(v, w) from v to w be the minimum of the bottleneck lengths of all paths from v to w. For a given source vertex s, design a single-source shortest bottleneck path algorithm, i.e. one that computes b(s, v) for all v. Be sure to justify the correctness of your algorithm, give a pseudo-code formulation of it, and state and justify its worst-case time complexity.

Design/Correctness: The primary observation is that for any given distance d, all vertices of distance d or less from s are exactly those reachable from s along the subgraph of all edges of weight not greater than d. Thus we seek to compute the distance to vertices in increasing order of distance and clearly it suffice to examine the weights of the edges as every distance will correspond to one of these E values. We maintain a heap such that at the start of each iteration the following inductive invariant is true: If the smallest edge in the heap has weight d, then every vertex of distance less than d has been found, and all the edges of weight d-or-more whose source is one of these vertices is in the heap. In an iteration of the algorithm, we extract the next minimum edge, determine all the vertices that can be reached through that edge along edges of equal or lesser score, and update the induction.

procedure Search(v: vertex, d: weight) { D[v] ← d for v → u ∈ E do if w(v → u) ≤ d and D[u] = ∞ then Search(u,d) else Add v → u to Heap }

D[v] ← ∞ ∀v D[s] ← 0 Heap ← {s → v} while Heap 6 = ∅ do { e(≡ v → u) ← extract min Heap if D[u] = ∞ then Search(u, w(e)) }

Complexity: An edge is added to the heap only if its source has just been assigned its distance (if then). Therefore each edge is added to the heap at most once. Moreover, Search is only called when a vertex is about to be assigned its and so is not called more than V times. The algorithm is thus O(E log V ) in the worst case.

5. NP-Completeness

Consider an undirected graph G = (V, E). A k-coloring of the graph is an assignment of k distinct colors to the vertices of the graph in such a way that no two vertices assigned the same color are adjacent (i.e. have an edge between them). The GRAPH-COLORABILITY problem is to find a coloring involving the fewest colors.

(a) State precisely the decision version of the GRAPH-COLORABILITY problem. Given an undirected graph G and integer k > 0, can the graph be colored with just k colors?

(b) Show that it is in NP.

One can guess a coloring, by producing a list of the vertices and its associated color assignment. This guess, or verification certificate, is certainly polynomial in the size of G. Verification simply entails checking that only k colors are assigned, and examing every edge of the graph and checking to see if the colors assigned to the vertices at each end are distinct. This is easily accomplished in O(V + E) time.

(c) Next we show that GRAPH-COLORABILITY is NP-hard by giving a polynomial reduc- tion from 3-SAT (i.e. is a given 3-CNF formula satisfiable) to GRAPH-COLORABILITY. Suppose you are given a formula with m clauses Ci and n variables xi. Construct a graph with 3n + m vertices v 1 , v 2 ,... vn, x 1 , x 2 ,... xn, ¯x 1 , x¯ 2 ,... x¯n, and c 1 , c 2 ,... , cm. Add exactly the following edges:

(a) (vi, vj ) for all i 6 = j. (b) (vi, xj ) and (vi, x¯j ) for all i 6 = j. (c) (xi, x¯i) for all i. (d) (xi, cj ) if xi is not a term in clause Cj , and (¯xi, cj ) if ¯xi is not a term in clause Cj.

Prove that for n ≥ 4, the constructed graph is n + 1 colorable if and only if the 3-CNF from which it is constructed is satisfiable. (Hint: The vi vertices form a complete graph or clique of n vertices and so must each be assigned a distinct color. Think about what to do with your one extra color.)

First suppose that the 3-SAT formula is satisfiable. We show that we can color the constructed graph with n + 1 colors. Assign a distinct color to each of the vi. For each variable xi, if it is true, then assign vertex xi the same color as vi and assign ¯xi the n + 1st^ color, which we call the special color. Switch the assignment of colors if xi is false. Since the formula is satisfiable, for each clause Cj there is at least one literal that is true and the corresponding vertex was assigned a color other than the special color. Color vertex cj with that color. cj is not adjacent to that vertex, and no other literal vertex has the color. Thus we have produced an n + 1 coloring.

Now suppose that there is an (n + 1) coloring of the constructed graph. Each of the vi must be assigned a unique color as their subgraph is a clique. The vertices xi and x¯i cannot have the same color, nor the color of any vj except for vi. Therefore one has the color of vi and the other has the (n + 1)st^ color, which we call the special color. If xi has the color of vi, set xi to true, otherwise set it to false. It remains only to show that each clause is satisfied by this assignment. If Cj were false, then cj is connected to three x vertices, corresponding to the complement of the literals in the clause, that do not have the special color, and to 2(n − 3) pairs of x vertices corresponding to the n − 3 variables not in the clause, of which n − 3 are non-special and at least one is the special color as n ≥ 4. Thus Cj is adjacent to n + 1 colors and would have to use an additional color. Therefore Cj must be true.