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

Backtracking Min options to sum a number using 1, 5 and 7 via recursion - JAVA

I want to create a recursion function that returns the minimum options to create a certain number using numbers 1, 5 and 7 (Fixed predetermined numbers). It is important that this is done only by recursion without loops at all. For example: if n =…
AnnaLA
  • 155
  • 1
  • 11
0
votes
1 answer

Knight's Tour code runs into infinite loop, does not reach a solution

My Recursive Backtracking approach to Knight's Tour runs into an infinite loop. At first, I thought the problem might be taking this much time in general but some solutions do it in an instant. Please tell what is wrong with my code. package…
0
votes
0 answers

Complexity of Maze problem if solution exist(Backtracking algorithm applied)

Here is the solution: Using recursion tree method, looks like it should be exponential i.e 4 power n. But solving it is not clear. Like how one would describe in an interview. Any help is appreciated. class Solution { public static boolean…
0
votes
1 answer

Writing a faster backtracking algorithm in Python

I am learning backtracking algorithms and I have written a program of subset sums. Here's the program: items = range(1, 10) result = [] def backtracking(array, target, temparray= []): if target == 0: result.append(temparray) …
Gsbansal10
  • 303
  • 5
  • 12
0
votes
1 answer

Weighted Job Scheduling using Recursion

I was trying a brute force solution to the weighted job scheduling problem. This is what I have tried. const solution = jobs => { let maxWeight = 0; for (let i = 0; i < jobs.length; i++) { const endTime = jobs[i][1]; const weight =…
0
votes
1 answer

maze example not generating maze

I am following Maze game tutorial from youtube Android Programming - Maze Game Pt4 The tutor's code in the video generates random mazes. I have followed the code to the 'T' throughout but my emulator only gives the following output - My emulator's…
zaidKnight
  • 5
  • 1
  • 3
0
votes
1 answer

Recursion without a base case? How does this function terminate?

I found this code in this stack overflow answer and I'm trying to understand how this recursive function ever terminates. My reasoning for not asking the question on that thread is that my question is about recursion, not so much about what was…
daniekpo
  • 157
  • 3
  • 9
0
votes
1 answer

Knight's Tour - BackTracking in Java, Out of Bound

I am trying to solve the problem where the problem states that, given a N * M chessboard, a Knight’s Tour is defined as the sequence of moves of a Knight, such that the Knight visits every square only once. Below is my code, however I am getting…
0
votes
0 answers

backtraking DUCU in java java

this code results an StackOverFlowError, but I can't pin point where the problem is. public boolean backtracking(int n, ArrayList R, ArrayList S) { ArrayList C = new ArrayList<>(); if…
Mina Fro
  • 1
  • 2
0
votes
1 answer

Better Version for Backtracking

Given n tasks, each can be performed in 1 unit of time and tasks can be performed in parallel. Each task can only be done within a given time bound, say between time t1 and t2 (both inclusive)(t1 <= t2). Aim is to find maximum tasks that can be…
0
votes
1 answer

How can I fix my out of bounds exception in my recursive backtracker maze?

My cells each have a coordinate. My problem is that when my current cell is a border-cell and my random chosen neighbor is not within the border I get an exception. How do I prevent this method from looking outside the border? I tried to add x and…
Raul
  • 1
  • 2
0
votes
1 answer

Why does this method work?

This is a recursive backtracking method that finds whether or not a given array can sum to a target amount. This works: public boolean groupSum(int start, int[] nums, int target) { if(start >= nums.length) return target ==0; …
0
votes
1 answer

I do not know why it is showing error in passing multidimensional array

I had been trying to find out the reason why this error is happening but found out nothing. This is the code for n-queen problem from backtracking. But this doesn't matter. I want to find out why this error is occurring and how to resolve…
0
votes
2 answers

Global variable for recursion in Python

I am facing some difficulties in backtracking. How do I define a global list to be used in the problems of backtracking? I saw a couple of answers and they have all suggested using 'global' keyword in front of the variable name, inside the…
0
votes
2 answers

Backtracking a maze using recursion in c++

So looking at all of the other forums about solving a maze using recursion, none of them had helped me with my issue. I get a maze from an input file and there is a start and end position. I find the start pos pass the x and y by reference and…