Summary of the shortest path Knowledge point (Dijkstra,floyd,spfa,bellman-ford)
Dijkstra algorithm:
Problems to solve:
The problem of single source shortest path on a direction graph with weights. And the weights are non-negative. If the implementation method is appropriate, the Dijkstra run time is lower than the Bellman-ford algorithm.
Ideas:
If there is a shortest path from I to J (Vi ...). VK,VJ), Vk is a vertex in front of Vj. So (Vi..) VK) must also be the shortest path from I to K. In order to find the shortest path, the algorithm of increasing the shortest path length and generating the shortest path Dijkstra is proposed. For example, for the source vertex V0, first select the vertex of its direct adjacent vertex of the shortest length of vertices VI, then the current known can be reached from V0 to VJ vertex of the shortest distance dist[j]=min{dist[j],dist[i]+matrix[i][j]}, the application of greedy thought. According to this idea, the pseudo code of the Dijkstra algorithm is given directly, which can be used to calculate the single source shortest path of the positive weight graph, and is suitable for both the non directed graph and the directed graph.
Clears the label of all points
Set D[0]=0, other D[i]=inf
Loop n times
{
In the label of all points, select the node x with the lowest D value
Mark the node X.
For all edges starting from X (x,y), update d[y]=min{d[y],d[x]+w (x,y)}
}
In addition to finding the shortest length, using the Dijkstra algorithm can easily print out the node 0 to all nodes of the minimum path itself.
Code implementation:
1 2 3 4 5 6 7 8 void Dijkstra (int Sta., at the same the same the same. RT)//From Start point { int i,j,k memset (vis,0, sizeof (VIS))//Mark whether Accessed for (i=1 i<=n; i++)//n to total points { & nbsp; if (i==start) dis[i]=0; Else Dis[i]=inf; } for (I=1 i<=n; i++) { int R; int Min=inf; for (j=1 j<=n; j + +) if (!Vis[j]&&dis[j]<min) { Min=dis[j]; r=j;