Data structure (implemented in C) ------- depth-first traversal of graphs, data structure -------
[This article is my own learning notes. You are welcome to repost it, but please note the Source: http://blog.csdn.net/jesson20121020]
Algorithm Description:
Assume that the initial State of a given graph G is that all vertices have never been accessed. If you select a vertex vi in G as the initial starting point, the depth-first traversal can be defined as follows: first, access the starting point vi, mark it as accessed. Then, traverse every adjacent vj of vi from vi in sequence. If vj has never accessed, starting from vj, we will continue to perform depth-first traversal until all vertices having the same paths with vi are accessed. Therefore, if G is a connected graph, the traversal process starting from the initial starting point ends, which means that the traversal of graph G is completed.
Algorithm Implementation:
Using the adjacent matrix and the adjacent table as the graph storage structure respectively, a recursive algorithm for in-depth preference traversal of connected graphs is presented. The algorithm is described as follows:
(1) access start point vi and mark it as accessed.
(2) traverse every adjacent vj of the vi. If the vj has never been accessed, continue the in-depth traversal with the vj as the new starting point.
Complete code:
The source code of the Deep-Priority Search Algorithm Using the adjacent matrix is as follows:
/*** Deep traversal graph **/void DFS_MG (MGraph MG, int I) {visit [I] = 1; printf ("% c \ t", MG. vexs [I]); int j; for (j = 1; j <= MG. vexnum; j ++) {if (visit [j] = 0 & MG. arcs [I] [j] = 1) DFS_MG (MG, j );}}
The source code of the Deep-priority search algorithm is as follows:
/***** Deep traversal graph **/void DFS_AG (ALGraph AG, int I) {ArcPtr p; printf ("% c \ t", AG. vertices [I]. vexdata); visit [I] = 1; p = AG. vertices [I]. firstarc; while (p! = NULL) {if (visit [p-> adjvex] = 0) DFS_AG (AG, p-> adjvex); p = p-> nextarc ;}}
Algorithm Description:
For a connected graph with n vertices and e edges, The DFS_MG and DFS_AG algorithms call n times. Except that the initial call is from the outside, the n-1 call is a recursive call from inside DFS_MG and DFS_AG. When using the adjacent matrix, O (n) is required to traverse all adjacent points of a vertex) time, O (n ^ 2) is required to traverse the entire graph, that is, the time complexity of DFS_MG is O (n ^ 2 ).
When using an adjacent table, all neighboring points of n vertices are scanned for edge table nodes. Therefore, the time complexity of the DFS_AG algorithm is O (n + e ).
The access flag is used when the depth-first traversal algorithm is used. Therefore, the space complexity of this algorithm is O (n ).
The complete code for implementing the deep Priority Search Algorithm Using the adjacent matrix is as follows:
/* ===================================================== ============================================================ Name: graph. c Author: jesson20121020 Version: 1.0 Description: create Graph using Adjacency Matrix, ansi-style ========================================== ======================================================= * /# include <stdio. h> # include <stdlib. h> # define MAX_VEX_NUM 50 typedef char VertexType; typedef enum {DG, UDG} GraphType; typedef str Uct {VertexType vexs [MAX_VEX_NUM]; int arcs [MAX_VEX_NUM] [MAX_VEX_NUM]; int vexnum, arcnum; GraphType type;} MGraph; // set the vertex access flag int visit [MAX_VEX_NUM] in the graph;/*** obtain the subscript of the specified vertex in the vertex set based on the name * vex vertex * return if it is found, the subscript is returned, otherwise, 0 */int getIndexOfVexs (char vex, MGraph * MG) {int I; for (I = 1; I <= MG-> vexnum; I ++) is returned) {if (MG-> vexs [I] = vex) {return I;} return 0;}/*** create an adjacent matrix */void create_MG (MGraph * MG) {int I, j, k; int v 1, v2, type; char c1, c2; printf ("Please input graph type DG (0) or UDG (1):"); scanf ("% d ", & type); if (type = 0) MG-> type = DG; else if (type = 1) MG-> type = UDG; else {printf ("Please input correct graph type DG (0) or UDG (1 )! "); Return;} printf (" Please input vexmun: "); scanf (" % d ", & MG-> vexnum); printf (" Please input arcnum :"); scanf ("% d", & MG-> arcnum); getchar (); for (I = 1; I <= MG-> vexnum; I ++) {printf ("Please input % dth vex (char):", I); scanf ("% c", & MG-> vexs [I]); getchar ();} // initialize the adjacent matrix for (I = 1; I <= MG-> vexnum; I ++) {for (j = 1; j <= MG-> vexnum; j ++) {MG-> arcs [I] [j] = 0 ;}/// enter the edge information and establish the adjacent matrix for (k = 1; k <= MG-> arcnum; k ++) {printf ("Please input % dth arc v1 (char) v2 (char):", k ); scanf ("% c", & c1, & c2); v1 = getIndexOfVexs (c1, MG); v2 = getIndexOfVexs (c2, MG ); if (MG-> type = 1) MG-> arcs [v1] [v2] = MG-> arcs [v2] [v1] = 1; elseMG-> arcs [v1] [v2] = 1; getchar () ;}/ *** print the adjacent matrix and vertex information */void print_MG (MGraph MG) {int I, j; if (MG. type = DG) {printf ("Graph type: Direct graph \ n");} else {printf ("Graph type: Undirect graph \ n ");} printf ("Graph vertex number: % d \ n", MG. vexnum); printf ("Graph arc number: % d \ n", MG. arcnum); printf ("Vertex set: \ n"); for (I = 1; I <= MG. vexnum; I ++) printf ("% c \ t", MG. vexs [I]); printf ("\ nAdjacency Matrix: \ n"); for (I = 1; I <= MG. vexnum; I ++) {j = 1; for (; j <MG. vexnum; j ++) {printf ("% d \ t", MG. arcs [I] [j]);} printf ("% d \ n", MG. arcs [I] [j]) ;}/ *** initialize the vertex access flag **/void init_Visit () {int I; for (I = 0; I <MAX_VEX_NUM; I ++) visit [I] = 0;}/*** depth traversal graph **/void DFS_MG (MGraph MG, int I) {visit [I] = 1; printf ("% c \ t", MG. vexs [I]); int j; for (j = 1; j <= MG. vexnum; j ++) {if (visit [j] = 0 & MG. arcs [I] [j] = 1) DFS_MG (MG, j) ;}/ *** main function */int main (void) {MGraph MG; create_MG (& MG); print_MG (MG); printf ("The result of DFS: \ n"); DFS_MG (MG, 1); return EXIT_SUCCESS ;}
The complete code for implementing the deep Priority Search Algorithm in the adjacent table is as follows:
/* ===================================================== ============================================================ Name: ALGraph. c Author: Maid Version: 1.0 Copyright: Your copyright notice Description: Graph using linkList, ansi-style ========================================== ======================================================= * /# include <stdio. h> # include <stdlib. h> # include <stdio. h> # define MAX_VERTEX_NUM 50 typedef enum {DG, UDG} Gra PhType; typedef char VertexType; // table node typedef struct ArcNode {int adjvex; // adjacent node int weight; // Edge weight struct ArcNode * nextarc; // next node pointer} ArcNode, * ArcPtr; // header node typedef struct {VertexType vexdata; int id; ArcPtr firstarc;} VNode; // header node array typedef struct {VNode vertices [MAX_VERTEX_NUM]; int vexnum, arcnum; GraphType type;} ALGraph; int visit [MAX_VERTEX_NUM]; /*** obtain the subscript In the vertex Array Based on the vertex character */int getIndexOfVexs (char vex, AL Graph * AG) {int I; for (I = 1; I <= AG-> vexnum; I ++) {if (AG-> vertices [I]. vexdata = vex) {return I;} return 0;}/*** create an adjacent table */void create_AG (ALGraph * AG) {ArcPtr p, q; int I, j, k, type; VertexType v1, v2; printf ("Please input graph type UG (0) or UDG (1):"); scanf ("% d ", & type); if (type = 0) AG-> type = DG; else if (type = 1) AG-> type = UDG; else {printf ("Please input correct graph type UG (0) or UDG (1 )! "); Return;} printf (" please input vexnum: "); scanf (" % d ", & AG-> vexnum); printf (" please input arcnum :"); scanf ("% d", & AG-> arcnum); getchar (); for (I = 1; I <= AG-> vexnum; I ++) {printf ("please input the % dth vex (char):", I); scanf ("% c", & AG-> vertices [I]. vexdata); getchar (); AG-> vertices [I]. firstarc = NULL;} for (k = 1; k <= AG-> arcnum; k ++) {printf ("please input the % dth arc v1 (char) v2 (char): ", k); scanf (" % c % C ", & v1, & v2); I = getIndexOfVexs (v1, AG); j = getIndexOfVexs (v2, AG ); // create an adjacent table based on the graph type // method 1, insert it to the linked list Header/* if (AG-> type = DG) {// Directed Graph p = (ArcPtr) malloc (sizeof (ArcNode); p-> adjvex = j; p-> nextarc = AG-> vertices [I]. firstarc; AG-> vertices [I]. firstarc = p;} else {// undirected graph p = (ArcPtr) malloc (sizeof (ArcNode); p-> adjvex = j; p-> nextarc = AG-> vertices [I]. firstarc; AG-> vertices [I]. firstarc = p; p = (ArcPtr) malloc (sizeof (ArcNode); p-> adjvex = I; p-> nextarc = AG-> vertices [j]. firstarc; AG-> vertices [j]. firstarc = p;} * // method 2, inserted to the end of the linked list if (AG-> type = DG) {// Directed Graph p = (ArcPtr) malloc (sizeof (ArcNode); p-> adjvex = j; // The table is empty if (AG-> vertices [I]. firstarc = NULL) {AG-> vertices [I]. firstarc = p;} else {// find the last table node q = AG-> vertices [I]. firstarc; while (q-> nextarc! = NULL) {q = q-> nextarc;} q-> nextarc = p;} p-> nextarc = NULL;} else {// undirected graph p = (ArcPtr) malloc (sizeof (ArcNode); p-> adjvex = j; // The table is empty if (AG-> vertices [I]. firstarc = NULL) {AG-> vertices [I]. firstarc = p;} else {// find the last table node q = AG-> vertices [I]. firstarc; while (q-> nextarc! = NULL) {q = q-> nextarc;} q-> nextarc = p;} p-> nextarc = NULL; p = (ArcPtr) malloc (sizeof (ArcNode )); p-> adjvex = I; // The table is empty. if (AG-> vertices [j]. firstarc = NULL) {AG-> vertices [j]. firstarc = p;} else {// find the last table node q = AG-> vertices [j]. firstarc; while (q-> nextarc! = NULL) {q = q-> nextarc;} q-> nextarc = p;} p-> nextarc = NULL;} getchar ();}} /*** output Graph Information */void print_AG (ALGraph AG) {ArcPtr p; int I; if (AG. type = DG) {printf ("Graph type: Direct graph \ n");} else {printf ("Graph type: Undirect graph \ n ");} printf ("Graph vertex number: % d \ n", AG. vexnum); printf ("Graph arc number: % d \ n", AG. arcnum); printf ("Vertex set: \ n"); for (I = 1; I <= AG. vexnum; I ++) printf ("% c \ t", AG. Vertices [I]. vexdata); printf ("\ nAdjacency List: \ n"); for (I = 1; I <= AG. vexnum; I ++) {printf ("% d", I); p = AG. vertices [I]. firstarc; while (p! = NULL) {printf ("--> % d", p-> adjvex); p = p-> nextarc;} printf ("\ n ");}} /*** initialize the vertex access flag **/void init_Visit () {int I; for (I = 0; I <MAX_VERTEX_NUM; I ++) visit [I] = 0;}/*** depth traversal graph **/void DFS_AG (ALGraph AG, int I) {ArcPtr p; printf ("% c \ t ", AG. vertices [I]. vexdata); visit [I] = 1; p = AG. vertices [I]. firstarc; while (p! = NULL) {if (visit [p-> adjvex] = 0) DFS_AG (AG, p-> adjvex); p = p-> nextarc ;}} int main (void) {ALGraph AG; create_AG (& AG); print_AG (AG); printf ("The result of DFS: \ n"); DFS_AG (AG, 1 ); return EXIT_SUCCESS ;}