18.Dynamic Programming 1

引例:

What’s the running time of a naive algorithm for finding an increasing subsequence from a 7-element sequence 5,3,13,6,8,7,10 by enumerating all possible subsequences?

The answer may not be unique, e.g.,
A → B[,,,,,,]
{3,6,7,10}, → {0,1,0,1,0,1,1}
{5,6,7,10}, → {1,0,0,1,0,1,1}
{3,6,8,10}, → {0,1,0,1,1,0,1}
{5,6,8,10}, → {1,0,0,1,1,0,1}
where B[i] = 0 if ai is not in the increasing sequence; otherwise, B[i] = 1. The above naive solution takes O(n · 2^n) time.

Dynamic Programming

Dynamic Programming (DP for short) is a method of solving an optimization problem (a minimization or maximization problem), by first solving some subproblems and then combining the results of subproblems(Divide and Conquer method)

The main requirements for dynamic programming are:

  1. The total number of (sub-)subproblems which may occur is fairly small.
  2. The solution to each subproblem can be deduced fairly easily from the solutions to the smaller subproblems.

The basic idea of dynamic programming is to solve the subproblems from smallest to largest, storing the solutions in a table

Example

The key observation : any shortest path from {A1,B1} to {An,Bn} consists of a shortest path from {A1,B1} to {An?1,Bn?1} + one more arrow.

  1. L[X,Y] = the length of (a directed edge) the arrow from X to Y.
    (eg. L[B2,A3] = 1)
  2. P(X) = the length of the shortest path from {A1,B1} to X.

Then, we have this recurrence:

  1. P(A1)=0andP(B1)=0
  2. For1≤i≤n:
    P(Ai) = min{P(Ai?1) + L[Ai?1, Ai], P(Bi?1) + L[Bi?1, Ai]}
    P(Bi) = min{P(Ai?1) + L[Ai?1, Bi], P(Bi?1) + L[Bi?1, Bi]}

Therefore, the shortest path from {A1,B1} to {A6,B6} has length 8.
The time complexity: O(n)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 你有沒(méi)有一種感覺(jué)? 去書(shū)城面對(duì)琳瑯滿目的書(shū),不知道怎么選,直接懵逼,可是基于對(duì)知識(shí)的饑渴(特別是心血來(lái)潮,興致...
    簡(jiǎn)繁君閱讀 1,363評(píng)論 0 2
  • 想當(dāng)年姐兒的姨媽剛來(lái)的時(shí)候,也曾經(jīng)無(wú)病無(wú)痛,活蹦亂跳的可勁撒歡來(lái)著。可不知道從何時(shí)起,姨媽一來(lái),命沒(méi)半條。痛的滿地...
    羋蟲(chóng)閱讀 630評(píng)論 1 8
  • sdfsdfsdf
    wang1006tao閱讀 184評(píng)論 0 0
  • 花葉當(dāng)時(shí)紅, 名利轉(zhuǎn)頭空。 多少木訥者, 竟是逍遙翁。
    hsl_7cbf閱讀 173評(píng)論 0 0

友情鏈接更多精彩內(nèi)容