Brief Introduction to DFS and dfs

Source: Internet
Author: User

Brief Introduction to DFS and dfs

The so-called DFS is a search that does not hit the south wall and does not look back. The time complexity is O (V + E ).

The entire path from the start point to the end point can be calculated. During Algorithm Execution, a visit [vi] array is required to maintain access to each node, so that repeated access is avoided. Note that when there are multiple paths from the same start point to the same end point, you must reset the visit [vi] status for each Recursive Backtracking. In addition, vector can be used to store nodes that pass through each process. Two vector arrays of the same type can be directly compared and directly assigned values, so DFS can simply find the optimal path.

Another difficulty in DFS recursion is to end the conditional setting of recursive start backtracking. Here, we often add the restrictions on the subject on the basis of reaching the end point. One trick is to first create an if (begin = end & question-qualified condition) {the logic to be processed after the end is reached} if (begin = end) {return ;} the so-called separation of logical processing and return to avoid confusion (because sometimes the execution logic is required and sometimes the execution logic is not needed ). This is the case in PAT's "traveling around the world": The first time you find a path that can reach the destination, you can directly return it without having to make any comparisons, but the second, third ,... You must perform logical comparison to determine the number of stations passing through each path and the number of transfer sites. Here, the number of stations is very simple. Each Recursive Execution adds one of the stations. Note that to maintain a parameter in the dfs function, you must add & (reference ).

Next we will talk about a method to improve efficiency in DFS: pruning. Taking PAT "Team food chain" as an example, the answer requirements of this question must be in the {a1 a2... aN} in the lexicographically less than {b1 b2... bN}. if and only if an integer K (1 <= K <= N) exists, it must satisfy the following conditions: aK <bK and for any positive integer I, ai = bi smaller than K. Therefore, node 1 must be the first element in the answer set, and the last element of the answer set must be connected to the first element in the graph represented by the adjacent table. Therefore, before each recursion, we can check whether the next vertex has not been accessed to reach Node 1. If not, we will not perform subsequent recursion.

To sum up, dfs is the most basic traversal policy in graph theory. It is suitable for finding the number of paths. You can use this method for non-hierarchical traversal. However, sometimes you have to give up this method due to time complexity constraints.

 

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.