Dijkstra

来源:互联网 发布:淘宝热卖网怎么进去 编辑:程序博客网 时间:2024/05/16 15:01

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中。

图解如下:






代码如下:


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "GraphLink.h"  
  2. #include "MinHeap.h"  
  3. #include <vector>;  
  4. using std::vector;  
  5.   
  6. class Dist  
  7. {  
  8. public:  
  9.     int index;  
  10.     int length;  
  11.     int pre;  
  12.   
  13.     bool operator < (const Dist &dist)  
  14.     {  
  15.         return length < dist.length;  
  16.     }  
  17.   
  18.     bool operator <= (const Dist &dist)  
  19.     {  
  20.         return length <= dist.length;  
  21.     }  
  22.   
  23.     bool operator > (const Dist &dist)  
  24.     {  
  25.         return length > dist.length;  
  26.     }  
  27.   
  28.     bool operator >= (const Dist &dist)  
  29.     {  
  30.         return length >= dist.length;  
  31.     }  
  32.   
  33.     bool operator == (const Dist &dist)  
  34.     {  
  35.         return length == dist.length;  
  36.     }  
  37. };  
  38.   
  39.   
  40. //dijkstra算法,其中参数G是图,参数s是源顶点,D是保存最短距离及其路径的数组  
  41. void dijkstra(Graph &graph, int s, Dist* &dist)  
  42. {  
  43.     dist = new Dist[graph.edgesNum()];  
  44.     for(int i = 0; i < graph.verticesNum();i++)// 初始化Mark数组、dist数组  
  45.     {  
  46.         graph.mark[i] = UNVISITED;  
  47.         dist[i].index = i;  
  48.         dist[i].length = INFINITE;  
  49.         dist[i].pre = s;  
  50.     }  
  51.   
  52.     dist[s].length = 0;  
  53.     MinHeap<Dist> heap(graph.edgesNum());// 最小值堆(minheap),用以存放各点到源点s的length  
  54.     heap.insert(dist[s]); //最初是加入源点s,length = 0;  
  55.   
  56.     /*queue<int> aque;//记录源点s到各个顶点所经过顶点的队列 
  57.     aque.push(s);//初始化源点s放入队列 
  58.     queue<int> bque;//辅助队列*/  
  59.   
  60.   
  61.     for(int i = 0; i < graph.edgesNum();i++)  
  62.     {  
  63.           
  64.         bool found = false;  
  65.         Dist d;  
  66.   
  67.         while(!heap.isEmpty())  
  68.         {  
  69.             heap.removeMin(d);//length最小值出堆  
  70.   
  71.             if(graph.mark[d.index] == UNVISITED)//找到距离s最近的顶点  
  72.             {  
  73.                 cout<< "vertex index: " <<d.index<<"   ";  
  74.                 cout<< "vertex pre: " <<d.pre<<"   ";  
  75.   
  76.             /*  cout<<"经过路径: "; 
  77.                 while(!aque.empty()) 
  78.                 { 
  79.                     cout << "V" << aque.front() << "  "; 
  80.                     bque.push(aque.front()); 
  81.                     aque.pop(); 
  82.                 } 
  83.  
  84.                 while(!bque.empty())//恢复aque原始记录 
  85.                 { 
  86.                     aque.push(bque.front()); 
  87.                     bque.pop(); 
  88.                 }    
  89.                 cout << endl;*/  
  90.   
  91.                 cout<< "V0 --> V" << d.index <<"  length    : " <<d.length<<endl;  
  92.                   
  93.                 found = true;  
  94.                 break;  
  95.             }  
  96.         }  
  97.   
  98.   
  99.         if(found)  
  100.         {  
  101.             int v  = d.index;  
  102.             graph.mark[v] = VISITED;// 把该点加入已访问组  
  103.   
  104.             // 因为v的加入,需要刷新v邻接点的dist值  
  105.             for(Edge edge = graph.firstEdge(v); graph.isEdge(edge); edge = graph.nextEdge(edge))  
  106.             {  
  107.                 if(dist[graph.toVertex(edge)].length > dist[v].length + graph.weight(edge))  
  108.                 {  
  109.                     dist[graph.toVertex(edge)].length = dist[v].length + graph.weight(edge);  
  110.                     dist[graph.toVertex(edge)].pre = v;  
  111.   
  112.                     heap.insert(dist[graph.toVertex(edge)]);  
  113.   
  114.                     /*heap.removeMin(d); 
  115.                     if((aque.back() != d.pre)) 
  116.                     { 
  117.                         aque.push(d.pre); 
  118.                     } 
  119.                     heap.insert(d);*/  
  120.               
  121.                 }  
  122.             }  
  123.         }  
  124.           
  125.     }  
  126. }  
  127.   
  128. //图7.19  单源最短路径的示例  
  129. int A[N][N] =  {  
  130.     //  v0  v1  v2  v3  v4    
  131. /*v0*/  0, 10,  0, 30, 100,  
  132. /*v1*/  0,  0, 50,  0,  0,   
  133. /*v2*/  0,  0,  0,  0, 10,   
  134. /*v3*/  0, 10, 20,  0, 60,   
  135. /*v4*/  0,  0,  0,  0,  0,   
  136. };  
  137.   
  138.   
  139.   
  140. int main()  
  141. {  
  142.     GraphLink<ListUnit> graphLink(N);              // 建立图   
  143.     graphLink.initGraph(graphLink, A,N); // 初始化图  
  144.       
  145.     Dist *dist;  
  146.     dijkstra(graphLink,0,dist);  
  147.     system("pause");  
  148.     return 0;  
  149. }  

0 0
原创粉丝点击