Dynamic Programming Problems经典问题集

2012年3月05日 09:21

1. The Integer Knapsack Problem (Duplicate Items Permitted):

You have $n$types of items, where the$i^{th}$ item type has an integer size $s_i$and a real value. $v_i$

You are trying to fill a knapsack of total capacity $C$with a selection of items of maximum value.

You can add multiple items of the same type to the knapsack.

Solution: Let M(j) denote the maximum value you can pack into a size j knapsack.

We can express M(j)recursively in terms of solutions to smaller problems as follows:

$$M(j)= \left\{
\begin{aligned}\overset{}\max\{M(j-1) , \max_{i=1...n}M(j-s_i)+v_i \}
\quad j\ge 1\\ \\ 0 \quad j \le 0 \end{aligned}\right. $$

Computing each M(j)value will require $\Theta(n)$ time, and we need to sequentially compute $C$such values.
Therefore, total running time is $\Theta(nC)$ . Total space is $\Theta(n)$ .

The value of $M(C)$ will contain the value of the optimal knapsack packing.

We can reconstruct the list of items in the optimal solution by maintaining and following “backpointers”

2. Maximum Value Contiguous Subsequence.

Given a sequence of $n$ real numbers $A_1,
... A_n$,

determine a contiguous subsequence $A_1, ... A_j$ for which the sum of elements in the subsequence is maximized.

Solution: Let $M(j)$denote the max sum over all windows ending at $j$

$M(j) = max\{
M(j-1)+A[j], A[j]\}$

It only takes linear time $O(n)$to run the program since it only need to solve $n$sub-problems and each takes constnt time.

3. Making Change: You are given $n$ types of coin denominations of values  $v_1 < v_2 ... < v_n$ (all integers).

Assume $v_1 =1$, so you can always make change for any amount of money C.

Give an algorithm which makes change for an amount of money C with as few coins as possible.

Solution: Let $M(j)$denote the min number of coins required to make changes for amount of money $j$

$M(j)=\min_{i=1...n}\{M(j-v_i)\}+1$

It takes $O(nC)$ time because we are solving $C$subproblems,

each of which requires minimization $O(n)$ different terms. (Similar to integer knapsack problem)

4. Longest Increasing Subsequence. Given a sequence of n real numbers $A_1,
... A_n$,

determine a subsequence (not necessarily contiguous) of maximum length in which the values in the subsequence form a strictly increasing sequence.

Solution: Let $L(j)denote the longest strictly increasing subsequence ending at position$j$ , therefore,

$L(j) =
\max\limits_{i<j}\limits_{A[i]<A[j]}\{ L(i)\}+1 $

to find the solution overall, it has to find the maximum over all potential ending points $j$of $L(j) :

$\max\limits_{j=1...n}\{L(j)\}$

since the longest increasing subsequence could end anywhere.

The algorithm takes $O(n^2)$time to run because I have to sort $n$subproblems and each of them takes $O(n)$time.

5.Box Stacking:

You are given a set of $n$ types of rectangular 3-D boxes, where the $i^{th}$ box has height $h_i$, width $w_i$ and depth $d_i$ (all real numbers).

You want to create a stack of boxes which is as tall as possible,

but you can only stack a box on top of another box if the dimensions of the 2-D base of the lower box are each strictly larger than those of the 2-D base of the higher box.

Of course, you can rotate a box so that any side functions as its base.

It is also allowable to use multiple instances of the same type of box.

Solution: constraint : can only stack box $i$on box $j$if $w_i<w_j$ and $d_i<d_j$

(without loss of generality, assume $w_i\le d_i$ ).

First sort the base area by decreasing order:

then let $H(j)$ denote the tallest box stack I could form with box $j$on top, so

$H(j) =
\max\limits_{i<j, w_i<w_j, d_i<d_j}\{ H(i)\}+h_j $

The algorithm takes $O(n^2)$time to run because I have to sort $n$subproblems and each of them takes $O(n)$time.

At the end, the solution is found by taking the max of all potential top boxes $j$of $H(j)$.

(Since we are not sure in the optimal solution, which box is on top)

6. Building Bridges.

Consider a 2-D map with a horizontal river passing through its center.

There are $n$cities on the southern bank with x-coordinates $a_1... a_n$and $n$ cities on the northern bank with x-coordinates $b_1 ... b_n$.

You want to connect as many north-south pairs of cities as possible with bridges such that no two bridges cross.

When connecting cities, you are only allowed to connect the the ith city on the northern ban to the ith city on the southern bank.

Solution: consider the south bank city (below the river) $i$,

let $x(i)$denote the index of corresponding city on the north bank ($x(i)=3$ in the figure).

So we could compute the $x(i)$by sorting in $O(n\log
n)$time.

Now we want to find the longest increasing subsequence through $x(1),
... x(n)$, so it is identical to problem 4.

7. Balanced Partition:You have a set of n integers each in the range 0 . . .K.

Partition these integers into two subsets such that you minimize $S_1 - S_2$,

where $S_1$ and $S_2$ denote the sums of the elements in each of the two subsets.

Solution: Use $$P(i,j)=
\left\{ \begin{aligned}\overset{}1 \quad \text{if some subset of }
\{A_1, ... A_n\} \text{has sum of j}\\0 \quad
\text{otherwise}\end{aligned}\right.$$ where $i \in [0,n]$ and $j \in
[0,nK]$

therefore, $P(i,j)=1$if $P(i-1, j)=1$ or $P(i-1,
j-A_i)=1$

dynamic programming formula is:

$P(i,j)=\max\{P(i-1,j), P(i-1,
j-A_i)\}$

this procedure takes $O(n^2K)$time to compute.

To solve the original problem, let $S=\sum(A_i)/2$, and the purpose is to make $S_1 - S_2$ close to 0 as possible.

what we want to find is:

$\min\limits_{i\le S}\{S-i:
P(n, i)=1\}$

8. Edit Distance.

Given two text strings A of length n and B of length m,

you want to transform A into B with a minimum number of operations of the following types:

delete a character from A, insert a character into A,

or change some character in A into a new character.

The minimal number of such operations required to transform A into B is called the edit distance between A and B.

Solution: Use $C_i, C_d, C_r$to denote the cost for inserting, deleting or replacing a letter.

The goal is to compute the minimum cost of translating A to B.

Let $T(i,j)$denote the minimum cost of translating $A[1...i]$into $B[1...j]$, then

$$T(i,j)= \left\{\begin{aligned}\overset{}C_d + T(i-1, j) \\
C_i+T(i, j-1) \\ \overset{} T(i-1, j-1) \quad if \quad A[i]=B[j] \\
C_d+T(i-1,j-1) \quad if \quad A[i] \neq B[j] \end{aligned}\right.$$

It takes $O(nm)$time to compute and final solution is stored in $T(n,m)$

评论(0) 阅读(11541)

转一个经典证明:扫雷是NP完全问题

2011年8月11日 16:02

曾经看到过自动扫雷软件,当时我就在想,扫雷游戏是否有什么牛B的多项式算法。最近才看到,扫雷问题居然是一个NP完全问题,并且这个定理有一个简单、直观而又神奇的证明。在这里和大家分享一下整个证明过程。
首先,扫雷一定是NP问题,它显然可以在多项式的时间里验证一个解。接下来,我们需要把一个已知的NP完全问题归约到扫雷问题上去。我们将给出一种把逻辑电路问题归约到扫雷问题的方法,这样的话我们就可以利用扫雷问题解决逻辑电路问题,从而说明逻辑电路问题不比扫雷难。我们将把逻辑电路问题转换成一种对应的扫雷布局,就像画画一样把逻辑电路画在扫雷的棋盘上。如果你还不知道什么叫NP完全问题,什么叫逻辑电路问题,你可以看一看我的这篇文章


上图就是一条带有Boolean值的线路。注意到x和x'中有且仅有一个有雷。如果(沿线路方向)前一个格子有雷,我们就说这条线路状态为True;反之如果后一个格子有雷,那么这条线路所传递的Boolean值就是False。每条线路的起始端都如下图左所示,其中符号*表示该格里必然有雷,x和x'中同样是有且仅有一个有雷,但到底是哪一个里面有雷谁也说不清楚。线路是可以拐弯的,如下图右所示,这可以保证转角后Boolean值相同。

我们需要构造一些特殊的扫雷布局来解释NOT门、AND门和OR门。构造NOT门最为简单,下图就是一个NOT门,注意经过了中间的NOT门后,x和x'的位置互换,True变成了False,False也将变成True。

AND门和OR门的构造就比较复杂了。下面是AND门的构造,U和V是输入的两条线路,T是输出的线路。为了说明这确实是一个AND门,我们将说明:在下面的构造中,如果线路T是True(即最右边那个格子t有雷)的话,那么格子u和v必须都有雷才行。如果最右边的格子t有雷,我们可以很快推断出,图中所有其它的t格都是有雷的,所有t'都是无雷的。观察a3正上方的那个"3",我们立即看出a2,a3都必须有雷,于是继续推得a1无雷,s有雷。类似地,我们可以知道r也是有雷的。在中间一行的*4t处,4的上下左右都已经有雷了,那么u'和v'必然无雷,于是继续往左推得u和v都有雷。

OR门的构造比较类似,如下图。如果r无雷的话,可知a2,a3有雷,a1无雷,s'有雷,进而s无雷。观察"6"可知u'和v'都有雷,于是u和v均无雷。

不断套用这几个逻辑门的构造图来连接电路,直到输出线路只剩下唯一的一条。把最后的输出线路从x或者x'处截断(相当于把最终输出的Boolean值定下来)后,整个布局就成了一个“扫雷版SAT问题”了。
最后还有一个容易忽略的问题:要是线路交叉了该咋办?下图的构造可以保证线路交叉后仍不改变原线路所带的Boolean值。至此,我们已经可以把任一逻辑电路布局到扫雷棋盘上,解决这个扫雷问题就相当于要解一个逻辑电路问题,因此扫雷问题至少和逻辑电路问题一样难。

评论(0) 阅读(2775)