FIG depth traversal 8648
Time limit: 1000MS Memory Limit: 1000K
Questions: Programming language title: Unlimited
Description
FIG implemented adjacent table storage structure and the basic operating functions. Depth traversal algorithm based on this graph and tested.Input
The first line: an integer from 0 to 3 (digraph: 0, there are the network: the network 2, No:: 1, undirected FIG. 3); second line: the number of vertices and edges inputs; a third line: the input value of each vertex (string, length <3); (a vertex traversal starts from the first input) of the fourth row: the input of each arc (edge) and arc arc tail head (as a space intervals), If the network is also input weights;
Output
The output of the depth traverse FIG.
Sample Input
0 3 3 a b c a b b c c b
Sample Output
a b c
// The following is the code
#include "string.h" #include "malloc.h" / * the malloc (), etc. * / #include "stdio.h" / * the EOF (or the Z = ^ F6), NULL * / #include "stdlib.h" / * Exit () * / typedef int the InfoType; / * type of vertex weights * / #define MAX_NAME. 3 / * maximum string length + 1 vertices * / typedef char VertexType [MAX_NAME]; / * string type * / / * adjacency graph showing table storage * / #define MAX_VERTEX_NUM 20 is typedef enum {DG, the DN, AG, the AN GraphKind}; / * {directed graph, directed net, undirected graphs, undirected network *} / typedef struct ArcNode { int adjvex; / * position of the vertex of the arc points to * / struct ArcNode * nextarc; / * point to the next arc pointer * / the InfoType * info; / * weight pointers network) * / } ArcNode; / * tABLE node * / typedef struct { VertexType Data; / * vertex information * / ArcNode * firstarc; / * table address of the first node, the first attachment point to the arc of the vertex pointer * / VertexType VA, VB; } VNode, AdjList [MAX_VERTEX_NUM]; / * first node * / typedef struct { AdjList the vertices; int vexnum, arcnum; / * current number of vertices and the arcs number graph * / int kind; / * type flag map * / } ALGraph; int LocateVex (ALGraph G, VertexType U) {/ * initial conditions: graph G exists, u and G vertex has the same characteristics * / / * result: If there is a vertex U G, then returns the vertex in the drawings position; * otherwise -1 / int I; for (I = 0; I <G.vexnum; I ++) IF (strcmp (U, G.vertices [I] .data) == 0) return I; return -1; } void CreateGraph (ALGraph * G) {/ * use of the adjacent table storage structure configured to FIG no information G (a configuration with four kinds of FIG function) * / int I, J, K; int W; / * weight * / // the printf ( "The type of the Enter Map: (~ 0. 3):"); Scanf ( "% D", & (G *) .kind); ArcNode *p; //printf("Enter Vertex number,Arc number: "); scanf("%d%d",&(*G).vexnum,&(*G).arcnum); //printf("Enter %d Vertex :\n",(*G).vexnum); for(i=0;i<(*G).vexnum;++i) /* 构造顶点向量 */ { scanf("%s",(*G).vertices[i].data); (*G).vertices[i].firstarc=NULL; } //if((*G).kind==1||(*G).kind==3) /* 网 */ // printf("Enter order every arc weight,head and tail (Takes the gap by the blank space ):\n"); //else /* 图 */ // printf("Enter order every arc head and tail (Takes the gap by the blank space ):\n"); for(k=0;k<(*G).arcnum;++k) /* 构造表结点链表 */ { if((*G).kind==1||(*G).kind == 3) / * net * / scanf("%d%s%s",&w,va,vb); Scanf ( "% S% S", VA, VB); else / * FIG * / I = LocateVex (* G, VA); / * Arc End * / J = LocateVex (* G, VB); / * arc header * / = P (* ArcNode) the malloc (the sizeof (ArcNode)); p-> J = adjvex; IF ((G *). 1 || .kind == (* G) == .kind. 3) / * network * / { p-> info = (int *) the malloc (the sizeof (int)); * (p-> info) = W; } the else p-> info = NULL; / * FIG * / p-> nextarc = (G *) .vertices [i] .firstarc; / * is inserted in the header * / (G *) .vertices [I] = P .firstarc; IF ((G *) .kind> = 2) / * undirected network or, generating a second node table * / { P = (* ArcNode) the malloc (the sizeof (ArcNode)); p-> adjvex = I; IF ((G *) == .kind. 3) / * * undirected network / { p-> info = (int *) the malloc (the sizeof (int)); * (p-> info) = W; } the else p-> info = NULL; / * undirected graph * / p-> nextarc = (G *) .vertices [J] .firstarc; / * * is inserted in the header / (G *) .vertices [J] = P .firstarc; } } } VertexType * GetVex (ALGraph G, V int) {/ * initial conditions: the presence of G, v G is the number of a vertex. Result: Returns the value of v * / IF (v> = G.vexnum || v <0) Exit (0); return & G.vertices [v] .data; } int FirstAdjVex (ALGraph G, VertexType v) {/ * initial conditions: the presence of G, v is a vertex in G * / / * result: a first number of adjacent vertices v returned. If the vertex is not adjacent to the vertex in G -1 * / return ArcNode P *; int V1; V1 = LocateVex (G, v); / * number V1 as a vertex v in the graph G * / P = G.vertices [V1] .firstarc; IF (P) p-return> adjvex; the else return -1; } int NextAdjVex (ALGraph G, VertexType V, W VertexType) {/ * initial conditions: the presence of G, v is a vertex in G, w is adjacent to vertex v * / / * Result: return v (with respect to w a) a number of vertices adjacent to the next. * / / * If w is the last neighbor of v, then -1 * / return ArcNode P *; int V1, W1; V1 = LocateVex (G, v); / * number V1 as a vertex v in the graph G * / W1 = LocateVex (G, w); / * W1 is the vertex w in G number of * / p = G.vertices [V1] .firstarc; the while (! && p-p> adjvex = W1) / * pointer p It is not empty and is not referred to in the table node * w / P = p-> nextarc; IF / w or w * is not found adjacent to the last point * / (p-P ||> nextarc!!) return -1; the else / * p-> adjvex w == * / return p-> nextarc-> adjvex; / * return v (relative to w) of the next adjacent vertex number * / } / * depth traversal * / int visited [MAX_VERTEX_NUM ]; / * access flag array (overall volume), flag 0 is not accessed, access tag 1 * / void (* VisitFunc) (char * v); / * function variable (overall volume) * / void the DFS (ALGraph G, int v) {/ * recursive depth-first traversal starting from FIG vertex v G. Algorithm 7.5 * / / * set the access flag is TRUE (visited) * / / * access the v-th vertex * / / * of v has not been visited neighbors w * recursive call the DFS / int W; visited [V] =. 1; VisitFunc (G.vertices [V] .data); for (W = FirstAdjVex (G, G.vertices [V] .data); W> = 0; NextAdjVex = W (G, G.vertices [V] .data, G.vertices [W] .data)) { IF (visited [W]) the DFS (G, W);! } } void DFSTraverse (ALGraph G, void (* Visit) (char *)) {/ * graph G for depth-first traversal. Algorithm 7.4 * / / * global variables VisitFunc, so that DFS is not necessary provided the function pointer parameters * / / * access flag array initialization * / / * vertex call DFS not yet visited * / int V; VisitFunc = Visit; for (V = 0; V <G.vexnum; ++ V) visited [V] = 0; for (V = 0; V <G.vexnum; ++ V) IF (visited [V]) the DFS (G, V);! the printf ( "\ n-"); } void Print (char * I) { printf("%s ",i); } int main() { ALGraph g; CreateGraph(&g); DFSTraverse(g,print); return 1; }
Reproduced in: https: //www.cnblogs.com/arcfat/archive/2012/12/11/2813704.html