1、問題描述
在下圖所給的有向圖g中,每一邊都有乙個非負邊權。要求圖g的從源頂點s到目標頂點t之間的最短路徑。
下圖是用優先佇列式分支限界法解有向圖g的單源最短路徑問題產生的解空間樹。其中,每乙個結點旁邊的數字表示該結點所對應的當前路長。
找到一條路徑:
目前的最短路徑是8,一旦發現某個結點的下界不小於這個最短路進,則剪枝:
同乙個結點選擇最短的到達路徑:

2.剪枝策略
在演算法擴充套件結點的過程中,一旦發現乙個結點的下界不小於當前找到的最短路長,則演算法剪去以該結點為根的子樹。
在演算法中,利用結點間的控制關係進行剪枝。從源頂點s出發,2條不同路徑到達圖g的同一頂點。由於兩條路徑的路長不同,因此可以將路長長的路徑所對應的樹中的結點為根的子樹剪去。
3.演算法思想
解單源最短路徑問題的優先佇列式分支限界法用一極小堆來儲存活結點表。其優先順序是結點所對應的當前路長。
演算法從圖g的源頂點s和空優先佇列開始。結點s被擴充套件後,它的兒子結點被依次插入堆中。此後,演算法從堆中取出具有最小當前路長的結點作為當前擴充套件結點,並依次檢查與當前擴充套件結點相鄰的所有頂點。如果從當前擴充套件結點i到頂點j有邊可達,且從源出發,途經頂點i再到頂點j的所相應的路徑的長度小於當前最優路徑長度,則將該頂點作為活結點插入到活結點優先佇列中。這個結點的擴充套件過程一直繼續到活結點優先隊列為空時為止。
#include #includeview code#include
#include
using
namespace
std;
struct
node_info
node_info ()
: index(
0),weight(0
) {}
node_info (
const node_info &ni)
: index (ni.index), weight (ni.weight) {}
friend
bool
operator
< (const node_info& lth,const node_info&rth)
public
:
int index; //
結點位置
int weight; //
權值};
struct
path_info
public
:
intfront_index;
intweight;
};//
single source shortest paths
class
ss_shortest_paths
//列印最短路徑
void print_spaths () const
//求最短路徑
void
shortest_paths ()
//未到達則遍歷
for (int i = 0; i < node_count; ++i)
}if(min_heap.empty())
}shortest_path =path[end_node].weight;
int index =end_node;
s_path_index.push_back(index) ;
while (true
) }
}private
: vector
int> > graph ; //
圖的陣列表示
int node_count; //
結點個數
const
int no_edge; //
無通路const
int end_node; //
目的結點
vector s_path_index; //
最短路徑
int shortest_path; //
最短路徑
};int
main()
for (int i = 0;i < size; ++i)
}graph[
0][1] = 2
; graph[
0][2] = 3
; graph[
0][3] = 4
; graph[
1][2] = 3
; graph[
1][5] = 2
; graph[
1][4] = 7
; graph[
2][5] = 9
; graph[
2][6] = 2
; graph[
3][6] = 2
; graph[
4][7] = 3
; graph[
4][8] = 3
; graph[
5][6] = 1
; graph[
5][8] = 3
; graph[
6][9] = 1
; graph[
6][8] = 5
; graph[
7][10] = 3
; graph[
8][10] = 2
; graph[
9][8] = 2
; graph[
9][10] = 2
; ss_shortest_paths ssp (graph,
10);
ssp.shortest_paths ();
ssp.print_spaths ();
return0;
}
測試資料(圖)
測試結果min weight : 8path: 0 2 6 9 10
**:
分支限界法之單源最短路徑
問題描述 下面以乙個例子來說明單源最短路徑問題 在下圖所給的有向圖g中,每一邊都有乙個非負邊權。要求圖g的從源頂點s到所有其他頂點的最短路徑。演算法思想 解單源最短路徑問題的優先佇列式分支限界法用一極小堆來儲存活結點表。其優先順序是結點所對應的當前路長。1 從圖g的源頂點s和空優先佇列開始。2 結點...
單源最短路徑(分支限界)
優先順序 當前路徑長度 剪枝函式 由於圖g中各邊的權均非負,所以結點所對應的當前路長也是解空間樹中以該結點為根的子樹中所有結點對應的路長的乙個下界。擴充套件結點的過程中,一旦發現乙個結點的下界不小於當前找到的最短路長,則演算法剪去以該結點為根的子樹。資料的儲存 二維陣列type g n n 儲存鄰接...
單源最短路徑分支限界法之java實現
public class short int c int n 4 int dist int c graph g new graph n,prev,c,dist g.shortestpath 1 system.out.println dist n class minheapnode 迴圈佇列 clas...