Dijstra algorithm C language implementation

Source: Internet
Author: User

It seems that the role of the group is really great.

Just now, I found that my abstract thinking capability is almost 0.

I always thought there was no way to deal with the set. Then the people in the group said they could use the bool array. Then I studied it.

When describing an algorithm, we can combine and subtract it. In C, we can use a bool array,

Initial Initialization is false.

Then, enter a value of true.

I found that writing programs by myself is easy to understand. I would like to ask other people some knowledge points and find out how they are implemented. I am not willing to read them.

I do not understand. The program is written by yourself. You can understand it yourself.

/* Author: linglingdate: 2011-10-8description: establish the directed graph's Adjacent matrix, traverse the depth of the Connection Matrix first, and find the shortest distance from one vertex to the other. */# include <stdio. h> # include <stdlib. h> # define maxvertexnum 100 typedef int vertextype; // User-Defined vertex type typedef int edgetype; // user-defined edge weight type typedef struct {vertextype vexs [maxvertexnum]; edgetype edges [maxvertexnum] [maxvertexnum]; int N, E;} mgrap; bool visited [maxvertexnum]; void createmgrap (mgrap *); void dfstraverse (Mgrap *); void DFS (mgrap *, INT); void Dijkstra (mgrap *, Int, int []); void main () {freopen ("input1.txt ", "r", stdin); mgrap * G = (mgrap *) malloc (sizeof (mgrap); createmgrap (g); For (INT I = 0; I <G-> N; I ++) {for (Int J = 0; j <G-> N; j ++) {printf ("%-8d ", g-> edges [I] [J]);} printf ("\ n");} dfstraverse (g); int d [5]; Dijkstra (G, 0, d);} void createmgrap (mgrap * g) {int I, J, K, W = 0, P; // printf ("Number of input vertices and edges \ n"); scanf ("% d, % d", & G-> N, & G-> E); For (P = 0; P <G-> N; P ++) {// fflush (stdin ); // clear the buffer // printf ("input vertex information"); scanf ("% d", & G-> vexs [p]);} // printf (" ing matrix initialization \ n"); for (I = 0; I <G-> N; I ++) for (j = 0; j <G-> N; j ++) g-> edges [I] [J] = 1000; For (k = 0; k <G-> E; k ++) {// printf ("reads the value W \ n of the edge between two vertices (I, j); scanf (" % d, % d, % d ", & I, & J, & W); G-> edges [I] [J] = W; // G-> edges [J] [I] = W; // printf ("Number of read I = % d, j = % d, W = % d \ n ", I, j, W) ;}} void dfstraverse (mgrap * g) {int I; (I = 0; I <G-> N; I ++) visited [I] = false; printf ("depth-first traversal sequence: \ n "); for (I = 0; I <G-> N; I ++) // make sure that each vertex has been traversed. If there is an isolated vertex, you can also if (! Visited [I]) {DFS (G, I);} printf ("\ n");} void DFS (mgrap * g, int I) {printf ("% d->", G-> vexs [I]); // fflush (stdout); visited [I] = true; For (Int J = 0; j <G-> N; j ++) if (G-> edges [I] [J] = 1 &&! Visited [J]) DFS (G, J);} void Dijkstra (mgrap * g, int S, int d []) {int K = 0; d [s] = 0; For (INT I = 0; I <G-> N; I ++) visited [I] = false; visited [s] = true; // initialize the shortest distance one-dimensional array for (INT I = 0; I <G-> N; I ++) if (! Visited [I]) d [I] = G-> edges [s] [I]; // modify the shortest distance, one-dimensional array for (INT I = 0; I <G-> n-1; I ++) {// find the smallest value, excluding the int min = 1000; For (Int J = 0; j <G-> N; j ++) {If (! Visited [J] & D [J] <min) {min = d [J]; k = J ;}} visited [k] = true; printf ("subscript % d \ n", k); // modify the distance value of the remaining vertex for (Int J = 0; j <G-> N; j ++) if (! Visited [J] & D [J]> d [k] + G-> edges [k] [J]) d [J] = d [k] + G-> edges [k] [J];} For (INT I = 0; I <G-> N; I ++) {printf ("the shortest distance from S to vertex % d \ n", I, d [I]);} printf ("\ n ");}

 

 

The last part is this algorithm. The rest is previously written and written on this basis. Otherwise, it cannot be executed by any algorithm.

Result

Visual View

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.