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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,376评论 6 491
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,126评论 2 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,966评论 0 347
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,432评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,519评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,792评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,933评论 3 406
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,701评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,143评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,488评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,626评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,292评论 4 329
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,896评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,742评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,977评论 1 265
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,324评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,494评论 2 348

推荐阅读更多精彩内容

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