2) DFS
The depth-first search always searches for the starting edge of the recently discovered node v until all the departure edges of that node are discovered
Once all the starting edges of node v are found, the search is traced back to the V's precursor node.
Implementation Details: Timestamp
Each node has a discovery time and a completion time.
The precursor of a DFS rear image forms a depth-first forest
1#include <iostream>2 using namespacestd;3 #defineNIL-14 intg[ +][ +];5 intN;6 structNode7 {8 intcolor;9 intD;Ten intF; One intPi; A}t[ +]; - intTime ; - the voidDFS (); - voidDfs_visit (intu); - - voidDFS () + { - for(intI=0; i<n;i++) + { AT[i].color =0; atT[i].pi =NIL; - } -Time=1; - for(intI=0; i<n;i++) - { - if(T[i].color = =0) in dfs_visit (i); - } to } + voidDfs_visit (intu) - { theT[U].D = time++; *T[u].color =1;/*****/ $ for(intv=0; v<n;v++)Panax Notoginseng { - if((g[u][v] = =1) && (T[v].color = =0)) the { +T[v].pi =u; A Dfs_visit (v); the } + } -T[U].F = time++; $ } $ /* - int main () - { the While (cin>>n) - {Wuyi for (int i=0;i<n;i++) the { - for (int j=0;j<n;j++) Wu { - cin>>g[i][j]; About } $ } - DFS (); - for (int i=0;i<n;i++) - cout<<i<< "<<t[i].d<<" "<<t[i].f<<" "<<endl; A } + return 0; the } - */
Graph algorithm (a)--basic graph algorithm (BFS,DFS and its application) (2)