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
0
votes
0 answers

recursive sudoku solver in javascript failing to backtrack through grid

i'm currently working on a sudoku solver in javascript which i want to visualize on a canvas with p5.js, but am having some trouble with the algorithm. it generates numbers and guesses correctly, but when it finds that a solution is not viable, it…
aryan
  • 9
  • 4
0
votes
1 answer

How to print the costs of all possible paths from a node to another?

I want to print all paths from source node to destination node, and the costs of these paths. So far, I have the following code: // Find all paths from source to destination. void search(Graph* graph, int src, int dst) { // Mark the current node…
0
votes
2 answers

How do I output the quickest path of a maze via backtracking?

I'm trying to solve a backtracking problem in python. Not only should my code be able to identify and reach the goal, but also output the quickest path to it. The maze is looks like this: xxxxxxx xSx x x xx x x x xxxx x x x x xx xx x E …
0
votes
1 answer

Fixing coin change Backtracking solution(bruteforce)

I know the optimal problem for this solution is using dynamic programming. However, I wanted to try this bruteforce backtracking approach where I subtract the coin from the amount and try to find combinations that match that amount and find the…
0
votes
0 answers

Variable changing in unexpected way Python

I am trying to make a function that will keep trying different numbers in a cell of a board made with a 2D list until its constraints are met. However, for some reason when I output the row and column that is being tested, it works normally until it…
Alan Shiah
  • 907
  • 1
  • 6
  • 19
0
votes
0 answers

Combination recursive - Backtracking

I've been trying to code 3 different recursive function(Backtracking). The first one should show all permutations(orders doesn't matters) and there's a specif size. The second one should show all cyclic permutations. The third one should show all…
0
votes
0 answers

Java program of the Backtracking algorithm for the Sum-of-Subsets problem

I'm currently working on a problem where I'm supposed to implement in Java the backtracking algorithm of the sum-of-subset. Backtracking is used to solve problems in which a sequence of objects is selected from a specified set so that the sequence…
0
votes
1 answer

How to resolve the runtime error in the following problem?

(Leetcode que 37) Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must…
0
votes
1 answer

Sudoku solver using recursion and backtracking

I am trying to implement a Sudoku solver using Java. This is the code I've written as of now. If I try to run it, it goes on to an endless loop that keeps on printing the first row of the Sudoku board, and that too with an incorrect solution. I…
0
votes
1 answer

Trying a backtracking algorithm

Ok so I wanted to try and write a backtracking algorithm to solve a simple game. The rules are as follows: There is a triangular board with 5 slots in the top row. There are 5 rows, each row has 1 fewer slot than the row above. Each slot, except for…
Martin S.
  • 1
  • 2
0
votes
1 answer

Why is my innerList is not adding in the answers?

This question is of combination sum. I tried it through recursion and backtracking but my output is becoming an empty list of lists every time. public class CombinationSum { public static void main(String[] args) { int[] candidates =…
0
votes
1 answer

Recursive function collects lists, but they are all the same

So the output i want should be something like this: [[9],[9,8],[9,8,7]...[9,8,7,6,5,4,3,2,1]] and this is the code that i am using: a=[] b=[] i=0 def recur(i): i+=1 if i<10: x=recur(i) else: return a b.append(i) …
0
votes
3 answers

Unexpected results for 'finding the digits' problem using recursion in Common Lisp

The "finding the digits problem" is this: Find unique decimal digits A, B, C such that CCC + BBB + AAA = CAAB To solve it using recursion in Common Lisp, I've written this code: (defun find! () (found? 0 ;; initially point…
0
votes
1 answer

Sudoku Solver code gives unexpected result

Question Link: https://leetcode.com/problems/valid-sudoku/description/ Below is my code for Sudoku Solver. I am expected to return true if the sudoku is solvable else false. class Solution { public: bool solveSudoku(vector> &board,…
Amaarockz
  • 4,348
  • 2
  • 9
  • 27
0
votes
0 answers

issue understanding backtracking (recursion) in java

i'm trying to understand a solution for the leetcode problem #22 where we generate parentheses recursively and I'm stuck on where the function returns to after we find the first combination of parentheses the question asks you to create all possible…