Questions tagged [recursive-backtracking]

Recursive backtracking is a general algorithm for finding solutions to computational problems that incrementally builds partial solutions and drops those which are identified as futile *as early as possible*.

Recursive backtracking is a general algorithm for finding solutions to computational problems that incrementally builds partial solutions and drops as early as possible those partial solutions which are identified as futile i.e. such that can not lead to a correct solution.

It essentially involves building nested loops through recursion. Each loop embodies a series of choices. Each inner loop can accept a partial-solution-so-far and try its own choices in a loop, or reject the partial solution it is presented with, through early exit or an equivalent of the continue or break etc. statement.

The control is inverted in that the full solution is achieved in the innermost loop, which can act on it in a particular manner, like printing it to the output / external file, or calling a callback with the solution as an argument.

The backtracking computational structure is embodied in the states of the nested loops, in each of the nested loops' loop variables.

314 questions
-1
votes
2 answers

Sudoku Recursive Backtracking in Go

I am trying to solve a sudoku puzzle in Go using a recursive backtracking algorithm. I created helper functions that check if a certain row, column, or block are valid (i.e no repeated values), as well as a function to print out the current state. I…
user3196284
-1
votes
1 answer

This is a sudoku solving program.I got recursive errors.I imported sys module and set the recursion limit to 1500,but still it shows errors

import sys sys.setrecursionlimit(1500) # the default recursion limit is 1000 def print_grid(arr): for i in range(9): for j in range(9): print(arr[i][j]) print('\n') def find_empty_location(arr, l): for i in…
-1
votes
1 answer

Python backtracking maze really slow

check the edit, I wrote some code while watching The Coding Train on youtube.. the maze backtracking generator. The youtuber wrote the code in javascript and I tried to understand the code while writing in python. It seems he had to change the…
-1
votes
1 answer

Catastrophic backtracking [A-Z]*([0-9A-Z])-[1-9]*([0-9])

I have a script running that sometimes causes the server to go 100% cpu. I suspect this is because of some regex. Is there any example input that could cause catastrophic backtracking for this regex: [A-Z]([0-9A-Z])-[1-9]([0-9]) It is used to match…
nicolattu
  • 191
  • 2
  • 11
-1
votes
1 answer

How to store values globally in recursive calls in python

I have a doubt regarding backtracking appraoch in python . How do i store the variables and list (temporary) so formed in different calls in recursion in python as we know as soon as the power stacks formed gets destroyed , the variable's value also…
-1
votes
3 answers

Get all possible combinations for a matrix in C++

We have a classroom. Our main goal is to make pairs of students to work together. How are we going to do it? Through a matrix. This matrix (n x n, n is pair) stores the level of 'preference' each student has with another one. For example, i is a…
alexhzr
  • 153
  • 1
  • 1
  • 12
-1
votes
1 answer

Two corners coin game in java

The 2 corners coin game receive an array. The goal of the game is accumulate the number of points (values of the elements in the array) the most. you can take points only from the 2 corners of the array. There are 2 conditions for the game: 1) The…
AnnaLA
  • 155
  • 1
  • 11
-1
votes
1 answer

Print all paths from origin to destination on board using recursion - Python

I have some problem to solve recursively without using any module and I need you to guide me please. You are placed on a grid board at the origin (0, 0) and you want to reach to the destination n, k (That is, n moves rightward and k moves upward).…
-1
votes
1 answer

Sequence of legal pairs of parentheses using recursion - Python

I have some problem to solve using recursion in Python. I'm simply bad in recursion and don't know how to start so please guide me. We will say that a string contains 'n' legal pairs of parentheses if the string contains only the chars '(',')' and…
HelpMe
  • 91
  • 10
-1
votes
1 answer

Changing a boolean value recursively in python

For some context, this is the interview question I'm working on (taken from https://leetcode.com/problems/path-sum/description/). I now realize that its cleaner to just return a boolean value from each recursive function, but I still don't…
Jay K.
  • 546
  • 2
  • 10
  • 17
-1
votes
1 answer

Backtracking to find all possbile paths

I got this question in a recent interview, Given a gird of letter and a word find all the possible paths that make a word Directions: horizontal, vertical or diagonal to any coordinate with a distance 1 Constraint: Each path has to be a unique set…
-1
votes
1 answer

Chess knight tour using recursive backtracking

My chess knight tour code using recursive-backtracking is not working. #include using namespace std; // goal: knight tour #define N 8 void printArray(int board[N][N]) { for(int i = 0; i < N; i ++) { for(int j = 0; j < N;…
-1
votes
1 answer

Backtrack Algorithm To Check Strings form Matrix

I have list: words = ["ALI", "SIN", "ASI", "LIR", "IRI", "INI", "KAR"] I want to check if they form matrix such as this: and return my solution as a list like: solution = ["ALI", "SIN", "IRI"] I have come up with this code: words=["ALI", "SIN",…
-1
votes
1 answer

How do I finish this recursive statement?

def search_sequence( seq, item ): """ Search a sequence for the given item. PROVIDE AN IMPLEMENTATION (TASK #2). This function should use **car** and **cdr**. :param seq: the sequence to be searched. :param item: the item to be…
-1
votes
1 answer

Recursive backtracking, showing the best solution

For school I am supposed to use recursive backtracking to solve a Boat puzzle. The user inputs a maximum weight for the boat, the amount of item types, and a weight and value for each item type. More than one of each item type can be placed on the…
Victoria Potvin
  • 127
  • 1
  • 11
1 2 3
20
21