Tải bản đầy đủ (.pdf) (41 trang)

alg problems

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (607.54 KB, 41 trang )

算法设计与分析课后作业
elie*
2012 年 2 月 16

*



1


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.

1.2 True or False?
True or false? In the stable matching S generated by G-S algorithm, each woman is
paired with her worst valid partner.
命题为真。 设 w1 的 worst valid partner 是 m1 . 假设 G-S 算法产生的稳定匹配使 w1
不与 m1 相配。不妨设 w1 与 m2 相配。那么 w1 更喜欢 m2 而非 m1(因为 m1 是 w1 的
wosrt valid patner)。设另一匹配 S 中 w1 与 m1 相配。m2 与 w2 相配。在 S ∗ 中 m2 的配
偶 w1 是 m2 的 best valid patner. 故 m2 更喜欢 w1 而非 w2 , 又 w1 更喜欢 m2 而非 m1 . 从而
w1 − m2 不稳定。


1.3 True or False
Suppose we have two television stations, A and B. There are n prime-time programming
slots, and each station has n TV shows. Each station wants to assign each show to a distinct
slot so as to attract as much market share as possible. Here is the way we determine how
well the two stations perform relative to each other, given their schedules. Each show has a
fixed score. We’ll assume that no two shows have exactly the same score. A station wins a
given time slot if the show that it schedules for the time slot has a higher score than the show
the other station schedules for that time slot. The goal of each station is to win as many time
slots as possible.
Suppose Station A reveals a schedule S and Station B reveals a schedule T . We’ll say
that the pair of schedule (S, T ) is stable if neither station can unilaterally change its own
schedule and win more time slots. That is, there is no schedule S ′ such that Station A wins
more slots with the pair (S ′ , T ) than it did with the pair (S, T ); and symmetrically, there is
no schedule T ′ such that Station B wins more slots with the pair (S, T ). Decide whether you

2


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 以改变策略获得更多的播出时
间。


1.4 Stable Matching
PSL is a shipping company that owns n ships and provide service to n ports. Each ship
has a schedule that says, for each day in some m(m > n) day period, whether the ship is at
sea or staying at one port. During the period, each ship visits each port once, and each visit
lasts exactly one day. For safety reasons, PSL has the following strict requirement:
(†) No two ships can be in the same port on the same day.
The company wants to perform maintenance on all the ships in the following m day
period. In this case, each ship Si will arrive in one scheduled port and simply remains there
for the rest of the period, ignoring the rest of its schedule. Now the question is: Given
the schedule for each ship, find when each ship should stop its travel so that condition (†)
continues to hold. Try to give an algorithm to find the solution.
此问题类似于稳定婚姻问题。只需将船类比成稳定婚姻问题中的男士,码头类比
成女士,
然后套用 G-S 算法。

1.5 Complexity Analysis
Design a data structure to support the following two operations for a set S of integers:
Insert(S, x) inserts x into set S;
Delete-Larger-Half(S) deletes the largest ⌊S/2⌋ elements from S.
Explain how to implement this data structure so that any sequence of m operations runs
in O(m) time.
将数据结构设计为一个动态数组 S.
1. Insert(插入操作)
每次向数组插入一个元素。当数组存满时,若执行插入操作,则另外申请加倍的
内存空间,并将原空间的数据拷贝到新申请的空间中,
删除原内存空间。
3


2. Delete-Larger-Half(删除操作)

忽略对删除操作前数据为空的判断时间。每次删除操作分两步:
• 获取数组中所有元素的中位数(可以采用《算法导论》
(第二版)9.3 节最坏
情况线性时间获取线性表中位数的算法)。
• 删除数组中最大的 ⌊S/2⌋ 个元素,只需扫描整个数组,如果扫描过程中当前
元素小于中位数,则添加到数组的前半部分。如果当前元素大于或等于中
位数,
则删除这个元素。
可以用平摊分析法,得出任何次序的插入和删除操作的时间复杂度是 O(n).

1.6 Complexity Analysis
In stock market, HH-index(historically highest) of the current price is k means that current price is the highest price in the previous k days, but not the highest one in the previous
k + 1 days.
For example, the price changes as showed in the following figure.
8+

HH

+

7
+

6
+

5
+

4


+

3
2

1

2

3

4

5

+
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.

4



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

5


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 y¯i , 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 ∨ y¯2 ∨ y¯3 and
y¯1 ∨ 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


transformation can be done in linear time (hence polynomial time).
Correctness:

If the clauses are satisfiable then consider the satisfying truth assignment. For each variable
yi , if yi is true, then the corresponding variable xi is assigned value 1, and if yi is false, then
the corresponding variable xi is assigned value 0. Consider any clause c ∈ C. Clearly, the
satisfying truth assignment makes at least one literal in the clause c true. If this literal is the
variable yi then xi has value 1 and the inequality corresponding to clause c is satisfied; and
if this literal is yi then xi has value 0, and so 1 − xi has value 1 and again the inequality
corresponding to clause c is satisfied. Since the argument applies to any clause c, it implies
that all the inequalities are satisfied by the above assignment. Conversely, if all the inequalities can be satisfied then consider the assignment of values to the variables xi that satisfies
the inequalities. For each variable xi , if xi is 1, then the corresponding variable yi is set to
true, and if xi is 0, then the corresponding variable yi is set to false. Consider any inequality
and the corresponding clause c ∈ C. Clearly, if the inequality is satisfied then one of the
three terms must be ≥ 1 (since all the terms are ≥ 0). If the term is of the form xi then
it implies that xi is 1 and the corresponding literal yi is true; and if the term is of the form
1 − xi then it implies that xi is 0 and the corresponding literal yi is true. In either case, the
clause c is satisfied. Our argument applies to any arbitrary clause c, hence the given clauses
are satisfiable.

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 Σx∈A x =
Σx∈B 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 ′ = {x′1 , x′2 , . . . , x′n }) 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


whole set. Let T be the sum of all the numbers in S.
T =

n


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.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. You’d 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 − bn−k+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 ≤ bn−k+1 ,
b1 ≤ b2 ≤ . . . bn−k ≤ ak+1 .
从而存在第一个数据库的元素 ak+1 和第二个数据库中的元素 bn−k+1 都大于集合
A = {a1 , a2 , . . . , ak , b1 , b2 , . . . , bn−k } 中的任何一个元素且 |A| = n. 易证两数据库中第
n 大的元素一定在集合 A 中,从而必是 max A = max{ak , bn−k }. 由此可根据二分法的
思想搜索出这样的 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]};

3.2 Divide and Conquer
Recall the problem of finding the number of inversions. As in the course, we are given
a sequence of n numbers a1 , . . . , an , which we assume are all distinct, and we difine an
inversion to be a pair i < j such that ai > aj .
We motivated the problem of counting inversions as a good measure of how different
two orderings are. However, one might feel that this measure is too sensitive. Let’s call a
pair a significant inversion if i < j and ai > 2aj . Given an O(nlogn) algorithm to count the
number of significant inversions between two orderings.
Sort-and-Count (A)
1: Divide A into two sub-sequence L and R;
2: (RCL , L) = Sort-and-Count(L);
3: (RCR , R) = Sort-and-Count(R);
4: (r, A) = Merge-and-Count(L, R);
5: return (RC = RCL + RCR + r, A);

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:

if L[i] > R[j] then
A[k] = L[j];
j + +;
else
A[k] = R[i];

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;

3.3 Divide and Conquer
Consider an n-mode complete binary tree T , where n = 2d − 1 for some d. Each node
v of T is labeled with a real number xv . You may assume that the real numbers labeling the
nodes are all distinct. A node v of T is a local minimum if the label xv is less than the label
xw for all nodes w that are joined to v by an edge.
You are given such a complete binary tree T , but the labeling is only specified in the
following implicit way: for each node v, you can determine the value xv by probing the
node v. Show how to find a local minimum of T using only O(log n) probes to the nodes of
T.
解:
算法如下
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() 接受一个树的节点的输入,返回该节点的值。

3.4 Divide and Conquer
Suppose now that you’re given an n × n grid graph G. (An n × n grid graph is just the
adjacency graph of an n × n chessboard. To be completely precise, it is a graph whose node
set is the set of all ordered pairs of natural numbers (i, j), where 1 ≤ i ≤ n and 1 ≤ j ≤ n;
the nodes (i, j) and (k, l) are joined by an edge if and only if |i − k| + |j − l| = 1.)
We use some of the terminology of the previous question. Again, each node v is labeled
by a real number xv ; you may assume that all these labels are distinct. Show how to find a
local minimum of G using only O(n) probes to the nodes of G. (Note that G has n2 nodes.)
解:考查这六条直线 i = 1; i = n; j = 1; j = n; i = ⌊(1 + n)/2⌋; j = ⌊(1 + n)/2⌋. 上
的点的值,并找出最小值对应的点。如果这个最小点的邻点的值比这个点的值大,那
么直接返回这个最小值点,它就是局部最小值点。否则,因为这六条直线将网格分成
了四个部分,最小值点所在的部分中一定含有极小值点。因此只需要在这个区域递归
地求解。

3.5 Divide and Conquer
Given a convex polygon with n vertices, we can divide it into several separated pieces,
such that every piece is a triangle. When n = 4, there are two different ways to divide the
polygon; When n = 5, there are five different ways.
Give an algorithm that decides how many ways we can divide a convex polygon with n
vertices into triangles.
解:
剖分的个数是 Catalan 数,组合数学上有详细的介绍,
这里就不再给出。

3.6 Divide and Conquer
Suppose you have n (n is even) coins, and you have a device to decide whether two coins

are made of the same material. (You cannot use the device to compare two piles of coins.)
15


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 I’m 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. I’m 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 employee’s 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 +



p∈post(m)

max{f1 (p), f0 (p)}

p∈post(m)

其中 conv(m) 表示雇员 m 的 Conviviality rating, post(m) 表示 m 的直接下级组成的集
合。所以要求的就是 max f0 (m0 ), f1 (m0 ) 以及取得最大值的方案,这里 m0 表示“顶头
上司”。
时间复杂度为 O(n), 因为每个雇员被计算两个值 f1 , f2 , 考虑计算 f1 的计算量(假
设 f0 已经计算出),当所有雇员的 f1 值都被计算后,所用的加法的次数为 2n − 1, 考
虑计算 f0 的计算量,它需要 n − 1 次比较和少于 n − 1 次的加法,因此,时间复杂度为
O(n).

4.2 Dynamic Programming
Suppose you have one machine and a set of n jobs a1 , a2 ,...,an to process on that machine.
Each job aj has a processing time tj , a profit pj , and a deadline dj . The machine can process
only one job at a time, and job aj must run uninterruptedly for tj consecutive time units. If
job aj is completed by its deadline dj , you receive a profit pj , but if it is completed after its
deadline, you receive a profit of 0. Give an algorithm to find the schedule that obtains the
maximum amount of profit, assuming that all processing times are integers between 1 and
n. What is the running time of your algorithm?
Solution:
Recursion Relation: We first order the jobs by increasing deadline so a1 . . . , an are or17



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, j) = max(P (i − 1, j), P (i − 1, j − ti ) + pi ), if j ≤ di ,



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.

4.3 Dynamic Programming
To assess how “well-connected” two nodes in a directed graph are, one can not only look
at the length of the shortest path between them, but can also count th number of shortest
paths.
This turns out to be a problem that be solved efficiently, subject to some restrictions on
the edge costs. Suppose we are given a directed graph G = (V, E), with costs on the edges;
the costs may be positive or negative, but every cycle in the graph has strictly positive cost.
We are also given two nodes v, w ∈ V . Give an efficient algorithm that computes the
number of shortest v − w paths in G.
Solution:
First we apply the Fullman-Fulkson algorithm to get a shortest path P and the length of the

path. We assume that the shortest path of the graph G is k. For every edge e of the path P ,
we get a graph Ge = G − e. We apply the Fulkson-Fulkson algorithm on every of these
graphs and get the shortest path’s length, denoted as ke . If there is a ke = k, for some e in
P , the shortest path is not uniqe, otherwise, it is uniqe.

4.4 Dynamic Programming
You‘re running a computing system capable of processing several terabytes of data per
day. For each of n days, you’re presented with xi terabytes on day i. Note that any unprocessed data is abandoned at the end of the day.
However, the computing system can only process a fixed number of terabytes in a given
day. Besides, the amount of data you can process goes down every day since the most recent
reboot of the system. On the first day after a reboot, you can process s1 terabytes, and so
on, up to sn ; we assume s1 > s2 > s3 > · · · > sn > 0. To get the system back to peak
performance, you can choose to reboot it; but on any day you choose to reboot the system,
you can’t process any data at all.
18


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.
i≤k≤j

其中 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)。当没有剩余时间时,也就没有可能处理
数据。
那么我们可以将问题看作是做一组决策:当还剩 i 天时,我们是否应该重启系统。
这两种决策的情形如下:
F (i, j) = max{F (i − 1, j + 1) + min{xn−i+1 , sj }, F (i − 1, 1) + 0}
需要注意的是:当还剩下 i 天时,需要处理的数据是 xn−i+1 , 而系统能处理的最大数据
量是 sj . 我们只能能其中较小的那一个。
这个算法初始化的 F (0, j) = 0, ∀j ∈ N+ , 并计算所有 i 的 F (i, j) 值。其中 j ≤
n − i + 1. 当我们算到第一天的 F (n, 1) 时,问题就解决了。
每一步需要耗费 O(1) 的时间,并且共有 O(n2 ) 步对应于所有的 i 和 j. 因此,算法的
时间复杂度是 O(n2 ).


4.5 Dynamic Programming
Given a sequence of n real numbers a1 , ..., an , determine a subsequence (not necessarily contiguous) of maximum length in which the values in the subsequence form a strictly
increasing sequence.
解:
假设我们知道了以字符 ai 结尾的子串长度 Ci , 因此对所有 k, 有:
Ck = 1 +

max

i∈{i|0≤i
19

Ci .


根据这个公式,我们可以得到以 a1 , a2 , . . . , an 结尾的所有子串长度。最后选择其中最
大的一个结果返回。
容易得到,
算法时间复杂度为 O(n2 ).

4.6 Dynamic Programming
In an army system, to notify everyone of the postponement, the ranking officer first calls
each of his/her direct subordinates, one at a time. As soon as each subordinate gets the
phone call, he/she must notify each of his/her direct subordinates, one at a time. The process
continues this way until everyone has been notified. Note that each person in this process
can only call direct subordinates on the phone.
We can picture this process as being divided into rounds. In one round, each person who
has already learned of the postponement can call one of his/her direct subordinates on the
phone. The number of rounds it takes for everyone to be notified depends on the sequence in

which each person calls their direct subordinate. In the following example, it will take only
two rounds if A starts by calling B, but it will take three rounds if A starts by calling D.

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) = max1≤j≤k {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∗ .

5.2 Greedy Algorithm
Let us say that a graph G = (V, E) is a near-tree if it is connected and has at most n + 8
edges, where n = |V |. Give an algorithm with running time O(n) that takes a near-tree G
with costs on its edges, and returns a minimum spanning tree of G. You may assume that all
the edge costs are distinct.
解:
当 G 不是树时,如下迭代:
• 用尝试优先搜索或广度优先搜索找到 G 中的一个环。
• 删除此环中最大的边
迭代 8+1 次,
留下正好含 n − 1 条边的树。每次迭代用结点的线性时间(每个结点最多
搜索、检查常数次)。因此时间复杂度是 O(n).

5.3 Greedy Algorithm
Given a list of n natural numbers d1 , d2 ,...,dn , show how to decide in polynomial time
whether there exits an undirected graph G = (V, E) whose node degrees are precisely the
numbers d1 , d2 ,...,dn . G should not contain multiple edges between the same pair of nodes,
or “loop” edges with both endpoints equal to the same node.
解:
算法如下: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, . . . , dn−1 − 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 vn−1(共 dn 条
边)。从而所得的图的度依次为 d(vi ) = di , i = 1, 2, . . . , n.
⇒) 设图 G 的各顶满足 d(vi ) = di , i = 1, 2, . . . , n. d1 < d2 < · · · < dn . 若与 vn 相邻的
顶点就是 vk , vk+1 , . . . , vn−1 , 那么命题得证。否则,首先将一端在 {vk , vk+1 , . . . , vn−1 },
另为 vn 的所有边删除,得到图 G1 .
记 A = {v ∈ {vk , vk+1 , . . . , vn−1 }|v不是vn 的邻点}. 对 A 中的每个元素 v, d(v) ≥ 1.
22


故可取一邻点 v ′ , 删除边 vv ′ , 对 vn 的一边 vv ′′ ∈ E(G1 ),删除边 vn v ′′ , 增加边 v ′ v ′′ . 当对
A 中每个元素都执行了以上操作之后,
记所得的图为 G′1 . 那么
dG′1 (vn ) = 0;(因为 |A| = E(G1 ))
dG′1 (v) = dG (v) − 1, ∀v ∈ {vk , vk+1 , . . . , vn−1 };
dG′1 (v) = dG (v), ∀v ∈ {v1 , v2 , . . . , vk−1 };
令 G′ = G′1 − vn . G′ 就是满足要求的图。
算法就是执行这样的图消减的过程:
每次 while 循环都删除了度最大的顶点。按照
上面的命题,
在执行一步 while 循环前的图的各顶的度为 d1 , d2 , . . . , dn , 那么一次 while
循环后的图的度为 d1 , d2 , . . . , dk − 1, dk+1 − 1, . . . , dn−1 − 1. 如果 d1 , d2 , . . . , dn−1 − 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 循环体语句


执行的次数一定小于 ni=1 di ≤ 2n(n − 1)/2 = O(n2 ).(当 ni=1 di ≥ n(n − 1) 时
程序已经返回 FALSE 并退出)。另外第 k 次 while 循环还进行了一次合并操作,
∑n−1
合并了 n − k 个元素,帮总共的合并代价为 O( k=1
(n − k)) = O(n2 ).
综上三条,有总的时间复杂度为 O(n2 ).
另一方面,事实上每执行一次 for 循环相当于从图中删除一边。对 n 阶完全图 Kn
共有 n(n − 1)/2 条边,
故总的时间代价为 Ω(n2 ). 于是有算法的时间复杂度为 Θ(n2 ).

5.4 Greedy Algorithm
Suppose you have n video streams that need to be sent, one after another, over a communication link. Stream i consists of a total of bi bits that need to be sent, at a constant rate,
over a period of ti seconds. You cannot send two streams at the same time. Besides, there
cannot be any delays between the end of one stream and the start of the next. Suppose your
schedule starts at time 0. We assume that all the values bi and ti are positive integers.
Now, the link imposes the following constraint, using a fixed parameter r:
(*) For each natural number t > 0, the total number of bits you send over the time
interval from 0 to t cannot exceed rt.
23


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 ∈ [Ti−1 , 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


只 需 ni=0 bi / ni=1 ≤ r 则 ∀t ∈ [0, Tn ] 都 不 会 超 过 rt 的 限 制。(另 一 方 面,如 果
∑n
∑n
b
/

i
i=0

i=1 ≥ r, 则最后一点必超过 rt 的限制。

5.5 Greedy Algorithm
The input consists of n skiers with heights p1 , p2 ,...,pn , and n skies with height s1 ,
s2 ,...,sn . The problem is to assign each skier a ski to minimize the AVERAGE DIFFERENCE between the height of a skier and his/her assigned ski. That is, if the skier i is given
the ski ai , then you want to minimize:
Σni=1 (|pi − sai |)/n
Solution:
The key observation is that there is no advantage to “cross matching” reversing the height
if order of skiers and skis. That is if s1 < s2 and p1 < p2 there is no reason to match s1
with p2 and s2 with p1 .To show this, we have to look at all the possible relationships of the 4
heights and show that the “cost” if matching the shorter of the skiers with the shorter of the
skis is always at least as good as the cost of cross matching them. Without loss of generality
we can assume that the height of s1 is the smallest of the 4 heights. With this assumption,
we have 3 cases (i.e.: the 3 possible orderings of s2 , p1 , and p2 given that p1 < p2 ).
• s1 < s 2 < p1 < p 2
|p1 − s1 | + |p2 − s2 | = p1 − s1 + p2 − s2 = |p1 − s2 | + |p2 − s1 |.
• s1 < p 1 < s 2 < p 2
|p1 − s1 | + |p2 − s2 | = p1 − s1 + p2 − s2 < |p1 − s2 | + |p2 − s1 | = s2 − p1 + p2 − s1 .
• s1 < p 1 < p 2 < s 2
|p1 − s1 | + |p2 − s2 | = p1 − s1 + p2 − s2 < |p1 − s2 | + |p2 − s1 | = s2 − p1 + p2 − s1 .
24


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).

5.6 Greedy Algorithm
The input to this problem consists of an ordered list of n words. The length of the ith
word is wi , that is the ith word takes up wi spaces. The goal is to break this ordered list of
words into lines, this is called a layout. Note that you can not reorder the words. The length
of a line is the sum of lengths of the words on that line. The ideal line length is L. No line
may be longer than L, although it may be shorter. The penalty of having a line of length K
is L − K. The total penalty is the sum of the line penalties. The problem is to find the layout
that minimizes the total penalty.
解:
对每一个词 (word):
• 如果该词能加入当前行,则将该词加入到当前行末尾;
• 如果当前行无法容纳该词,则另起一新行,
并添加这个词到该新行。
假设贪心算法输出结果为 G. 若对于 G 中第 i 行(任意 i),将此行末尾的词放入第
i + 1 行开头,
• 如果第 i + 1 行能容纳这个词,可得总惩罚值并不会减少。
• 如果第 i + 1 不能容纳这个词,则将第 i + 1 行的末尾超出容量的几个词放入第
i + 2 行的开头,
不断重复这样的操作,
直到某行能容纳上一行的超出的词。可得
总惩罚值也不会减少。
因此证明了使用贪心算法的正确性。
算法时间复杂度:
O(n),n 为词数。

25



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×