Similarities and differences between "algorithm" backtracking method and depth-first traversal

Source: Internet
Author: User

1, the same point:

Backtracking is also in the implementation of depth-first, that is, step-by-step exploration, and not like the breadth first, from near a piece of land sweep.

2, different points

(1) Access order

Depth-First traversal:

The purpose is "traversal", which is essentially unordered. This means that the order of access is not important, and it is important to have been visited.

Refer to the title surrounded regions, the depth first only need to get the "O" from the boundary to all access.

Therefore, in the implementation, it is sufficient for each location record to be visited.

Backtracking method:

The aim is to "solve the process", the essence is orderly. In other words, each step must be a required order.

You can refer to Word Search, which requires a deep first exploration in order of requirements, and must be met at every step.

Therefore, in the implementation, visited records cannot be used, because different sequential accesses of the same content will result in different results, rather than simply being "accessed".

To use Access state to record, that is, for each point record has visited the direction of the neighbor, after backtracking back to the new direction to access the neighbor.

It is not important to have been visited before this point, and it is important not to access it in the current order.

(2) Number of visits

Depth-First traversal: a node that has been visited is no longer accessible, and all points are accessed only once.

Backtracking: Points that have been visited may be accessed again, or there may be points that have not been visited.

Similarities and differences between "algorithm" backtracking method and depth-first traversal

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.