Network Shortest Path Dijkstra algorithm analysis
Recently I have been studying algorithms and have seen such an algorithm. I decided to extract it as my learning notes:
/** File: shortest. c * Description: the Dijkstra Algorithm * Shortest Path Dijkstra Algorithm * Created: 2001/11/25 * Author: Justin Hou [mailto: [email protected] */# include
# Define true 1 # define false 0 # define I 9999/* infinity */# define N 20/* Number of city vertices */int cost [N] [N] = {{ 0, 3, i, 1, I, I}, {3, 0, 5, I, 6, I, I}, {I, 5, 0, 4, I, 1, I, I}, {I, i, 4,0, 2, I, 6, I, I}, {I, i, I, 2, 0, I, 7, I, I}, {1, i, 0, 1, I, 2, I, I}, {I, 6, I, 1, 0, 6, I, 7, I, I}, {I, I, 1, I, 6, 0, 2, I, 3, I }, {I, 6, I, 2, 0, 8, I, 4, I }, {I, I, 7, I, 8, 0, I, 5, I }, {I, I, 2, I, 0, 4, I, I, 3, I }, {I, I, 7, I, 4, 0, 3, I, 4, I, I }, {I, I, 3, I, 3, I, 5, I, I }, {I, I, 4, I, 3, 0, 7, I, 2, I }, {I, I, 5, I, I, 7, 0, I, 3 }, {I, I }, {I, I, I, 4, I, 5, 0, 8, I, I}, {I, i, 5, I, 8, 0, 6, I}, {I, i, 2, I, 6, 0, 4}, {I, i, 3, I, 4, 0}; int dist [N]; /* store the current shortest path length */int v0 = 'a'-65;/* The initial point is A */void main () {int final [N], I, v, w, min;/* initialize shortest path length data. All data is not final data */for (v = 0; v <N; v ++) {final [v] = false; dist [v] = cost [v0] [v];}/* The distance between v0 and v0 is the shortest first, final data */final [v0] = true ;/* Find another N-1 node */for (I = 0; I <N-1; I ++) {min = I; /* The initial shortest length is infinite * // * find the shortest edge */for (w = 0; w <N; w ++) {if (! Final [w] & dist [w] <min) {min = dist [w]; v = w ;}} final [v] = true; /* Add new edge */for (w = 0; w <N; w ++) {/* update dist [] Data */if (! Final [w] & dist [v] + cost [v] [w] <dist [w]) {dist [w] = dist [v] + cost [v] [w] ;}}for (I = 0; I <N; I ++) {/* display to monitor */printf ("% c-> % c: % 2d \ t", v0 + 65, I + 65, dist [I]);}
Running result: