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

prolog depth first iterative deepening

I am trying to implement a depth first iterative deepening search of a state space graph. I have a graph with three vertices and their are two activating edges and two inhibition edges. Each node has a binary value, collectively this is the state of…
8
votes
1 answer

Backtracking paradigm: is it possible to do it without recursion?

Example: sudoku solving with backtracking How do you backtrack without recursion - using loops? I only found solutions when you call backtrack() itself.
ERJAN
  • 23,696
  • 23
  • 72
  • 146
5
votes
1 answer

Backtracking in a grid

Let's say there's a 2D grid of 1s and 0s, for example - 0 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 The grid is "collapsed" to form a smaller grid of 1 fewer row and 1 fewer column, so the above example would be "collapsed" to form a grid of 3 rows and 3…
4
votes
4 answers

How to collect results of recursive backtracking?

I'm teaching myself recursive backtracking. For a dice summing problem I can't figure out how to elegantly collect the results. For reference here's my code that just prints any dice roll which meets the criteria. Ideally I want to change it so…
Josh R
  • 1,970
  • 3
  • 27
  • 45
4
votes
1 answer

Constructing a randomised matrix with no duplicates but fixed partial input

I´m facing a problem with constructing a randomised matrix where I partially already have values (that need to stay fixed - so no further randomisation there). Lets see: matrix should end up being 10 by 10 n <- 10 I do want my first rows to be…
shampoo
  • 57
  • 6
4
votes
2 answers

Generating All Subsets of a Set Using Recursive Backtracking (Python)

I'm trying to understand backtracking but I'm stuck in this problem, here's the prompt: Given a set of distinct integers, return all possible subsets. Example input: [1,2,3] Example output: [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2,…
YSA
  • 799
  • 1
  • 12
  • 30
4
votes
2 answers

Recursive backtracking in Java for solving a crossword

I need to solve a crossword given the initial grid and the words (words can be used more than once or not at all). The initial grid looks like that: ++_+++ +____+ ___+__ +_++_+ +____+ ++_+++ Here is an example word list: pain nice pal id The task…
gdrt
  • 3,160
  • 4
  • 37
  • 56
4
votes
1 answer

Finish n jobs in d steps by backtracking

I have several groups of tasks, each group is a chain of tasks, groups are independent of each other. Tasks within a group can only be processed in the order which is determined by the chain of that group. Each task has an ID and a cost. Tasks are…
PlsWork
  • 1,958
  • 1
  • 19
  • 31
4
votes
1 answer

Efficient Nonogram Solver

So I recently saw this puzzle posted by the British GCHQ: It involves solving a 25x25 nonogram: A nonogram is picture logic puzzles in which cells in a grid must be colored or left blank according to numbers at the side of the grid to reveal a…
gowrath
  • 3,136
  • 2
  • 17
  • 32
4
votes
1 answer

Recursive backtracking algorithm not solving some cases

I am currently programming a recursive algorithm to solve a game of Peg Solitaire. The algorithm needs to use the "backtracking" approach to solve the board. I think I have managed to get a very close to correct solution. It seems that my code…
Josh
  • 366
  • 2
  • 8
  • 20
3
votes
1 answer

N-Queens Python solution generating incorrect output

I am trying to solve the n-queens problem. I wrote a solution for the problem as follows: def isSafe(board,row,col): for i in range(row): if board[i][col]==1: return False x,y = row,col # print(x,y) while…
3
votes
1 answer

Why for backtracking sometimes we need to explicitly pop after recursion, and sometimes we don't?

For example let consider a task where we need to find all permutations for given string preserving the character sequence but changing case. Here is backtracking solution without .pop(): def letterCasePermutation(S): """ :type S: str …
3
votes
2 answers

Why after pressing semicolon program is back in deep recursion?

I'm trying to understand the semicolon functionality. I have this code: del(X,[X|Rest],Rest). del(X,[Y|Tail],[Y|Rest]) :- del(X,Tail,Rest). permutation([],[]). permutation(L,[X|P]) :- del(X,L,L1), permutation(L1,P). It's the simple predicate…
3
votes
4 answers

Representing an amount of money with specific bills

I want to write a function in Racket which takes an amount of money and a list of specific bill-values, and then returns a list with the amount of bills used of every type to make the given amount in total. For example (calc 415 (list 100 10 5 2 1))…
3
votes
1 answer

Recursive backtracking Algorithm in JavaScript

I am currently trying to learn about backtracking Algorithms and have been working on a Sudoku game. I understand the basic working of the algorithm and have written a Sudoku solver using it. My current problem is related to removing a set amount of…
1
2 3
20 21