Using adjacency matrix to create a forward net N
Analysis: The creation of the graph is mainly using the input vertices, and stored in a vector (one-dimensional array), and then by entering two vertices and weights to create arcs, using a two-dimensional array representation, therefore, the use of adjacency matrix to create a graph storage requires two arrays: a one-dimensional array and a two-dimensional array.
#include <stdio.h> #include <string.h> #include <malloc.h> #include <stdlib.h>typedef Char vertextype[4];typedef char infoptr;typedef int vrtype; #define INFINITY 10000//defines an infinitely large value # define MAXSIZE 100// Maximum number of vertices typedef enum {dg,dn,ug,un}graphkind;//diagram type: Forward graph, forward net, non-direction graph, and non-direction network typedef struct{vrtype adj;//for unauthorized graphs, 1 for adjacent, 0 for nonadjacent For weighted graphs, storage weights infoptr *info;//and arcs or edges related information}arcnode,adjmatrix[maxsize][maxsize];typedef struct//diagram type definition {vertextype vex[ The maxsize];//is used to store vertex adjmatrix arc;//adjacency matrices, to store the information of an edge or arc int vexnum,arcnum;//the number of vertices and the number of edges Graphkind kind;//diagram type}mgraph;int Locatevertex (mgraph N,vertextype v)//Find the vertex in the vertex vector v, find the ordinal returned in the vector, otherwise return -1{int i;for (i=0;i<n.vexnum; ++i) if (strcmp ( N.vex [I],v] ==0) return i;return-1; void Creategraph (Mgraph *n)//using adjacency matrix notation to create a mesh n{int I,j,k,w,infoflag,len;char s[maxsize]; Vertextype v1,v2;printf ("Please enter the number of vertices with the net N, number of arcs, arc information (yes: 1, no: 0):"), scanf ("%d,%d,%d",& (*n). Vexnum,& (*n). Arcnum, &infoflag);p rintf ("Please enter a value of%d vertices (<%d characters): \ n", N->vexnum, MAXSIZE); for (i=0;i<n->vexnum;i++)//Create an array, ForSave each vertex of the net scanf ("%s", N->vex [i]), for (i=0;i<n->vexnum; i++)//Initialize adjacency matrix 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}printf ("Enter the arc-tail-arc-head weights for%d arcs (with spaces as intervals): \ n", n->arcnum); for (k=0;k<n-> Arcnum; k++) {scanf ("%s%s%d", v1,v2,&w);//input two vertices and arcs of weights i=locatevertex (*N,V1); J=locatevertex (*N,V2); N->arc [I][j].adj =w;if (Infoflag)//If Arc contains additional information {printf ("Please enter information about arcs:"), gets (s); Len=strlen (s); if (len) {N->arc [i][ J].info = (char*) malloc ((len+1) *sizeof (char));//To strcpy (N->arc [I][j].info, s);}}} The type of the N->kind =dn;//diagram is a mesh} void Destroygraph (Mgraph *n)//Destroy net {int i,j;for (i=0;i<n->vexnum; i++) for (j=0;j<n- >vexnum; j + +) if (N->arc [I][j].adj!=infinity)//If there is an arc if (N->arc [I][j].info!=null)//If the arc has relevant information, the space occupied by this information is freed { Free (N->arc [i][j].info); N->arc [I][j].info =null;} N->vexnum =0;//The number of vertices of the net to 0n->arcnum =0;//the number of arcs in the net is set to 0}void displaygraph (mgraph N)//output adjacency matrix storage represents the diagram N{int i,j;printf (" There are%d vertex%d arcs with a forward net, the vertices are in order: ", N.vexnum, N.arcnum); for (i=0;i&Lt N.vexnum; ++i) printf ("%s", N.vex [i]);//The vertex of the output network printf ("\ n: \ n");//output Net N arc printf ("Ordinal i="); for (i=0;i<n.vexnum; i + +) printf ("%8d", i);p rintf ("\ n"), for (i=0;i<n.vexnum; i++) {printf ("%8d", I), and for (J=0;j<n.vexnum; j + +) printf ("% 8d ", N.arc [I][j].adj);p rintf (" \ n ");}} void Creategraph (Mgraph *n); int Locatevertex (mgraph n,vertextype v); void Destroygraph (Mgraph *n); void Displaygraph ( Mgraph N); void Main () {Mgraph n;printf ("Create a net: \ n"); Creategraph (&n);p rintf ("Vertex and arc of output network: \ n");D isplaygraph (N);p rintf ("Destroy Net: \ n");D estroygraph (&n);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Creating diagrams with adjacency matrices