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
2 answers

How many sum of repeatable combinations of length N?

How many sum of repeatable combinations of length N ? Like [1,3,5,10], N = 4. And there gonna be [1,1,1,1] -> sum is 4 [1,1,1,3] -> sum is 6 ... [10,10,10,10] -> sum is 40 I perform a backtracking algo. by python3 res = set() n = 4 def…
0
votes
1 answer

Memory limit exceeded in DFS algorithm

I implemented this code. But unfortunately it becomes "time limit exceeded". The algorithm for finding the path from the first A[0][0] to the last A[n][n] two-dimensional array. I used the "DFS" algorithm. import…
0
votes
0 answers

Backtracking chopsticks in java

I have an exercise that is killing my brain; which is: I have x sticks, and I break them into x chunks which I measure, they are a vector of numbers, the solution to the problem is to find the minimum number to create sticks of the same size: Sample…
0
votes
0 answers

Recursive backtracking to find all subsets

I have tried to solve a problem of generating all subsets of an array which contains no duplicates from leetcode : https://leetcode.com/problems/subsets/ . e.g if input is [1,2,3], output should be [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]. Since…
0
votes
0 answers

Draw and visiualize recursive tree online

I have written a code for a coding question on the leetcode. Question is that we are given a vector and we have to find all the permutation of that vector. I have tried this question using bitmasking. My code: #include using namespace…
user13217404
0
votes
1 answer

Recursive backtracking alogrithm for a maze solver in C is not back tracking and gets stuck

I am trying to write a program in C that tracks the entire search process of a recursive backtracking algorithm of a maze solver. Thus, I'll be printing out every step taken. The maze is a simple one with '#' representing walls, '.' representing…
0
votes
2 answers

Time complexity of functions

What's the time complexity of the following two functions? int fun1(int n){ if(n>0) return (2*fun1(n-1)+1); else return 1; } int fun2(int n){ if(n>0) return (fun2(n-1)+fun2(n-1)+1); else return 1; } Obviously for fun2 we write recursive equation…
striker
  • 560
  • 6
  • 13
0
votes
0 answers

How to speed up backtracking algorithm?

A problem I was given requires us to solve using a backtracking style algorithm. I wrote one based upon a given solution to a similar problem, but I need it to be faster (run all test cases in under 3 seconds.) The problem statement is as…
0
votes
1 answer

Array from recursive call being overwritten

We're making a program to solve an asterisk sudoku via a recursive approach with back tracking. The solveIt method calls the solve method which is the recursive method. grid is declared before to be a 9x9 2D array that contains the puzzle to be…
0
votes
1 answer

Calculating maximum sum of any random k elements in an array Recursively ( without any constraints)

What would be the fastest way to find the maximum of n elements within an array. (They don't need to be consecutive) Eg a={1 ,3, 2, 5, 0, 10} maximum sum when n=2 will be 15 Eg if n was 2 then we would use two loops n was 3 we would use 3 loops…
L m
  • 551
  • 1
  • 5
  • 8
0
votes
1 answer

Where has my logic gone wrong in my attempt to implement a sudoku solver in Javascript?

const N = 4; const fourbyfour = [ [1, 0, 0, 4], [3, 4, 1, 2], [2, 1, 4, 3], [4, 3, 2, 1], ]; function solve(board) { for (let i = 0; i < N; i++) { for (let j = 0; j < N; j++) { if (board[i][j] === 0) { for (let k = 1;…
0
votes
0 answers

C++: Weights and Balances Problem with 2 functions

bool isMeasurable(int target, std::vector& weights, int current, unsigned int index){ if(index<=weights.size()-1){ if(target==current){ return true; } else { return isMeasurable(target+weights[index],…
0
votes
0 answers

C++ Boggle Solver Issues

void scoreBoardHelper(Grid& board, Lexicon& lex, Set& visitedPrefixes, string current, GridLocation location, Set& foundWords, Set visitedTiles){ if(lex.contains(current) && current.size()>3){ …
0
votes
3 answers

How to save the return in recursive function to a variable?

I wrote a JavaScript to solve a Sudoku Puzzle using backtracking. My goal is to save the solution in a variable. Right now, I only can call the solution once. var grid = [ [0,1], [1,0] ] function solve() { /// ... the recursive function that…
0
votes
2 answers

Backtracking to find the maze exit. Why does my function always return True?

My code successfully makes its way through all the possible paths in the maze... but it always returns True, even though true only updates if certain conditions are met (having reached the edge of the maze). It seems I have misunderstanding of…