Dijkstra算法是典型最短路算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。Dijkstra算法能得出最短路径的最优解,但由于它遍历计算的节点很多,所以效率低。
Dijkstra算法思想为:设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合(用S表示,初始时S中只有一个源点,以后每求得一条最短路径 , 就将 加入到集合S中,直到全部顶点都加入到S中,算法就结束了),第二组为其余未确定最短路径的顶点集合(用U表示),按最短路径长度的递增次序依次把第二组的顶点加入S中。在加入的过程中,总保持从源点v到S中各顶点的最短路径长度不大于从源点v到U中任何顶点的最短路径长度。此外,每个顶点对应一个距离,S中的顶点的距离就是从v到此顶点的最短路径长度,U中的顶点的距离,是从v到此顶点只包括S中的顶点为中间顶点的当前最短路径长度。
Dijkstra算法具体步骤:
(1)初始时,S只包含源点,即S=,v的距离为0。U包含除v外的其他顶点,U中顶点u距离为边上的权(若v与u有边)或 )(若u不是v的出边邻接点)。(2)从U中选取一个距离v最小的顶点k,把k,加入S中(该选定的距离就是v到k的最短路径长度)。
(3)以k为新考虑的中间点,修改U中各顶点的距离;若从源点v到顶点u(u U)的距离(经过顶点k)比原来距离(不经过顶点k)短,则修改顶点u的距离值,修改后的距离值的顶点k的距离加上边上的权。
(4)重复步骤(2)和(3)直到所有顶点都包含在S中。
图解如下:
代码如下:
#include "GraphLink.h"
#include "MinHeap.h"
#include <vector>;
#include <map>;
#include <stack>;
using std::vector;
using std::map;
using std::stack;
class Dist
{
public:
int index;
int length;
int pre;
bool operator < (const Dist &dist)
{
return length < dist.length;
}
bool operator <= (const Dist &dist)
{
return length <= dist.length;
}
bool operator > (const Dist &dist)
{
return length > dist.length;
}
bool operator >= (const Dist &dist)
{
return length >= dist.length;
}
bool operator == (const Dist &dist)
{
return length == dist.length;
}
};
//dijkstra算法,其中参数G是图,参数s是源顶点,D是保存最短距离及其路径的数组
void dijkstra(Graph &graph, int s, Dist* &dist)
{
dist = new Dist[graph.edgesNum()];
for(int i = 0; i < graph.verticesNum();i++)// 初始化Mark数组、dist数组
{
graph.mark[i] = UNVISITED;
dist[i].index = i;
dist[i].length = INFINITE;
dist[i].pre = s;
}
dist[s].length = 0;
MinHeap<Dist> heap(graph.edgesNum());// 最小值堆(minheap),用以存放各点到源点s的length
heap.insert(dist[s]); //最初是加入源点s,length = 0;
map<int,int> route;//用以记录前驱顶点、当前顶点关系的map,此map用以保持路径
for(int i = 0; i < graph.edgesNum();i++)
{
bool found = false;
Dist d;
while(!heap.isEmpty())
{
heap.removeMin(d);//length最小值出堆
if(graph.mark[d.index] == UNVISITED)//找到距离s最近的顶点
{
cout<< "vertex index: " <<d.index<<" ";
cout<< "vertex pre: " <<d.pre<<" ";
if(d.pre == s)
{
cout << "route is :" << d.pre << "->" << d.index << "\t";
route[d.index] = d.pre;
}
else
{
stack<int> stk;
route[d.index] = d.pre;
int previous = d.pre;
while(previous != s)
{
stk.push(previous);
previous = route[previous];
}
stk.push(previous);
cout << "root is:";
while(!stk.empty())
{
cout << stk.top() << "->";
stk.pop();
}
cout << d.index << "\t";
}
cout<< "V0 --> V" << d.index <<" length: " <<d.length<<endl;
found = true;
break;
}
}
if(found)
{
int v = d.index;
graph.mark[v] = VISITED;// 把该点加入已访问组
// 因为v的加入,需要刷新v邻接点的dist值
for(Edge edge = graph.firstEdge(v); graph.isEdge(edge); edge = graph.nextEdge(edge))
{
if(dist[graph.toVertex(edge)].length > dist[v].length + graph.weight(edge))
{
dist[graph.toVertex(edge)].length = dist[v].length + graph.weight(edge);
dist[graph.toVertex(edge)].pre = v;
heap.insert(dist[graph.toVertex(edge)]);
}
}
}
}
}
//图7.19 单源最短路径的示例
int A[N][N] = {
// v0 v1 v2 v3 v4
/*v0*/ 0, 10, 0, 30, 100,
/*v1*/ 0, 0, 50, 0, 0,
/*v2*/ 0, 0, 0, 0, 10,
/*v3*/ 0, 10, 20, 0, 60,
/*v4*/ 0, 0, 0, 0, 0,
};
int main()
{
GraphLink<ListUnit> graphLink(N); // 建立图
graphLink.initGraph(graphLink, A,N); // 初始化图
Dist *dist;
int root[N] = {-1};
dijkstra(graphLink,0,dist);
system("pause");
return 0;
}