Graph depth-first traversal algorithm-Nirvana huofeng-blog Garden
Depth-first Traversal Algorithm
Correct the mistakes in the previous blog: the previous blog post has the suspicion of non-undirected graphs and not having to traverse the breadth first. In fact, the breadth-first search can also be used in Directed Graphs, because an undirected graph is a special directed graph. Here, the breadth-first traversal is used to traverse all nodes of a connected graph. For a directed graph, if one directed edge exists, then the two nodes are connected, so traversal can be wound on this edge.
Depth-first traversal, that is, traversing the nodes of a graph as deep as possible, also uses three-color markers and recursive functions. Here there is also a timestamp-stamped operation. When a node is found, the time is recorded. When a node is completed, the time is recorded. Based on the time stamp, many properties and applications can be found.
In fact, in many places, the breadth-first search and depth-first searchCodeAll functions have the following features: extended search uses a queue to organize loops, while extended search uses recursive functions to achieve "depth" first.
The first application of deep traversal is topological sorting:
Correct the mistakes in the previous blog: the previous blog post has the suspicion of non-undirected graphs and not having to traverse the breadth first. In fact, the breadth-first search can also be used in Directed Graphs, because an undirected graph is a special directed graph. Here, the breadth-first traversal is used to traverse all nodes of a connected graph. For a directed graph, if one directed edge exists, then the two nodes are connected, so traversal can be wound on this edge.
Depth-first traversal, that is, traversing the nodes of a graph as deep as possible, also uses three-color markers and recursive functions. Here there is also a timestamp-stamped operation. When a node is found, the time is recorded. When a node is completed, the time is recorded. Based on the time stamp, many properties and applications can be found.
In fact, in many places, the breadth-first search and depth-first search code are characteristic: the breadth-first search uses a queue to organize loops, deep priority search uses recursive functions to achieve "depth" first.
The first application of deep traversal is topological sorting: