码迷,mamicode.com
首页 > 其他好文 > 详细

动态规划-重叠子问题

时间:2017-06-20 19:14:11      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:说明   ide   repeat   ems   mic   text   when   art   value   

动态规划-重叠子问题

flyfish 2015-8-23

名词解释
重叠子问题 overlapping subproblems

动态规划策略将问题分解为一个或者多个子问题

重叠子问题是一个递归解决方式里包括的子问题尽管非常多,但不同子问题非常少。少量的子问题被重复解决非常多次。

比如LCS(最长公共子序列)问题,给定两个序列X和Y,长度各自是m和n,穷举子问题是指数级的,而不同子问题的数量仅仅是mn.

a recurisive algorithn for the problem solves the same subproblems over and over,rather than always generating new subproblems.
用来解原问题的递归算法能够重复地解相同的子问题。而不是总在产生新的子问题。

over and over 重复。再三
rather than 而不是。宁可…也不愿

When a recursive algorithm revisits the same problems repeatedly,we say that the optimization problem has overlapping subproblems.
当一个递归算法不断地调用同一问题时,我们说该最优问题包括重叠子问题

revisit 英 [ri?’v?z?t] 美 [ri’v?z?t]
vt. 重游;再訪;重临
n. 再訪问

repeatedly 英 [r?’pi?t?dl?] 美 [r?’pit?dli]
adv. 重复地;再三地。屡次地

In contrast, a problem for which a divide-and-conquer approach is suitalbe usually generates brand-new problems at each step of the recursion.
相反地,适用于分治法解决这个问题往往在递归的每一个步上都产生全新的问题。

contrast英 [‘k?ntrɑ?st] 美 [‘kɑntr?st]
vi. 对照;形成对照
vt. 使对照;使与…对照
n. 对照;区别。对照物

divide-and-conquer
分治法

approach 英 [?’pr??t?] 美 [?’prot?]
n. 方法;途径;接近
vt. 接近;着手处理
vi. 靠近

suitable 英 [‘su?t?b(?)l] 美 [‘sut?bl]
adj. 适当的。相配的

brand-new
adj. 崭新的;近期获得的

brand 英 [br?nd] 美 [br?nd]
vt. 铭刻于,铭记;打烙印于;印…商标于
n. 商标,牌子;烙印

以斐波那契数列为例说明

斐波那契数列指的是这样一个数列:1、1、2、3、5、8、13
从第3个数開始。每一个数都是前面两个数之和。
測试时 输入的 n>=3,但也不要太大,防止溢出。
递归式

Consider again the recursive function for computing the nth Fibonacii number
.
再次考虑使用递归函数计算第n个斐波那契数。

    int Fibonacci(int n)
    {
        if( (1==n) || (2==n) )
        return 1;

        return Fibonacci(n-1) + Fibonacci(n-2);     
    }

调用 Fibonacci(7)。

存表方式
动态规划算法总是充分利用重叠子问题。即通过每一个子问题仅仅解一次,把解保存在一个须要时就能够查看的表中,而每次查表的时间为常数。


Dynamic-programming algorithms typically take advantage of overlapping subproblems by solving each subproblem once and then storing the solution in a table where it can be looked up when needed, using constant time per lookup.

typically 英 [‘t?p?k?l?] 美 [‘t?p?kli]
adv. 代表性地;作为特色地,典型地

take advantage of
利用

    int Fibonacci(int n,int* Values)
    {
        if(n<=2)
        return 1;

        Values[n]=Fibonacci(n-1,Values) + Fibonacci(n-2,Values);

        return  Values[n];  
    }

调用
int Values[8]={0,0,0,0,0,0,0,0};
Fibonacci(7,Values); 

不须要表存储全部值。仅仅存储前两个值

int Fibonacci(int n)
    {
        long past,prev,curr;
        past=prev=curr=1;

        for(int i=3;i<=n;i++)
        {
            past=prev;      // past holds Fibonacci(i-2)
            prev=curr;      // past holds Fibonacci(i-1)
            curr=past+prev; // current now Fibonacciholds (i)
        }
        return curr;

    }


调用 Fibonacci(7)。

动态规划-重叠子问题

标签:说明   ide   repeat   ems   mic   text   when   art   value   

原文地址:http://www.cnblogs.com/clnchanpin/p/7055688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!