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
1 answer

C++ - Having trouble understanding binary tree recursive function (insertion)

I have a binary tree that looks like this struct Node { int key; double data; Node* right; Node* left; }; and I have this "insert" function for inserting new nodes and building the tree void insert(Node*& p, int key, double to_be_inserted)…
Schytheron
  • 715
  • 8
  • 28
0
votes
0 answers

Unable to understand recursive function calls

I'm new to programming and was learning about recursive functions from "Intro to Java Programming ~Liang". While recursive functions fascinate me, I think I'm not able to completely understand how the methods call work internally. I understand that…
Mohit Tomar
  • 160
  • 1
  • 11
0
votes
1 answer

Recursive backtracking 2D array (can water fall off the map)

I am trying to solve a recursive backtracking problem in which we must start at a given coordinate in a 2D array of ints, and find a path to an edge cell of the array, such that each successive element along the path is less than or equal to the…
0
votes
1 answer

What is the error in my code to solve Sudoku problem using recursive-backtracking?

I am learning backtracking and recursion. I wrote a code for solving a Sudoku problem but I am getting a wrong output and I can't figure out why ? Please help me out! def print_sudoku(arr): for i in range(9): for j in range(9): …
0
votes
1 answer

Question about recursive return statement order

I have been working on a question that calculates the sums of each branch on a binary tree and returns them in an array. Its pretty much a DFS problem in which you accumulate the solutions into an array. I am just struggling on understanding where…
0
votes
2 answers

How to use a shared variable during Recursion in Python

I am implementing a backtracking based recursive solution and want to update the variable min_path based on the conditions. If I define the variable outside the recursive function, I get a reference error, what would be the best way to implement…
nikhil.g777
  • 882
  • 3
  • 12
  • 24
0
votes
0 answers

How to escape infinite loop in this recursive code?

I am implementing N-Queen problem solver with backjumping algorithm and I have caught infinite loop error in recursive call. I have mainly caused trouble in returning function.I think I have error in designing recursive calls. package…
Cecelia
  • 1
  • 6
0
votes
1 answer

How do I interpret this backtracking code?

I'm having a hard time understanding backtracking code. In particular, I know we always explore and backtrack when we don't find a solution, but I don't understand the logic behind the path.pop() line. I know we must pop elements after exploration,…
Kashif
  • 3,063
  • 6
  • 29
  • 45
0
votes
1 answer

Backtracking algorithm that finds solutions for sudoku

I'm studying the theory of algorithms and their possible resolutions methods.In this case I have some problems with backtracking. I want to write a function that fill a sudoku. But it doesn't print anything. Where is the error? The function…
0
votes
1 answer

recursive backtracking to list all subsets with a given sum?

I'm trying to print all possible subarrays that sum up to a given target number. # arr -- the array # n -- length of the array # target_sum -- sum we want # target_arr -- subarray we test for having the right sum # ite -- iterator def…
Kashif
  • 3,063
  • 6
  • 29
  • 45
0
votes
1 answer

List assignment index out of range despite creating list

I am trying to write a basic backtracking piece of code and have ran into an error with my list. I have created my list at the bottom just before calling the function, however I still get list assignment index out of range! I have tried leaving the…
Alex
  • 35
  • 3
0
votes
1 answer

Filling an array with all combinations of formula N^R

For a homework question i need to fill an array with all combinations of the formula N^R. The variable R is constant and is 6. The variable N is not constant and let's say it's 2. So 2^6 = 64. Now what i need is an array with all the combinations…
0
votes
1 answer

Try to convert backtracking code in java to javascript

Here is a backtracking question I tried to convert this answers from java to javascript Java code public class Solution { private static final String[] KEYS = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; …
kenpeter
  • 7,404
  • 14
  • 64
  • 95
0
votes
2 answers

Topic: Intuition behind using backtracking (and not just recursive DFS)

For starters, I am not trying to ask the difference between a car and a DeLorean. So, I am solving this LeetCode question: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of…
0
votes
0 answers

How to learn Backtracking-recursion without knowledge of structs? - c

edit: someone brought to my attention- that SO isn't the place to ask for tutorials. If so I'd be glad if someone could explain to me how to implement recursive backtracking using arrays (I don't know how to use linked lists etc.) TL;DR: Looking for…
Micha Blum
  • 15
  • 5