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

练习:

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 是一个基于上个的结果中一个最佳的结果(可能会有多个parents)得出的现有结果的过程。总而言之,是一个由下往上的过程。一旦得到一个结果,那么它在之后的过程中,作为往后计算的基础,一直都不会改变。

最下面的初始化阶段,就是把问题的size减小到最小,得出初始化结果,再慢慢往上走(不断扩大size)。本质上的问题都一样,只是scale不同。

所以,我们只需要确定他的初始化状态,状态转移的条件和状态转移的对象。就可以得出 optimal solution recursively

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 周六是个很平常的日子,应该有很多人和我一样宅在家里哪也不想去,懒懒的睡到自然醒。然后玩着手机,慢慢的、慢慢...
    aT传建阅读 364评论 0 0
  • 不曾死去 那就认真的活着 人生就像是一个问号 不能理解的事情很多 不能预知的未来数不胜数 生命的起始就如一条孤独的...
    惊涛的诗阅读 433评论 2 13
  • 1. 别把时间花在找东西上 杂乱无章的东西,找起来会浪费时间。杂乱的卧室,让早上起床的心情就开始拖沓。 杂乱的电脑...
    拉小登阅读 446评论 0 0