单源最短路径分支限界法之C++实现

本文介绍了一种利用分支限界法结合优先队列解决单源最短路径问题的方法,并通过具体实例展示了如何使用C++实现这一算法。文章详细解释了算法的工作原理、数据结构的设计以及关键步骤。

#include<iostream>
using namespace std;
const int size = 100;
const int inf = 5000;   //两点距离上界


//第一组测试参数
const int n = 6;    //图顶点个数加1
int prev[n];     //图的前驱顶点
int dist[] = {0,0,5000,5000,5000,5000};       //最短距离数组
int c[n][n] = {{0,0,0,0,0,0},{0,0,2,3,5000,5000},    //图的邻接矩阵
    {0,5000,0,1,2,5000},{0,5000,5000,0,9,2},
   {0,5000,5000,5000,0,2},{0,5000,5000,5000,5000,0}};
  /*
//第二组测试参数
const int n = 5;   //图顶点个数加1
int prev[n];     //图的前驱顶点
int dist[] = {0,0,5000,5000,5000};
int c[][n] = {{0,0,0,0,0},{0,0,2,3,5000},{0,5000,0,1,2},{0,5000,5000,0,9},{0,5000,5000,5000,0}};
 */ 

class MinHeapNode{

public :
 int i;    //顶点编号
 int length;   //当前路长

};

//循环队列
class CirQueue{
private:
  int front,rear;
  MinHeapNode data[size];
public:
   CirQueue(){
   front = rear = 0;
  }
  
  //元素入队操作
  void queryIn(MinHeapNode e){
   
   if((rear +1)%size != front){
    rear = (rear+1)%size; //队尾指针在循环意义下加1
    data[rear] = e;   //在队尾插入元素
   }
  }
  
  //元素出队操作
  MinHeapNode queryOut(){
   if(rear != front){
    front = (front+1)%size;  //队列在循环意义下加1
    return data[front];
   }
  }
  //读取队头元素,但不出队
  MinHeapNode getQuery(){
   if(rear != front){
    return data[(front+1)%size];
   }
  }
  
  //判断队列是否为空
  bool empty(){
   return front == rear;
   
  }
  //判断队列是否为满
  bool full(){
   return (rear +1)%size == front;
  }
 };//CirQueue结束


//图的表示
class Graph{

public:
 /**
  * 单源最短路径问题的优先队列式分支限界法
  */
 void shortestPath(int v){
  //创建队列
  CirQueue qq;
  
  //定义源为初始扩展结点
  MinHeapNode e;
  e.i = v;
  e.length = 0;
  dist[v] = 0;
  qq.queryIn(e);
  //搜索问题的解空间
  while(true){
   for(int j = 1;j<n;j++){
    if(j>=n){
     break;
    }
    MinHeapNode m = qq.getQuery();
    if((c[m.i][j]<inf)&&(m.length + c[m.i][j] < dist[j])){
     
     //顶点i到顶点j可达,且满足控制约束
     dist[j] = m.length + c[m.i][j];
     prev[j] = m.i;
    //加入活结点优先队列
    MinHeapNode mi;
    mi.i = j;
    mi.length  = dist[j];
    if(qq.full()){
     break;
    }
    qq.queryIn(mi);   //元素入队
    }
   }//for循环结束
   if(qq.empty()){
     break;
    }
   qq.queryOut();    //当该结点的孩子结点全部入队后,删除该结点
  }//while循环结束
 }//方法结束
};//类结束


int main(){

  Graph g;
  g.shortestPath(1);
  cout<<"最短路径长为 "<<dist[n-1]<<endl;

  cout<<"中间路径为: ";
  for(int i =3;i<n;i++){
   cout<<prev[i]<<" ";
  }

 cout<<endl<<"欢迎使用本系统"<<endl;
 return 0;
}

### 单源最短路径问题的分支限界法 C++ 实现 单源最短路径问题可以通过分支限界法来解决,这种方法利用优先队列存储活结点表,并以极小堆的形式实现。以下是基于分支限界法C++ 实现示例: ```cpp #include <iostream> #include <queue> #include <vector> #include <limits> using namespace std; const int INF = numeric_limits<int>::max(); struct Node { int vertex; int cost; bool operator<(const Node& other) const { return cost > other.cost; // 优先队列需要最小堆 } }; class Graph { public: int n; // 顶点数 vector<vector<int>> adj; // 邻接矩阵 vector<int> dist; // 最短距离数组 vector<int> prevNode; // 前驱数组 Graph(int size) : n(size), adj(n, vector<int>(n, INF)), dist(n, INF), prevNode(n, -1) {} void addEdge(int u, int v, int weight) { adj[u][v] = weight; } void shortestPaths(int source) { priority_queue<Node> pq; dist[source] = 0; pq.push(Node{source, 0}); while (!pq.empty()) { Node current = pq.top(); pq.pop(); int u = current.vertex; int currentCost = current.cost; if (currentCost > dist[u]) continue; // 剪枝:如果当前路径更长,则跳过 for (int v = 0; v < n; ++v) { if (adj[u][v] != INF) { // 如果存在边 int newCost = dist[u] + adj[u][v]; if (newCost < dist[v]) { // 更新最短路径 dist[v] = newCost; prevNode[v] = u; pq.push(Node{v, newCost}); } } } } } void printShortestPaths(int source) { cout << "Shortest distances from source " << source << ":\n"; for (int i = 0; i < n; ++i) { cout << "To " << i << ": " << (dist[i] == INF ? "INF" : to_string(dist[i])) << "\n"; } } }; int main() { int n = 5; // 顶点数 Graph graph(n); graph.addEdge(0, 1, 10); graph.addEdge(0, 4, 5); graph.addEdge(1, 2, 1); graph.addEdge(2, 3, 4); graph.addEdge(4, 1, 3); graph.addEdge(4, 2, 9); graph.addEdge(4, 3, 2); graph.shortestPaths(0); // 计算从源点 0 到其他点的最短路径 graph.printShortestPaths(0); return 0; } ``` 上述代码实现单源最短路径问题的分支限界法[^2]。其中,`Graph` 类包含邻接矩阵、最短距离数组和前驱数组等成员变量,并通过优先队列实现扩展结点的过程。在算法中,当发现某个结点的下界不小于当前找到的最短路径长度时,会剪去该结点为根的子树。 #### 关键点说明 - **优先队列**:用于存储活结点表,按照当前路径长度排序,确保每次扩展的是具有最小路径长度的结点。 - **剪枝策略**:如果某条路径的当前成本已经大于或等于已知的最短路径长度,则不再继续扩展该路径。 - **前驱数组**:记录每个顶点的前驱顶点,便于回溯完整的最短路径。 ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值