The implementation of the depth-first traversal of the non-graph, and the adjacency table for the non-graph represents the representation of the graph: adjacency Matrix and adjacency table.
The example diagram used by the program is:
Key points of implementation:
Each node has three status -1,0,1, which indicates that no discovery has been found and has been processed.
The code is as follows:
#include <stdio.h>#include <stdlib.h>#include "graph_represent.h"//post-traversal diagramvoidDFS (structvnode** adj,intVint* color) {structvnode* W; COLOR[V] =0;printf("%d", v);/** Here the pre-Order Processing node **/W = adj[v]; while(w! = NULL) {if(color[w->value]==-1) {DFS (adj,w->value,color); } w = w->next; }/** here post-processing node **/COLOR[V] =1;}//Parameters: Adjacency table, number of nodes, start node,voidDfs_wraper (structvnode** adj,intNints) {int* color = (int*)malloc(sizeof(int) *n);intI for(i=0; i<n;i++) {Color[i] =-1;//Set all vertices to the uncovered state. } DFS (Adj,s,color);/* If the diagram is connected and not connected, call the code below for the for (i=0;i<n;i++) DFS (Adj,i,color); */ printf("\ n");}voidMain () {//Get default diagram, total 7 nodes intn =7;structvnode** adjvertics = Default_wraper ();printf("\ndeepth First search:\n"); Dfs_wraper (Adjvertics,n,2);//Traversal starting from 2}
This is a depth traversal from 2, with the result: 2 0 1 3 5 4 6
Non-graph depth first traversal C language implementation