#include <stdio.h> #include <stdlib.h> #include <string.h>typedef char vertextype[4];typedef Char infoptr;typedef int vrtype; #define INFINITY 100000//defines an infinitely large value # define MaxSize 50//maximum vertex number typedef int PATHMATRIX[MAXSIZE] [MaxSize] [maxsize];//defines a two-dimensional array that saves the shortest path typedef int SHORTPATHLENGTH[MAXSIZE][MAXSIZE];//defines an array typedef that holds the shortest distance from vertex v0 to vertex v ENUM{DG , Dn,ug,un}graphkind;typedef Struct{vrtype adj;//for the right graph, with 1 for adjacent, 0 for non-adjacent, and for the weighted graph, the storage weights infoptr *info;//and arc or edge related information}arcnode, The type definition of the adjmatrix[maxsize][maxsize];//diagram typedef struct{vertextype vex[maxsize];//is used to store the vertex adjmatrix arc;//adjacency matrix, Information for storing edges or arcs int vexnum,arcnum;//The number of vertices and edges (arcs) Graphkind the type of kind;//diagram}mgraph;//add a storage network of rows, columns, and weights for the type definition typedef struct{int row; int col;int weight;} gnode;//using adjacency matrix notation to create a mesh nvoid creategraph (mgraph *n,gnode *value,int vnum,int arcnum,vertextype *ch) {int I,j,k,w;char s[ MaxSize]; Vertextype V1,v2; n->vexnum=vnum; N->arcnum=arcnum;for (i=0;i<vnum;i++) strcpy (N->vex[i],ch[i]);//Initialize adjacency matrix for (i=0;i<n->vexnum;i++) for (j=0;j<n->vexnum;j++) {n->arc[i][j].adj=infinity; n->arc[i][j].info=null;//Arc information is initialized to null}for (k=0;k<arcnum;k++) {i=value[k].row;j=value[k].col; N->arc[i][j].adj=value[k].weight;} The type of the n->kind=dn;//graph is the}//output adjacency matrix storage representation of the graph Nvoid displaygraph (mgraph N) {int i,j;printf ("The forward mesh has%d vertex%d arcs, the vertices are sequentially:", N.vexnum,n.arcnum), for (i=0;i<n.vexnum;++i)//The vertex of the output network */printf ("%s", N.vex[i]);p rintf ("\ n" \ n: \ n ");/* Output NET N arc */ printf ("Ordinal i="), for (i=0;i<n.vexnum;i++) printf ("%11d", i);p rintf ("\ n"), for (i=0;i<n.vexnum;i++) {printf ("%-6d ", i); for (j=0;j<n.vexnum;j++) printf ("%-11d ", N.arc[i][j].adj);p rintf (" \ n ");}} The Floyd algorithm is used to find the shortest path between the vertices V and W of the network N, where path[v][w][u] means that u is the vertex void Floyd (mgraph n,pathmatrix path) from V to W currently seeking the shortest path. Shortpathlength dist) {int u,v,w,i;for (v=0;v<n.vexnum;v++)//Initialize array path and distfor (w=0;w<n.vexnum;w++) {dist[v][w ]=n.arc[v][w].adj;//Initial, vertex v to vertex W's shortest path to V to W ARC's weight for (u=0;u<n.vexnum;u++) path[v][w][u]=0;//path matrix is initialized to 0 if (dist[v][w ]<infinity) {//if V to W has a path, the path from V to W passes through V and W two points path[v][w][v]=1;path[v][w][w]=1;}} for (u=0;u<n.vexnum;u++) For (v=0;v<n.vexnum;v++) for (w=0;w<n.vexnum;w++) if (dist[v][u]<infinity&&dist[u][w]<infinity && (Dist[v][u]+dist[u][w]<dist[v][w]) {//a path from V through U to W is the shortest path currently dist[v][w]=dist[v][u]+dist[u][w];// Update V to W Shortest path for (i=0;i<n.vexnum;i++)//The path from V to W passes through all paths from V to u and from u to W path[v][w][i]=path[v][u][i]| | Path[u][w][i];}} void Main () {int w,u,v,vnum=3,arcnum=4; Mgraph N; Gnode value[]={{0,1,5},{1,0,10},{1,2,6},{2,0,9}}; Vertextype ch[]={"V0", "V1", "V2"}; Pathmatrix path;/* uses a two-dimensional array to store the shortest path of the vertex */shortpathlength dist;/* with a one-dimensional array to hold the shortest path length */creategraph (&n,value,vnum,arcnum, CH); /* Create a n*/for (v=0;v<n.vexnum;v++) n.arc[v][v].adj=0;/* Freud algorithm requires a diagonal element value of 0, since two points are the same, their distance is 0 */displaygraph (N);/* Output has a forward net N */floyd (n,path,dist);p rintf (Shortest path length matrix dist:\n between vertices); for (u=0;u<n.vexnum;u++) {for (v=0;v<n.vexnum;v++) printf ("%6d", Dist[u][v]);p rintf ("\ n");} for (u=0;u<n.vexnum;u++) for (v=0;v<n.vexnum;v++) if (u!=v) printf ("The shortest distance from%s to%s is%d\n", N.vex[u],n.vex[v],dist[u] [v]); printf ("The vertex of the shortest path between vertices: \ n"); for (u=0;u<n.vexnum;u++) for (v=0;v<n.vexnum;v++) if (u!=v) {printf ("from%s to%s by:", N.vex[u],n.vex[v]); for (w=0;w<n.vexnum;w++) if (path[u][v][w]==1 printf ("%s", N.vex[w]);p rintf ("\ n");} System ("Pause");}
Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint |copyright©2011-2015,supernatural, all rights Reserved.
_datastructure_c_impl:floyd algorithm to find the shortest path between the vertices V and W of the network n