Shortest Path ~ Dicos algorithm
The Dicos algorithm was proposed by the Dutch computer scientist Aids Shal Dexla . The algorithm uses breadth-first search to solve the single-source shortest path problem of nonnegative weighted graph, and the algorithm finally obtains a shortest path tree. This algorithm is often used for routing algorithms or as a sub-module of other graph algorithms, this algorithm is used to find a point to all other points between the shortest path.
The use of variables in this algorithm:
Map[][] Two-dimensional array records the weights between two points, for example Map[i][j] to store the weight of the I-point to J-Point, when as a graph, give i,j need to store only a map[][], but the general situation is the use of the graph, need to save two map[][], that map[i][j]= map[j][i]= weight value.
Dis[] A one-dimensional array holds the shortest distance from point to point, mark[] one-dimensional array marks used points.
Single Source shortest circuit:
Ⅰ, the shortest path length from one point to all other points
Ⅱ, basic operation: relax operation.
Ⅲ, Dis[j] > Dis[vir] + map[vir][j] Such an edge (VIR,J) becomes tight, it can be relaxed operation.
The code for relaxation of all points can be found in the following:
for (int j = 1; J <= N; j + +)
{
if (Dis[j] > Dis[vir] + map[vir][j] &&!mark[j])
DIS[J] = Dis[vir] + map[vir][j];
}
Ⅳ, the first to give each point a very large dis value, from dis[s] = 0; start, continue to relax the point of relaxation operations, until the shortest path to all points.
This algorithm requires that there is no negative weight edge in the graph. It can be proved that the points with the smallest dis[i] are not added to the shortest path, and the points thereafter cannot be loosened. So every time you look for the nearest point for relaxation.
Please refer to the code for details:
#include <stdio.h> #define INF 0x3f3f3f3f//defines a large value that initializes the int map[1010][1010];//to hold the weight value int dis[1010];//between two points The distance from the starting point int mark[1010];//marks the used point int n,m;//has n points, numbered 1~n, with M set of data void Dijkstra (int s) {int vir,min;for (int i=1;i<=n;i+ +)//Initialize the tag array and the distance array {mark[i]=0;//0 Indicates that this point is not used dis[i]=inf;} dis[s]=0;for (int i=1;i<=n;i++) {min=inf;for (int j=1;j<=n;j++)//Find the least weighted point {if (!mark[j]&&dis[j]<min) {min=dis[j];vir=j;}} if (min==inf) break;//if not found or has been searched, jump out of the mark[vir]=1;//will find the point mark for (int j=1;j<=n;j++)//will not find and can relax the point of relaxation operation {if (!mark[j] &&DIS[J]>DIS[VIR]+MAP[VIR][J]) dis[j]=dis[vir]+map[vir][j];}} int main () {while (scanf ("%d%d", &n,&m)!=eof) {for (int i=1;i<=n;i++)//Initialize map array for (int j=1;j<=n;j++) map[ i][j]=inf;for (int i=0;i<m;i++) {int x,y,d;scanf ("%d%d%d", &x,&y,&d);//input two points and weights if (map[x][y]>d)// If there is repetition, take the shorter {Map[x][y]=d;map[y][x]=d;}} int start,end;scanf ("%d%d", &start,&end);//input start and end point Dijkstra (start);//Call Dicos Beecher algorithm printf ("%d\n", Dis[end]);// The shortest distance between the output starting point and the end point} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Summary of Dicos algorithm