Dynamic Programming 動態規劃,dynamicprogramming
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 mathematical 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.ccode date : 2014.11.25e-mail : jasonleaster@gmail.comcode description:Here is a implementation of funciont @fib()which will help us to compute the fibonacci arrayand what we should pay attention to is that this method is using recursion!Reader must know that this method is convenientway which is used to compute fibonacci array but not aelegent 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.ccode date : 2014.11.25e-mail : jasonleaster@gmail.comcode 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 youwill 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 : EOFCode date : 2015.02.03e-mail : jasonleaster@163.comCode description : Here is a implementation in Python as a solutionfor 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 pathdef 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被屏蔽之後也是蠻逗的。。。