Graph depth-first traversal algorithm-Nirvana huofeng-blog garden graph depth-first Traversal Algorithm

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.