Shortest Path algorithm

Source: Internet
Author: User

Djkstra algorithm (single source Shortest path)

The basic idea of the algorithm is to find the closest vertex of the source point (the source point of the above example is the vertex of number 1th) each time, and then extend the vertex as the center, and finally get the shortest path from the source point to all the remaining points. The basic steps are as follows:

    • Divides all the vertices into two parts: the vertex set P and the unknown shortest path of the vertices collection Q for the shortest distance known. At first, only the source point is a vertex in the vertex collection P of the known shortest path. Here we use a book[i] array to record which points are in the set P. For example, for a vertex i, if book[i] is 1 it means that the vertex is in the set P, if book[i] is 0 it means that the vertex is in the set Q.
    • Set the source point S to its own shortest path of 0 that is dis=0. dis[i] is set to e[s][i] if there is a vertex I that can be reached directly from the source point. At the same time, all other vertices (which are not directly reachable by the source point) are set to ∞ with the shortest path.
      Select one of the closest vertices of the source point S (that is, the dis[u] minimum) in all vertices of the set Q to join the set P. It also investigates all edges with point u as the starting point and relaxes each edge. For example, if there is an edge from u to V, you can expand a path from S to V by adding the edge u->v to the tail, which is the length of dis[u]+e[u][v]. If this value is smaller than the value currently known as Dis[v], we can replace the value in the current Dis[v] with the new value.
    • Repeat the 3rd step, if the set Q is empty, the algorithm ends. The value in the final dis array is the shortest path from the source point to all vertices.
#include <stdio.h>    intMain () {inte[Ten][Ten],dis[Ten],book[Ten],i,j,n,m,t1,t2,t3,u,v,min;intinf=99999999;//Use INF (infinity abbreviation) to store a positive infinity value we think        //Read in N and m,n to indicate the number of vertices, m represents the number of edges        scanf("%d%d", &n,&m);//Initialize         for(i=1; i<=n;i++) for(j=1; j<=n;j++)if(I==J) e[i][j]=0;ElseE[i][j]=inf;//read in side         for(i=1; i<=m;i++) {scanf(" %d%d%d", &AMP;T1,&AMP;T2,&AMP;T3);        E[T1][T2]=T3; }//Initialize the DIS array, this is the initial distance from vertex number 1th to the rest of the vertices         for(i=1; i<=n;i++) dis[i]=e[1][i];//book Array Initialization         for(i=1; i<=n;i++) book[i]=0; book[1]=1;//dijkstra algorithm Core statement         for(i=1; i<=n-1; i++) {//Find the nearest vertex from vertex 1thMin=inf; for(j=1; j<=n;j++) {if(book[j]==0&& dis[j]<min) {min=dis[j];                U=j; }} book[u]=1; for(v=1; v<=n;v++) {if(E[u][v]<inf) {if(Dis[v]>dis[u]+e[u][v]) dis[v]=dis[u]+e[u][v]; }            }        }//Output The final result         for(i=1; i<=n;i++)printf("%d", Dis[i]);        GetChar (); GetChar ();return 0; }
Floyd algorithm (any two-point shortest path)

The basic idea of this code is that it is only allowed to go through the number 1th vertices at first, and then only the 1 and 2nd vertices are allowed to relay ... The shortest distance between any two points is allowed through all vertices of the 1~n number. In a nutshell: From the I vertex to the J vertex only passes the shortest distance from the front K points. Actually, it's a kind of "dynamic planning" idea.

#include <stdio.h>    intMain () {inte[Ten][Ten],K,I,J,N,M,T1,T2,T3;intinf=99999999;//Use INF (infinity abbreviation) to store a positive infinity value we think        //Read in N and m,n to indicate the number of vertices, m represents the number of edges        scanf("%d%d", &n,&m);//Initialize         for(i=1; i<=n;i++) for(j=1; j<=n;j++)if(I==J) e[i][j]=0;ElseE[i][j]=inf;//read in side         for(i=1; i<=m;i++) {scanf(" %d%d%d", &AMP;T1,&AMP;T2,&AMP;T3);        E[T1][T2]=T3; }//floyd-warshall algorithm Core statement         for(k=1; k<=n;k++) for(i=1; i<=n;i++) for(j=1; j<=n;j++)if(E[i][j]>e[i][k]+e[k][j]) e[i][j]=e[i][k]+e[k][j];//Output The final result         for(i=1; i<=n;i++) { for(j=1; j<=n;j++) {printf("%10d", E[i][j]); }printf("\ n"); }return 0; }
?

Shortest Path algorithm

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.