Fig Dijkstra Algorithm

Source: Internet
Author: User

The C language is implemented as follows: (using the adjacent matrix storage) # include <stdio. h> # include <malloc. h> # define VERTEXNUM 6 // edge element that stores the shortest path, typedef struct edge {int vertex; int value; struct edge * next;} st_edge; void createGraph (int (* edge) [VERTEXNUM], int start, int end, int value); void displayGraph (int (* edge) [VERTEXNUM]); void displayPath (st_edge ** path, int startVertex, int * shortestPath); void dijkstra (int (* edge) [VERTEXNUM], st_edge *** path, int ** ShortestPath, int startVertex, int * vertexStatusArr); int getDistance (int value, int startVertex, int start, int * shortestPath); void createPath (st_edge ** path, int startVertex, int start, int end, int edgeValue); int main (void) {// dynamically create a two-dimensional array int (* edge) for storing edges [VERTEXNUM] = (int (*) [VERTEXNUM]) malloc (sizeof (int) * VERTEXNUM); int I, j; for (I = 0; I <VERTEXNUM; I ++) {for (j = 0; j <VERTEXNUM; j ++) {edge [I] [J] = 0 ;}// stores the vertices traversal status, 0: not traversed, 1: traversed int * vertexStatusArr = (int *) malloc (sizeof (int) * VERTEXNUM); for (I = 0; I <VERTEXNUM; I ++) {vertexStatusArr [I] = 0;} printf ("after init: \ n "); displayGraph (edge); // create a graph createGraph (edge, 6); createGraph (edge, 5); createGraph (edge, 1); createGraph (edge, 1, 2, 5); createGraph (edge, 3); createGraph (edge, 2, 4, 6); createGraph (edge, 2, 3, 5); createGraph (edge, 2, 5, 4); c ReateGraph (edge, 3, 5, 2); createGraph (edge, 4, 5, 6); printf ("after create: \ n"); displayGraph (edge ); // Shortest path/* The storage structure is as follows: path [0]: edge0-> NULLpath [1]: edge1-> NULLpath [2]: edge1-> edge2-> NULLpath [3]: edge1-> edge2-> edge3-> NULLpath [4]: edge4-> NULL: the shortest path from vertex 0 to zero: from 0 to 0 the shortest path from vertex 0 to 1: from 0 to 1 the shortest path from vertex 0 to 2: from 0 to 1, starting from 1 to 2 ...... */st_edge ** path = NULL; // weight of the shortestPath stored/* shortestPath [0] = 0; shortestPath [1] = 8; shortestPath [2] = 12; from vertex 0 The path to 0 is 0. The path from vertex 0 to 1 is 8. The path from vertex 0 to 2 is 12 */int * shortestPath = NULL; // start from vertex 0 to find the shortest path int startVertex = 0; // shortestPath dijkstra (edge, & path, & shortestPath, startVertex, vertexStatusArr); printf ("the path is: \ n "); displayPath (path, startVertex, shortestPath); free (edge); free (path); return 0 ;}// create a diagram void createGraph (int (* edge) [VERTEXNUM], int start, int end, int value) {edge [start] [end] = value; edge [end] [start] = value;} // Print the stored graph void displayGraph (int (* edge) [VERTEXNUM]) {int I, j; for (I = 0; I <VERTEXNUM; I ++) {for (j = 0; j <VERTEXNUM; j ++) {printf ("% d", edge [I] [j]);} printf ("\ n") ;}// print the Shortest path void displayPath (st_edge ** path, int startVertex, int * shortestPath) {int I; st_edge * p; for (I = 0; I <VERTEXNUM; I ++) {printf ("Path from % d to % d:", startVertex, I ); p = * (path + I); while (p! = NULL) {printf ("% d (% d)", p-> vertex, p-> value); p = p-> next ;} printf ("\ n"); printf ("the count is: % d \ n", shortestPath [I]);} // shortestPath void dijkstra (int (* edge) [VERTEXNUM], st_edge *** path, int *** shortestPath, int startVertex, int * vertexStatusArr) {// initialize the shortest path * path = (st_edge **) malloc (sizeof (st_edge *) * VERTEXNUM); int I, j; for (I = 0; I <VERTEXNUM; I ++) {if (I = startVertex) {st_edge * e = (st_edge *) malloc (sizeof (st _ Edge); e-> vertex = startVertex; e-> value = 0; e-> next = NULL; (* path) [I] = e ;} else {(* path) [I] = NULL ;}// initialize the shortestPath value * shortestPath = (int *) malloc (sizeof (int) * VERTEXNUM ); for (I = 0; I <VERTEXNUM; I ++) {if (I = startVertex) {(* shortestPath) [I] = 0;} else {(* shortestPath) [I] =-1 ;}/// starting from vertex 0, vertex 0 is the accessed vertexStatusArr [startVertex] = 1; int shortest, distance, start, end, edgeValue, vNum = 1; // If the vertex has not been accessed, while (vNu M <VERTEXNUM) {shortest = 9999; for (I = 0; I <VERTEXNUM; I ++) {// select the accessed vertex if (vertexStatusArr [I] = 1) {for (j = 0; j <VERTEXNUM; j ++) {// select an unaccessed vertex if (vertexStatusArr [j] = 0) {// select an edge with the smallest value if (edge [I] [j]! = 0 & (distance = getDistance (edge [I] [j], startVertex, I, * shortestPath) <shortest) {shortest = distance; edgeValue = edge [I] [j]; <SPAN style = "WHITE-SPACE: pre"> </SPAN> start = I; end = j ;}}}}} vNum ++; // set the vertex to vertexStatusArr [end] = 1; // Save the shortestPath (* shortestPath) [end] = shortest; // Save the Shortest path createPath (* path, startVertex, start, end, edgeValue) ;}// returns the distance from startVertex to the new vertex int getDistance (int valu E, int startVertex, int start, int * shortestPath) {if (start = startVertex) {return value;} else {return shortestPath [start] + value ;}} // Save the Shortest path void createPath (st_edge ** path, int startVertex, int start, int end, int edgeValue) {if (start = startVertex) {st_edge * newEdge = (st_edge *) malloc (sizeof (st_edge); newEdge-> vertex = end; newEdge-> value = edgeValue; newEdge-> next = NULL; st_edge ** p = path + end; whil E (* p )! = NULL) {p = & (* p)-> next);} * p = newEdge;} else {st_edge ** pCopySrc = path + start; st_edge ** pCopyDes = path + end; st_edge * newEdge = NULL; while (* pCopySrc )! = NULL) {newEdge = (st_edge *) malloc (sizeof (st_edge); * newEdge = ** pCopySrc; newEdge-> next = NULL; * pCopyDes = newEdge; pCopySrc = & (* pCopySrc)-> next); pCopyDes = & (* pCopyDes)-> next);} newEdge = (st_edge *) malloc (sizeof (st_edge); newEdge-> vertex = end; newEdge-> value = edgeValue; newEdge-> next = NULL; * pCopyDes = newEdge ;}}
# Include <stdio. h> # include <malloc. h> # define VERTEXNUM 6 // elements in the vertex's adjacent table // The edge element typedef struct edge {int vertex; int value; struct edge * next;} st_edge; void createGraph (st_edge ** edge, int start, int end, int value); void displayGraph (st_edge ** edge); void delGraph (st_edge ** edge ); void dijkstra (st_edge ** edge, st_edge *** path, int ** shortestPath, int startVertex, int * vertexStatusArr); void displayPath (St_edge ** path, int startVertex, int * shortestPath); int getDistance (int value, int startVertex, int start, int * shortestPath); void createPath (st_edge ** path, int startVertex, int start, int end, int edgeValue); int main (void) {// dynamically create a pointer array st_edge ** edge = (st_edge **) for storing Edges **) malloc (sizeof (st_edge *) * VERTEXNUM); int I; for (I = 0; I <VERTEXNUM; I ++) {edge [I] = NULL ;} // store the vertex traversal status. values 0: not traversed; 1: vertexStatusArr = (int *) Malloc (sizeof (int) * VERTEXNUM); for (I = 0; I <VERTEXNUM; I ++) {vertexStatusArr [I] = 0;} printf ("after init: \ n "); displayGraph (edge); // create a graph createGraph (edge, 6); createGraph (edge, 5); createGraph (edge, 1); createGraph (edge, 5); createGraph (edge, 3); createGraph (edge, 6); createGraph (edge, 5 ); createGraph (edge, 2, 5, 4); createGraph (edge, 3, 5, 2); createGraph (edge, 4, 5, 6); printf ("after create: \ n"); displa YGraph (edge); // Shortest path/* The storage structure is as follows: path [0]: edge0-> NULLpath [1]: edge1-> NULLpath [2]: edge1-> edge2-> NULLpath [3]: edge1-> edge2-> edge3-> NULLpath [4]: edge4-> NULL: the shortest path from vertex 0 to zero: from 0 to 0 the shortest path from vertex 0 to 1: from 0 to 1 the shortest path from vertex 0 to 2: from 0 to 1, starting from 1 to 2 ...... */st_edge ** path = NULL; // weight of the shortestPath stored/* shortestPath [0] = 0; shortestPath [1] = 8; shortestPath [2] = 12; the path from vertex 0 to 0 is the path from vertex 0 to 1. The path from vertex 0 to 2 is 12 */int * shortestPath = NULL; int startVertex = 0; // The Shortest Path Path dijkstra (edge, & path, & shortestPath, startVertex, vertexStatusArr); printf ("the path is: \ n"); displayPath (path, startVertex, shortestPath ); delGraph (edge); edge = NULL; delGraph (path); path = NULL; if (vertexStatusArr! = NULL) {free (vertexStatusArr); vertexStatusArr = NULL;} if (shortestPath! = NULL) {free (shortestPath); shortestPath = NULL;} return 0;} // create a diagram void createGraph (st_edge ** edge, int start, int end, int value) {st_edge * newedge1 = (st_edge *) malloc (sizeof (st_edge); newedge1-> vertex = end; newedge1-> value = value; newedge1-> next = NULL; st_edge ** edge1 = edge + start; while (* edge1! = NULL) {edge1 = & (* edge1)-> next);} * edge1 = newedge1; st_edge * newedge2 = (st_edge *) malloc (sizeof (st_edge )); newedge2-> vertex = start; newedge2-> value = value; newedge2-> next = NULL; st_edge ** edge2 = edge + end; while (* edge2! = NULL) {edge2 = & (* edge2)-> next);} * edge2 = newedge2;} // print the stored figure void displayGraph (st_edge ** edge) {int I; st_edge * p; for (I = 0; I <VERTEXNUM; I ++) {printf ("% d:", I ); p = * (edge + I); while (p! = NULL) {printf ("% d (% d)", p-> vertex, p-> value); p = p-> next ;} printf ("\ n") ;}// print the Shortest path void displayPath (st_edge ** path, int startVertex, int * shortestPath) {int I; st_edge * p; for (I = 0; I <VERTEXNUM; I ++) {printf ("Path from % d to % d:", startVertex, I ); p = * (path + I); while (p! = NULL) {printf ("% d (% d)", p-> vertex, p-> value); p = p-> next ;} printf ("\ n"); printf ("the count is: % d \ n", shortestPath [I]);} // release the memory occupied by the neighboring table void delGraph (st_edge ** edge) {int I; st_edge * p; st_edge * del; for (I = 0; I <VERTEXNUM; I ++) {p = * (edge + I); while (p! = NULL) {del = p; p = p-> next; free (del);} edge [I] = NULL;} free (edge );} // dijkstra calculates the Shortest path void dijkstra (st_edge ** edge, st_edge *** path, int ** shortestPath, int startVertex, int * vertexStatusArr) {// initialize the shortest path * path = (st_edge **) malloc (sizeof (st_edge *) * VERTEXNUM); int I, j; for (I = 0; I <VERTEXNUM; I ++) {if (I = startVertex) {st_edge * e = (st_edge *) malloc (sizeof (st_edge); e-> vertex = startVertex; e-> value = 0; e-> Next = NULL; (* path) [I] = e;} else {(* path) [I] = NULL ;}} // initialize the shortestPath = (int *) malloc (sizeof (int) * VERTEXNUM); for (I = 0; I <VERTEXNUM; I ++) {if (I = startVertex) {(* shortestPath) [I] = 0;} else {(* shortestPath) [I] =-1 ;}} vertexStatusArr [startVertex] = 1; st_edge * p; int shortest, distance, edgeValue, start, end, vNum = 1; // If the while (vNum <VERTEXNUM) {shortest = 9999; for (I = 0; I <VERTEXNUM; I ++ ){/ /Select the accessed vertex if (vertexStatusArr [I] = 1) {for (j = 0; j <VERTEXNUM; j ++) {// select an unaccessed vertex if (vertexStatusArr [j] = 0) {p = * (edge + I); while (p! = NULL) {// if the distance from startVertex to j is less than shortestif (distance = getDistance (p-> value, startVertex, I, * shortestPath )) <shortest & p-> vertex = j) {shortest = distance; edgeValue = p-> value; start = I; end = j ;} p = p-> next ;}}}} vNum ++; vertexStatusArr [end] = 1; // Save the shortestPath weight (* shortestPath) [end] = shortest; // Save the shortest path createPath (* path, startVertex, start, end, edgeValue );}} // returns the distance from startVertex to the new vertex int getDistance (int val Ue, int startVertex, int start, int * shortestPath) {if (start = startVertex) {return value;} else {return value + shortestPath [start];} // Save the Shortest path void createPath (st_edge ** path, int startVertex, int start, int end, int edgeValue) {if (start = startVertex) {st_edge * newEdge = (st_edge *) malloc (sizeof (st_edge); newEdge-> vertex = end; newEdge-> value = edgeValue; newEdge-> next = NULL; st_edge ** p = path + end; whi Le (* p )! = NULL) {p = & (* p)-> next);} * p = newEdge;} else {st_edge ** pCopySrc = path + start; st_edge ** pCopyDes = path + end; st_edge * newEdge = NULL; while (* pCopySrc )! = NULL) {newEdge = (st_edge *) malloc (sizeof (st_edge); * newEdge = ** pCopySrc; newEdge-> next = NULL; * pCopyDes = newEdge; pCopySrc = & (* pCopySrc)-> next); pCopyDes = & (* pCopyDes)-> next);} newEdge = (st_edge *) malloc (sizeof (st_edge); newEdge-> vertex = end; newEdge-> value = edgeValue; newEdge-> next = NULL; * pCopyDes = newEdge ;}}

 

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.