#include <stdio.h> #include <stdlib.h> #include <string.h>typedef char vertextype[4];typedef Char infoptr;typedef int vrtype; #define MAXSIZE 50//maximum vertex number typedef enum{dg,dn,ug,un}graphkind;//EDGE node type definition typedef struct Arcnode{int the position of the vertex to which the adjvex;//arc points infoptr *info;//the weight of the arc struct Arcnode *nextarc;//indicates the next vertex adjacent to the vertex}arcnode;// The type of the head node defines the typedef struct VNODE{VERTEXTYPE data;//for storing vertices arcnode *firstarc;//indicates the first vertex to be adjacent to the vertex}vnode,adjlist[maxsize];// The type of the graph defines the TypeDef struct{adjlist vertex;//head node int vexnum,arcnum;//The number of vertices of the graph and the number of arcs Graphkind kind;//diagram type}adjgraph;//in Figure g, All vertices of the distance vertex V0 shortest path is k void Bsflevel (Adjgraph g,int v0,int k) {int visited[maxsize];//a vertex access flag array, 0 means no access, 1 means that int queue[has been accessed maxsize][2];//Queue Queue[][0] stores the ordinal of a vertex, queue[][1] stores the path length of the current vertex distance v0 int front=0,rear=-1,v,i,level,yes=0; Arcnode *p;for (i=0;i<g.vexnum;i++)//Initialize flag array visited[i]=0;rear= (rear+1)%maxsize;//vertex v0 into queue queue[rear][0]=v0; queue[rear][1]=1;visited[v0]=1;//access array flag set to 1level=1;//sets the current level do{v=queue[front][0];//take out the queue vertex level=queue[front][1]; Front= (front+1)%maxsizE;p=g.vertex[v].firstarc;//p points to the first adjacency point of V while (P!=null) {if (visited[p->adjvex]==0) {//If the adjacency point is not accessed if (level==k) {// If the nearest path to the adjacency point is k for the distance v0, its output if (yes==0) printf (the vertex with the shortest path from%s to%2d is:%s), G.vertex[v0].data,k,g.vertex[p->adjvex].data ); elseprintf (",%s", g.vertex[p->adjvex].data); Yes=1;} The visited[p->adjvex]=1;//access flag is set to 1rear= (rear+1)%maxsize;//and the vertex is enqueued queue[rear][0]=p->adjvex;queue[rear][1]= level+1;} p=p->nextarc;//If the current vertex is already accessed, p moves to the next adjacency point}}while (front!=rear&&level<k+1);p rintf ("\ n");} Returns the position of the vertex in the graph int Locatevertex (adjgraph G,vertextype v) {int i;for (i=0;i<g.vexnum;i++) if (strcmp (G.vertex[i].data), V) ==0) return i;return-1;} The adjacency table storage structure is used to create the nvoid graph Creategraph (adjgraph *g) {int i,j,k,w; Vertextype v1,v2;/* defines two vertices v1 and v2*/arcnode *p;printf ("Enter the number of vertices of the graph, the number of sides (separated by commas):"), scanf ("%d,%d",& (*g) .vexnum,& (*g ). Arcnum);p rintf ("Enter values for%d vertices:", g->vexnum), for (i=0;i<g->vexnum;i++)/* To store vertices in the header node */{scanf ("%s",g-> Vertex[i].data); g->vertex[i].firstarc=null;/* The associated vertex to an empty */}printf ("Please enter two vertices of the edge (separated by a space): \ n"); for (k=0;k<g->arcnum;k++)/* Create Edge List */{scanf ("%s%s", V1,v2); I=locatevertex (*G,V1); J=locatevertex (*G,V2);/* J creates adjacency table for edge I for Out Edge */p= (arcnode*) malloc (sizeof (Arcnode));p->adjvex=j;p->info= (infoptr*) malloc (sizeof (INFOPTR)) /* Insert the node P points into the side table */p->nextarc=g->vertex[i].firstarc; G->vertex[i].firstarc=p;/*i create adjacency table for Edge J for Out side */p= (arcnode*) malloc (sizeof (Arcnode));p->adjvex=i;p->info=null ;p->nextarc=g->vertex[j].firstarc; G->vertex[j].firstarc=p;} (*g). Kind=ug;} Destroy the Gvoid destroygraph (adjgraph *g) {int i; Arcnode *p,*q;for (i=0;i<g->vexnum;++i)/* Release Benzi node in diagram */{p=g->vertex[i].firstarc;/*p point to the first node of the edge table */if (P!=NULL) /* If the edge table is not empty, release the node of the Edge table */{q=p->nextarc;free (p);p =q;}} (*g). vexnum=0;/* the vertex count to 0*/(*g). arcnum=0;/* the number of edges is set to the output void 0*/}//(Displaygraph g) of the adjacency table of the adjgraph graph G {int i; Arcnode *p;printf ("There are%d vertices in the graph:", g.vexnum), for (i=0;i<g.vexnum;i++) printf ("%s", G.vertex[i].data);p rintf ("\ n %d edges: \ n ", 2*g.arcnum), for (i=0;i<g.vexnum;i++) {P=g.vertex[i].firstarc;while (p) {printf (" (%s,%s) ", G.vertex[i]. Data,g. vertex[p->adjvex].data);p =p->nextarc;} printf ("\ n");}} void Main () {adjgraph G; Creategraph (&G);/* The adjacency table storage structure is used to create the diagram G*/displaygraph (G);/* Output No Direction graph g*/bsflevel (g,0,2);/* Figure G Distance vertex v0 Shortest path to 2 vertex */ Destroygraph (&G);/* Destroy Diagram G*/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: In Figure G, find the distance vertex v0 all vertices with the shortest path K