Implementation of undirected graph depth-first traversal in C Language
Implementation of depth-first traversal for undirected graphsAdjacent tableRepresent an undirected graph: the adjacent matrix and the adjacent table.
The sample image used by the program is as follows:
Implementation points:
Each node has three states: 1, 0, and 1, indicating no, detected, and processed.
The Code is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> # Include # Include # Include "graph_represent.h" // post-order traversal graph void DFS (struct vNode ** adj, int v, int * color) {struct vNode * w; color [v] = 0; printf ("% d", v);/** process nodes in the forward order here **/w = adj [v]; while (w! = NULL) {if (color [w-> value] =-1) {DFS (adj, w-> value, color);} w = w-> next ;} /** processing nodes in the descending order **/color [v] = 1;} // parameter: number of nodes, number of nodes, Start Node, void dfs_wraper (struct vNode ** adj, int n, int s) {int * color = (int *) malloc (sizeof (int) * n); int I; for (I = 0; I
Here, we start from 2, and the result is: 2 0 1 3 5 4 6.