Dijkstra shortest path algorithm code

xiaoxiao2021-03-05  25

These include test programs that can be directly compiled

Compiled in Windows XP Professional, DEV-CPP V4.9.9.0

Refer to the computer network (third edition)

/ ************************************** Name: Dijkstra Routing Algorithm Example ** Program: FINYTANG ** Date: 2005.4.10 ** Version: 1.0 *********************************************** /

#include #include

#define infinity 10000000 / * is not arrogant * / # define max_nodes 8 / * maximum number of nodes * /

Struct State / * Node Structure Variable * / {INT Predecessor; Int Length; Enum {Permanent = 0, Tentative} Label;} State [MAX_NODES];

INT n = max_nodes;

/ * Node configuration diagram, 0 means not adjacent * / int Dist [max_nodes] = {0, 2, 0, 0, 0, 0, 0}, {2, 0, 7, 0, 2, 0, 7, 0, 3, 0, 3, 0, 0}, {0, 0, 3, 0, 0, 0, 0, 8}, {0, 2, 0, 0, 0, 2, 1, 0}, {0, 0, 3, 0, 2, 0, 0, 2}, {6, 0, 0, 0, 1, 0, 0, 8}, { 0, 0, 0, 2, 0, 2, 4, 0}};

Void dijkstra_ra (int S, int T, int path []); / * core algorithm function, S end node, Path shortest path * / void outputdata (); / * Output function * /

INT Main (int Argc, char * argv []) {int path [max_nodes]; INT i; int S, t; for (i = 0; i max_nodes-1 || t <0 || t> max_nodes-1 || s == t) {Printf (" The shytest path is not available / n "); system (" pause "); return 0;} Dijkstra_RA (S, T, PATH); Printf ("THE Shortest Path IS"); for (i = 0; i = 0) {Printf ("% D", PATH [I]);}}}} printf ("/ n"); system ("pause"); return 0;}

Void dijkstra_ra (int S, int T, int path []) {INT I, K, Min; Struct State * p; for (p = state; p PREDECESSOR = -1; P-> length = infinity;} state [t] .length = 0; state [t] .Label = permanent; k = t; do {for (i = 0; i = 0);

转载请注明原文地址:https://www.9cbs.com/read-33174.html

New Post(0)