圖的最短路徑問題


  1. 無權圖的單源最短路徑算法(鄰接表存儲)

(相似於圖的遍歷的廣度優先算法)

 1 typedef int Vertex;
 2 void UnWeighted(LGraph Graph,Vertex S){
 3     int i;
 4     ListNode W;
 5     int Dist[MaxVertexNumber],Path[MaxVertexNumber];//Dist存儲到源點S的最短距離,Path存儲到源點S路徑
 6     Queue Q;
 7     Q=CreatQueue(Graph->Nn);
 8     for(i=0;i<Graph->Nn;i++){
 9         Dist[i]=Path[i]=-1;   //將Dist和Path初始化為-1
10     }
11     Dist[S]=0;
12     AddQ(Q,S);
13     while(!IsEmpty(Q)){
14         V=DeleteQ(Q);
15         for(W=Graph->G[V].FirstEdge;W;W=W.next){
16             if(Dist[W->AdjV]==-1){
17                 Dist[W->AdjV]=Dist[V]+1;
18                 Path[W->AdjV]=V;
19                 AddQ(Q,W->AdjV);
20             } 
21         }
22     }
23 }

 

 2.有權圖的單源最短路徑算法(鄰接矩陣存儲)

 

 1 typedef int Vertex;
 2 #define Infinity 10000;
 3 int Dist[VertexMaxNum],Path[VertexMaxNum];
 4 Vertex FindMinDist(MGraph Graph,int Dist[],int Collected[]){
 5     MinDist=Infinity;
 6     Vertex V,MinV;
 7     for(V=0;V<Graph->Nv;V++){
 8         if(Collected[V]==false && Dist[V]<MinDist){
 9             MinV=V;
10             MinDist=Dist[V];
11         }
12     }
13     if(MinDist<Infinity){
14         return MinV;
15     }
16     else{
17         return ERRORS;
18     }
19 }
20 bool Dijkstra(MGraph Graph,int Dist[],int Path[],Vertex S){
21     bool Collected[VertexMaxNum];
22     Vertex V,W;
23     for(V=0;V<Graph->Nv;V++){
24         Dist[V]=Graph->G[S][V];//默認鄰接矩陣中不存在的邊用INFINITY表示
25         Path[V]=-1;
26         if(G[S][V]<Infinity){
27             Path[V]=S;
28         }
29         Collected[V]=false;
30     }
31     Dist[S]=0;
32     Collected[S]=true;
33     Collected[V]=true;
34     while(1){
35         V=FindMinDist(Graph,Dist,Collected);
36         Collected[V]=true;
37         if(V==ERRORS){
38             break;
39         }
40         for(W=0;W<Graph->Nv;W++){
41             if(G[V][M]<0) return false;
42             if(Graph->G[V][M]<Infinity && Collected[W]==false){
43                 if(Dist[V]+Graph[V][W]<Dist[W]){
44                    Dist[W]=Dist[V]+Graph[V][W];
45                    Path[W]=V;
46                 }   
47             }
48         }
49     }
50     return true;
51 }

 3.有權圖多源最短路徑算法

1,若有Nv個頂點,將Dijkstra算法調用N遍

2,folyd算法(不能有負值圈)

 1 bool Floyd(MGraph Graph,int Dist[][],Vertex Path[][] ){
 2     Vertex i,j,k;
 3     for(i=0;i<Graph->Nv;i++){
 4         for(j=0;j<Graph->Nv;j++){
 5             Dist[i][j]=Graph->G[i][j];
 6             Path[i][j]=-1;
 7         }
 8     }
 9     for(k=0;k<Graph->Nv;k++){
10         for(i=0;i<Graph->Nv;i++){
11             for(j=0;j<Graph->Nv;j++){
12                 if(Dist[i][k]+Dist[k][j]<Dist[i][j]){
13                     Dist[i][j]=Dist[i][k]+Dist[k][j];
14                     if(i==j && Dist[i][j]<0) return false; //有負值,返回錯誤
15                     Path[i][j]=k;
16                 }
17             }
18         }
19     }
20     return true;
21 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM