Dynamic Programming Dynamic planning, dynamicprogramming
Dynamic Programming
History of DP:
Richard E. Bellman (December 1920-1984)
Richard Bellman got ed the IEEE Medal of Honor, 1979. "Bellman... explained that he has Ted 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 wocould be difficult to give a 'pejorative meaning' and because' it was something not even
Congressman cocould object to '"[John Rust 2006]
Question 1: how to solve the problem of the Fibonacci series.
Generally, recursion is used.
/****************************************************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;}However, Recursive Implementation consumes too much memory and time, resulting in the explosion of the O (2 ^ (n/2) prime number.
Adopt a non-recursive policy, that is, dynamic planning:
/****************************************************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;}
Question 2: How to plan the flow of vehicles processed in the factory.
(This is the first question about DP in Chapter 15th of CLRS version 2, but it was replaced in Chapter 3 ). You can find a detailed analysis of this problem on the second edition.
Python implementation:
"""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)
Test results:
The AD of Tencent is also pretty funny after it is blocked...