分支限界法

来源:互联网 发布:合肥工业大学网络视频 编辑:程序博客网 时间:2024/06/10 19:27

分支限界法

基本思想:

分支限界法常以广度优先或以最小耗费(最大效益)优先的方式搜索问题的解空间树。
在分支限界法中,每一个活结点只有一次机会成为扩展结点。活结点一旦成为扩展结点,就一次性产生其所有儿子结点。在这些儿子结点中,导致不可行解或导致非最优解的儿子结点被舍弃,其余儿子结点被加入活结点表中。
此后,从活结点表中取下一结点成为当前扩展结点,并重复上述结点扩展过程。这个过程一直持续到找到所需的解或活结点表为空时为止。

分支限界法与回溯法的区别:

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

常见的两种分支限界法:

(1)队列式(FIFO)分支限界法
按照队列先进先出(FIFO)原则选取下一个节点为扩展节点。
(2)优先队列式分支限界法
按照优先队列中规定的优先级选取优先级最高的节点成为当前扩展节点。

例子:单源最短路径问题(参考http://www.cnblogs.com/chinazhangjie/archive/2010/11/01/1866136.html)

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


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


找到一条路径:


目前的最短路径是8,一旦发现某个结点的下界不小于这个最短路进,则剪枝:


同一个结点选择最短的到达路径:



2.剪枝策略
在算法扩展结点的过程中,一旦发现一个结点的下界不小于当前找到的最短路长,则算法剪去以该结点为根的子树。
在算法中,利用结点间的控制关系进行剪枝。从源顶点s出发,2条不同路径到达图G的同一顶点。由于两条路径的路长不同,因此可以将路长长的路径所对应的树中的结点为根的子树剪去。 
3.算法思想
解单源最短路径问题的优先队列式分支限界法用一极小堆来存储活结点表。其优先级是结点所对应的当前路长。
算法从图G的源顶点s和空优先队列开始。结点s被扩展后,它的儿子结点被依次插入堆中。此后,算法从堆中取出具有最小当前路长的结点作为当前扩展结点,并依次检查与当前扩展结点相邻的所有顶点。如果从当前扩展结点i到顶点j有边可达,且从源出发,途经顶点i再到顶点j的所相应的路径的长度小于当前最优路径长度,则将该顶点作为活结点插入到活结点优先队列中。这个结点的扩展过程一直继续到活结点优先队列为空时为止。

源码如下:

  1. /* 主题:单源最短路径问题 
  2. * 作者:chinazhangjie 
  3. * 邮箱:chinajiezhang@gmail.com 
  4. * 开发语言:C++ 
  5. * 开发环境:Mircosoft Virsual Studio 2008 
  6. * 时间: 2010.11.01 
  7. */  
  8.   
  9. #include <iostream>  
  10. #include <vector>  
  11. #include <queue>  
  12. #include <limits>  
  13. using namespace std;  
  14.   
  15. struct node_info  
  16. {  
  17. public:  
  18.     node_info (int i,int w)   
  19.         : index (i), weight (w) {}  
  20.     node_info ()   
  21.         : index(0),weight(0) {}  
  22.     node_info (const node_info & ni)   
  23.         : index (ni.index), weight (ni.weight) {}  
  24.   
  25.     friend   
  26.     bool operator < (const node_info& lth,const node_info& rth) {  
  27.         return lth.weight > rth.weight ; // 为了实现从小到大的顺序  
  28.     }  
  29.   
  30. public:  
  31.     int index; // 结点位置  
  32.     int weight; // 权值  
  33. };  
  34.   
  35. struct path_info   
  36. {  
  37. public:  
  38.     path_info ()  
  39.         : front_index(0), weight (numeric_limits<int>::max()) {}  
  40.   
  41. public:  
  42.     int front_index;  
  43.     int weight;  
  44. };  
  45.   
  46. // single source shortest paths  
  47. class ss_shortest_paths  
  48. {  
  49.        
  50. public:  
  51.     ss_shortest_paths (const vector<vector<int> >& g,int end_location)   
  52.         :no_edge (-1), end_node (end_location), node_count (g.size()) , graph (g)   
  53.     {}  
  54.   
  55.     // 打印最短路径  
  56.     void print_spaths () const {  
  57.         cout << "min weight : " << shortest_path << endl;  
  58.         cout << "path: " ;  
  59.         copy (s_path_index.rbegin(),s_path_index.rend(),  
  60.             ostream_iterator<int> (cout, " "));  
  61.         cout << endl;  
  62.     }  
  63.   
  64.     // 求最短路径  
  65.     void shortest_paths () {  
  66.         vector<path_info> path(node_count);  
  67.         priority_queue<node_info,vector<node_info> > min_heap;  
  68.         min_heap.push (node_info(0,0));    // 将起始结点入队  
  69.   
  70.         while (true) {  
  71.             node_info top = min_heap.top ();    // 取出最大值  
  72.             min_heap.pop ();  
  73.   
  74.             // 已到达目的结点  
  75.             if (top.index == end_node) {  
  76.                 break ;  
  77.             }  
  78.             // 未到达则遍历  
  79.             for (int i = 0; i < node_count; ++ i) {  
  80.                 // 顶点top.index和i间有边,且此路径长小于原先从原点到i的路径长   
  81.                 if (graph[top.index][i] != no_edge &&   
  82.                     (top.weight + graph[top.index][i]) < path[i].weight) {  
  83.                     min_heap.push (node_info (i,top.weight + graph[top.index][i]));  
  84.                     path[i].front_index = top.index;  
  85.                     path[i].weight = top.weight + graph[top.index][i];  
  86.                 }  
  87.             }  
  88.             if (min_heap.empty()) {  
  89.                 break ;  
  90.             }  
  91.         }  
  92.   
  93.         shortest_path = path[end_node].weight;  
  94.         int index = end_node;  
  95.         s_path_index.push_back(index) ;  
  96.         while (true) {  
  97.             index = path[index].front_index ;  
  98.             s_path_index.push_back(index);  
  99.             if (index == 0) {  
  100.                 break;  
  101.             }  
  102.         }  
  103.     }  
  104.   
  105. private:  
  106.     vector<vector<int> >    graph ;            // 图的数组表示  
  107.     int                        node_count;        // 结点个数  
  108.     const int                no_edge;        // 无通路  
  109.     const int                end_node;        // 目的结点  
  110.     vector<int>                s_path_index;    // 最短路径  
  111.     int                        shortest_path;    // 最短路径  
  112. };  
  113.   
  114. int main()  
  115. {  
  116.     const int size = 11;   
  117.     vector<vector<int> > graph (size);  
  118.     for (int i = 0;i < size; ++ i) {  
  119.         graph[i].resize (size);  
  120.     }  
  121.     for (int i = 0;i < size; ++ i) {  
  122.         for (int j = 0;j < size; ++ j) {  
  123.             graph[i][j] = -1;  
  124.         }  
  125.     }  
  126.     graph[0][1] = 2;  
  127.     graph[0][2] = 3;  
  128.     graph[0][3] = 4;  
  129.     graph[1][2] = 3;  
  130.     graph[1][5] = 2;  
  131.     graph[1][4] = 7;  
  132.     graph[2][5] = 9;  
  133.     graph[2][6] = 2;  
  134.     graph[3][6] = 2;  
  135.     graph[4][7] = 3;  
  136.     graph[4][8] = 3;  
  137.     graph[5][6] = 1;  
  138.     graph[5][8] = 3;  
  139.     graph[6][9] = 1;  
  140.     graph[6][8] = 5;  
  141.     graph[7][10] = 3;  
  142.     graph[8][10] = 2;  
  143.     graph[9][8] = 2;  
  144.     graph[9][10] = 2;  
  145.   
  146.     ss_shortest_paths ssp (graph, 10);  
  147.     ssp.shortest_paths ();  
  148.     ssp.print_spaths ();  
  149.     return 0;  
  150. }  

测试数据(图)


测试结果:

min weight :8
path:
026910


原创粉丝点击