今日研究分支限界算法,拿了王晓东老师的算法设计与分析教材。仔细读懂了书中算法和代码且找出了算法的些许错误,在我写的第一篇原创博客中和大家分享。
对于装载问题这个实例,老师首先找到了O(2^n)复杂度的算法找到最大装载问题的解,然后对这个算法进行类似剪枝的操作,使得算法更加优化。为了找到最优解对应的装载策略,对代码进行了改进,在搜索子集树中保存当前已构造出的子集树中的路径指针,从而可在结束搜索后向根结点回溯,从而构造出最优装载。
直接上代码:
#include"queue.h"
template<class Type>
class Qnode
{
//friend void enqueue(Queue<Qnode<Type>*>&q, Qnode<Type>*&qn,Qnode<Type>*&best,Type result,Type bestw,int i,int n,bool ch,int bestx[]);
//friend Type maxloading(Type a[],Type c, int n, int bestx[]);
public:
Qnode<Type>* parent;
bool chosen;
Type weigth;
};