The strategy used by the depth-first search is as implicit in its name: As far as possible, "in-depth" in the diagram.
The steps for depth-first search are as follows:
1. After accessing any of the nodes in the graph, access any of its adjacency nodes W1
2. From W1, Access node W2 (down-depth) adjacent to W1 but not yet visited
3. Repeat procedure 2 (if the node you are currently accessing cannot "drill down", fall back to the previous node and access its other adjacency node) until all nodes have been accessed.
Depth-First search algorithm
Like the breadth-first search algorithm, the depth-first search algorithm also paints nodes in the search process to indicate the state of the nodes.
Unlike breadth-first search, the precursor graph of breadth-first search forms a tree, and the precursor of depth-first search is a depth-priority forest made up of multi-lesson depth-priority trees.
Pseudo code
1 DFS (G)2 forEach vertex u∈white3X.color= White4u.π=NIL5Time=06 forEach vertex u∈g.v7 ifu.color== White8dfs-VISIT (g,u)9 Ten Onedfs-VISIT (g,u) Atime=time+1 -U.d= Time -U.color=GRAY the forEach V∈g.adj[u] - ifV.color= White -v.π=u -dfs-VISIT (g,v) +U.color=black
Where V.D records the time the node V was first discovered (painted in gray)
V.F records the time that the search completed the neighboring list scan of V (painted black time)
The following is a process by which the depth-first search algorithm runs on a graph
Depth-first search for graphs