The shortest Path Knowledge point Summary (Dijkstra,floyd,spfa,bellman-ford) _ Knowledge,

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.