Implementation of single source shortest path Dijkstra algorithm

Source: Internet
Author: User

This paper records the implementation of the Dijkstra algorithm, the graph is represented by the adjacency matrix, the assumption is that the graph is undirected graph, and connected, there is a graph, the practice of non-connected graph is similar.

Algorithm Description:

    1. First determine the source of the "single source", assuming it is a No. 0 vertex.

    2. Maintain three arrays dist[], color[], path[], set their subscript to 0...i...n-1:
      Dist[] represents the shortest distance from the source point to the vertex I, at initialization, if the source point to vertex i has a path, it is initialized to the weight of the path, otherwise initialized to Int_max;
      The color[] array actually represents a collection of two sets, or color[i] with a value of 1 representing the set of points that have determined the shortest path, and a color[i] value of 0 means that there is no set of points that determine the shortest path. Initialize to set the color of the source point to 1 and the remaining points to 0;
      path[] Array is stored to the path of vertex I, if path[i]=3,path[3]=2,paht[2]=0, this shortest path is 0->2->3->i, and the order given by the array is reversed.

    3. Select a minimum dist value from the dist[] array, assuming that the vertex coordinates are index, the dist value is the point of the final determined distance, the color value of the update point is 1, the following operation is the focus of the Dijkstra algorithm, and only such a focus operation, namely: in a collection that does not have a minimum distance defined (that is, a collection of points with a color value of 0), if the distance from the source point to index, plus the distance from index to these points, is less than the original Dist value, update the DIST value and update the path value.

    4. Repeats the 3rd operation until the collection with a color value of 0 is empty.

The following gives the C language implementation code, the methods are commented, here no longer explain, if the shortcomings please put forward (using the default diagram below):

#include <stdlib.h>#include <stdio.h>#include <limits.h>intNintSource =0;//Find the shortest path from the No. 0 node to the other nodeint* DIST;int* PATH;int* COLOR;//Color 1 indicates that the shortest path has been found, and 1 indicates that the shortest path is not found//Get the default diagram, as shown, using the adjacency matrix representationint* * Get_graph () {int* * MATRIX;intI,j;intStart,end,weight;printf("input vertex num:\n");scanf("%d", &n); Matrix = (int**)malloc(sizeof(int*) *n); for(i=0; i<n;i++) {Matrix[i] = (int*)malloc(sizeof(int) *n); for(j=0; j<n;j++) {if(i!=j) matrix[i][j] = Int_max;ElseMATRIX[I][J] =0; }    }printf("Input start end weight, stop by-1\n"); for(;;) {scanf("%d", &start);if(start==-1){ Break; }scanf("%d%d", &end,&weight);        Matrix[start][end] = weight;    Matrix[end][start] = weight; }returnMatrix;}//Using Dijkstra algorithm to find the shortest path of single sourcevoidSingle_source_shortest_path (int* * Matrix,intSOURCE) {intI,j,index,min; Dist = (int*)malloc(sizeof(int) *n); color = (int*)malloc(sizeof(int) *n); Path = (int*)malloc(sizeof(int) *n);//Initialize Shortest path:    //directly connected to initialize to weight, not directly connected to initialize to Int_max     for(i=0; i<n;i++) {dist[i] = Matrix[source][i]; Color[i] =0;if(I!=source && Dist[i]!=int_max)        {Path[i] = source; }Else{Path[i] =-1; }} Color[source] =1; Path[source] =0;//Find the shortest path from the source point to the other node     for(j=0; j<n;j++) {min = Int_max; index =-1; for(i=0; i<n;i++) {if(!color[i] && dist[i]<min)                {index = i;            min = Dist[i]; }        }if(index==-1){//The final distance of all fixed points is determined             Break; } Color[index] =1;//marked as a fixed point for which the shortest distance has been determined        //Next update to the distance of each fixed point of the shortest distance        //If the source point is the shortest distance from the node you just added + the distance from the node you just added to the distance from the fixed point of the shortest distance < the shortest distance from the source, the update         for(i=0; i<n;i++) {if(!color[i] && matrix[index][i]!=int_max && dist[index]+matrix[index][i]<dist[i])                 {Dist[i] = Dist[index]+matrix[index][i];            Path[i] = index; }        }    }}intMain () {int* * Matrix = get_graph ();intI,t; Single_source_shortest_path (Matrix,source);printf("\ n"); for(i=0; i<n;i++) {printf("%d:%d,and the path is (inverse order):%d", i,dist[i],i); t = Path[i]; while(1){printf("%d", t);if(t==0){ Break;        } t = Path[t]; }printf("\ n"); }printf("\ n");returnexit_success;}

The results of the operation are as follows:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Implementation of single source shortest path Dijkstra 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.