Back Tracking Search Algorithms

My real question is: “Why doesn't it speed up my search?” But I'm not sure if this makes sense without additional context ...

This question is really just academic - the code "works" and my program finds the solutions that I expect .... but I want to make sure that I understand the terminology. To illustrate this, let me use a concrete example where we need a search algorithm - the n-Queens problem.

n-queens problem - Placing n queens on an n × n chessboard so that no queen can attack another.

One solution

Example Solution

There are many code examples on the Internet that can be found searching for “N-queens backtracking,” and the Wikipedia article on refund even uses N-Queens in explaining what happens in the opposite direction ( http://en.wikipedia.org/ wiki / Backtracking ). The idea, as I understand it, is that if the board configuration is incorrect - let them say two places of the queens that can attack each other, the algorithm ignores all the configurations of the boards that will be made by adding additional parts.

I also implemented a (non-recursive / non-reverse) third-first and fifth version of my search. As expected, both options check the exact number of states. I expected that a depth-based recursive algorithm with a backtracking algorithm should test fewer states. But I don’t see it.

Depth First Found 92 solutions in 10.04 seconds Tested 118969 nodes (1.2k nodes per second) Largest Memory Set was 64 nodes BackTracking Found 92 solutions in 9.89 seconds Tested 118969 nodes (1.2k nodes per second) Largest Memory Set was 170 nodes Breadth First Found 92 solutions in 12.52 seconds Tested 118969 nodes (0.95k nodes per second) Largest Memory Set was 49415 nodes 

My actual implementation will be general in nature, so I don't use mirrors / rotate boards or anything else smart.

It seems to me that I should be a misunderstanding, but I don’t see what benefit rolls back from me?

Wikipedia explains that as soon as a certain state is considered invalid, its subtree is skipped (trimmed), but rational allocation of queens (except Q1 in a8 and Q2 in a7) seems to prevent any situations that can be cut?

In what configurations of the board should the first attempt to introduce the first breath be implemented to avoid refusal to return?

+4
source share
3 answers

Backtracking is one of several ways to avoid looking for the wrong paths. Heuristics, as well as the correct placement of the queen, are different. Your solutions without backtracking should have enough heuristics to avoid looking for all invalid paths. A solution without trimming would generally draw each (64 choice 8) arrangement of queens on the board.

+4
source

Vanilla rollback is the same as depth search. You become deeper and deeper in your branch, and when you cannot move further (because no more queens can be added to the board), you will track your way back to the root and try another branch of the tree --- hence “kickbacks”.

Your first search in depth and the search for “backward search” is most likely the same algorithm with just a cosmetic difference. When you have 6 queens on the board, and there are no more queens, you cannot "rationally" continue the search, so the likelihood that your depth search will stop at first, instead of listing all the invalid configurations that you get from adding the 7th and the 8th queen anywhere on board).

+1
source

The Wikipedia description of backtracking conflates (i) revises nodes to consider invisible branches and (ii) pruning nodes, for example, algorithms that provide local consistency . If you do not (ii), then the number of visited nodes will not decrease.

0
source

Source: https://habr.com/ru/post/1492603/


All Articles