Questions tagged [backtracking]

Backtracking is a general algorithm for finding solutions to some computational problem, that incrementally builds candidates to the solutions.

Backtracking is an important tool for solving constraint satisfaction problems, such as crosswords, verbal arithmetic, Sudoku, and many other puzzles. It is often the most convenient (if not the most efficient) technique for parsing, for the knapsack problem and other combinatorial optimization problems. It is also the basis of the so-called logic programming languages such as Icon, Planner and Prolog. Backtracking is also utilized in the (diff) difference engine for the MediaWiki software.

1606 questions
9
votes
2 answers

N-queen backtracking in Python: how to return solutions instead of printing them?

def solve(n): #prepare a board board = [[0 for x in range(n)] for x in range(n)] #set initial positions place_queen(board, 0, 0) def place_queen(board, row, column): """place a queen that satisfies all the conditions""" …
Maximus S
  • 10,759
  • 19
  • 75
  • 154
8
votes
1 answer

Does a combination of K integers exist, so that their sum is equal to a given number?

I've been breaking a sweat over this question I've been asked to answer (it's technically homework). I've considered a hashtable but I'm kind of stuck on the exact specifics of how I'd make this work Here's the question: Given k sets of integers…
Arnon
  • 2,237
  • 15
  • 23
8
votes
3 answers

Improve a word search game worst case

Consider: a c p r c x s o p c v o v n i w g f m n q a t i t An alphabet i_index is adjacent to another alphabet j_index in the tile if i_index is next to j_index in any of the following positions: * * * * x * * * * Here all the * indicates the…
phoxis
  • 60,131
  • 14
  • 81
  • 117
8
votes
2 answers

Backtracking in regexp faster than expected

According to perlre, the following code should take several seconds to execute: $ time perl -E '$_="((()" . ("a") x 18; say "ok" if m{ \(([^()]+|\( [^()]* \))+\)}x;' real 0m0.006s user 0m0.000s sys 0m0.005s The documentation says: Consider…
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
8
votes
3 answers

Implementing Backtracking on Haskell

I have a problem making Backtracking on Haskell, I know how to do recursive functions but I get troubles when I try to get multiple solutions or the best one (backtracking). There's a list with some strings, then I need to get the solutions to get…
Juan Figueira
  • 161
  • 2
  • 11
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
8
votes
2 answers

What are some good resources for learning backtracking, branch-and-bound and dynamic programming algorithms?

CLRS doesn't seem to cover bactracking/branch-and-bound. I tried several resources online, though I get the idead behind these, I am unable to write code for, let's say, Knapsack problem. So, I want something that, may be, takes a problem and…
7
votes
6 answers

Recursive solution to Sudoku generator

I'm trying to code an algorithm that creates a legal Sudoku board in either Java or Javascript. Neither work, and I'm not entirely sure why. Essentially, the problem in both programs is that either x or y is getting incremented more than it should…
SomeKittens
  • 38,868
  • 19
  • 114
  • 143
7
votes
3 answers

How to stop backtracking in Scala?

Suppose I am solving a problem (e.g. N-Queen) with backtracking. What if I want to find the only one (the 1-st) solution rather than all of them. I guess I can do it imperatively (e.g. with a mutable boolean flag). I wonder how I can do it…
Michael
  • 10,185
  • 12
  • 59
  • 110
7
votes
3 answers

Recursive-backtracking algorithm for solving the partitioning problem

Hey, i'm looking for some help to find an algorithm which divides an array of positive numbers into k-parts so that each part has the (approximately) the same sum ... let's say we have 1,2,3,4,5,6,7,8,9 en k=3 thn the algorithm should partition it…
7
votes
1 answer

Optimal weights subset sum using backtracking

I'm trying to solve a problem. I have some weights. [2,7,20,70,200,700] After a given input, for example 1507, it should return the optimal combination of those weights. Which in this case would be [700,200,200,200,200,7]. Unfortunately my algorithm…
BilalReffas
  • 8,132
  • 4
  • 50
  • 71
7
votes
3 answers

Collect all "minimum" solutions from a predicate

Given the following facts in a database: foo(a, 3). foo(b, 2). foo(c, 4). foo(d, 3). foo(e, 2). foo(f, 6). foo(g, 3). foo(h, 2). I want to collect all first arguments that have the smallest second argument, plus the value of the second argument.…
user1812457
7
votes
1 answer

Browser tab stucks when my RegEx is executed and do not match the input

The problem is next. I created an input field which has validation and this is valid data: 1-12, 14, 16, 22, 25-35, 41, 49, 55-90 1230-1992, 2001-2099, 9931 1-2 13 1,3,4,5,6,10 all Basically, any combination of those numbers (ranges,…
admix
  • 1,752
  • 3
  • 22
  • 27
7
votes
2 answers

Determine whether a Sudoku has a unique solution

I'm struggling with a backtracking algorithm to determine wether a Sudoku has a unique solution or if it has multiple Solutions. Here's the backtracking code i use: static boolean solve(int i, int j, int[][] cells) { if (i == 9) { i…
user4758246
  • 575
  • 5
  • 13
7
votes
3 answers

how to stop prolog from examining impossible solutions infinitely?

suppose the following program: nat(0). nat(s(N)) :- nat(N). /* 0+b=b */ plus(0,B,B) :- nat(B). /* (a+1)+b = c iff a+(b+1)=c */ plus(s(A),B,C) :- plus(A,s(B),C). it works great for adding two numbers, but when i attempt a query of the following…
yurib
  • 8,043
  • 3
  • 30
  • 55