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

8Queen code not working

I have been trying to debug this code I made, it outputs only 1's in the first row of the array and all other elements are zero(from second row till the last element.), is it the problem of function calling of passing the array by value or something…
Sahir
  • 329
  • 1
  • 2
  • 9
-2
votes
1 answer

How to return 'count' of some recursions in python?

How to update and return count variable to outside of fucntion in python. for input : [[0,0,0],[0,1,0],[0,0,0]] std out : 1 1 #as count is incrementing inside function but in each recursion it's changing it's value to 0. returned output : 0 Expected…
Jaswanth
  • 1
  • 1
-2
votes
1 answer

How do I break out of this recursive call?

I am a newbie to recursion and I am still learning it, so please tolerate my poor logic if it is bad. I have this function which has 5 parameters a,b,c,x,y. so what I essentially want to do is take an element out of either of these variables and add…
-2
votes
2 answers

Why does this function print the result, but return none?

def solve(sudoku): for y in range(9): for x in range(9): if sudoku[y][x] == 0: for n in range(1, 10): if possible(x, y, n): sudoku[y][x] = n …
Henri
  • 61
  • 1
  • 3
-2
votes
1 answer

Different behaviors with same time complexity

I am solving the following question on LeetCode: Write a function that takes an integer n and return all possible combinations of its factors. For e.g., 12 should return: [    [2, 6],    [2, 2, 3],    [3, 4] ] I…
user6490375
  • 424
  • 1
  • 4
  • 13
-2
votes
1 answer

How does this implementation of Sudoku solver work in spite of not storing state?

This link has a Backtracking implementation of the Sudoku Solver algorithm. Notice how line number 42, reverts the value initially assigned to a cell, to another value, in case the initially assigned value did not give a valid output. However, I…
-2
votes
1 answer

Optimisation: Backtracking algorithm

I have a school project, where I have to write a program in c#. I think I'm on the right track but I stuck on this problem. In this section of the program I have to write a backtracking algorithm. I have classes(English, Physics, Maths etc.) and…
nethuszar
  • 225
  • 3
  • 13
-2
votes
1 answer

ArrayList add a new object and overwrite the object that I added before

I know there are a lot of questions about that topic. In fact, I was searching in others questions and I tried to use some solutions but it was not enough. I have to do a program that simulate puffball, using backtracking, and now I am developing…
-2
votes
1 answer

Python algorithm to solve triomines puzzle with backtracking

Let's say I have a board like this . . x . . . . . . . . . . . x . . x x is used box and '.' are free. I need to put triomines to fill all the area, so there will be no free cells. Triomines are L-shaped, and I mark same triomino with the same…
lenik
  • 23,228
  • 4
  • 34
  • 43
-3
votes
1 answer

finding all possible permutations of a given ArrayList, and Storing them in global variable

In the code given below I am adding the a particular permutation of ArrayList in the function permute but while printing in the main function all possible permutations are not printed. Instead, the same ArrayList is printed n! times where n…
-3
votes
1 answer

how to make maze with recursive backtracker java

I have been trying to write a recursive backtracker using the given Wikipedia description of it but I can't grasp how to write it in java. Right now I have been using a double for-loop to go through all of the cells and using Math.random() to help…
-3
votes
1 answer

Why doesn't my Sudoku program return an output?

So I have tried to implement a Sudoku via backtracking algorithm. I don't see why my code is not giving an expected output. What I did was, I created a loop in which it checks for an empty cell (represented with 0) in the sudoku. As it finds it,…
-3
votes
2 answers

Is my regex catastrophically backtracking?

I use a regex to validate number formats. [-+]?([0-90-9]+((\,([0-90-9]{2,}))*\,([0-90-9]{3}))*)?(\.[0-90-9]*)? When I handled a large number of inputs for certain inputs it seems to loop infinitely .I read other answers regarding catastrophic…
Sainath S.R
  • 3,074
  • 9
  • 41
  • 72
-4
votes
2 answers

Java Recursive Backtracking

I study computer sciene and I'm currently practising backtracking as I#m very bad at it. I found that exercise online: You are selling apples. This is a current price tables for your apples: Count 1 2 3 4 5 6 7 8 Price 1 5 8 9 10 17 17 20 So if…
waayne
  • 49
  • 6
1 2 3
20
21