文章标题

      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

  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. }   
#include <iostream>

template<class Type>
class Graph;

template<class T> 
class MinHeap 
{ 
    template<class Type>
    friend class Graph;
    public: 
        MinHeap(int maxheapsize = 10); 
        ~MinHeap(){delete []heap;} 

        int Size() const{return currentsize;} 
        T Max(){if(currentsize) return heap[1];} 

        MinHeap<T>& Insert(const T& x); 
        MinHeap<T>& DeleteMin(T &x); 

        void Initialize(T x[], int size, int ArraySize); 
        void Deactivate(); 
        void output(T a[],int n);
    private: 
        int currentsize, maxsize; 
        T *heap; 
}; 

template <class T> 
void MinHeap<T>::output(T a[],int n) 
{ 
    for(int i = 1; i <= n; i++) 
    cout << a[i] << " "; 
    cout << endl; 
} 

template <class T> 
MinHeap<T>::MinHeap(int maxheapsize) 
{ 
    maxsize = maxheapsize; 
    heap = new T[maxsize + 1]; 
    currentsize = 0; 
} 

template<class T> 
MinHeap<T>& MinHeap<T>::Insert(const T& x) 
{ 
    if(currentsize == maxsize) 
    { 
        return *this; 
    } 
    int i = ++currentsize; 
    while(i != 1 && x < heap[i/2]) 
    { 
        heap[i] = heap[i/2]; 
        i /= 2; 
    } 

    heap[i] = x; 
    return *this; 
} 

template<class T> 
MinHeap<T>& MinHeap<T>::DeleteMin(T& x) 
{ 
    if(currentsize == 0) 
    { 
        cout<<"Empty heap!"<<endl; 
        return *this; 
    } 

    x = heap[1]; 

    T y = heap[currentsize--]; 
    int i = 1, ci = 2; 
    while(ci <= currentsize) 
    { 
        if(ci < currentsize && heap[ci] > heap[ci + 1]) 
        { 
            ci++; 
        } 

        if(y <= heap[ci]) 
        { 
            break; 
        } 
        heap[i] = heap[ci]; 
        i = ci; 
        ci *= 2; 
    } 

    heap[i] = y; 
    return *this; 
} 

template<class T> 
void MinHeap<T>::Initialize(T x[], int size, int ArraySize) 
{ 
    delete []heap; 
    heap = x; 
    currentsize = size; 
    maxsize = ArraySize; 

    for(int i = currentsize / 2; i >= 1; i--) 
    { 
        T y = heap[i]; 
        int c = 2 * i; 
        while(c <= currentsize) 
        { 
            if(c < currentsize && heap[c] > heap[c + 1]) 
                c++; 
            if(y <= heap[c]) 
                break; 
            heap[c / 2] = heap[c]; 
            c *= 2; 
        } 
        heap[c / 2] = y; 
    } 
} 

template<class T> 
void MinHeap<T>::Deactivate() 
{ 
    heap = 0; 
} 
     2、6d2.cpp

  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. }  
//单源最短路径问题 分支 限界法求解




include "stdafx.h"

include "MinHeap2.h"

include <iostream>

include <fstream>

using namespace std;

ifstream fin("6d2.txt");

template<class Type>
class Graph
{
friend int main();
public:
void ShortesPaths(int);
private:
int n, //图G的顶点数
*prev; //前驱顶点数组
Type **c, //图G的领接矩阵
*dist; //最短距离数组
};

template<class Type>
class MinHeapNode
{
friend Graph<Type>;
public:
operator int ()const{return length;}
private:
int i; //顶点编号
Type length; //当前路长
};

template<class Type>
void Graph<Type>::ShortesPaths(int v)//单源最短路径问题的优先队列式分支限界法
{
MinHeap<MinHeapNode<Type>> H(1000);
MinHeapNode<Type> E;

//定义源为初始扩展节点
E.i=v;
E.length=0;
dist[v]=0;

while (true)//搜索问题的解空间
{
    for (int j = 1; j &lt;= n; j++)
        if ((c[E.i][j]!=0)&amp;&amp;(E.length+c[E.i][j]&lt;dist[j])) {

             // 顶点i到顶点j可达,且满足控制约束
             dist[j]=E.length+c[E.i][j];
             prev[j]=E.i;

             // 加入活结点优先队列
             MinHeapNode&lt;Type&gt; N;
             N.i=j;
             N.length=dist[j];
             H.Insert(N);
        }`
    try 
    {
        H.DeleteMin(E); // 取下一扩展结点
    }        
    catch (int) 
    {
        break;
    }  
    if (H.currentsize==0)// 优先队列空
    {
        break;
    }
}

}

int main()
{
int n=11;
int prev[12] = {0,0,0,0,0,0,0,0,0,0,0,0};

int dist[12]={1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000};

cout&lt;&lt;"单源图的邻接矩阵如下:"&lt;&lt;endl;
int **c = new int*[n+1];

for(int i=1;i&lt;=n;i++)
{
    c[i]=new int[n+1];
    for(int j=1; j&lt;=n; j++)
    {
        fin&gt;&gt;c[i][j];
        cout&lt;&lt;c[i][j]&lt;&lt;" ";
    }
    cout&lt;&lt;endl;
}

int v=1;
Graph&lt;int&gt; G;
G.n=n;

G.c=c;
G.dist=dist;
G.prev=prev;
G.ShortesPaths(v);

cout&lt;&lt;"从S到T的最短路长是:"&lt;&lt;dist[11]&lt;&lt;endl;
for (int i = 2; i &lt;= n; i++)
{
    cout&lt;&lt;"prev("&lt;&lt;i&lt;&lt;")="&lt;&lt;prev[i]&lt;&lt;"   "&lt;&lt;endl;
}

for (int i = 2; i &lt;= n; i++)
{
    cout&lt;&lt;"从1到"&lt;&lt;i&lt;&lt;"的最短路长是:"&lt;&lt;dist[i]&lt;&lt;endl;
}

for(int i=1;i&lt;=n;i++)
{
    delete []c[i];
}

delete []c;
c=0;    
return 0;

}     程序运行结果如图:



                </div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值