19. Dynamic Programming 2

  1. Dynamic Programming typically applies to optimization problems in which a set of choices must be made in order to get an optimal solution
  2. As choices are made, subproblems of the same form often arise.

Dynamic programming is effective when the given subproblem may arise from more than one partial set of choices
Therefore, dynamic programming is applicable when the subproblems are not independent.

Four Steps of Developing DP Algorithms

  1. Characterize the structure of an optimal solution
  2. Define the value of the optimal solution recursively
  3. Compute the value of the optimal solution in a bottom-up fashion
  4. Construct the optimal solution from the computed information (stored in tables).

Steps 1 - 3 form the basis of a DP solution to an optimization problem

Assembly-line scheduling problem

Each assembly line has n stations. Notation:
Si,j is the jth station in line i
ai, j is the processing time at station Si, j
ti,j is the time to transfer from Si,j to Si', j+1 (i != i')
ei is the entry time for line i
xi is the exit time for line i
i = 1,2 and j = 1,2,...,.

  1. Step 1: The structure of the fastest way (optimal solution) through the factory.
    The car must pass through each stage of assembly. Stage j can be reached either from stage j -1 in the same assembly line, or by transferring (with a cost) from stage j -1 in the other assembly line.

  2. Step 2:A recursive solution


  1. Step 3: Apply the recurrence bottom-up.
    To find the actual shortest path, just record which of the two choices gave the minimum at each step, then work back from the finishing point.

we use an array to memorize from where the value comes.

  1. Step 4: Construct the optimal solution
    The array will be used to find the optimal solution

Shortest path in a directed acyclic graph

The way to solve this problem is same to above, here we will calculate the Time complexity.
Suppose that for each vi, we have a list of incoming arcs.

In the worst case we need to look at each node and each arc at least once, so the worst case complexity of the algorithm is theta(n + m).

練習(xí):

Given a sequence of n elements, find a longest increasing subsequence from the sequence.

Solution:

  1. We construct a directed graph G = (V , E ), each element in the sequence is a node in V, there is a directed edge in E from a node vi to another node vj if the element of vi is less than the element vj, and the weight of this directed edge is assigned 1.
  2. Clearly, G is a DAG. Then, Add a virtual node v0 and add a directed edge from v0 to v for each v belongs to V, and assign the edge with weight 0. LetG0 bethe final DAG.

Find a longest path in G0 from v0. This path corresponds to the longest increasing subsequence, which takes O(n + m) = O(n^2) time as m = O(n^2)

Personal Consideration about DP

DP 是一個(gè)基于上個(gè)的結(jié)果中一個(gè)最佳的結(jié)果(可能會(huì)有多個(gè)parents)得出的現(xiàn)有結(jié)果的過程。總而言之,是一個(gè)由下往上的過程。一旦得到一個(gè)結(jié)果,那么它在之后的過程中,作為往后計(jì)算的基礎(chǔ),一直都不會(huì)改變。

最下面的初始化階段,就是把問題的size減小到最小,得出初始化結(jié)果,再慢慢往上走(不斷擴(kuò)大size)。本質(zhì)上的問題都一樣,只是scale不同。

所以,我們只需要確定他的初始化狀態(tài),狀態(tài)轉(zhuǎn)移的條件和狀態(tài)轉(zhuǎn)移的對(duì)象。就可以得出 optimal solution recursively

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 周六是個(gè)很平常的日子,應(yīng)該有很多人和我一樣宅在家里哪也不想去,懶懶的睡到自然醒。然后玩著手機(jī),慢慢的、慢慢...
    aT傳建閱讀 393評(píng)論 0 0
  • 不曾死去 那就認(rèn)真的活著 人生就像是一個(gè)問號(hào) 不能理解的事情很多 不能預(yù)知的未來(lái)數(shù)不勝數(shù) 生命的起始就如一條孤獨(dú)的...
    驚濤的詩(shī)閱讀 452評(píng)論 2 13
  • 1. 別把時(shí)間花在找東西上 雜亂無(wú)章的東西,找起來(lái)會(huì)浪費(fèi)時(shí)間。雜亂的臥室,讓早上起床的心情就開始拖沓。 雜亂的電腦...
    拉小登閱讀 477評(píng)論 0 0

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