Reprinted from http://blog.csdn.net/jnu_simba/article/details/8867687
algorithm: Graph depth First Search (Depth)Category: Data structure and algorithm 2013-04-30 09:19 1723 people read comments (2) Favorites Search by traversal depth of the report map
The traversal of the graph is similar to the traversal of the tree, and we want to go through the rest of the graph from one vertex in the graph, and make each vertex accessible only once, a process called graph traversal (Traverse graph).
There are generally two kinds of traversal methods of graphs, the first one is depth first search, also known as depth-first searching, referred to as DFS (Depth). The second is "breadth first traversal" (breadth first search), also known as the breadth of priority searching, referred to as BFS. We have described in more detail the strategy of depth-first search in stack and depth first search, which is no longer discussed here. We can also take a diagram as a maze, set a starting point, walk through the vertices of all the graphs and mark them until all the vertices are accessed. As shown in Figure 7-5-2, it should be noted that the node I was accessed from the time of a->h,h-> backtracking->d,d->i, and that we recursively entered the path from a to the following, so that it would go back to the origin of "a" until the end of the traversal.
The following only gives the algorithm code for depth-first traversal of graphs when adjacency matrix and adjacency table are stored. Do not give the overall test run of the code, in fact, only need to write a function to create a diagram can be the overall test, you can refer to the "Adjacency matrix Creation Diagram" and "adjacency Table creation diagram"
First, if we are using the adjacency matrix, the code is as follows: (adapted from the "Liar Data Structure")
C + + Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
|
typedef char Vertextype; /* Vertex type should be defined by user/* typedef int EDGETYPE; /* Side of the weight value type should be defined by the user * * #define MAXSIZE 9/* Storage space Initial allocation * * #define Maxedge 15 #define Maxvex 9 typedef struct { Vertextype Vexs[maxvex]; /* Vertex Table * * Edgetype Arc[maxvex][maxvex]; /* adjacency matrix, can be considered as a side table * * int numvertexes, numedges; /* The current number of vertices and the number of edges in the diagram * * } mgraph; BOOL Visited[maxvex]; /* An array of access flags * * Depth-first recursive algorithm for/* adjacency Matrix * * void DFS (mgraph MG, int i) { Int J; Visited[i] = true; cout << mg.vexs[i] << '; /* Print vertices, can also be other operations * * for (j = 0; J < Mg.numvertexes; J + +) if (mg.arc[i][j] = = 1 &&!visited[j]) DFS (MG, J); /* pairs for the access of the adjacent vertex recursive call * * } /* adjacency Matrix DEPTH Traversal operation * * void Dfstraverse (Mgraph MG) { int i; for (i = 0; i < mg.numvertexes; i++) Visited[i] = false; /* Initial all vertex states are not visited state * * for (i = 0; i < mg.numvertexes; i++) if (!visited[i]) DFS (MG, i); /* Call Dfs on the unreachable vertex, if connected graph, only execute once/ } |
The traversal result is: A B C D E F G H I (diagram structure shown above)
If we use the way of adjacency table, the code is as follows: (adapted from the "Liar Data Structure")
C + + Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
|
/* adjacency Table Structure ****************** * * typedef struct EDGENODE * Side Table node * * { int Adjvex; /* Adjacent point field, storing the corresponding subscript of the vertex * * * int weight; /* Used to store weights, for non-network maps can not be required * * struct Edgenode *next; /* Link field, point to next adjacent point * * } Edgenode; typedef struct VERTEXNODE/* Vertex table node * * { int in; Knot Point Entry Degree char data; /* Vertex domain, storage vertex information * * Edgenode *firstedge; /* Side Table head pointer * * } Vertexnode, Adjlist[maxvex]; typedef struct { Adjlist adjlist; int numvertexes, numedges; /* The current number of vertices and number of edges in the diagram * * } graphadjlist, *graphadjlist; BOOL Visited[maxvex]; /* An array of access flags * * Depth-first recursive algorithm for/* adjacency table * * void DFS (graphadjlist GL, int i) { Edgenode *p; Visited[i] = true; cout << gl->adjlist[i].data << '; /* Print vertices, can also be other operations * * p = gl->adjlist[i].firstedge; while (p) { if (!visited[p->adjvex]) DFS (GL, P->adjvex); /* pairs for the access of the adjacent vertex recursive call * * p = p->next; } } /* adjacency Table DEPTH Traversal operation * * void Dfstraverse (Graphadjlist GL) { int i; for (i = 0; i < gl->numvertexes; i++) Visited[i] = false; /* Initial all vertex states are not visited state * * for (i = 0; i < gl->numvertexes; i++) if (!visited[i]) DFS (GL, i); /* Call Dfs on the unreachable vertex, if connected graph, only execute once/ } |
The traversal result is: A F G H E D I C B (diagram structure shown above)
The
can be seen from the results, because we use different storage methods, and even if we use the same depth-first search, the results of the traversal are different.