Here, we use an adjacent table to implement depth-first traversal and recursion.
# Include
Using namespace std; # define VERTEXNUM 5 // Number of knots struct edgenode {int to; int weight; // edgenode * next;}; struct vnode {int from; edgenode * first;}; void createGraph (vnode * adjilist, int start, int end, int weight); void displayGraph (vnode * adjilist, int nodeNum ); void DFT (vnode * adjilist, int * vertexStatusArr, int nodeNum); void DFTcore (vnode * adjilist, int I, int * vertexStatusArr); int main (void) {// create a vnode adjilist [VERTEXNUM]; int nodeNum = VERTEXNUM; for (int I = 0; I
To = end; p-> weight = weight; p-> next = adjilist [start]. first; adjilist [start]. first = p;} // print the stored figure void displayGraph (vnode * adjilist, int nodeNum) {int I, j; edgenode * p; for (I = 0; I
Next ;}}// the void DFT (vnode * adjilist, int * vertexStatusArr, int nodeNum) {printf ("start BFT graph: \ n"); int I; for (I = 0; I
Next) {DFTcore (adjilist, p-> to, vertexStatusArr );}}
The time complexity is O (V + E ).