分支限界法

来源:互联网 发布:重庆市科委人工智能 编辑:程序博客网 时间:2024/04/29 20:57
 

0033算法笔记——【分支限界法】分支限界法与单源最短路径问题

分类: 算法 408人阅读 评论(0) 收藏 举报
分支限界法单源最短路径问题算法笔记队列式分支限界法优先队列式分支限界法

      1、分支限界法

    (1)描述:采用广度优先产生状态空间树的结点,并使用剪枝函数的方法称为分枝限界法。

     所谓“分支”是采用广度优先的策略,依次生成扩展结点的所有分支(即:儿子结点)。
     所谓“限界”是在结点扩展过程中,计算结点的上界(或下界),边搜索边减掉搜索树的某些分支,从而提高搜索效率。

    (2)原理按照广度优先的原则,一个活结点一旦成为扩展结点(E-结点)R后,算法将依次生成它的全部孩子结点,将那些导致不可行解或导致非最优解的儿子舍弃,其余儿子加入活结点表中。然后,从活结点表中取出一个结点作为当前扩展结点。重复上述结点扩展过程,直至找到问题的解或判定无解为止。

    (3)分支限界法与回溯法

     1)求解目标:回溯法的求解目标是找出解空间树中满足约束条件的所有解,而分支限界法的求解目标则是找出满足约束条件的一个解,或是在满足约束条件的解中找出在某种意义下的最优解。 
     
2)搜索方式的不同:回溯法以深度优先的方式搜索解空间树,而分支限界法则以广度优先或以最小耗费优先的方式搜索解空间树。

   (4)常见的分支限界法

     1)FIFO分支限界法(队列式分支限界法)

     基本思想:按照队列先进先出(FIFO)原则选取下一个活结点为扩展结点。

     搜索策略:一开始,根结点是唯一的活结点,根结点入队。从活结点队中取出根结点后,作为当前扩展结点。对当前扩展结点,先从左到右地产生它的所有儿子,用约束条件检查,把所有满足约束函数的儿子加入活结点队列中。再从活结点表中取出队首结点(队中最先进来的结点)为当前扩展结点,……,直到找到一个解或活结点队列为空为止。

    2)LC(least cost)分支限界法(优先队列式分支限界法)

    基本思想:为了加速搜索的进程,应采用有效地方式选择活结点进行扩展。按照优先队列中规定的优先级选取优先级最高的结点成为当前扩展结点。

    搜索策略:对每一活结点计算一个优先级(某些信息的函数值),并根据这些优先级;从当前活结点表中优先选择一个优先级最高(最有利)的结点作为扩展结点,使搜索朝着解空间树上有最优解的分支推进,以便尽快地找出一个最优解。再从活结点表中下一个优先级别最高的结点为当前扩展结点,……,直到找到一个解或活结点队列为空为止。

     (5)分支限界法搜索应用举例

      1)0-1背包问题,当n=3时,w={16,15,15}, p={45,25,25}, c=30


     队列式分支限界法(处理法则:先进先出):{}—>{A}—>{B,C}—>{C,D,E}(D是不可行解,舍弃)—>{C,E}—>{E,F,G}—>{F,G,J,K}(J是不可行解,舍弃)—>{F,G,K}—>{G,K,L,M}—>{K,L,M,N,O}—>{}

     优先队列式分支限界法(处理法则:价值大者优先):{}—>{A}—>{B,C}—>{C,D,E}—>{C,E}—>{C,J,K}—>{C}—>{F,G}—>{G,L,M}—>{G,M}—>{G}—>{N,O}—>{O}—>{}

     2)旅行员售货问题


     队列式分支限界法(节点B开始):{ }—{B}—{C,D,E}—{D,E,F,G}—{E,F,G,H,I}—{F,G,H,I,J,K}—{G,H,I,J,K,L}—{H,I,J,K,L,M}—{I,J,K,L,M,N}—{J,K,L,M,N,O}—{K,L,M,N,O,P}—{L,M,N,O,P,Q}—{M,N,O,P,Q}—{N,O,P,Q}—{O,P,Q}—{P,Q}—{Q}—{ }

     优先队列式分支限界法:优先级是结点的当前费用:{ }—{B}—{C,D,E}—{C,D,J,K}—{C,J,K,H,I}—{C,J,K,I,N}—{C,K,I,N,P}—{C,I,N,P,Q}—{C,N,P,Q,O}—{C,P,Q,O}—{C,Q,O}—{Q,O,F,G}—{Q,O,G,L}—{Q,O,L,M}—{O,L,M}—{O,M}—{M}—{ }

     2、单源最短路径问题

    问题描述

     在下图所给的有向图G中,每一边都有一个非负边权。要求图G的从源顶点s到目标顶点t之间的最短路径。


     下图是用优先队列式分支限界法解有向图G的单源最短路径问题产生的解空间树。其中,每一个结点旁边的数字表示该结点所对应的当前路长。

     

    算法设计

     算法从图G的源顶点s和空优先队列开始。结点s被扩展后,它的儿子结点被依次插入堆中。此后,算法从堆中取出具有最小当前路长的结点作为当前扩展结点,并依次检查与当前扩展结点相邻的所有顶点。如果从当前扩展结点i到顶点j有边可达,且从源出发,途经顶点i再到顶点j的所相应的路径的长度小于当前最优路径长度,则将该顶点作为活结点插入到活结点优先队列中。这个结点的扩展过程一直继续到活结点优先队列为空时为止。

    在算法扩展结点的过程中,一旦发现一个结点的下界不小于当前找到的最短路长,则算法剪去以该结点为根的子树
    在算法中,利用结点间的控制关系进行剪枝。从源顶点s出发,2条不同路径到达图G的同一顶点。由于两条路径的路长不同,因此可以将路长长的路径所对应的树中的结点为根的子树剪去。 

     算法具体代码如下:

     1、MinHeap2.h

[cpp] view plaincopy
  1. #include <iostream>  
  2.   
  3. template<class Type>  
  4. class Graph;  
  5.   
  6. template<class T>   
  7. class MinHeap   
  8. {   
  9.     template<class Type>  
  10.     friend class Graph;  
  11.     public:   
  12.         MinHeap(int maxheapsize = 10);   
  13.         ~MinHeap(){delete []heap;}   
  14.   
  15.         int Size() const{return currentsize;}   
  16.         T Max(){if(currentsize) return heap[1];}   
  17.   
  18.         MinHeap<T>& Insert(const T& x);   
  19.         MinHeap<T>& DeleteMin(T &x);   
  20.   
  21.         void Initialize(T x[], int size, int ArraySize);   
  22.         void Deactivate();   
  23.         void output(T a[],int n);  
  24.     private:   
  25.         int currentsize, maxsize;   
  26.         T *heap;   
  27. };   
  28.   
  29. template <class T>   
  30. void MinHeap<T>::output(T a[],int n)   
  31. {   
  32.     for(int i = 1; i <= n; i++)   
  33.     cout << a[i] << " ";   
  34.     cout << endl;   
  35. }   
  36.   
  37. template <class T>   
  38. MinHeap<T>::MinHeap(int maxheapsize)   
  39. {   
  40.     maxsize = maxheapsize;   
  41.     heap = new T[maxsize + 1];   
  42.     currentsize = 0;   
  43. }   
  44.   
  45. template<class T>   
  46. MinHeap<T>& MinHeap<T>::Insert(const T& x)   
  47. {   
  48.     if(currentsize == maxsize)   
  49.     {   
  50.         return *this;   
  51.     }   
  52.     int i = ++currentsize;   
  53.     while(i != 1 && x < heap[i/2])   
  54.     {   
  55.         heap[i] = heap[i/2];   
  56.         i /= 2;   
  57.     }   
  58.   
  59.     heap[i] = x;   
  60.     return *this;   
  61. }   
  62.   
  63. template<class T>   
  64. MinHeap<T>& MinHeap<T>::DeleteMin(T& x)   
  65. {   
  66.     if(currentsize == 0)   
  67.     {   
  68.         cout<<"Empty heap!"<<endl;   
  69.         return *this;   
  70.     }   
  71.   
  72.     x = heap[1];   
  73.   
  74.     T y = heap[currentsize--];   
  75.     int i = 1, ci = 2;   
  76.     while(ci <= currentsize)   
  77.     {   
  78.         if(ci < currentsize && heap[ci] > heap[ci + 1])   
  79.         {   
  80.             ci++;   
  81.         }   
  82.   
  83.         if(y <= heap[ci])   
  84.         {   
  85.             break;   
  86.         }   
  87.         heap[i] = heap[ci];   
  88.         i = ci;   
  89.         ci *= 2;   
  90.     }   
  91.   
  92.     heap[i] = y;   
  93.     return *this;   
  94. }   
  95.   
  96. template<class T>   
  97. void MinHeap<T>::Initialize(T x[], int size, int ArraySize)   
  98. {   
  99.     delete []heap;   
  100.     heap = x;   
  101.     currentsize = size;   
  102.     maxsize = ArraySize;   
  103.   
  104.     for(int i = currentsize / 2; i >= 1; i--)   
  105.     {   
  106.         T y = heap[i];   
  107.         int c = 2 * i;   
  108.         while(c <= currentsize)   
  109.         {   
  110.             if(c < currentsize && heap[c] > heap[c + 1])   
  111.                 c++;   
  112.             if(y <= heap[c])   
  113.                 break;   
  114.             heap[c / 2] = heap[c];   
  115.             c *= 2;   
  116.         }   
  117.         heap[c / 2] = y;   
  118.     }   
  119. }   
  120.   
  121. template<class T>   
  122. void MinHeap<T>::Deactivate()   
  123. {   
  124.     heap = 0;   
  125. }   
     2、6d2.cpp

[cpp] view plaincopy
  1. //单源最短路径问题 分支 限界法求解  
  2. #include "stdafx.h"  
  3. #include "MinHeap2.h"  
  4. #include <iostream>  
  5. #include <fstream>   
  6. using namespace std;  
  7.   
  8. ifstream fin("6d2.txt");   
  9.   
  10. template<class Type>  
  11. class Graph  
  12. {  
  13.     friend int main();  
  14.     public:  
  15.         void ShortesPaths(int);  
  16.     private:  
  17.         int     n,         //图G的顶点数  
  18.                 *prev;     //前驱顶点数组  
  19.         Type    **c,       //图G的领接矩阵  
  20.                 *dist;     //最短距离数组  
  21. };   
  22.   
  23. template<class Type>  
  24. class MinHeapNode  
  25. {  
  26.    friend Graph<Type>;  
  27.    public:  
  28.        operator int ()const{return length;}  
  29.    private:  
  30.        int       i;       //顶点编号  
  31.        Type  length;      //当前路长  
  32. };   
  33.   
  34. template<class Type>  
  35. void Graph<Type>::ShortesPaths(int v)//单源最短路径问题的优先队列式分支限界法  
  36. {   
  37.     MinHeap<MinHeapNode<Type>> H(1000);  
  38.     MinHeapNode<Type> E;  
  39.   
  40.     //定义源为初始扩展节点  
  41.     E.i=v;  
  42.     E.length=0;  
  43.     dist[v]=0;  
  44.   
  45.     while (true)//搜索问题的解空间  
  46.     {  
  47.         for (int j = 1; j <= n; j++)  
  48.             if ((c[E.i][j]!=0)&&(E.length+c[E.i][j]<dist[j])) {  
  49.   
  50.                  // 顶点i到顶点j可达,且满足控制约束  
  51.                  dist[j]=E.length+c[E.i][j];  
  52.                  prev[j]=E.i;  
  53.   
  54.                  // 加入活结点优先队列  
  55.                  MinHeapNode<Type> N;  
  56.                  N.i=j;  
  57.                  N.length=dist[j];  
  58.                  H.Insert(N);  
  59.             }`  
  60.         try   
  61.         {  
  62.             H.DeleteMin(E); // 取下一扩展结点  
  63.         }          
  64.         catch (int)   
  65.         {  
  66.             break;  
  67.         }    
  68.         if (H.currentsize==0)// 优先队列空  
  69.         {  
  70.             break;  
  71.         }  
  72.     }  
  73. }  
  74.   
  75. int main()  
  76. {    
  77.     int n=11;  
  78.     int prev[12] = {0,0,0,0,0,0,0,0,0,0,0,0};    
  79.   
  80.     int dist[12]={1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000};  
  81.   
  82.     cout<<"单源图的邻接矩阵如下:"<<endl;  
  83.     int **c = new int*[n+1];  
  84.   
  85.     for(int i=1;i<=n;i++)  
  86.     {  
  87.         c[i]=new int[n+1];  
  88.         for(int j=1; j<=n; j++)  
  89.         {  
  90.             fin>>c[i][j];  
  91.             cout<<c[i][j]<<" ";  
  92.         }  
  93.         cout<<endl;  
  94.     }  
  95.   
  96.     int v=1;  
  97.     Graph<int> G;  
  98.     G.n=n;  
  99.   
  100.     G.c=c;  
  101.     G.dist=dist;  
  102.     G.prev=prev;  
  103.     G.ShortesPaths(v);  
  104.   
  105.     cout<<"从S到T的最短路长是:"<<dist[11]<<endl;  
  106.     for (int i = 2; i <= n; i++)  
  107.     {  
  108.         cout<<"prev("<<i<<")="<<prev[i]<<"   "<<endl;  
  109.     }  
  110.   
  111.     for (int i = 2; i <= n; i++)  
  112.     {  
  113.         cout<<"从1到"<<i<<"的最短路长是:"<<dist[i]<<endl;  
  114.     }  
  115.   
  116.     for(int i=1;i<=n;i++)  
  117.     {  
  118.         delete []c[i];  
  119.     }  
  120.   
  121.     delete []c;  
  122.     c=0;      
  123.     return 0;  
  124. }  
     程序运行结果如图:



 

0034算法笔记——【分支限界法】最优装载问题

分类: 算法 279人阅读 评论(0) 收藏 举报
最优装载问题分支限界法算法笔记最大堆优先队列式

        问题描述

      有一批共个集装箱要装上2艘载重量分别为C1和C2的轮船,其中集装箱i的重量为Wi,且装载问题要求确定是否有一个合理的装载方案可将这个集装箱装上这2艘轮船。如果有,找出一种装载方案。 

     容易证明:如果一个给定装载问题有解,则采用下面的策略可得到最优装载方案。 
     (1)首先将第一艘轮船尽可能装满;
     (2)将剩余的集装箱装上第二艘轮船。 

     1、队列式分支限界法求解

      在算法的循环体中,首先检测当前扩展结点的左儿子结点是否为可行结点。如果是则将其加入到活结点队列中。然后将其右儿子结点加入到活结点队列中(右儿子结点一定是可行结点)。2个儿子结点都产生后,当前扩展结点被舍弃。

     活结点队列中的队首元素被取出作为当前扩展结点,由于队列中每一层结点之后都有一个尾部标记-1,故在取队首元素时,活结点队列一定不空。当取出的元素是-1时,再判断当前队列是否为空。如果队列非空,则将尾部标记-1加入活结点队列,算法开始处理下一层的活结点。

     节点的左子树表示将此集装箱装上船,右子树表示不将此集装箱装上船。设bestw是当前最优解;ew是当前扩展结点所相应的重量;r是剩余集装箱的重量。则当ew+r<bestw时,可将其右子树剪去,因为此时若要船装最多集装箱,就应该把此箱装上船。另外,为了确保右子树成功剪枝,应该在算法每一次进入左子树的时候更新bestw的值。

     为了在算法结束后能方便地构造出与最优值相应的最优解,算法必须存储相应子集树中从活结点到根结点的路径。为此目的,可在每个结点处设置指向其父结点的指针,并设置左、右儿子标志。

     找到最优值后,可以根据parent回溯到根节点,找到最优解。

     算法具体代码实现如下:

     1、Queue.h

[cpp] view plaincopy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. template <class T>  
  5. class Queue  
  6. {  
  7.     public:  
  8.         Queue(int MaxQueueSize=50);  
  9.         ~Queue(){delete [] queue;}  
  10.         bool IsEmpty()const{return front==rear;}  
  11.         bool IsFull(){return ( (  (rear+1)  %MaxSize==front )?1:0);}  
  12.         T Top() const;  
  13.         T Last() const;  
  14.         Queue<T>& Add(const T& x);  
  15.         Queue<T>& AddLeft(const T& x);  
  16.         Queue<T>& Delete(T &x);  
  17.         void Output(ostream& out)const;  
  18.         int Length(){return (rear-front);}  
  19.     private:  
  20.         int front;  
  21.         int rear;  
  22.         int MaxSize;  
  23.         T *queue;  
  24. };  
  25.   
  26. template<class T>  
  27. Queue<T>::Queue(int MaxQueueSize)  
  28. {  
  29.     MaxSize=MaxQueueSize+1;  
  30.     queue=new T[MaxSize];  
  31.     front=rear=0;  
  32. }  
  33.   
  34. template<class T >  
  35. T Queue<T>::Top()const  
  36. {  
  37.     if(IsEmpty())  
  38.     {  
  39.         cout<<"queue:no element,no!"<<endl;  
  40.         return 0;  
  41.     }  
  42.     else return queue[(front+1) % MaxSize];  
  43. }  
  44.   
  45. template<class T>  
  46. T Queue<T> ::Last()const  
  47. {  
  48.     if(IsEmpty())  
  49.     {  
  50.         cout<<"queue:no element"<<endl;  
  51.         return 0;  
  52.     }  
  53.     else return queue[rear];  
  54. }  
  55.   
  56. template<class T>  
  57. Queue<T>&  Queue<T>::Add(const T& x)  
  58. {  
  59.     if(IsFull())cout<<"queue:no memory"<<endl;  
  60.     else  
  61.     {  
  62.         rear=(rear+1)% MaxSize;  
  63.         queue[rear]=x;  
  64.     }  
  65.     return *this;  
  66. }  
  67.   
  68. template<class T>  
  69. Queue<T>&  Queue<T>::AddLeft(const T& x)  
  70. {  
  71.     if(IsFull())cout<<"queue:no memory"<<endl;  
  72.     else  
  73.     {  
  74.         front=(front+MaxSize-1)% MaxSize;  
  75.         queue[(front+1)% MaxSize]=x;  
  76.     }  
  77.     return *this;  
  78. }  
  79.   
  80. template<class T>  
  81. Queue<T>&  Queue<T> ::Delete(T & x)  
  82. {  
  83.     if(IsEmpty())cout<<"queue:no element(delete)"<<endl;  
  84.     else   
  85.     {  
  86.         front=(front+1) % MaxSize;  
  87.         x=queue[front];  
  88.     }  
  89.     return *this;  
  90. }  
  91.   
  92.   
  93. template<class T>  
  94. void Queue <T>::Output(ostream& out)const  
  95. {  
  96.     for(int i=rear%MaxSize;i>=(front+1)%MaxSize;i--)  
  97.        out<<queue[i];  
  98. }  
  99.   
  100. template<class T>  
  101. ostream& operator << (ostream& out,const Queue<T>& x)  
  102. {x.Output(out);return out;}  
      2、6d3-1.cpp

[cpp] view plaincopy
  1. //装载问题 队列式分支限界法求解   
  2. #include "stdafx.h"  
  3. #include "Queue.h"  
  4. #include <iostream>  
  5. using namespace std;  
  6.   
  7. const int N = 4;  
  8.   
  9. template<class Type>  
  10. class QNode  
  11. {  
  12.     template<class Type>  
  13.     friend void EnQueue(Queue<QNode<Type>*>&Q,Type wt,int i,int n,Type bestw,QNode<Type>*E,QNode<Type> *&bestE,int bestx[],bool ch);  
  14.   
  15.     template<class Type>  
  16.     friend Type MaxLoading(Type w[],Type c,int n,int bestx[]);  
  17.   
  18.     private:  
  19.         QNode *parent;  //指向父节点的指针  
  20.         bool LChild;    //左儿子标识  
  21.         Type weight;    //节点所相应的载重量  
  22. };  
  23.   
  24. template<class Type>  
  25. void EnQueue(Queue<QNode<Type>*>&Q,Type wt,int i,int n,Type bestw,QNode<Type>*E,QNode<Type> *&bestE,int bestx[],bool ch);  
  26.   
  27. template<class Type>  
  28. Type MaxLoading(Type w[],Type c,int n,int bestx[]);  
  29.   
  30. int main()  
  31. {  
  32.     float c = 70;    
  33.     float w[] = {0,20,10,26,15};//下标从1开始    
  34.     int x[N+1];    
  35.     float bestw;  
  36.     
  37.     cout<<"轮船载重为:"<<c<<endl;    
  38.     cout<<"待装物品的重量分别为:"<<endl;    
  39.     for(int i=1; i<=N; i++)    
  40.     {    
  41.         cout<<w[i]<<" ";    
  42.     }    
  43.     cout<<endl;    
  44.     bestw = MaxLoading(w,c,N,x);    
  45.     
  46.     cout<<"分支限界选择结果为:"<<endl;    
  47.     for(int i=1; i<=4; i++)    
  48.     {    
  49.         cout<<x[i]<<" ";    
  50.     }    
  51.     cout<<endl;    
  52.     cout<<"最优装载重量为:"<<bestw<<endl;  
  53.     
  54.     return 0;    
  55. }  
  56.   
  57. //将活节点加入到活节点队列Q中  
  58. template<class Type>  
  59. void EnQueue(Queue<QNode<Type>*>&Q,Type wt,int i,int n,Type bestw,QNode<Type>*E,QNode<Type> *&bestE,int bestx[],bool ch)  
  60. {  
  61.     if(i == n)//可行叶节点  
  62.     {  
  63.         if(wt == bestw)  
  64.         {  
  65.             //当前最优装载重量  
  66.             bestE = E;  
  67.             bestx[n] = ch;            
  68.         }  
  69.         return;  
  70.     }  
  71.     //非叶节点  
  72.     QNode<Type> *b;  
  73.     b = new QNode<Type>;  
  74.     b->weight = wt;  
  75.     b->parent = E;  
  76.     b->LChild = ch;  
  77.     Q.Add(b);  
  78. }  
  79.   
  80. template<class Type>  
  81. Type MaxLoading(Type w[],Type c,int n,int bestx[])  
  82. {//队列式分支限界法,返回最优装载重量,bestx返回最优解  
  83.  //初始化  
  84.     Queue<QNode<Type>*> Q;      //活节点队列  
  85.     Q.Add(0);                   //同层节点尾部标识  
  86.     int i = 1;                  //当前扩展节点所处的层  
  87.     Type Ew = 0,                //扩展节点所相应的载重量  
  88.          bestw = 0,             //当前最优装载重量  
  89.          r = 0;                 //剩余集装箱重量  
  90.   
  91.     for(int j=2; j<=n; j++)  
  92.     {  
  93.         r += w[j];  
  94.     }  
  95.       
  96.     QNode<Type> *E = 0,           //当前扩展节点  
  97.                 *bestE;         //当前最优扩展节点  
  98.   
  99.     //搜索子集空间树  
  100.     while(true)  
  101.     {  
  102.         //检查左儿子节点  
  103.         Type wt = Ew + w[i];  
  104.         if(wt <= c)//可行节点  
  105.         {  
  106.             if(wt>bestw)  
  107.             {  
  108.                 bestw = wt;  
  109.             }  
  110.             EnQueue(Q,wt,i,n,bestw,E,bestE,bestx,true);  
  111.         }  
  112.   
  113.         //检查右儿子节点  
  114.         if(Ew+r>bestw)  
  115.         {  
  116.             EnQueue(Q,Ew,i,n,bestw,E,bestE,bestx,false);  
  117.         }  
  118.         Q.Delete(E);//取下一扩展节点  
  119.   
  120.         if(!E)//同层节点尾部  
  121.         {  
  122.             if(Q.IsEmpty())  
  123.             {  
  124.                 break;  
  125.             }  
  126.             Q.Add(0);       //同层节点尾部标识  
  127.             Q.Delete(E);    //取下一扩展节点  
  128.             i++;            //进入下一层  
  129.             r-=w[i];        //剩余集装箱重量  
  130.         }  
  131.         Ew  =E->weight;      //新扩展节点所对应的载重量  
  132.     }  
  133.   
  134.     //构造当前最优解  
  135.     for(int j=n-1; j>0; j--)  
  136.     {  
  137.         bestx[j] = bestE->LChild;  
  138.         bestE = bestE->parent;  
  139.     }  
  140.     return bestw;  
  141. }  
     程序运行结果如图:


     2、优先队列式分支限界法求解

      解装载问题的优先队列式分支限界法用最大优先队列存储活结点表。活结点x在优先队列中的优先级定义为从根结点到结点x的路径所相应的载重量再加上剩余集装箱的重量之和。
     优先队列中优先级最大的活结点成为下一个扩展结点。以结点x为根的子树中所有结点相应的路径的载重量不超过它的优先级。子集树中叶结点所相应的载重量与其优先级相同。
     在优先队列式分支限界法中,一旦有一个叶结点成为当前扩展结点,则可以断言该叶结点所相应的解即为最优解。此时可终止算法。

     算法具体代码实现如下:

     1、MaxHeap.h

[cpp] view plaincopy
  1. template<class T>  
  2. class MaxHeap  
  3. {  
  4.     public:  
  5.         MaxHeap(int MaxHeapSize = 10);  
  6.         ~MaxHeap() {delete [] heap;}  
  7.         int Size() const {return CurrentSize;}  
  8.   
  9.         T Max()   
  10.         {          //查  
  11.            if (CurrentSize == 0)  
  12.            {  
  13.                 throw OutOfBounds();  
  14.            }  
  15.            return heap[1];  
  16.         }  
  17.   
  18.         MaxHeap<T>& Insert(const T& x); //增  
  19.         MaxHeap<T>& DeleteMax(T& x);   //删  
  20.   
  21.         void Initialize(T a[], int size, int ArraySize);  
  22.   
  23.     private:  
  24.         int CurrentSize, MaxSize;  
  25.         T *heap;  // element array  
  26. };  
  27.   
  28. template<class T>  
  29. MaxHeap<T>::MaxHeap(int MaxHeapSize)  
  30. {// Max heap constructor.  
  31.     MaxSize = MaxHeapSize;  
  32.     heap = new T[MaxSize+1];  
  33.     CurrentSize = 0;  
  34. }  
  35.   
  36. template<class T>  
  37. MaxHeap<T>& MaxHeap<T>::Insert(const T& x)  
  38. {// Insert x into the max heap.  
  39.     if (CurrentSize == MaxSize)  
  40.     {  
  41.         cout<<"no space!"<<endl;   
  42.         return *this;   
  43.     }  
  44.   
  45.     // 寻找新元素x的位置  
  46.     // i——初始为新叶节点的位置,逐层向上,寻找最终位置  
  47.     int i = ++CurrentSize;  
  48.     while (i != 1 && x > heap[i/2])  
  49.     {  
  50.         // i不是根节点,且其值大于父节点的值,需要继续调整  
  51.         heap[i] = heap[i/2]; // 父节点下降  
  52.         i /= 2;              // 继续向上,搜寻正确位置  
  53.     }  
  54.   
  55.    heap[i] = x;  
  56.    return *this;  
  57. }  
  58.   
  59. template<class T>  
  60. MaxHeap<T>& MaxHeap<T>::DeleteMax(T& x)  
  61. {// Set x to max element and delete max element from heap.  
  62.     // check if heap is empty  
  63.     if (CurrentSize == 0)  
  64.     {  
  65.         cout<<"Empty heap!"<<endl;   
  66.         return *this;   
  67.     }  
  68.   
  69.     x = heap[1]; // 删除最大元素  
  70.     // 重整堆  
  71.     T y = heap[CurrentSize--]; // 取最后一个节点,从根开始重整  
  72.   
  73.     // find place for y starting at root  
  74.     int i = 1,  // current node of heap  
  75.        ci = 2; // child of i  
  76.   
  77.     while (ci <= CurrentSize)   
  78.     {  
  79.         // 使ci指向i的两个孩子中较大者  
  80.         if (ci < CurrentSize && heap[ci] < heap[ci+1])  
  81.         {  
  82.             ci++;  
  83.         }  
  84.         // y的值大于等于孩子节点吗?  
  85.         if (y >= heap[ci])  
  86.         {  
  87.             break;   // 是,i就是y的正确位置,退出  
  88.         }  
  89.   
  90.         // 否,需要继续向下,重整堆  
  91.         heap[i] = heap[ci]; // 大于父节点的孩子节点上升  
  92.         i = ci;             // 向下一层,继续搜索正确位置  
  93.         ci *= 2;  
  94.     }  
  95.   
  96.     heap[i] = y;  
  97.     return *this;  
  98. }  
  99.   
  100. template<class T>  
  101. void MaxHeap<T>::Initialize(T a[], int size,int ArraySize)  
  102. {// Initialize max heap to array a.  
  103.     delete [] heap;  
  104.     heap = a;  
  105.     CurrentSize = size;  
  106.     MaxSize = ArraySize;  
  107.   
  108.     // 从最后一个内部节点开始,一直到根,对每个子树进行堆重整  
  109.    for (int i = CurrentSize/2; i >= 1; i--)  
  110.    {  
  111.         T y = heap[i]; // 子树根节点元素  
  112.         // find place to put y  
  113.         int c = 2*i; // parent of c is target  
  114.                    // location for y  
  115.         while (c <= CurrentSize)   
  116.         {  
  117.             // heap[c] should be larger sibling  
  118.             if (c < CurrentSize && heap[c] < heap[c+1])  
  119.             {  
  120.                 c++;  
  121.             }  
  122.             // can we put y in heap[c/2]?  
  123.             if (y >= heap[c])  
  124.             {  
  125.                 break;  // yes  
  126.             }  
  127.   
  128.             // no  
  129.             heap[c/2] = heap[c]; // move child up  
  130.             c *= 2; // move down a level  
  131.         }  
  132.         heap[c/2] = y;  
  133.     }  
  134. }  
     2、6d3-2.cpp

[cpp] view plaincopy
  1. //装载问题 优先队列式分支限界法求解   
  2. #include "stdafx.h"  
  3. #include "MaxHeap.h"  
  4. #include <iostream>  
  5. using namespace std;  
  6.   
  7. const int N = 4;  
  8.   
  9. class bbnode;  
  10.   
  11. template<class Type>  
  12. class HeapNode  
  13. {  
  14.     template<class Type>  
  15.     friend void AddLiveNode(MaxHeap<HeapNode<Type>>& H,bbnode *E,Type wt,bool ch,int lev);  
  16.     template<class Type>  
  17.     friend Type MaxLoading(Type w[],Type c,int n,int bestx[]);  
  18.     public:  
  19.         operator Type() const{return uweight;}  
  20.     private:  
  21.         bbnode *ptr;        //指向活节点在子集树中相应节点的指针  
  22.         Type uweight;       //活节点优先级(上界)  
  23.         int level;          //活节点在子集树中所处的层序号  
  24. };  
  25.   
  26. class bbnode  
  27. {  
  28.     template<class Type>  
  29.     friend void AddLiveNode(MaxHeap<HeapNode<Type>>& H,bbnode *E,Type wt,bool ch,int lev);  
  30.     template<class Type>  
  31.     friend Type MaxLoading(Type w[],Type c,int n,int bestx[]);  
  32.     friend class AdjacencyGraph;  
  33.   
  34.     private:  
  35.         bbnode *parent;     //指向父节点的指针  
  36.         bool LChild;        //左儿子节点标识  
  37. };  
  38.   
  39. template<class Type>  
  40. void AddLiveNode(MaxHeap<HeapNode<Type>>& H,bbnode *E,Type wt,bool ch,int lev);  
  41.   
  42. template<class Type>  
  43. Type MaxLoading(Type w[],Type c,int n,int bestx[]);  
  44.   
  45.   
  46. int main()  
  47. {  
  48.     float c = 70;    
  49.     float w[] = {0,20,10,26,15};//下标从1开始    
  50.     int x[N+1];    
  51.     float bestw;  
  52.     
  53.     cout<<"轮船载重为:"<<c<<endl;    
  54.     cout<<"待装物品的重量分别为:"<<endl;    
  55.     for(int i=1; i<=N; i++)    
  56.     {    
  57.         cout<<w[i]<<" ";    
  58.     }    
  59.     cout<<endl;    
  60.     bestw = MaxLoading(w,c,N,x);    
  61.     
  62.     cout<<"分支限界选择结果为:"<<endl;    
  63.     for(int i=1; i<=4; i++)    
  64.     {    
  65.         cout<<x[i]<<" ";    
  66.     }    
  67.     cout<<endl;    
  68.     cout<<"最优装载重量为:"<<bestw<<endl;  
  69.     
  70.     return 0;   
  71. }  
  72.   
  73. //将活节点加入到表示活节点优先队列的最大堆H中  
  74. template<class Type>  
  75. void AddLiveNode(MaxHeap<HeapNode<Type>>& H,bbnode *E,Type wt,bool ch,int lev)  
  76. {  
  77.     bbnode *b = new bbnode;  
  78.     b->parent = E;  
  79.     b->LChild = ch;  
  80.     HeapNode<Type> N;  
  81.   
  82.     N.uweight = wt;  
  83.     N.level = lev;  
  84.     N.ptr = b;  
  85.     H.Insert(N);  
  86. }  
  87.   
  88. //优先队列式分支限界法,返回最优载重量,bestx返回最优解  
  89. template<class Type>  
  90. Type MaxLoading(Type w[],Type c,int n,int bestx[])  
  91. {  
  92.     //定义最大的容量为1000  
  93.     MaxHeap<HeapNode<Type>> H(1000);  
  94.   
  95.     //定义剩余容量数组  
  96.     Type *r = new Type[n+1];  
  97.     r[n] = 0;  
  98.   
  99.     for(int j=n-1; j>0; j--)  
  100.     {  
  101.         r[j] = r[j+1] + w[j+1];  
  102.     }  
  103.   
  104.     //初始化  
  105.     int i = 1;//当前扩展节点所处的层  
  106.     bbnode *E = 0;//当前扩展节点  
  107.     Type Ew = 0; //扩展节点所相应的载重量  
  108.   
  109.     //搜索子集空间树  
  110.     while(i!=n+1)//非叶子节点  
  111.     {  
  112.         //检查当前扩展节点的儿子节点  
  113.         if(Ew+w[i]<=c)  
  114.         {  
  115.             AddLiveNode(H,E,Ew+w[i]+r[i],true,i+1);  
  116.         }  
  117.         //右儿子节点  
  118.         AddLiveNode(H,E,Ew+r[i],false,i+1);  
  119.   
  120.         //取下一扩展节点  
  121.         HeapNode<Type> N;  
  122.         H.DeleteMax(N);//非空  
  123.         i = N.level;  
  124.         E = N.ptr;  
  125.         Ew = N.uweight - r[i-1];  
  126.     }  
  127.   
  128.     //构造当前最优解  
  129.     for(int j=n; j>0; j--)  
  130.     {  
  131.         bestx[j] = E->LChild;  
  132.         E = E->parent;  
  133.     }  
  134.   
  135.     return Ew;  
  136. }  
     程序运行结果如图:


 


 

0037算法笔记——【分支限界法】最大团问题

分类: 算法 264人阅读 评论(0) 收藏 举报
最大团问题独立集算法笔记优先队列分支限界法空子图

        问题描述

     给定无向图G=(V, E),其中V是非空集合,称为顶点集;E是V中元素构成的无序二元组的集合,称为边集,无向图中的边均是顶点的无序对,无序对常用圆括号“( )”表示。如果U∈V,且对任意两个顶点u,v∈U有(u, v)∈E,则称U是G的完全子图(完全图G就是指图G的每个顶点之间都有连边)G的完全子图U是G的团当且仅当U不包含在G的更大的完全子图中。G的最大团是指G中所含顶点数最多的团

     如果U∈V且对任意u,v∈U有(u, v)不属于E,则称U是G的空子图。G的空子图U是G的独立集当且仅当U不包含在G的更大的空子图中。G的最大独立集是G中所含顶点数最多的独立集

     对于任一无向图G=(V, E),其补图G'=(V', E')定义为:V'=V,且(u, v)∈E'当且仅当(u, v)∈E。
     如果U是G的完全子图,则它也是G'的空子图,反之亦然。因此,G的团与G'的独立集之间存在一一对应的关系。特殊地,U是G的最大团当且仅当U是G'的最大独立集。

     例:如图所示,给定无向图G={V, E},其中V={1,2,3,4,5},E={(1,2), (1,4), (1,5),(2,3), (2,5), (3,5), (4,5)}。根据最大团(MCP)定义,子集{1,2}是图G的一个大小为2的完全子图,但不是一个团,因为它包含于G的更大的完全子图{1,2,5}之中。{1,2,5}是G的一个最大团。{1,4,5}和{2,3,5}也是G的最大团。右侧图是无向图G的补图G'。根据最大独立集定义,{2,4}是G的一个空子图,同时也是G的一个最大独立集。虽然{1,2}也是G'的空子图,但它不是G'的独立集,因为它包含在G'的空子图{1,2,5}中。{1,2,5}是G'的最大独立集。{1,4,5}和{2,3,5}也是G'的最大独立集。


     算法设计

      最大团问题的解空间树也是一棵子集树。子集树的根结点是初始扩展结点,对于这个特殊的扩展结点,其cliqueSize的值为0。 算法在扩展内部结点时,首先考察其左儿子结点。在左儿子结点处,将顶点i加入到当前团中,并检查该顶点与当前团中其它顶点之间是否有边相连。当顶点i与当前团中所有顶点之间都有边相连,则相应的左儿子结点是可行结点,将它加入到子集树中并插入活结点优先队列,否则就不是可行结点。

     接着继续考察当前扩展结点的右儿子结点。当upperSize>bestn时,右子树中可能含有最优解,此时将右儿子结点加入到子集树中并插入到活结点优先队列中。算法的while循环的终止条件是遇到子集树中的一个叶结点(即n+1层结点)成为当前扩展结点。
    对于子集树中的叶结点,有upperSize=cliqueSize。此时活结点优先队列中剩余结点的upperSize值均不超过当前扩展结点的upperSize值,从而进一步搜索不可能得到更大的团,此时算法已找到一个最优解。

     算法具体实现如下:

     1、MaxHeap.h

[cpp] view plaincopy
  1. template<class T>  
  2. class MaxHeap  
  3. {  
  4.     public:  
  5.         MaxHeap(int MaxHeapSize = 10);  
  6.         ~MaxHeap() {delete [] heap;}  
  7.         int Size() const {return CurrentSize;}  
  8.   
  9.         T Max()   
  10.         {          //查  
  11.            if (CurrentSize == 0)  
  12.            {  
  13.                 throw OutOfBounds();  
  14.            }  
  15.            return heap[1];  
  16.         }  
  17.   
  18.         MaxHeap<T>& Insert(const T& x); //增  
  19.         MaxHeap<T>& DeleteMax(T& x);   //删  
  20.   
  21.         void Initialize(T a[], int size, int ArraySize);  
  22.   
  23.     private:  
  24.         int CurrentSize, MaxSize;  
  25.         T *heap;  // element array  
  26. };  
  27.   
  28. template<class T>  
  29. MaxHeap<T>::MaxHeap(int MaxHeapSize)  
  30. {// Max heap constructor.  
  31.     MaxSize = MaxHeapSize;  
  32.     heap = new T[MaxSize+1];  
  33.     CurrentSize = 0;  
  34. }  
  35.   
  36. template<class T>  
  37. MaxHeap<T>& MaxHeap<T>::Insert(const T& x)  
  38. {// Insert x into the max heap.  
  39.     if (CurrentSize == MaxSize)  
  40.     {  
  41.         cout<<"no space!"<<endl;   
  42.         return *this;   
  43.     }  
  44.   
  45.     // 寻找新元素x的位置  
  46.     // i——初始为新叶节点的位置,逐层向上,寻找最终位置  
  47.     int i = ++CurrentSize;  
  48.     while (i != 1 && x > heap[i/2])  
  49.     {  
  50.         // i不是根节点,且其值大于父节点的值,需要继续调整  
  51.         heap[i] = heap[i/2]; // 父节点下降  
  52.         i /= 2;              // 继续向上,搜寻正确位置  
  53.     }  
  54.   
  55.    heap[i] = x;  
  56.    return *this;  
  57. }  
  58.   
  59. template<class T>  
  60. MaxHeap<T>& MaxHeap<T>::DeleteMax(T& x)  
  61. {// Set x to max element and delete max element from heap.  
  62.     // check if heap is empty  
  63.     if (CurrentSize == 0)  
  64.     {  
  65.         cout<<"Empty heap!"<<endl;   
  66.         return *this;   
  67.     }  
  68.   
  69.     x = heap[1]; // 删除最大元素  
  70.     // 重整堆  
  71.     T y = heap[CurrentSize--]; // 取最后一个节点,从根开始重整  
  72.   
  73.     // find place for y starting at root  
  74.     int i = 1,  // current node of heap  
  75.        ci = 2; // child of i  
  76.   
  77.     while (ci <= CurrentSize)   
  78.     {  
  79.         // 使ci指向i的两个孩子中较大者  
  80.         if (ci < CurrentSize && heap[ci] < heap[ci+1])  
  81.         {  
  82.             ci++;  
  83.         }  
  84.         // y的值大于等于孩子节点吗?  
  85.         if (y >= heap[ci])  
  86.         {  
  87.             break;   // 是,i就是y的正确位置,退出  
  88.         }  
  89.   
  90.         // 否,需要继续向下,重整堆  
  91.         heap[i] = heap[ci]; // 大于父节点的孩子节点上升  
  92.         i = ci;             // 向下一层,继续搜索正确位置  
  93.         ci *= 2;  
  94.     }  
  95.   
  96.     heap[i] = y;  
  97.     return *this;  
  98. }  
  99.   
  100. template<class T>  
  101. void MaxHeap<T>::Initialize(T a[], int size,int ArraySize)  
  102. {// Initialize max heap to array a.  
  103.     delete [] heap;  
  104.     heap = a;  
  105.     CurrentSize = size;  
  106.     MaxSize = ArraySize;  
  107.   
  108.     // 从最后一个内部节点开始,一直到根,对每个子树进行堆重整  
  109.    for (int i = CurrentSize/2; i >= 1; i--)  
  110.    {  
  111.         T y = heap[i]; // 子树根节点元素  
  112.         // find place to put y  
  113.         int c = 2*i; // parent of c is target  
  114.                    // location for y  
  115.         while (c <= CurrentSize)   
  116.         {  
  117.             // heap[c] should be larger sibling  
  118.             if (c < CurrentSize && heap[c] < heap[c+1])  
  119.             {  
  120.                 c++;  
  121.             }  
  122.             // can we put y in heap[c/2]?  
  123.             if (y >= heap[c])  
  124.             {  
  125.                 break;  // yes  
  126.             }  
  127.   
  128.             // no  
  129.             heap[c/2] = heap[c]; // move child up  
  130.             c *= 2; // move down a level  
  131.         }  
  132.         heap[c/2] = y;  
  133.     }  
  134. }  
     2、6d6.cpp

[cpp] view plaincopy
  1. //最大团问题 优先队列分支限界法求解   
  2. #include "stdafx.h"  
  3. #include "MaxHeap.h"  
  4. #include <iostream>  
  5. #include <fstream>  
  6. using namespace std;  
  7.   
  8. const int N = 5;//图G的顶点数  
  9. ifstream fin("6d6.txt");     
  10.   
  11. class bbnode  
  12. {  
  13.     friend class Clique;  
  14.     private:  
  15.         bbnode *parent;     //指向父节点的指针  
  16.         bool LChild;        //左儿子节点标识  
  17. };  
  18.   
  19. class CliqueNode  
  20. {  
  21.     friend class Clique;  
  22.     public:  
  23.         operator int() const  
  24.         {     
  25.             return un;  
  26.         }  
  27.     private:  
  28.         int cn,         //当前团的顶点数  
  29.             un,         //当前团最大顶点数的上界  
  30.             level;      //节点在子集空间树中所处的层次  
  31.         bbnode *ptr;    //指向活节点在子集树中相应节点的指针  
  32. };  
  33.   
  34. class Clique  
  35. {  
  36.     friend int main(void);  
  37.     public:  
  38.         int BBMaxClique(int []);  
  39.     private:  
  40.         void AddLiveNode(MaxHeap<CliqueNode>&H,int cn,int un,int level,bbnode E[],bool ch);  
  41.         int **a,        //图G的邻接矩阵  
  42.             n;          //图G的顶点数  
  43. };  
  44.   
  45. int main()  
  46. {  
  47.     int bestx[N+1];  
  48.     int **a = new int *[N+1];    
  49.     for(int i=1;i<=N;i++)      
  50.     {      
  51.         a[i] = new int[N+1];      
  52.     }   
  53.       
  54.     cout<<"图G的邻接矩阵为:"<<endl;  
  55.     for(int i=1; i<=N; i++)    
  56.     {    
  57.         for(int j=1; j<=N; j++)    
  58.         {    
  59.             fin>>a[i][j];        
  60.             cout<<a[i][j]<<" ";      
  61.         }    
  62.         cout<<endl;    
  63.     }  
  64.   
  65.     Clique c;  
  66.     c.a = a;  
  67.     c.n = N;  
  68.   
  69.     cout<<"图G的最大团顶点个数为:"<<c.BBMaxClique(bestx)<<endl;  
  70.     cout<<"图G的最大团解向量为:"<<endl;  
  71.     for(int i=1;i<=N;i++)      
  72.     {     
  73.         cout<<bestx[i]<<" ";  
  74.     }   
  75.     cout<<endl;  
  76.   
  77.     for(int i=1;i<=N;i++)      
  78.     {     
  79.         delete[] a[i];     
  80.     }   
  81.     delete []a;  
  82.     return 0;  
  83. }  
  84.   
  85. //将活节点加入到子集空间树中并插入到最大堆中  
  86. void Clique::AddLiveNode(MaxHeap<CliqueNode> &H, int cn, int un, int level, bbnode E[], bool ch)  
  87. {  
  88.     bbnode *b = new bbnode;  
  89.     b->parent = E;  
  90.     b->LChild = ch;  
  91.   
  92.     CliqueNode N;  
  93.     N.cn = cn;  
  94.     N.ptr = b;  
  95.     N.un = un;  
  96.     N.level = level;  
  97.     H.Insert(N);  
  98. }  
  99.   
  100. //解最大团问题的优先队列式分支限界法  
  101. int Clique::BBMaxClique(int bestx[])  
  102. {  
  103.     MaxHeap<CliqueNode> H(1000);  
  104.   
  105.     //初始化  
  106.     bbnode *E = 0;  
  107.     int i = 1,  
  108.         cn = 0,  
  109.         bestn = 0;  
  110.   
  111.     //搜集子集空间树  
  112.     while(i!=n+1)//非叶节点  
  113.     {  
  114.         //检查顶点i与当前团中其他顶点之间是否有边相连  
  115.         bool OK = true;  
  116.         bbnode *B = E;  
  117.         for(int j=i-1; j>0; B=B->parent,j--)  
  118.         {  
  119.             if(B->LChild && a[i][j]==0)  
  120.             {  
  121.                 OK = false;  
  122.                 break;  
  123.             }  
  124.         }  
  125.   
  126.         if(OK)//左儿子节点为可行结点  
  127.         {  
  128.             if(cn+1>bestn)  
  129.             {  
  130.                 bestn = cn + 1;  
  131.             }  
  132.             AddLiveNode(H,cn+1,cn+n-i+1,i+1,E,true);  
  133.         }  
  134.   
  135.         if(cn+n-i>=bestn)//右子树可能含有最优解  
  136.         {  
  137.             AddLiveNode(H,cn,cn+n-i,i+1,E,false);  
  138.         }  
  139.   
  140.         //取下一扩展节点  
  141.         CliqueNode N;  
  142.         H.DeleteMax(N); //堆非空  
  143.         E = N.ptr;  
  144.         cn = N.cn;  
  145.         i = N.level;  
  146.     }  
  147.   
  148.     //构造当前最优解  
  149.     for(int j=n; j>0; j--)  
  150.     {  
  151.         bestx[j] = E->LChild;  
  152.         E = E->parent;  
  153.     }  
  154.   
  155.     return bestn;  
  156. }  


0039算法笔记——【分支限界法】电路板排列问题

分类: 算法 248人阅读 评论(0) 收藏 举报
电路板排列问题优先级队列分支限界法算法笔记最优排列密度

     问题描述

     将n块电路板以最佳排列方式插入带有n个插槽的机箱中。n块电路板的不同排列方式对应于不同的电路板插入方案。设B={1, 2, …, n}是n块电路板的集合,L={N1, N2, …, Nm}是连接这n块电路板中若干电路板的m个连接块。Ni是B的一个子集,且Ni中的电路板用同一条导线连接在一起。设x表示n块电路板的一个排列,即在机箱的第i个插槽中插入的电路板编号是x[i]。x所确定的电路板排列Density (x)密度定义为跨越相邻电路板插槽的最大连线数。

    例:如图,设n=8, m=5,给定n块电路板及其m个连接块:B={1, 2, 3, 4, 5, 6, 7, 8},N1={4, 5, 6},N2={2, 3},N3={1, 3},N4={3, 6},N5={7, 8};其中两个可能的排列如图所示,则该电路板排列的密度分别是2,3。

       

     左上图中,跨越插槽2和3,4和5,以及插槽5和6的连线数均为2。插槽6和7之间无跨越连线。其余插槽之间只有1条跨越连线。在设计机箱时,插槽一侧的布线间隙由电路板的排列的密度确定因此,电路板排列问题要求对于给定的电路板连接条件(连接块),确定电路板的最佳排列,使其具有最小密度

     算法思路

      电路板排列问题的解空间是一颗排列树。采用优先队列式分支限界法找出所给电路板的最小密度布局。算法中采用最小堆表示活节点优先级队列。最小堆中元素类型是BoradNode,每一个BoardNode类型的节点包含域x,表示节点所相应的电路板排列;s表示该节点已确定的电路板排列x[1:s];cd表示当前密度,now[j]表示x[1:s]中所含连接块j中的电路板数。

     算法开始时,将排列树的根结点置为当前扩展结点。在do-while循环体内算法依次从活结点优先队列中取出具有最小cd值的结点作为当前扩展结点,并加以扩展。算法将当前扩展节点分两种情形处理:

     1)首先考虑s=n-1的情形,当前扩展结点是排列树中的一个叶结点的父结点。x表示相应于该叶结点的电路板排列。计算出与x相应的密度并在必要时更新当前最优值和相应的当前最优解。

     2)当s<n-1时,算法依次产生当前扩展结点的所有儿子结点。对于当前扩展结点的每一个儿子结点node,计算出其相应的密度node.cd。当node.cd<bestd时,将该儿子结点N插入到活结点优先队列中。

     算法具体实现如下:

    1、MinHeap2.h

[cpp] view plaincopy
  1. #include <iostream>  
  2.   
  3. template<class Type>  
  4. class Graph;  
  5.   
  6. template<class T>   
  7. class MinHeap   
  8. {   
  9.     template<class Type>  
  10.     friend class Graph;  
  11.     public:   
  12.         MinHeap(int maxheapsize = 10);   
  13.         ~MinHeap(){delete []heap;}   
  14.   
  15.         int Size() const{return currentsize;}   
  16.         T Max(){if(currentsize) return heap[1];}   
  17.   
  18.         MinHeap<T>& Insert(const T& x);   
  19.         MinHeap<T>& DeleteMin(T &x);   
  20.   
  21.         void Initialize(T x[], int size, int ArraySize);   
  22.         void Deactivate();   
  23.         void output(T a[],int n);  
  24.     private:   
  25.         int currentsize, maxsize;   
  26.         T *heap;   
  27. };   
  28.   
  29. template <class T>   
  30. void MinHeap<T>::output(T a[],int n)   
  31. {   
  32.     for(int i = 1; i <= n; i++)   
  33.     cout << a[i] << " ";   
  34.     cout << endl;   
  35. }   
  36.   
  37. template <class T>   
  38. MinHeap<T>::MinHeap(int maxheapsize)   
  39. {   
  40.     maxsize = maxheapsize;   
  41.     heap = new T[maxsize + 1];   
  42.     currentsize = 0;   
  43. }   
  44.   
  45. template<class T>   
  46. MinHeap<T>& MinHeap<T>::Insert(const T& x)   
  47. {   
  48.     if(currentsize == maxsize)   
  49.     {   
  50.         return *this;   
  51.     }   
  52.     int i = ++currentsize;   
  53.     while(i != 1 && x < heap[i/2])   
  54.     {   
  55.         heap[i] = heap[i/2];   
  56.         i /= 2;   
  57.     }   
  58.   
  59.     heap[i] = x;   
  60.     return *this;   
  61. }   
  62.   
  63. template<class T>   
  64. MinHeap<T>& MinHeap<T>::DeleteMin(T& x)   
  65. {   
  66.     if(currentsize == 0)   
  67.     {   
  68.         cout<<"Empty heap!"<<endl;   
  69.         return *this;   
  70.     }   
  71.   
  72.     x = heap[1];   
  73.   
  74.     T y = heap[currentsize--];   
  75.     int i = 1, ci = 2;   
  76.     while(ci <= currentsize)   
  77.     {   
  78.         if(ci < currentsize && heap[ci] > heap[ci + 1])   
  79.         {   
  80.             ci++;   
  81.         }   
  82.   
  83.         if(y <= heap[ci])   
  84.         {   
  85.             break;   
  86.         }   
  87.         heap[i] = heap[ci];   
  88.         i = ci;   
  89.         ci *= 2;   
  90.     }   
  91.   
  92.     heap[i] = y;   
  93.     return *this;   
  94. }   
  95.   
  96. template<class T>   
  97. void MinHeap<T>::Initialize(T x[], int size, int ArraySize)   
  98. {   
  99.     delete []heap;   
  100.     heap = x;   
  101.     currentsize = size;   
  102.     maxsize = ArraySize;   
  103.   
  104.     for(int i = currentsize / 2; i >= 1; i--)   
  105.     {   
  106.         T y = heap[i];   
  107.         int c = 2 * i;   
  108.         while(c <= currentsize)   
  109.         {   
  110.             if(c < currentsize && heap[c] > heap[c + 1])   
  111.                 c++;   
  112.             if(y <= heap[c])   
  113.                 break;   
  114.             heap[c / 2] = heap[c];   
  115.             c *= 2;   
  116.         }   
  117.         heap[c / 2] = y;   
  118.     }   
  119. }   
  120.   
  121. template<class T>   
  122. void MinHeap<T>::Deactivate()   
  123. {   
  124.     heap = 0;   
  125. }   
     2、6d8.cpp

[cpp] view plaincopy
  1. //电路板排列问题 优先队列分支限界法求解   
  2. #include "stdafx.h"  
  3. #include "MinHeap2.h"  
  4. #include <iostream>  
  5. #include <fstream>   
  6. using namespace std;  
  7.   
  8. ifstream fin("6d8.txt");   
  9.   
  10. class BoardNode  
  11. {  
  12.     friend int BBArrangement(int **,int,int,int *&);  
  13.     public:  
  14.         operator int() const  
  15.         {  
  16.             return cd;  
  17.         }  
  18.     private:  
  19.         int *x,         //x[1:n]记录电路板排列  
  20.             s,          //x[1:s]是当前节点所相应的部分排列  
  21.             cd,         //x[1:s]的密度  
  22.             *now;       //now[j]是x[1:s]所含连接块j中电路板数  
  23. };  
  24.   
  25. int BBArrangement(int **B,int n,int m,int *&bestx);  
  26.   
  27. int main()  
  28. {  
  29.     int m = 5,n = 8;  
  30.     int *bestx;  
  31.   
  32.     //B={1,2,3,4,5,6,7,8}  
  33.     //N1={4,5,6},N2={2,3},N3={1,3},N4={3,6},N5={7,8}  
  34.   
  35.     cout<<"m="<<m<<",n="<<n<<endl;  
  36.     cout<<"N1={4,5,6},N2={2,3},N3={1,3},N4={3,6},N5={7,8}"<<endl;  
  37.     cout<<"二维数组B如下:"<<endl;  
  38.   
  39.     //构造B  
  40.     int **B = new int*[n+1];  
  41.     for(int i=1; i<=n; i++)  
  42.     {  
  43.         B[i] = new int[m+1];  
  44.     }  
  45.   
  46.     for(int i=1; i<=n; i++)  
  47.     {  
  48.         for(int j=1; j<=m ;j++)  
  49.         {  
  50.             fin>>B[i][j];  
  51.             cout<<B[i][j]<<" ";  
  52.         }  
  53.         cout<<endl;  
  54.     }  
  55.   
  56.     cout<<"当前最优密度为:"<<BBArrangement(B,n,m,bestx)<<endl;  
  57.     cout<<"最优排列为:"<<endl;  
  58.     for(int i=1; i<=n; i++)  
  59.     {  
  60.         cout<<bestx[i]<<" ";  
  61.     }  
  62.     cout<<endl;  
  63.   
  64.     for(int i=1; i<=n; i++)  
  65.     {  
  66.         delete[] B[i];  
  67.     }  
  68.     delete[] B;  
  69.   
  70.     return 0;  
  71. }  
  72.   
  73. //解电路板排列问题的优先队列式分支限界法  
  74. int BBArrangement(int **B,int n,int m,int *&bestx)  
  75. {  
  76.     MinHeap<BoardNode> H(1000);//活节点最小堆  
  77.     BoardNode E;  
  78.     E.x = new int[n+1];  
  79.     E.s = 0;  
  80.     E.cd = 0;  
  81.   
  82.     E.now = new int[m+1];  
  83.     int *total = new int[m+1];  
  84.     //now[i] = x[1:s]所含连接块i中电路板数  
  85.     //total[i] = 连接块i中的电路板数  
  86.   
  87.     for(int i=1; i<=m; i++)  
  88.     {  
  89.         total[i] = 0;  
  90.         E.now[i] = 0;  
  91.     }  
  92.   
  93.     for(int i=1; i<=n; i++)  
  94.     {  
  95.         E.x[i] = i;//初始排列为1,2,3……n  
  96.         for(int j=1;j<=m;j++)  
  97.         {  
  98.             total[j] += B[i][j];//连接块中电路板数  
  99.         }  
  100.     }  
  101.   
  102.     int bestd = m + 1;  
  103.     bestx = 0;  
  104.   
  105.     do//节点扩展  
  106.     {  
  107.         if(E.s == n-1)//仅一个儿子节点  
  108.         {     
  109.             int ld  = 0;//最后一块电路板的密度  
  110.             for(int j=1; j<=m; j++)  
  111.             {  
  112.                 ld += B[E.x[n]][j];  
  113.             }  
  114.             if(ld<bestd)//密度更小的电路排列  
  115.             {  
  116.                 delete[] bestx;  
  117.                 bestx = E.x;  
  118.                 bestd = max(ld,E.cd);  
  119.             }  
  120.             else  
  121.             {  
  122.                 delete []E.x;  
  123.             }  
  124.             delete []E.now;  
  125.         }  
  126.         else//产生当前扩展节点的所有儿子节点  
  127.         {  
  128.             for(int i=E.s+1;i<=n;i++)  
  129.             {  
  130.                 BoardNode N;  
  131.                 N.now = new int[m+1];  
  132.                 for(int j=1; j<=m; j++)  
  133.                 {  
  134.                     //新插入的电路板  
  135.                     N.now[j] = E.now[j] + B[E.x[i]][j];  
  136.                 }  
  137.                 int ld = 0;//新插入的电路板密度  
  138.                 for(int j=1; j<=m; j++)  
  139.                 {  
  140.                     if(N.now[j]>0 && total[j]!=N.now[j])  
  141.                     {  
  142.                         ld++;  
  143.                     }  
  144.                 }  
  145.                 N.cd = max(ld,E.cd);  
  146.                 if(N.cd<bestd)//可能产生更好的叶子节点  
  147.                 {  
  148.                     N.x = new int[n+1];  
  149.                     N.s = E.s + 1;  
  150.                     for(int j=1;j<=n;j++)  
  151.                     {  
  152.                         N.x[j] = E.x[j];  
  153.                     }  
  154.                     N.x[N.s] = E.x[i];  
  155.                     N.x[i] = E.x[N.s];  
  156.                     H.Insert(N);  
  157.                 }  
  158.                 else  
  159.                 {  
  160.                     delete []N.now;  
  161.                 }  
  162.             }  
  163.             delete []E.x;  
  164.         }//完成当前节点扩展  
  165.         if(H.Size() == 0)  
  166.         {  
  167.             return bestd;//无扩展节点  
  168.         }  
  169.         H.DeleteMin(E);           
  170.     }while(E.cd<bestd);  
  171.   
  172.     //释放做小堆中所有节点  
  173.     do  
  174.     {  
  175.         delete []E.x;  
  176.         delete []E.now;  
  177.         if(H.Size() == 0)  
  178.         {  
  179.             break;  
  180.         }  
  181.         H.DeleteMin(E);  
  182.     }while(true);  
  183.     return bestd;  
  184. }  
    程序运行结果如图:


 

0040算法笔记——【分支限界法】批处理作业调度问题

分类: 算法 493人阅读 评论(0) 收藏 举报
批处理作业调度问题优先级队列式分支限界法算法笔记限界函数

      问题描述

     给定n个作业的集合{J1,J2,…,Jn}。每个作业必须先由机器1处理,然后由机器2处理。作业Ji需要机器j的处理时间为tji。对于一个确定的作业调度,设Fji是作业i在机器j上完成处理的时间。所有作业在机器2上完成处理的时间和称为该作业调度的完成时间和。

     批处理作业调度问题要求对于给定的n个作业,制定最佳作业调度方案,使其完成时间和达到最小

      例:设n=3,考虑以下实例:


     这3个作业的6种可能的调度方案是1,2,3;1,3,2;2,1,3;2,3,1;3,1,2;3,2,1;它们所相应的完成时间和分别是19,18,20,21,19,19。易见,最佳调度方案是1,3,2,其完成时间和为18。

     限界函数

     批处理作业调度问题要从n个作业的所有排列中找出具有最小完成时间和的作业调度,所以如图,批处理作业调度问题的解空间是一颗排列树     

     在作业调度问相应的排列空间树中,每一个节点E都对应于一个已安排的作业集。以该节点为根的子树中所含叶节点的完成时间和可表示为:


     设|M|=r,且L是以节点E为根的子树中的叶节点,相应的作业调度为{pk,k=1,2,……n},其中pk是第k个安排的作业。如果从节点E到叶节点L的路上,每一个作业pk在机器1上完成处理后都能立即在机器2上开始处理,即从pr+1开始,机器1没有空闲时间,则对于该叶节点L有:

注:(n-k+1)t1pk,因为是完成时间和,所以,后续的(n-k+1)个作业完成时间和都得算上t1pk

     如果不能做到上面这一点,则s1只会增加,从而有:

     类似地,如果从节点E开始到节点L的路上,从作业pr+1开始,机器2没有空闲时间,则:


     同理可知,s2是的下界。由此得到在节点E处相应子树中叶节点完成时间和的下界是:


     注意到如果选择Pk,使t1pk在k>=r+1时依非减序排列,S1则取得极小值。同理如果选择Pk使t2pk依非减序排列,则S2取得极小值。 


     这可以作为优先队列式分支限界法中的限界函数。 

     算法描述

      算法中用最小堆表示活节点优先队列。最小堆中元素类型是MinHeapNode。每一个MinHeapNode类型的节点包含域x,用来表示节点所相应的作业调度。s表示该作业已安排的作业时x[1:s]。f1表示当前已安排的作业在机器1上的最后完成时间;f2表示当前已安排作业在机器2上的完成时间;sf2表示当前已安排的作业在机器2上的完成时间和;bb表示当前完成时间和下界。二维数组M表示所给的n个作业在机器1和机器2所需的处理时间。在类Flowshop中用二维数组b存储排好序的作业处理时间。数组a表示数组M和b的对应关系。算法Sort实现对各作业在机器1和2上所需时间排序。函数Bound用于计算完成时间和下界。

     函数BBFlow中while循环完成对排列树内部结点的有序扩展。在while循环体内算法依次从活结点优先队列中取出具有最小bb值(完成时间和下界)的结点作为当前扩展结点,并加以扩展。 算法将当前扩展节点E分两种情形处理:

 1)首先考虑E.s=n的情形,当前扩展结点E是排列树中的叶结点。E.sf2是相应于该叶结点的完成时间和。当E.sf2 < bestc时更新当前最优值bestc和相应的当前最优解bestx。 

    2)当E.s<n时,算法依次产生当前扩展结点E的所有儿子结点。对于当前扩展结点的每一个儿子结点node,计算出其相应的完成时间和的下界bb。当bb < bestc时,将该儿子结点插入到活结点优先队列中。而当bb bestc时,可将结点node舍去。 

    算法具体实现如下:

     1、MinHeap2.h

[cpp] view plaincopy
  1. #include <iostream>  
  2.   
  3. template<class Type>  
  4. class Graph;  
  5.   
  6. template<class T>   
  7. class MinHeap   
  8. {   
  9.     template<class Type>  
  10.     friend class Graph;  
  11.     public:   
  12.         MinHeap(int maxheapsize = 10);   
  13.         ~MinHeap(){delete []heap;}   
  14.   
  15.         int Size() const{return currentsize;}   
  16.         T Max(){if(currentsize) return heap[1];}   
  17.   
  18.         MinHeap<T>& Insert(const T& x);   
  19.         MinHeap<T>& DeleteMin(T &x);   
  20.   
  21.         void Initialize(T x[], int size, int ArraySize);   
  22.         void Deactivate();   
  23.         void output(T a[],int n);  
  24.     private:   
  25.         int currentsize, maxsize;   
  26.         T *heap;   
  27. };   
  28.   
  29. template <class T>   
  30. void MinHeap<T>::output(T a[],int n)   
  31. {   
  32.     for(int i = 1; i <= n; i++)   
  33.     cout << a[i] << " ";   
  34.     cout << endl;   
  35. }   
  36.   
  37. template <class T>   
  38. MinHeap<T>::MinHeap(int maxheapsize)   
  39. {   
  40.     maxsize = maxheapsize;   
  41.     heap = new T[maxsize + 1];   
  42.     currentsize = 0;   
  43. }   
  44.   
  45. template<class T>   
  46. MinHeap<T>& MinHeap<T>::Insert(const T& x)   
  47. {   
  48.     if(currentsize == maxsize)   
  49.     {   
  50.         return *this;   
  51.     }   
  52.     int i = ++currentsize;   
  53.     while(i != 1 && x < heap[i/2])   
  54.     {   
  55.         heap[i] = heap[i/2];   
  56.         i /= 2;   
  57.     }   
  58.   
  59.     heap[i] = x;   
  60.     return *this;   
  61. }   
  62.   
  63. template<class T>   
  64. MinHeap<T>& MinHeap<T>::DeleteMin(T& x)   
  65. {   
  66.     if(currentsize == 0)   
  67.     {   
  68.         cout<<"Empty heap!"<<endl;   
  69.         return *this;   
  70.     }   
  71.   
  72.     x = heap[1];   
  73.   
  74.     T y = heap[currentsize--];   
  75.     int i = 1, ci = 2;   
  76.     while(ci <= currentsize)   
  77.     {   
  78.         if(ci < currentsize && heap[ci] > heap[ci + 1])   
  79.         {   
  80.             ci++;   
  81.         }   
  82.   
  83.         if(y <= heap[ci])   
  84.         {   
  85.             break;   
  86.         }   
  87.         heap[i] = heap[ci];   
  88.         i = ci;   
  89.         ci *= 2;   
  90.     }   
  91.   
  92.     heap[i] = y;   
  93.     return *this;   
  94. }   
  95.   
  96. template<class T>   
  97. void MinHeap<T>::Initialize(T x[], int size, int ArraySize)   
  98. {   
  99.     delete []heap;   
  100.     heap = x;   
  101.     currentsize = size;   
  102.     maxsize = ArraySize;   
  103.   
  104.     for(int i = currentsize / 2; i >= 1; i--)   
  105.     {   
  106.         T y = heap[i];   
  107.         int c = 2 * i;   
  108.         while(c <= currentsize)   
  109.         {   
  110.             if(c < currentsize && heap[c] > heap[c + 1])   
  111.                 c++;   
  112.             if(y <= heap[c])   
  113.                 break;   
  114.             heap[c / 2] = heap[c];   
  115.             c *= 2;   
  116.         }   
  117.         heap[c / 2] = y;   
  118.     }   
  119. }   
  120.   
  121. template<class T>   
  122. void MinHeap<T>::Deactivate()   
  123. {   
  124.     heap = 0;   
  125. }   
    2、6d9.cpp

[cpp] view plaincopy
  1. //批作业调度问题 优先队列分支限界法求解   
  2. #include "stdafx.h"  
  3. #include "MinHeap2.h"  
  4. #include <iostream>  
  5. using namespace std;  
  6.   
  7. class Flowshop;  
  8. class MinHeapNode  
  9. {  
  10.     friend Flowshop;  
  11.     public:  
  12.         operator int() const  
  13.         {  
  14.             return bb;  
  15.         }  
  16.     private:      
  17.         void Init(int);  
  18.         void NewNode(MinHeapNode,int,int,int,int);  
  19.         int s,          //已安排作业数  
  20.             f1,         //机器1上最后完成时间  
  21.             f2,         //机器2上最后完成时间  
  22.             sf2,        //当前机器2上完成时间和  
  23.             bb,         //当前完成时间和下界  
  24.             *x;         //当前作业调度  
  25. };  
  26.   
  27. class Flowshop  
  28. {  
  29.     friend int main(void);  
  30.     public:  
  31.         int BBFlow(void);  
  32.     private:  
  33.         int Bound(MinHeapNode E,int &f1,int &f2,bool **y);  
  34.         void Sort(void);  
  35.         int n,          //作业数  
  36.             ** M,       //各作业所需的处理时间数组  
  37.             **b,        //各作业所需的处理时间排序数组  
  38.             **a,        //数组M和b的对应关系数组  
  39.             *bestx,     //最优解  
  40.             bestc;      //最小完成时间和  
  41.         bool **y;       //工作数组  
  42. };  
  43.   
  44. template <class Type>  
  45. inline void Swap(Type &a, Type &b);  
  46.   
  47. int main()  
  48. {  
  49.     int n=3,bf;  
  50.     int M1[3][2]={{2,1},{3,1},{2,3}};  
  51.   
  52.     int **M = new int*[n];  
  53.     int **b = new int*[n];  
  54.     int **a = new int*[n];  
  55.     bool **y = new bool*[n];  
  56.     int *bestx = new int[n];    
  57.   
  58.     for(int i=0;i<=n;i++)  
  59.     {  
  60.         M[i] = new int[2];  
  61.         b[i] = new int[2];  
  62.         a[i] = new int[2];  
  63.         y[i] = new bool[2];  
  64.     }  
  65.     cout<<"各作业所需要的时间处理数组M(i,j)值如下:"<<endl;  
  66.   
  67.     for(int i=0;i<n;i++)  
  68.     {  
  69.         for(int j=0;j<2;j++)  
  70.         {  
  71.             M[i][j]=M1[i][j];  
  72.         }  
  73.     }  
  74.   
  75.     for(int i=0;i<n;i++)  
  76.     {  
  77.         cout<<"(";  
  78.         for(int j=0;j<2;j++)  
  79.         cout<<M[i][j]<<" ";  
  80.         cout<<")";  
  81.     }  
  82.     cout<<endl;  
  83.   
  84.     Flowshop flow;  
  85.     flow.n = n;  
  86.     flow.M = M;  
  87.     flow.b = b;  
  88.     flow.a = a;  
  89.     flow.y = y;  
  90.     flow.bestx = bestx;  
  91.     flow.bestc = 1000;//给初值  
  92.   
  93.     flow.BBFlow();  
  94.   
  95.     cout<<"最优值是:"<<flow.bestc<<endl;  
  96.     cout<<"最优调度是:";  
  97.   
  98.     for(int i=0;i<n;i++)  
  99.     {  
  100.         cout<<(flow.bestx[i]+1)<<" ";  
  101.     }  
  102.     cout<<endl;  
  103.   
  104.     for(int i=0;i<n;i++)  
  105.     {  
  106.         delete[] M[i];  
  107.         delete[] b[i];  
  108.         delete[] a[i];  
  109.         delete[] y[i];        
  110.     }  
  111.     return 0;  
  112. }  
  113.   
  114. //最小堆节点初始化  
  115. void MinHeapNode::Init(int n)  
  116. {  
  117.     x = new int[n];  
  118.     for(int i=0; i<n; i++)  
  119.     {  
  120.         x[i] = i;  
  121.     }  
  122.     s = 0;  
  123.     f1 = 0;  
  124.     f2 = 0;  
  125.     sf2 = 0;  
  126.     bb = 0;  
  127. }  
  128.   
  129. //最小堆新节点  
  130. void MinHeapNode::NewNode(MinHeapNode E,int Ef1,int Ef2,int Ebb,int n)  
  131. {  
  132.     x = new int[n];  
  133.     for(int i=0; i<n; i++)  
  134.     {  
  135.         x[i] = E.x[i];  
  136.     }  
  137.     f1 = Ef1;  
  138.     f2 = Ef2;  
  139.     sf2 = E.sf2 + f2;  
  140.     bb = Ebb;  
  141.     s =  E.s + 1;  
  142. }  
  143.   
  144. //对各作业在机器1和2上所需时间排序  
  145. void Flowshop::Sort(void)  
  146. {  
  147.     int *c = new int[n];  
  148.     for(int j=0; j<2; j++)  
  149.     {  
  150.         for(int i=0; i<n; i++)  
  151.         {  
  152.             b[i][j] = M[i][j];  
  153.             c[i] = i;  
  154.         }  
  155.   
  156.         for(int i=0; i<n-1; i++)  
  157.         {  
  158.             for(int k=n-1; k>i; k--)  
  159.             {  
  160.                 if(b[k][j]<b[k-1][j])  
  161.                 {  
  162.                     Swap(b[k][j],b[k-1][j]);  
  163.                     Swap(c[k],c[k-1]);  
  164.                 }  
  165.             }  
  166.         }     
  167.   
  168.         for(int i=0; i<n; i++)  
  169.         {  
  170.             a[c[i]][j] = i;  
  171.         }  
  172.     }  
  173.   
  174.     delete []c;  
  175. }  
  176.   
  177. //计算完成时间和下界  
  178. int Flowshop::Bound(MinHeapNode E,int &f1,int &f2,bool **y)  
  179. {  
  180.     for(int k=0; k<n; k++)  
  181.     {  
  182.         for(int j=0; j<2; j++)  
  183.         {     
  184.             y[k][j] = false;  
  185.         }  
  186.     }  
  187.   
  188.     for(int k=0; k<=E.s; k++)  
  189.     {  
  190.         for(int j=0; j<2; j++)  
  191.         {  
  192.             y[a[E.x[k]][j]][j] = true;  
  193.         }  
  194.     }  
  195.   
  196.     f1 = E.f1 + M[E.x[E.s]][0];  
  197.     f2 = ((f1>E.f2)?f1:E.f2)+M[E.x[E.s]][1];  
  198.     int sf2 = E.sf2 + f2;  
  199.     int s1 = 0,s2 = 0,k1 = n-E.s,k2 = n-E.s,f3 = f2;  
  200.   
  201.     //计算s1的值  
  202.     for(int j=0; j<n; j++)  
  203.     {  
  204.         if(!y[j][0])  
  205.         {  
  206.             k1--;  
  207.             if(k1 == n-E.s-1)  
  208.             {  
  209.                 f3 = (f2>f1+b[j][0])?f2:f1+b[j][0];  
  210.             }  
  211.             s1 += f1+k1*b[j][0];  
  212.         }  
  213.     }  
  214.   
  215.     //计算s2的值  
  216.     for(int j=0; j<n; j++)  
  217.     {     
  218.         if(!y[j][1])  
  219.         {  
  220.             k2--;  
  221.             s1 += b[j][1];  
  222.             s2 += f3 + k2*b[j][1];  
  223.         }  
  224.     }  
  225.   
  226.     //返回完成时间和下界  
  227.     return sf2 +((s1>s2)?s1:s2);  
  228. }  
  229.   
  230. //解批处理作业调度问题的优先队列式分支限界法  
  231. int Flowshop::BBFlow(void)  
  232. {  
  233.     Sort();//对各作业在机器1和2上所需时间排序  
  234.     MinHeap<MinHeapNode> H(1000);  
  235.   
  236.     MinHeapNode E;  
  237.     //初始化  
  238.     E.Init(n);  
  239.     //搜索排列空间树  
  240.     while(E.s<=n)  
  241.     {  
  242.         //叶节点  
  243.         if(E.s == n)  
  244.         {  
  245.             if(E.sf2<bestc)  
  246.             {  
  247.                 bestc = E.sf2;  
  248.                 for(int i=0; i<n; i++)  
  249.                 {  
  250.                     bestx[i] = E.x[i];  
  251.                 }  
  252.             }  
  253.             delete []E.x;  
  254.         }  
  255.         else//产生当前扩展节点的儿子节点  
  256.         {  
  257.             for(int i=E.s; i<n; i++)  
  258.             {  
  259.                 Swap(E.x[E.s],E.x[i]);  
  260.                 int f1,f2;  
  261.                 int bb = Bound(E,f1,f2,y);  
  262.                 if(bb<bestc)  
  263.                 {  
  264.                     //子树可能含有最优解  
  265.                     //节点插入最小堆  
  266.                     MinHeapNode N;  
  267.                     N.NewNode(E,f1,f2,bb,n);  
  268.                     H.Insert(N);  
  269.                 }  
  270.                 Swap(E.x[E.s],E.x[i]);  
  271.             }  
  272.             delete []E.x;//完成节点扩展  
  273.         }  
  274.         if(H.Size() == 0)  
  275.         {  
  276.             break;  
  277.         }  
  278.         H.DeleteMin(E);//取下一扩展节点  
  279.     }  
  280.     return bestc;  
  281. }  
  282.   
  283. template <class Type>  
  284. inline void Swap(Type &a, Type &b)  
  285. {   
  286.     Type temp=a;   
  287.     a=b;   
  288.     b=temp;  
  289. }  
     程序运行结果如图: