MIT:算法导论——15.动态规划

【设计一个动态规划算法的四个步骤】

1、刻画一个最优解的特征。 (最优子结构?!)

2、递归地定义最优解的值。

3、计算最优解的值,通常采用自底向上方法。

4、利用计算出的信息构造一个最优解。


适合动态规划方法求解的最优化问题需要具备的两个性质:

【最优子结构(optimal substructure)】

问题的最优解由相关子问题的最优解组合而成,而这些子问题可以独立求解。(动态规划具体实现之处)

【重叠子问题(overlapping subproblem)】

如果递归算法反复求解相同的子问题,我们就称最优化问题具有重叠子问题性质。(动态规划实现优化之处)

【动态规划两种等价实现方法】

1、 带备忘的自顶向下法(top-down with memoizatioin)

2、自底向上方法(bottom-up method)


问题一:钢条切割

背景:长度为i的钢条价格为p[i],长为n的钢条的怎么切割有最大收益。

长度i:   1 2 3 4 5 6 7 8 9 10

价格p[i]: 1 5 8 9 10 17 17 20 24 30

方案:

收益r[i] = max(p[j],r[i-j]),j:[1,i]**********************************************(2)


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>

#define MAXINT 0x7FFFFFFF
#define MININT -(signed int)MAXINT - 1

using namespace std;

int bottom_up_cut_rod( int *p, int n, int *s );
int bp_cut_rod(vector<int> &vPrices, vector<int> &vCuts);
void output_cut_rod_solution(vector<int> &vPrices, vector<int> &vCuts);
int main( void )
{
	cout << "********最大值/最小值******************" << endl; 
	cout << MAXINT << endl;
	cout << MININT << endl;

	cout << "********动态规划:钢条切割最大收益*****" << endl;
	int p[] = { 0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30 };
	int n = sizeof( p ) / sizeof( int ) - 1;
	//cout << sizeof( p ) / sizeof( int ) << endl; // 输出11,sizeof(p),p为数组,为总大小44
	
	vector<int> vPrices(p, p + n + 1);
	vector<int> vCuts(n + 1);

	output_cut_rod_solution(vPrices, vCuts);
	cout << "最优解总数据:" << endl;
	for( int i = 0; i <= n; ++i )
		cout << vCuts[i] << " ";
	cout << endl;

	
	return 0;
}

/*
 * 动态规划算法——自底向上计算:钢条切割,求切为多长为最优解。
 * i = j + (i -j),即将长度为i的钢条分割为长度为j(整段)、长度为i - j(再分割段)的两段,
 * j = [1,i],即只且长为1的,至整段出售;而i - j在之前(bottom_up)已经计算。
 * rod[rɒd] n. 棒 —— cut rod
 *
 * 输出参数:int *s     输出参数,存储最优解
 * 返回值  :int	    长度为n的棒,最大收益
 * 
 */
int bp_cut_rod(vector<int> &vPrices, vector<int> &vCuts)
{
	vector<int>::size_type n = vPrices.size() - 1;
	vector<int> vRods(n + 1); // 长为i的最优价值
	// vRods.reserve(n + 1); // 此时如果直接访问,会报错;因为只是预留空间,并未初始化。
	vRods[0] = 0;

	for (vector<int>::size_type i = 1; i <= n; i += 1){ // 求长为i的最优收益
		int iSum = 0;
		int iVal = 0;
		for (vector<int>::size_type j = 1; j <= i; j += 1){
			iSum = vPrices[j] + vRods[i - j];
			if (iSum > iVal){
				iVal = iSum;
				vCuts[i] = j; // 记录最优解
			}
		}
		vRods[i] = iVal;
	}
	return vRods[n];
}

void output_cut_rod_solution(vector<int> &vPrices, vector<int> &vCuts)
{
	cout << "最优价值:" << bp_cut_rod(vPrices, vCuts) << endl;
	cout << "最优解:" << endl;
	
	int n = vCuts.size() - 1;
	while (n > 0){
		cout << vCuts[n] << endl;
		n -= vCuts[n];
	}
}


int bp_cut_rod(vector<int> &vPrices, vector<int> &vCuts) *******************************(3)

算法复杂度分析:

其内层for循环的迭代次数构成一个等差数列,所以运行时间复杂度为O(n^2)。


void output_cut_rod_solution(vector<int> &vPrices, vector<int> &vCuts) *******************(4)





==============================================

LCS最长公共子序列:不连续子序列和连续子序列

【最长公共子序列问题】longest-common-subsequence problem
给定两个序列X = <x1, x2, ..., xm>和Y = <y1, y2, ..., yn>,求X和Y的最长公共子序列。
【步骤1】刻画最长公共子序列的特征
定理15.1(LCS的最优子结构) 令X = <x1, x2, ..., xm>和Y = <y1, y2, ..., yn>为两个序列,
Z = <z1, z2, ..., zk>为X和Y的任意LCS。有:
(1)如果xm = yn,则zk = xm = yn, 且Zk-1是Xm-1和Yn-1的一个LCS。
(2)如果xm != yn,那么zk != xm意味着Z是Xm-1和Y的一个LCS。
(3)如果xm != yn,那么zk != yn意味着Z是X和Yn-1的一个LCS。
【步骤2】一个递归解
【LCS问题的重叠子问题性质】为求X和Y的一个LCS,我们可能需要求Xm-1和Y的一个LCS以及X和Yn-1的一个LCS,
但是这个子问题都包含求解Xm-1和Yn-1的LCS的子问题。
设计递归算法,首先要设计最优解的递归式:
c[i,j] = { 0 if i == 0 || j == 0
  { c[i-1, j-1] + 1if i,j > 0 && Xi == Yj
  { max(c[i-1, c[j-1])if i,j > 0 && Xi != Yj
【步骤3】计算LCS的长度
(1)采用自底向上方法
(2)c[i, j]保存在表c[0...m, 0...n]中,并按行主次序。
(3)表b[0...m, 0...n]保存c[i,j]所选择的子问题,以构造最优解
【步骤4】构造LCS
从b[m, n]开始,向上追踪:当箭头(→)指向左上角时,xi == yj。


【当要求必须连续时】
c[i,j] = { 0 if i == 0 || j == 0
  { c[i-1, j-1] + 1if i,j > 0 && Xi == Yj
  { 0 if i,j > 0 && Xi != Yj
(1)由于要求连续则Xi和Yj一定为结尾的字符,当i,j > 0 && Xi != Yj时,c[i, j] = 0。
(2)当if i,j > 0 && Xi == Yj,需要记录X的low,high,max.以及tmpL,tmpH,tmpMax = 0。


不要求连续的代码实现:

#ifndef ALGORITHM_H
#define ALGORITHM_H

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <bitset>

#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>

#include <utility>
#include <map>
#include <set>

#include <numeric>
#include <algorithm>

#endif

#include "algorithm.h"
#define LEFT	1
#define LU		0
#define UP		2

using namespace std;

int lcs_length(string &sX, string &sY);
int main(void)
{
	string sX("abcbdab");
	string sY("bdcaba");
	cout << lcs_length(sX, sY) << endl;

	return 0;
}


int lcs_length(string &sX, string &sY)
{
	int m = sX.size();
	int n = sY.size();
	int **c = new int*[m + 1];
	int **b = new int*[m + 1];
	for (int i = 0; i <= m; i += 1){
		c[i] = new int[n + 1];
		b[i] = new int[n + 1];
	}

	for (int i = 0; i <= m; i += 1){
		for (int j = 0; j <= n; j += 1){
			if (i == 0 || j == 0){
				c[i][j] = 0;
				b[i][j] = 3;
			}else if (sX[i - 1] == sY[j - 1]){
				c[i][j] = c[i - 1][j - 1] + 1;
				b[i][j] = LU;
			}else{
				if (c[i - 1][j] > c[i][j - 1]){
					c[i][j] = c[i - 1][j];
					b[i][j] = UP;
				}else{
					c[i][j] = c[i][j - 1];
					b[i][j] = LEFT;
				}
			}
			cout << c[i][j] << "," << b[i][j] << "\t";
		}// end-for
		cout << endl;
	}// end-for

	int i = m;
	int j = n;
	stack<char> stCh;
	while (i > 0 && j > 0 && c[i][j] > 0){
		if (b[i][j] == LU){
			stCh.push(sX[i - 1]);
			i -= 1;
			j -= 1;
		}else{
			if (b[i][j] == UP){
				i -= 1;
			}else{
				j -= 1;
			}
		}
	}
	while (!stCh.empty()){
		cout << stCh.top();
		stCh.pop();
	}
	cout << endl;
	int nLCSLen = c[m][n];

	for (i = 0; i <= m; i += 1){
		delete [] c[i];
		delete [] b[i];
	}
	delete [] c;
	delete [] b;
	return nLCSLen;
}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值