Alg Problemsdfds
Alg Problemsdfds
2012 2 16
1
1.1 True or False?
True or false? Consider an instance of the Stable Matching Problem in which there exits
a man m and a woman w such that m is ranked first on the preference list of w and w is
ranked first on the preference list of m. Then in every stable matching S for this instance,
the pair (m, w) belongs to S.
S(m, w) S.
w m , (m, w ) S, (m , w) S. m, w
w , w, m m , S S
S,(m, w) S.
think the following statement is true or false. If it is true, give an algorithm. If it is false,
give a counterexample.
True or false? For every set of TV shows and scores, there is always a stable pair of
schedules.
n = 2 A a1 , a2 , B
b1 , b2 , a1 > b1 > a2 > b2 .
a1 , b1
a2 , b2 B B
b1 a2 b2 a1 B
a1 b2 a2 b1 A
G-S
2. Delete-Larger-Half
9.3
S/2
O(n).
HH
7
+
6
+
5
+
3
2
+
6
The price in Day 5 is the highest in Days 5, 4, 3, 2, but not highest one in Days 5, 4, 3, 2, 1,
thus HH(5) = 4;
The price in Day 4 is the highest in Days 4, 3, 2, but not highest one in Days 4, 3, 2, 1,
thus HH(4) = 3;
The price in Day 3 is the highest in Day 3, but not highest one in Days 3, 2, thus HH(3) =
1;
The input in this example is 8, 4, 3, 5, 6, 2, the answer is 1, 1, 1, 3, 4, 1.
Given the prices of n days, please give an algorithm of O(n) time complexity to calculate
the HH-index of all days.
( O(n))
while do
Q
S
k1 = |S|; k2 = |Q|;
while S = do
t = s. pop();
HH[t] = k;
while Q = and v[Q. top()] > v[t] do
tq = Q. pop();
HH[ta ] = 1;
end while
HH[t]+ = |Q| + 1;
end while
while Q = do
tq = Q. pop();
HH[tq ] = 1;
end while
end while
2 NP
2.1 NP-completeness
The subgraph-isomorphism problem takes two graphs G1 and G2 and asks whether
G1 is isomorphic to a subgraph of G2 . Show that the subgragh-isomorphism problem is
NP-complete.
Proof. First we prove that the Subgraph Isomorphism problem is in NP. The certificate is
(G1 = (V1 , E1 ), G2 = (V2 , E2 ), V1 V2 ). The verifying algorithm checks if is a oneto-one function, and for all u, v V1 whether (u, v) E1 if and only if ((u), (v)) E2 .
Secondly, we prove that CIQUE P Subgragh Isomorphism. Let (G = (V, E), k) be
an input instance for CLIQUE. Define G1 to be the complete graph on k vertices, and G2
to be the grapgh G. Then (G1 , G2 ) Subgragh Isomorphism if and only if (G, k)
CLIQUE.
2.2 NP-completeness
Given an integer m n matrix A and an integer m-vector b, the 0-1 Integer Programming problem asks whether there is an integer n-vector x with elements in the set {0, 1}
such that Ax b. Prove that 0-1 Integer Programming is NP-complete.
Proof. First we show that 0-1 Integer-Programming is in NP. The certificate is the nvector x with elements in the set 0, 1. Given the certificate, it can be verified in linear time
(hence, polynomial time) that it satisfies Ax b.
3SAT P 0-1 Integer-Programming
Goal:
Next we want to show that 3SAT is polynomially reducible to the 0-1 Integer-Programming
(IP). That is, we want a polynomial time computable function, which given an instance of
3SAT (a set of variables Y and clauses C) outputs an instance of IP (an m-by-n matrix A and
a m-vector b) such that the clauses C are satisfiable if and only if there is an integer n-vector
x with elements in the set {0, 1} which satisfies Ax b.
Transformation:
For each clause C we write out an inequality as follows. If the literal is variable yi , we replace it by the variable xi ; and if the literal is yi , we replace it by the quantity 1 xi . In what
follows, we will call this quantity that replaces the literal a term. Finally, the sum of these
terms is set 1. For example, if the 3SAT instance consists of two clauses y1 y2 y3 and
y1 y3 y4 , then the inequalities are x1 + (1 x2 ) + (1 x3 ) 1 and (1 x1 ) + x3 + x4 1.
One can obtain the matrix A and vector b from these inequalities quite easily. The entire
6
2.3 NP-completeness
The set-partition problem takes as input a set S of numbers. The question is whether
the numbers can be partitioned into two sets A and B and B = S A such that xA x =
xB x. Show that the set-partition problem is NP-complete.
Proof. First we show that Set-partition is in NP. The certificate for the Set-partition
problem consists of the two sublists S1 and S2 . Given the sublists, in polynomial time we
can compute the sums of the elements in these lists and verify that they are equal. Subset
Sum P set-partition
Goal:
Next we want to show that SubsetSum (SS) is polynomially reducible to the set-partition
problem (PART). That is, we want a polynomial time computable function f , which given
an instance of SS (a set of numbers S = {x1 , x2 , . . . , xn } and a target value t) outputs an
instance of PART (a set of numbers S = {x1 , x2 , . . . , xn }) such that S has a subset summing
to t if and only if S can be partitioned into subsets S1 and S2 that sum to the same value.
Transformation: Observe that the Set-partition problem is a special case of the subset sum
problem where we are trying to find a set of numbers S1 that sum to half the total sum of the
7
xi .
i=1
If t = T /2 then the subset sum problem is an instance of the partition problem, and we are
done. If not, then the reduction will create a new number, which if added to any subset that
sums to t, will now cause that set to sum to half the elements of the total. The problem is that
when we add this new element, we change the total as well, so this must be done carefully.
We may assume that t T /2, since otherwise the subset sum problem is equivalent to
searching for a subset of size T t, and then taking the complement. Create a new element
x0 = T 2t, and call partition on this modified set. Let S be S together with this new
element: S = S {x}. Clearly the transformation can be done in polynomial time.
Correctness: To see why this works, observe that the sum of elements in S is T + T 2t =
2(T t). If there is a solution to the subset sum problem, then by adding in the element
x0 we get a collection of elements that sums to t + (T 2t) = T t, but this is one half
of the total 2(T t), and hence is a solution to the Set-partition problem. Conversely,
if there is a solution to this Set-partition problem, then one of the halves of the partition
contains the element x0, and the remaining elements in this half of the partition must sum to
(T t) (T 2t) = t. Thus these elements (without x0 ) form a solution to the subset sum
problem.
2.4 NP-completeness
In the Half-3SAT problem, we are given a 3SAT formula with n variables and m
clauses, where m is even. We wish to determine whether there exists a truth assignment to
the variables of such that exactly half the clauses evaluate to 0 and exactly half the clauses
evaluate to 1. Prove that the Half-3SATproblem is NP-complete.
Proof. In order to prove that the Half-3SAT problem is NP-Complete, we need to prove
two things:
1. Half 3SAT NP,
2. L NP, L P Half-3SAT, or Half-3SAT NP-Hard
In order to prove that Half-3SAT 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 algorithm which takes an instance of Half-3SAT 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.
8
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-3SAT is NP-Hard, we can consider a reduction from a known
NP-Complete problem to Half-3SAT (or prove it directly, which we won
t even consider).
We choose to reduce from 3SAT to Half-3SAT. Because Circuit-SAT P SAT P 3SAT,
if we are able to show 3SAT P Half 3SAT, then this implies that Circuit-SAT P Half
3-SAT. And since L P Circuit-SAT, L NP, then this implies that L P Half 3SAT,
L NP or that Half 3SAT NP-hard.
Consider the following reduction. First, we need to convert an instance of 3SAT to Half
3SAT. 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)
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)
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. Clearly, this conversion takes polynomial time because
we have only added 3 variables, and 3m clauses.
We also need to show that there exists a yes instance of 3SAT if and only if there exists
a yes instance of Half 3SAT.
= that there exists a truth assignment which causes to be true. Then, the m clauses
which correspond to in are true and there are m clauses which are always true.
Thus, simply let p and r be false, and there exists a truth assignment which satisfies
Half 3SAT, where half the clauses are true and half are false.
= that there exists a truth assignment which causes Half 3SAT to be satisfied, or
a truth assignment that causes half the clauses in to be true and half false. But m
clauses in are always true, which means that the 2m clauses cannot be true if Half
3SAT is satisfied (because then 3m clauses would be true which is more than half).
Thus, the 2m clauses must be false, which means that is true, which means that
3SAT is also satisfied.
Thus, we have shown that a yes instance of 3SAT produces a yes instance of Half 3SAT,
and vice versa, which concludes our proof.
9
2.5 NP-completeness
Show that Undirected-Hamilton-Cycle problem is NP-complete.
Undirected-Hamilton-Cycle(decision problem):
Input: an undirected gragh G
Output: 1 if G has a Hamilton cycle which visits each vertex exactly once and also returns
to the starting vertex, 0 for others.
NP NP-Hard Hamilton
HP, Hamilton HC, Hamilton DHC, Hamilton
DHP.
HP P HC. , HP I: G u0 , v0 V (G),
HC f (I) : G = G + v0 u0 . G Hamilton P (u0 , v0 )
G Hamilton
DHC P DHP ,
DHP P HP. G va , vb V (G ), DHP
, G G
vi V (G ), G vi1 , vi2 , vi3 ,
vi1 vi2 , vi2 vi3 . vi vj E(G ), G vi3 vj1 .f (I) G va1 , vb3
G va1 vb3 Hamilton G
va vb Hamilton G va vb Hamilton
G va1
vb3 Hamilton P (va1 , vb3 ) G Hamilton
va1 P (va1 , vb3 ) P (va1 , vb3 ) vi1 vi2 vi3 2 vi ,
G va vb Hamilton
DHC P DHP P HP P HC, DHC NPC
HC NPC 2
2.6 NP-completeness
In the Half-Clique problem, we are given a graph G with n nodes, where n is even.
We wish to determine whether there exists a subgragh of G with n/2 nodes that is a clique.
Show that the Half-Clique problem is NP-complete.
1. (Half-Clique) NP
H G f , H
n/2, v1 , v2 V (H), (f (v1 ), f (v2 )) E(G).
2. CLIQUE P Half-Clique. G1 , k
k |G|/2
G j = 2k |G| G . G
10
G k
k < |G|/2
G m = |G| 2k G
G . G 2|G| 2k G |G| k
G k + |G| 2k = |G| k
11
3
3.1 Divide and Conquer
You are interested in analyzing some hard-to-obtain data from two separate databases.
Each database contains n numerical values-so there are 2n values total-and you may assume
that no two values are the same. Youd like to determine the median of this set of 2n values,
which we will define here to be the nth smallest value.
However, the only way you can access these values is through queries to the databases.
In a single query, you can specify a value k to one of the two databases, and the chosen
database will return the k th smallest value that it contains. Since queries are expensive, you
would like to compute the median using as few queries as possible.
Give an algorithm that finds the median value using at most O(logn) queries.
k ak , k bk .
f (k) = ak bnk+1
. f (k) k f (1) 0 a1 bn
n bn . f (n) 0, n an . k
f (k) 0, f (k + 1) 0.
a1 a2 . . . ak bnk+1 ,
b1 b2 . . . bnk ak+1 .
ak+1 bnk+1
A = {a1 , a2 , . . . , ak , b1 , b2 , . . . , bnk } |A| = n.
n A max A = max{ak , bnk }.
k, n
FindNthSmallest(a[1..n], b[1..])
i = 1, j = n;
if a[1] b[n] then
return b[n];
end if
if a[n] b[1] then
return a[n];
end if
while j i > 1 do
12
k = j+i
;
2
temp = a[k];
t = temp b[n k + 1];
if t > 0 then
j = k;
else if t < 0 then
i = k;
else
return temp;
end if
end while
return max{a[k], b[n k]};
Merge-and-Count (L, R)
1: InverseCount = 0;
2: i = 1; j = 1;
3: for k = l to r do
4:
5:
6:
7:
8:
13
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
i + +;
end if
end for
i = 1, j = 1;
for k = Ltor do
if L[i] > 3 R[j] then
InverseCount+ = length(L) i + 1;
+ + j;
else
+ + i;
end if
end for
return InverseCount and A;
FindLocalMin(T )
1: vl = probe(T.leftchild());
2: vr = probe(T.rightchild());
3: v = probe(T.root())
4: if v < v1 then
5:
6:
7:
8:
9:
10:
11:
if v < vr then
return T.root();
else
FindLocalMin(T.rightchild());
end if
else
if vl < vr then
14
12:
13:
14:
15:
16:
FindLocalMin(T.leftchild());
else
FindLocalMin(T.rightchild());
end if
end if
probe()
Catalan
Now you want to decide whether there is a set of more than n/2 of these coins that are made
of the same material.
(a) Give an algorithm using O(n log n) time to solve this problem.
(b) Give an algorithm using O(n) time and O(1) space to solve this problem.
sorry! I have an algorithm but fail to give a proof. So Im not sure whether the algorithm
is correct. Under this circumstance, I decide not to give the solution of this problem and am
hoping someone else will give a satisfying answer. Im looking forward for your answer.
16
4
4.1 Dynamic Programming
Professor Stewart is consulting for the president of a corporation that is planning a company party. The company has a hierarchical structure; that is, the supervisor relation forms
a tree rooted at the president. The personnel office has ranked each employee with a conviviality rating, which is a real number. In order to make the party fun for all attendees, the
president does not want both an employee and his or her immediate supervisor to attend.
Professor Stewart is given the tree that describes the structure of the corporation, using
the left-child, right-sibling representation. Each node of the tree holds, in addition to the
pointers, the name of an employee and that employees conviviality ranking. Describe an
algorithm to make up a guest list that maximizes the sum of the conviviality ratings of the
guests. Analyze the running time of your algorithm.
f( p),
f1 (m) = conv(m) +
f0 (m) = 0 +
ppost(m)
ppost(m)
O(n), f1 , f2 , f1
f0 f1 2n 1,
f0 n 1 n 1
O(n).
dered by deadline. We then recurse on P (i, j), the maximum profit we can make using
jobs 1, 2, . . . , i in time j. The relation is:
0,
if i = 0,
P (i 1, j),
else.
Running Time: Since all processing times are no more than n, we need only compute j
to n2 . Therefore, P is size O(n3 ). Filling in each square of P requires O(1) time for a total
running time of O(n3 ). Note that the O(n log n) sorting cost has been absorbed.
Now please give an efficient algorithm that takes values for x1 , x2 , . . . , xn and s1 , s2 , ..., sn
and returns the total number of terabytes processed by an optimal solution.
0 n + 1
f [i, j]
i 1 j + 1 i j
f [1, n] f [1, n]
min{s1 , xi },
i = j,
f [i, j] = 0,
j < i,
max
{f [i, k 1] + f [k + 1, j], g[i, j]}, else.
ikj
g[i, j] i 1 i j i
j
O(n3 ).
F (i, j) i
j n 1
F (n, 1);
F (0, j) = 0 j
ai Ci , k,
Ck = 1 +
max
i{i|0i<k,ai <ak }
19
Ci .
a1 , a2 , . . . , an
O(n2 ).
Give an algorithm that determines the minimum number of rounds needed for everyone
to be notified, and outputs a sequence of phone calls that achieves this minimum number of
rounds.
r(a) a
r 0.
x r x
r x r
n1 n2 . . . , nk , r(x) = max1jk {nj + j}.
20
5
5.1 True or False
Decide whether you think the following statement is true or false. If it is true, give a
short explanation. If it is false, give a counterexample.
True or false? Let G be an arbitrary connected, undirected graph with a distinct cost c(e)
on every edge e. Suppose e is the cheapest edge in G; that is, c(e) < c(e) for every edge
e = e. Then there is a minimum spanning tree T of G that contains the edge e.
e T ,
c(T ) e e
A, B T A, B T
T e . A, B
T
c(T ) < c(T ). T
e .
G
G
8+1
n 1
O(n).
isGraphsDegrees(d[1..n])
n
1: p =
i=1 d[i];
2: if p > n(n 1)/2 or p is odd then
21
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
return FALSE;
end if
Sort d[1..n] in ascending order;
while n > 1 and d[n] = 0 do
k = n d[n];
if k < 1 then
return TRUE
end if
for i = k to n 1 do
d[i] = d[i 1];
if d[i] < 0 then
return FALSE;
end if
end for
n = n 1;
if k > 2 and d[k] < d[k 1] then
d = merge(d[1..k 1], d[k..n]);
end if
end while
if d[1] = 0 then
return FALSE;
else
return TRUE;
end if
G d1 , d2 , . . . , dn G ,
d1 , d2 , . . . , dk 1, dk+1 1, . . . , dn1 1, k n dn .
) G , vi
d ,
1 i k;
i
di =
d 1, k i n 1.
i
k = n dn . vn , vn vn vk , vn vk+1 , . . . , vn vn1 dn
d(vi ) = di , i = 1, 2, . . . , n.
) G d(vi ) = di , i = 1, 2, . . . , n. d1 < d2 < < dn . vn
vk , vk+1 , . . . , vn1 , {vk , vk+1 , . . . , vn1 }
vn G1 .
A = {v {vk , vk+1 , . . . , vn1 }|vvn }. A v, d(v) 1.
22
v , vv , vn vv E(G1 ) vn v , v v .
A
G1 .
dG1 (vn ) = 0; |A| = E(G1 )
dG1 (v) = dG (v) 1, v {vk , vk+1 , . . . , vn1 };
dG1 (v) = dG (v), v {v1 , v2 , . . . , vk1 };
G = G1 vn . G
while
while d1 , d2 , . . . , dn , while
d1 , d2 , . . . , dk 1, dk+1 1, . . . , dn1 1. d1 , d2 , . . . , dn1 1
d1 , d2 , . . . , dn
while d[1] = 0
d[1] = 0 G = ({v}, )
1. ni=1 di n 1
2. d1 , d2 , . . . , dn O(n log n)
3. while n 1 k while for k 1
for
Note that this constraint is only imposed for time intervals that start at 0, not for time
intervals that start at any other value.
Give an algorithm that takes a set of n streams, each specified by its number of bits bi
and its time duration ti , as well as the link parameter r, and determines whether there exists
a valid schedule. The running time of your algorithm should be polynomial in n.
bi /ti
i
b1 /t1 , b2 /t2 , . . . , bn /tn t0 = 0, Ti =
j=0 tj ; f (t) =
t
bi /ti , t [Ti1 , Ti ); F (t) = 0 f ()d. F (t) 0 t
f (t) F (t) t [0, Tn ]
Tn
n
f (t)dt
bi
i=0
F (t) n
t
t= 0
Tn
i=1 ti
i
i=0
i=1 r, rt
To show that there is always an optimal solution with no cross matching, let S be an
optimal matching. If S has no cross matches, we are done. Otherwise, consider two skiers
and two skies in a cross match, and reverse their matching so that the shorter of the two skiers
has the shorter of the two skis. Using the cases above one can show that this change cannot
increase the cost of the match, to the revised solution is at least as good as S.
Simply sort both the skiers and the skis by height, and match the i-th skier with the ith pair of skis. This algorithm is optimal because another assignment would have a cross
match, hence could not have a better cost. The running time of the algorithms is just the time
it takes to sort the two list, O(n log n).
(word):
G. G i i
i + 1
i + 1
i + 1 i + 1
i + 2
O(n),n
25
6
6.1 Prime and Dual
Suppose that we are given a linear program L in standard form, and suppose that for both
L and the dual of L, the basic solutions associated with the initial slack forms are feasible.
Show that the optimal objective value of L is 0.
max f = cT x,
s.t.Ax b;
I s
max f = cT x + 0s
( )
(
) x
s.t. A I
= b.
s
s = b, x = 0, f = cT x = 0.
min f = bT y,
s.t.AT y c;
y = 0 f = bT x = 0.
f = f f = 0.
x x LP
26
x LP
x x
x
(a)
(b) LPDLP
27
Time Period
Staff Needed
5:009:00
9:0013:00
13:0017:00
17:0021:00
21:001:00
1:005:00
S1
S2
S3
S4
S5
S6
Time Period
0:008:00
8:0016:00
16:0024:00
C1
C2
C3
All staff works in 8-hour-shifts, which means every person will do continuous work for 8
hours. There are six possible shifts that start on the hour in the beginning of each 4-hour
period in the table. Now if the railway station want to minimize the total salary paid in one
day, please formulate this problem as an integer linear programming problem.
5:00 4 5:009:00 1
9:0013:00 2
i yi i xi
8 i xi2 6
yi = yi1 xi2 + xi Si , i = 1, 2, . . . , 6.
0:008:00
C1 , 0:001:00
y5
1:00-5:00
y6
5:00-8:00
y1
w1 = C1 y5 + 4C1 y6 + 3C1 y1 ;
8:0016:00
123
28
w = w1 + w2 + w3 .
min w = w1 + w2 + w3 ,
yi = yi1 xi2 + xi Si , i = 1, 2, . . . , 6,
w2 = C1 (y5 + 4y6 + 3y1 ),
w2 = C2 (y1 + 4y2 + 3y3 ),
w3 = C3 (y3 + 4y4 + 3y5 ),
xi
yi .i = 1, 2, . . . , 6.
Ck
1, k ,
Ck =
0, k .
0-1
min
Ck ,
i=1
m (x + xjk ) mij Ck ,
ij ik
s.t. Ck {0, 1},
For example, the original target can be modified to finding the minimum number of paths so
that youre able to visit all nodes when you go through all these paths. The beginning and
end of the paths are not restricted. In the following instance, the minimum number of paths
is 2, and the paths are colored red.
Try to formulate this problem as a linear programming problem or an integer linear programming problem (see the definition in Problem 4) or a 0-1 linear programming problem
(see the definition in Problem 5).
1
30
7.1 1
31
7
7.1 Problem Reduction
In the Network Flow Problem, we add a group of constraints: for every node i, there has
a capacity limitation ki , the flow over the ith node cannot be greater than ki . Please reduce
the problem to the traditional Network Flow Problem.
P = (V, E, k, c, s, t). V E k : V
R+ , k(i) = ki , i V ; c : E R+ s V t V
V = {s, t, v1 , v2 , . . . , vm }, V = {s , t , v1 , v2 , . . . , vm
}, V = V V . (v, v )
E , v V . (u, v) E, (u , v) E
c : E R+
P = (V , E , c , s, t)
P c v V
k(v). u, v v V , P
P
7.2
If each vertex of a graph G carries a positive weight, then the weight of a set of S of
vertices is defined as the sum of weights of all the vertices in S and the optimal cover problem
requires finding a cover of the smallest weight. Show that this problem can be turned into
the minimum cut problem whenever G is bipartie.
G V1 , V1 G
w(V1 )
V (G) = X Y . X
+, uv E(G),
c(u, v) = w(v), u = s,
w(u), v = t.
G (S, T ) G
u S, v T , (u, v) E(G ). c(S, T ) = +.
32
({s}, V (G ) s)
G A V (G)
c(s, v) +
uS
vT
c(s, v) +
vT X
c(u, t)
c(u, t)
uS Y
B = (T X) (S Y )
c(S , T ) = w(B)
B G e = uv E(G), u T X, e
u
/ T X u X. u X
(T X), v
/ S Y , (u, v) E(G ) u S , v T .
c(S , T ) = + c(S , T ) < c(S, T ) v S Y . e E(G). e B
c(S , T ) = w(B) < c(S, T ) = w(A), A G (S, T )
G
(a) The matrix
0 1 0
0 1 0
1 0 1
is not rerangeable.
(b)
V = {v1 , v2 , . . . , vn , v11 , v22 , . . . , vnn } {s, t}
E0 = ni=1 {(s, vi )},
E1 = ni=1 {(vi , vkk )|mik = 1},
E2 = ni=1 {(vii , t)},
E = E0 E1 E2 ,
1,
e E0 E2 ,
c(e) =
+, e E
1
this is the question: Which of the remaining applications, if any, should be moved? Give a
polynomial-time algorithm to find a set S {2, 3, . . . , n} for which the sum of the benefits
minus the expense of moving the applications in S to the new system is maximized.
Hoping others to give a solution...
35
Thus
|V ||E|
iV2
|E| +
(|E| dj + 2) +
jV1
|V ||E| + 2|V1 |
jV1
(dj
ci,j
iV2 ,jV1
ci,j )
iV2
0 2|V1 | 2e(V1 )
e(v1 )
.
|V1 |
On the other hand, if V1 = , we know that the inequality above is reversed, so we know
e(V1 )/|V 1| for any set V1 V .
Why should we have expected this network to solve the problem? The symmetries of
the problem imply that all nodes should be connected to the source and to the sink, and
that the only pieces of information that we can use to distinguish the weights of these links
must be the degrees of the nodes. We wish the procedure to isolate vertices that are highly
connected on one side, and ill connected on the other side. Hence by making the links to
the sink depend on the difference between 2 and the degree, we encourage nodes of high
degree to connect to the source, and nodes of low degree to connect to the sink. We add a
constant to the edges to the source and the edges to the sink to ensure all edges have positive
weight. And the connectivity of vertices inside the graph ensures that nodes of moderate
degree connected to high degree nodes also segregate into the part of the partition with their
high degree neighbors.
36
8
8.1 Approximation Algorithm
Consider the following algorithm for (unweighted) Vertex Cover: In each connected
component of the input graph execute a depth first search (DFS). Output the nodes that are
not the leaves of the DFS tree. Show that the output is indeed a vertex cover, and that it
approximates the minimum vertex cover within a factor of 2.
1 G
G G i Ci ,
634
APPROX-VERTEX-COVER(G)
1: C ;
2: E E(G);
3: while E = do
4:
5:
6:
7:
8:
C. |C|/|C | 2. 4 DFS
(u, v) E , A C, |A| |C|. |A|/|C | |C|/|C | 2.
8.2
Apptoximation Algorithm
Given a graph G = V, E with edge costs and set T V of terminal vertices, the Steiner
Tree Problem is to find a minimum cost tree in G containing every vertex in T (vertices in
V T may or may not be used in T ).
(a)Give a 2-approximation algorithm if the edge costs satisfy the triangle inequality.
(b)Give a 2-approximation algorithm for general edge costs (The graph also need not be
37
complete).
Solution: (a) We call the restricted (by the triangle inequality) problem metric Steiner tree
problem
Let R denote the set of required vertices. Clearly, a minimum spanning tree (MST) on R
is a feasible solution for this problem. We will prove that the cost of an MST on R is within
2 OPT.
Consider a Steiner tree of cost OPT. By doubling its edges we obtain an Eulerian graph
connecting all vertices of R and, possibly, some Steiner vertices. Find an Euler tour of this
graph, for example by traversing the vertices in DFS (depth first search) order:
The cost of this Euler tour is 2 OPT. Next obtain a Hmiltom cycle on the vertices of
R by traversing the Euler tour and short-cutting Steiner vertices and previously visited
vertices of R:
38
Because of triangle inequality, the shortcuts do not increase the cost of the tour. If we
delete one edge of this Hamiltonian cycle, we obtain a path that spans R and has cost at most
2 OPT. This path is else a spanning tree on R. Hence, the MST on R has cost at most
2 OPT.
(b)Now Ill prove that there is an approximation factor preserving reduction from the
Steiner tree problem to the metric Steiner tree problem.
We will transform, in polynomial time, an instance I of the Steiner tree problem, consisting of graph G = (V, E), to an instance I of the metric Steiner tree problem as follows.
Let G be the complete undirected graph on vertex set V . Define the cost of edge (u, v) in
G to be the cost of a shortest u v path in G. G is called the metric closure of G. The
partition of V into required and Steiner vertices in I is the same as in I.
For any edge (u, v) E, its cost in G is no more than its cost in G. Therefore, the cost
of an optimal solution in I dose not exceed the cost of an optimal solution in I.
Next, given a Steiner tree T in I , we will show how to obtain, in polynomial time, a
Steiner tree T in I of at most the same cost. The cost of an edge (u, v) in G corresponds
to the cost of a path in G. Replace each edge of T by the corresponding path to obtain a
subgraph of G. Clearly, in this subgraph, all the required vertices are connected. However,
this subgraph may, in general, contain cycles. If so, remove edges to obtain tree T This
completes the approximation factor preserving reduction.
39
M = M {t};
T = T {tand all its neighbors in T };
end while;
ai
ai S
You would like to select a feasible subset S of A whose total sum is as large as possible.
Example: If A = {8, 2, 4} and B = 11, then the optimal solution is the subset S = {8, 2}
Give a polynomial-time approximation algorithm for this problem with the following
guarantee: It returns a feasible set S A whose total sum is at least half as large as the
maximum total sum of any feasible set S A. Your algorithm should have a running tim
of most O(n log n).
40
x A
B x B/2, B/2 x1 , x2 , . . . , xn ,
B/2
3
2
for
(a)
Bin b H, Bin c. b H
for i=0 to n do
t = H
if ai H Bin b then
ai b ;
c(b) = c(b) ai ;
else
Bin t ai t
c(t) = 1 ai ;
t H
end if
end for
1/2
1/2,