【动态规划】Fibonacci, Shortest Paths

connect dots ...

Part one

basic concepts

  • DP, a general, powerful algorithm design technique
  • DP ≈ a careful brute force
    • try all possibilities ,carefully
    • get polynomial time
  • DP ≈ subproblems + reuse
  • DP ≈ recursion + memoization + guessing
    • memoize(remember) & re-use solutions to subproblems that help solve the problem

Fibonacci

problems:

F1 = F2 = 1
Fn = Fn-1 + Fn-2
Goal: compute Fn

Naive recursive algorithms

fib(n):
  if n <= 2: f = 1
  else: f = fib(n-1) + fib(n+2)
  return f
  • time complexity
    • EXPONENTIAL TIME
    • T(n): the time of compute F(n)
    • T(n) = T(n-1) + T(n-2) + Θ(1) >= Fn ≈ Φn >= 2T(n-2) = Θ(2n/2)

Memoized DP algorithm

memo = {}
fib(n):
  if n in memo: return memo[n]
  if n <= 2: f = 1
  else: f = fib(n-1) + fib(n-2)
  memo[n] = f
  return f
  • idea is simple: whenever we compute a Fibonacci number, we put it into a dictionary.And then we need to compute the nth Fibonacci we check, is it already in the dictionary? Did we already solve this problem? If so, return that answer. Otherwise, compute it.
  • fib(k) only recurses the first time it's called.∀k
    • memoized calls cost O(1)
    • non-memoized calls is n:
    • non-recursive work per call:Θ(1)
      => time = Θ(n)
    • time = subproblems * time/subproblem

Bottom-up DP algorithm

fib = {}
for k in range(1, n+1):
  if k <= 2: f = 1
  else: f = fib[k-1] + fib[k-2]
  fib[k] = f
return fib[n]
  • exactly same computation
  • topological sort of subproblem dependency DAG
  • save space (only need the last two value)

shortest paths

problems:

δ(s, v)∀v

tool:

Guessing, a very powerful tool.a tried and tested method for solving any problem

  • suppose you don't know something, but you'd like to know it.
  • so what's the answer to this question?I don't know.
  • How am I going to answer the question?Guess!
  • Don't try any guess.try them all. & take the best one

There are some incoming edges to vertex v. The shortest path to v comes through one of those edges. We don't know which one. So before solving the problem of finding shortest path to v we have solve smaller problems of finding shortest path to the parent vertices. If we have 2 incoming edges u1 -> v and u2 -> v, then we have to first find the shortest paths to vertices u1 and u2. Once we know them we can choose one edge between u1 -> v and u2 -> v which will result in the shortest path to v.

guessing & DAG view

  • infinite time on graphs with cycles
    DAGS: O(v+e)

    • use formula: time = subproblems * time/subproblem
    • number of subproblems: v
    • how much time do I spend per subproblem? : incoming edges to v. indegree(v)
  • if you are a acyclic:time = subproblems * time/subproblem

  • if you are a cyclic: make it to acyclic

DAGS
cyclic graph
Shortest Paths
Memoized Fibonacci
DAG

遗留

  • Bellman-Fords's algorithm

PS

因为教授是自己非常喜欢的一位导师,他总是能够用轻松自如的态度和浅显的语言表述来授课,但记录到一半的时候会怀疑自己投入大量时间来这样做的价值。——毕竟还有很多事情需要去做。
到底是会做dp的题就OK了,还是细细解剖每一个细节,争取得到比DP本身更多的东西?比如思维方式,比如逻辑推理,比如面对一般性的问题如何去处理,又比如智慧本身。
再记录更多的时候,会得到一种喜悦。可以通过模仿,可以通过不断演练,都可以。其他需要我去做的事情皆黯淡无光。此时此刻,聚光灯,打在舞台正中央,打在我身上。

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

推荐阅读更多精彩内容

  • 西古魔战记-目录 西古魔战记(九)魔石碎了 高德离开万神殿只有短短一个月的时间,遇到了如此多的幸与不幸,此时,高德...
    冷小二阅读 777评论 0 2
  • 殻になった是日语里面的一个常用语。变成了空壳,类似于行尸走肉给人的感觉。 觉得是一个比较准确描述了现在自己状态的词...
    kou_sienn阅读 382评论 0 0
  • 小时候,爷爷常常喜欢带着我去街头看猴把戏,那些猴子被主人穿戴上花花绿绿的服装和面具,在咚咚锵锵地锣鼓声中耍戏...
    胡岱阅读 707评论 0 1