zoukankan      html  css  js  c++  java
  • Dynamic programming

    Hallmarks of dynamic programming

    #1

    Optimal substructure: an optimal solution to a problem (instance) contains optimal solutions to subproblems.

    e.g. if z is a longest common sequence (lcs) of x and y, then any prefix of z is an LCS of a prefix of x and a prefix of y. 

    LCS(x, y, i, j)

    if x[i] == y[j]

        then c[i,j] <- lcs(x,y,i-1, j-1) +1

      else c[i,j] <- max{lcs(x,y,i-1,j), lcs(x,y,i,j-1)}

    return c[i,j];

    Worst case: x[i] != y[j] for all i, j

    the height of the recursion tree would be m+n in the worst case.

    Exponential time complexity

    # 2

    Overlapping subproblems: A recursive solution contains a "small" number of distinct subproblems repeated many times.

    LCS: subproblems space contains mn distinct subproblems.

    Memoization algorithm (not memorization, memo: keep a memo on what's done already)

    O(mn) time and space complexity

    Dynamic programming:

    Idea: compute a table bottom up

    Reconstruct LCS by tracing backwards

    Notes of MIT OCW: Introduction to Algorithms

    http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-introduction-to-algorithms-sma-5503-fall-2005/

  • 相关阅读:
    理解cookie
    浏览器解析url后执行过程
    如何使用D3绘制折线图
    Django 笔记
    vi命令
    PEP8编程规范
    Python_入门第一篇【持续更新...】
    DjangoWeb _ 登录页开发test
    Django开发流程
    Django 笔记2018.2.7
  • 原文地址:https://www.cnblogs.com/shalijiang/p/3999105.html
Copyright © 2011-2022 走看看