Graph Traversal:Definition: A vertex that accesses the remaining vertices in the graph along the edge and makes each vertex accessible only once.
There are usually two kinds of traversal order schemes:?Depth-first traversal (DFS)---similar to a pre-order traversal?Breadth-First traversal (BFS)---similar to sequence traversal
? Depth-First traversal (DFS)
Algorithm idea Description: Access the starting vertex v when v also has adjacency vertex not accessed: (Starting condition) ?Depth traversal of inaccessible adjacency vertex w when all adjacency vertices of v are accessed: ?If all vertices in the graph have been accessed, the algorithm ends (termination condition) ?If there are still non-visited vertices in the graph, traverse with an unreachable vertex as the starting vertex depth
? Breadth-First traversal (BFS)
The algorithm thought describes: 1. Access to the vertex v02. Access V0 Each adjacency point in turn v01,v02 ... 3. Assuming that the last visited vertex is vi1,vi2 ..., then visit vi1,vi2 ... The unreachable adjacency point 4. Repeat 3 until all vertices are accessed
In the adjacency linked listimplementing Depth-first traversal (DFS) and breadth-first traversal (BFS):
static void Dfs_recursive (tl_graph* graph, int v, int *visited, graph_printf* p_func) {int i = 0;p_func (graph->v[v]); Vis ITED[V] = 1;printf (","); for (i=0; I<linklist_length (Graph->la[v]); i++) {t_list_node* node = (t_list_node*) linklist_get (Graph->la[v], i), if (!visited[node->v]) {dfs_recursive ( Graph, Node->v, visited, P_func);}}} void Dfs_traverse (lgraph* graph, int v, graph_printf* p_func) {tl_graph* t_graph = (tl_graph*) graph;int *visited = NULL;int OK = 1;ok = (OK) && (t_graph! = null) && (P_func! = null); ok = (ok) && (0 <= v) && (v < T_graph->count); */* Reference the way it applies to array space */ok = (OK) && ((visited = (int*) calloc (t_graph->count, sizeof (int))) ! = NULL), if (OK) {int i = 0;dfs_recursive (T_graph, V, visited, P_func); for (i=0; i<t_graph->count; i++) {if (!visited[ I]) {dfs_recursive (t_graph, I, visited, P_func);}} printf ("\ n");} Free (visited);} static void Bfs_recursive (tl_graph* graph, int v, int visited[], graph_printf* p_func) {Linkqueue* queue = Linkqueue_create (), if (Queue! = NULL) {int i = 0;t_list_node* node = null;visited[v] = 1;/* will be placed in the queue with the vertices labeled V in the figure, and VK can be Can be 0, and when the queue is implemented, the parameters of the incoming queue cannot be zero, plus the base address of Graph->v (Graph->v is just a useless address), thus avoiding the error of V 0 o'clock. The Remove queue element is supposed to be subtracted from the base address */linkqueue_append (queue, graph->v + V); while (Linkqueue_length (queue) > 0) {/* passed in the label addresses, Outgoing nature is also the label address */v = (l_vertex**) linkqueue_retrieve (queue)-Graph->v;p_func (Graph->v[v]);p rintf (","); for (i=0; I<linklist_length (Graph->la[v]); i++) {node = (t_list_node*) linklist_get (Graph->la[v], i), if (!visited[node->v]) {linkqueue_append (queue, graph- >v + node->v); visited[node->v] = 1;}} }}linkqueue_destroy (queue);} void Bfs_traverse (lgraph* graph, int v, graph_printf* p_func) {tl_graph* t_graph = (tl_graph*) graph;int *visited = NULL;int OK = 1;ok = (OK) && (t_graph! = null) && (P_func! = null); ok = (ok) && (0 <= v) && (v < T_graph->count); */* Reference the way it applies to array space */ok = (OK) && ((visited = (int*) calloc (T_graph->count, Sizeof (int))) = NULL), if (OK) {int i = 0;bfs_recursive (T_graph, V, visited, P_func); for (i=0; i<t_graph->count; i++) { if (!visited[i]) {bfs_recursive (t_graph, I, visited, P_func);}} printf ("\ n");} Free (visited);}
in the adjacency matrixImplementing Depth-first traversal (DFS) versus breadth-first traversal (BFS):
/* Parameter Description: Graph: Pointer to the head node V: The vertex is labeled under the entire vertex array visited: Mark Access vertex p_func: Print node information function */static void dfs_recursive (tm_graph* graph, int V, int *visited, graph_printf* p_func) {int i = 0;p_func (Graph->v[v]); Visited[v] = 1;printf (","); for (i=0; i<graph->count; i++) {if ((graph->matrix[v][i]! = 0) && (!visited[i])) {dfs_recursive (graph, I, visited, P_func);}}} void Dfs_traverse (mgraph* graph, int v, graph_printf* p_func) {tm_graph* t_graph = (tm_graph*) graph;int *visited = NULL;int OK = 1;ok = (OK) && (t_graph! = null) && (P_func! = null); ok = (ok) && (0 <= v) && (v < T_graph->count); */* Reference the way it applies to array space */ok = (OK) && ((visited = (int*) calloc (t_graph->count, sizeof (int))) ! = NULL), if (OK) {int i = 0;dfs_recursive (T_graph, V, visited, P_func); for (i=0; i<t_graph->count; i++) {if (!visited[ I]) {dfs_recursive (t_graph, I, visited, P_func);}} printf ("\ n");} Free (visited);} static void Bfs_recursive (tm_graph* graph, int v, int visited[], graph_printf* p_Func) {linkqueue* queue = linkqueue_create (); if (queue! = NULL) {int i = 0;visited[v] = 1; Linkqueue_append (queue, Graph->v + V), while (Linkqueue_length (queue) > 0) {v = (m_vertex**) Linkqueue_retrieve ( Queue)-Graph->v;p_func (Graph->v[v]);p rintf (","); for (i=0; i<graph->count; i++) {if ((graph->matrix[v][i) && (!visited[i])) {linkqueue_append (queue, graph->v + i); visited[i] = 1;}}} }linkqueue_destroy (queue);} void Bfs_traverse (mgraph* graph, int v, graph_printf* p_func) {tm_graph* t_graph = (tm_graph*) graph;int *visited = NULL;int OK = 1;ok = (OK) && (t_graph! = null) && (P_func! = null); ok = (ok) && (0 <= v) && (v < T_graph->count); */* Reference the way it applies to array space */ok = (OK) && ((visited = (int*) calloc (t_graph->count, sizeof (int))) ! = NULL), if (OK) {int i = 0;bfs_recursive (T_graph, V, visited, P_func); for (i=0; i<t_graph->count; i++) {if (!visited[ I]) {bfs_recursive (t_graph, I, visited, P_func);}} printf ("\ n");} Free (visited);}
Note:1, for the operation of the hierarchical traversal, all need to use the queue to achieve
2, the use of the queue when the shortcomings (because of the shortcomings of the implementation)When implementing a breadth-first traversal, the value of the incoming queue element cannot be 0, so be aware that the underlying value of the incoming graph cannot be zero. You can add a certain number on the basis of the underlying value, then subtract the previously added value.
The traversal of graphs