The gentle introduction to go back

Is a flexible testing process for different issues of data science and construction solutions that make up – slightly as navigating maze. In this article, we will briefly come up with the idea of backback before accessing a few visual examples, examples of coded Python.
Note: All types of exemplary code in the following sections are made by the author of this article.
Views all of the mind
At the higher level, the return process includes step test by the vacancy step (usually is an optional problem as the sensitive satisfaction or incorporated financial satisfaction). In each stage of the testing, we continue the exception, looking for problems with problems satisfied as we walk.
If we hit a valid solution during our assessment, we write it. During this time, we can get rid of the search if our problem only requires access to one valid solution. If the problem requires access to many (or all) solutions, we can continue to check the extensions of the solution available.
However, if any time the issues are broken, we Backtrack; This means getting back to the final site where our searches where the partial solution is built (and when it appears valid solutions), and continues to search our way from there. This process is forward and back to the test can be conducted as required until all the resolution is tested and all valid solutions are assessed.
Examples of the hands
To resolve a suitaku
The Sudoku puzzle is a very good example of the problem of oppression of practical apps in various fields from the operating system in Cryptography. The standard type of puzzle contains a 9-year-old grid, made of 3-by-3-by-3 grids grids (or blocks). In the first puzzle configuration, some 81 cells in the Grid is named in the digits range from 1 to 9. To attach the following digits: No sticking to the following structures: No sticky, no one, or 3-by-3 block can contain a double digit.
The Python code below shows how to use sudoku solver using a backtracking, and the correct function of printing the grid. Note that the solver awaits empty cells to be shown (or activated) with zeros.
from copy import deepcopy
def is_valid(board, row, col, num):
# Check if num is not in the current row or column
for i in range(9):
if board[row][i] == num or board[i][col] == num:
return False
# Check if num is not in the 3-by-3 block
start_row, start_col = 3 * (row // 3), 3 * (col // 3)
for i in range(start_row, start_row + 3):
for j in range(start_col, start_col + 3):
if board[i][j] == num:
return False
return True
def find_empty_cell(board):
# Find the next empty cell (denoted by 0)
# Return (row, col) or None if puzzle is complete
for row in range(9):
for col in range(9):
if board[row][col] == 0:
return row, col
return None
def solve_board(board):
empty = find_empty_cell(board)
if not empty:
return True # Solved
row, col = empty
for num in range(1, 10):
if is_valid(board, row, col, num):
board[row][col] = num
if solve_board(board):
return True
board[row][col] = 0 # Backtrack
return False
def solve_sudoku(start_state):
board_copy = deepcopy(start_state) # Avoid overwriting original puzzle
if solve_board(board_copy):
return board_copy
else:
raise ValueError("No solution exists for the given Sudoku puzzle")
def print_board(board):
for i, row in enumerate(board):
if i > 0 and i % 3 == 0:
print("-" * 21)
for j, num in enumerate(row):
if j > 0 and j % 3 == 0:
print("|", end=" ")
print(num if num != 0 else ".", end=" ")
print()
Now, suppose we put the Sudoku puzzle, starting empty cells with zeros, and conducting a solver as follows:
puzzle = [
[5, 0, 0, 0, 3, 0, 0, 0, 7],
[0, 0, 0, 4, 2, 7, 0, 0, 0],
[0, 2, 0, 0, 6, 0, 0, 4, 0],
[0, 1, 0, 0, 9, 0, 0, 2, 0],
[0, 7, 0, 0, 0, 0, 0, 5, 0],
[4, 0, 6, 0, 0, 0, 7, 0, 1],
[0, 4, 2, 0, 7, 0, 6, 1, 0],
[0, 0, 0, 0, 4, 0, 0, 0, 0],
[7, 0, 0, 9, 5, 6, 0, 0, 2],
]
solution = solve_sudoku(puzzle)
print_board(solution)
Solver will produce the next solution within Millisends:
5 6 4 | 1 3 9 | 2 8 7
1 9 8 | 4 2 7 | 5 6 3
3 2 7 | 8 6 5 | 1 4 9
---------------------
8 1 5 | 7 9 4 | 3 2 6
2 7 9 | 6 1 3 | 8 5 4
4 3 6 | 5 8 2 | 7 9 1
---------------------
9 4 2 | 3 7 8 | 6 1 5
6 5 3 | 2 4 1 | 9 7 8
7 8 1 | 9 5 6 | 4 3 2
Cracking Mathlyliad Problem
Math Olmpiidys are prior universities and has difficult statements to be solved under timely conditions without using calculators. Since the full test of the full solution to these problems impossible, effective ways of approaching are often leaning on analysis and coins, exploit clear issues and clear problems and pornography. Some problems should act in serious satisfaction and efficiency in the science of the industry (eg. Therefore, whether CLECT MATCHEMATICS SOLUTION SOLUTION The Olympic problem, may teach to investigate alternative methods (such as backtracking) that exploits the power of broadcasters with similar problems.
For example, consider the following problem from the Over 1 British Mathematics Olympiat on November 2018: “A list of five missing values in the growing order on the blackboard. The total number of five numbers is 3, 1, 8, 9 appeared in writing.
As it happens, the solution for the above problem is 288. The video below describes the Cleverly resolving a Cleverly Solving Some of Clear Problems (eg.
The Python Code below shows how to go back how can it be used to solve the problem:
def is_valid_combination(numbers):
# Checks if each digit from 0-9 appears exactly once in a list of numbers
digits = set()
for number in numbers:
digits.update(str(number))
return len(digits) == 10
def find_combinations():
multiples_of_3 = [i for i in range(12, 100)
if i % 3 == 0 and '0' not in str(i)[0]]
valid_combinations = []
def backtrack(start, path):
if len(path) == 5:
if is_valid_combination(path):
valid_combinations.append(tuple(path))
return
for i in range(start, len(multiples_of_3)):
backtrack(i + 1, path + [multiples_of_3[i]])
backtrack(0, [])
return valid_combinations
print(f"Solution: {len(find_combinations())} ways")
Work is_valid_combination() Specifies important difficulties that should hold each of the 5 books found during the search space. List multiples_of_3 Includes option numbers that can appear in a valid 5-digit list. Work find_combinations() applied back to try every 5 unique combination of number from multiples_of_3.
Work is_valid_combination() and the understanding of a list used to produce multiples_of_3 can be changed to resolve a comprehensive range of similar problems.
Without a backtracking
As we have seen, the return is a simple but powerful way to solve different kinds of critical satisfaction and financial combination problems. However, some strategies such as search-searches (DFS) and powerful programs (DP) are also located in the face – so they look like to get back instead of these methods?
BackTracking can be considered as DFS techniques, where the Contrairint test is the main feature of each step step, and wrong ways may be left early. At that time, DP can be used for two properties that show two properties: Substance that is less with Right registration. The problem has a letter below if the same subproproblems need more often while solving a major problem; To reserve and re-use the repeated subproproblems results (eg implementing memoization) is an important feature of the DP. In addition, the problem has suitable substructure if the proper solution to the problem can be constructed by creating appropriate solutions on its integrated sides.
Now, think about the N-Queens problem, looking at how to set Ni Queens in Ni-TheNi Chessboard, so that no two queens can be terrorists; This is the classic problem with the applications in a few global cases where important conflicts (eg N-Queeens problem is not required to solve the complete board. The creation problem of the N-Queess problem It is unprepared for the power of the DP, while returning is naturally understood by the problem structure.



