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

Dynamic Programming 动态规划

时间:2015-02-04 14:46:41      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:algorithm   python   算法   

Dynamic Programming

DP 的历史渊源:

Richard E. Bellman (1920-1984)
    Richard Bellman received the IEEE Medal of Honor, 1979. “Bellman . . . explained that he invented the name ‘dynamid programming’ to hide the fact that he was doing mathe-matical research at RAND under a Secretary of Defense who ‘had a pathological fear and hatred of the term, research’. He settled on the term ‘dynamic programming’ because it would be difficult to give a ‘pejorative meaning’ and because ‘it was something not even a
Congressman could object to’ ” [John Rust 2006]



问题一:Fibonacci 数列求解问题。

一般的会采用递归实现。

技术分享

/****************************************************
code file : Fibonacci.c
code date : 2014.11.25
e-mail    : jasonleaster@gmail.com

code description:
	
	Here is a implementation of funciont @fib()
which will help us to compute the fibonacci array
and what we should pay attention to is that this 
method is using recursion!

	Reader must know that this method is convenient
way which is used to compute fibonacci array but not a
elegent way.

*****************************************************/
#include <stdio.h>

int fib(int num)
{
	if(num <= 1)
	{
		return 1;
	}
	else
	{
		return fib(num-1) + fib(num - 2);
	}
}

int main()
{
	int number = 0;
	
	while(!scanf("%d",&number))
	{
		getchar();
		printf("\nerror!input again!\n");
	}

	printf("The %dth fibonacci number is %d\n",number,fib(number));

	return 0;
}
但是递归实现的内存消耗和时间消耗都太大了,是O(2^(n/2))质数爆炸


采用非递归的策略,即动态规划:

技术分享


/****************************************************
code file : Fibonacci.c
code date : 2014.11.25
e-mail    : jasonleaster@gmail.com

code description:
	
	Here is a implementation of funciont @fib()
which will help us to compute the fibonacci array.

	Aha! Just have glance with my code and you
will find that we didn't use recursion!YES! We do!

	It's very elegent to store the last two var-
ible @prev and @prev_prev which is close to 
current number.Something like this, we have a array,
1 2 3 4 5, @prev is 4 @prev_prev 3, current number is 
5 :)
	This method that we call it as dynamic pro-
gramming.

*****************************************************/

#include <stdio.h>

int main()
{
	int number = 0;
	int prev   = 1;
	int prev_prev = 1;
	int result = 1;
	
	while(!scanf("%d",&number))
	{
		getchar();
		printf("\nerror!input again!\n");
	}

	int temp = 0;
	for(temp = 3;temp <= number;temp++)
	{
		result = prev + prev_prev;
		prev_prev = prev;
		prev = result;
	}

	printf("The %dth fibonacci number is %d\n",number,result);
	
	return 0;
}



问题二: 工厂加工汽车的流水工作规划问题。

(这个是CLRS第二版上面第15章讲DP的第一个问题,但是第三章的时候被替换了)。可以从第二版上找到这个问题的详细分析。

技术分享

Python 实现:

"""
Code writer : EOF
Code date   : 2015.02.03
e-mail      : jasonleaster@163.com

Code description :
    Here is a implementation in Python as a solution
for Colonel-problem which is in <CLRS> second edition 
chapter 15.

"""

def fastest_way(a, t, e, x, n) :

    f1 = []
    f2 = []
    path = [[],[]]
    # when j == 0
    f1 += [ e[0] + a[0][0] ]
    f2 += [ e[1] + a[1][0] ]

    for j in range(1, n) :
        if (f1[j-1] + a[0][j]) <= (f2[j-1] + t[1][j-1] + a[0][j]) :
            f1 += [ f1[j-1] + a[0][j] ]
            path[0] += [0]
        else :
            f1 += [ f2[j-1] + t[1][j-1] + a[0][j] ]
            path[0] += [1]

        if (f2[j-1] + a[1][j]) <= (f1[j-1] + t[0][j-1] + a[1][j]) :
            f2 += [ f2[j-1] + a[1][j] ]
            path[1] += [1]
        else :
            f2 += [ f1[j-1] + t[0][j-1] + a[1][j] ]
            path[1] += [0]

    if f1[n-1] + x[0] <= f2[n-1] + x[1] :
        smallest_cost = f1[n-1] + x[0]
        path[0] += [0]
        path[1] += [0]
    else :
        smallest_cost = f2[n-1] + x[1]
        path[0] += [1]
        path[1] += [1]

    return path

def print_stations(path) :

    n = len(path[0])
    i = path[0][n-1]
    print "line ", i, ", station", n

    for j in range(n-1, -1, -1) :
        i = path[i][j]
        print "line ", i, ", station", j

#---------------- for testing -----------------------------------------
a = [[7,9,3,4,8,4],[8,5,6,4,5,7]]
t = [[2,3,1,3,4]  ,[2,1,2,2,1]  ]

e = [2,4]
x = [3,2]
n = len(a[0])


path = fastest_way(a, t, e, x, n)
print_stations(path)

测试结果:

技术分享













                                     Tencent 的AD被屏蔽之后也是蛮逗的。。。

技术分享


Dynamic Programming 动态规划

标签:algorithm   python   算法   

原文地址:http://blog.csdn.net/cinmyheart/article/details/43484775

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