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
18
votes
5 answers

12 dominating knights puzzle (backtracking)

I've been searching for hours and haven't found a fully working solution for this kind of puzzle yet. So I followed similar problem with bishops. What I need to do is place 12 knights on the chess board in such a way, that all free squares of the…
RendoJack
  • 366
  • 1
  • 5
  • 17
17
votes
4 answers

Is dynamic programming backtracking with cache

I've always wondered about this. And no books state this explicitly. Backtracking is exploring all possibilities until we figure out one possibility cannot lead us to a possible solution, in that case we drop it. Dynamic programming as I understand…
Mohan
  • 1,850
  • 1
  • 19
  • 42
17
votes
5 answers

N-queens in Haskell without list traversal

I searched the web for different solutions to the n-queens problem in Haskell but couldn't find any that could check for unsafe positions in O(1) time, like that one that you keep an array for the / diagonals and one for the \ diagonals. Most…
hugomg
  • 68,213
  • 24
  • 160
  • 246
16
votes
2 answers

Why do /\w+:/ and /\S+:/ handle backtracking differently?

I analyzed these two regexes using regex101. I think the backtrack of /\S+:/ is right. But I can't understand that difference. Am I wrong?
Mr.kang
  • 587
  • 6
  • 17
14
votes
3 answers

Interview question, recursion + backtracking

This question was asked in interview and deals with recursion/backtracking. Suppose we have two arrays of booleans:bool* source and bool* target,each of them the same length n(source/target/n are given as arguments). The goal of the question is to…
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
14
votes
2 answers

Prolog GNU - Univ operator? Explanation of it

So the univ operator. I don't exactly understand it. For example this: foo(PredList,[H|_]) :- bar(PredList,H). foo(PredList,[_|T]) :- foo(PredList,T),!. bar([H|_],Item) :- G =.. [H,Item],G. bar([_|T],Item) :- bar(T,Item). What is this doing? This…
Matt
  • 7,049
  • 7
  • 50
  • 77
14
votes
5 answers

How do I resolve the "Crypt Kicker" exercise proposed in "Programming Challenges (The Programming Contest Training Manual)"?

"Programming Challenges (The Programming Contest Training Manual)" is probably one of the nicest exercises book on algorithms. I've resolved the first 11 exercises, but now I am stuck with "Crypt Kicker" problem: Crypt Kicker A common but…
Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118
14
votes
2 answers

Can you write between/3 in pure prolog?

I've been trying to understand how to produce a series of values from a Prolog predicate on backtracking. The builtin predicate between/3 will generate all the integers in a range one at a time on backtracking, so an example of how that is written…
postfuturist
  • 22,211
  • 11
  • 65
  • 85
13
votes
2 answers

Backtracking solution for programming exercise (fitting pipes)

I'm reviewing a programming problem from a local programming contest. You can download the problem here (pdf). It's in dutch but the pictures will help to understand it. You receive a m*m grid as input with some pipes and some missing spots (the…
Jasper
  • 599
  • 1
  • 7
  • 19
13
votes
5 answers

Solving crosswords efficiently

I have a crossword puzzle and a list of words which can be used to solve it (words can be placed multiple times or not even once). There is always a solution for the given crossword and word list. I searched for clues on how to solve this problem…
PlsWork
  • 1,958
  • 1
  • 19
  • 31
12
votes
3 answers

Implementing the Prolog Unification algorithm in Python? Backtracking

I'm trying to implement Unification, but having problems.. already got dozen of examples,but all they do is to muddy the water. I get more confused than enlightened…
sten
  • 7,028
  • 9
  • 41
  • 63
12
votes
3 answers

How to use the Select monad to solve n-queens?

I'm trying to understand how the Select monad works. Apparently, it is a cousin of Cont and it can be used for backtracking search. I have this list-based solution to the n-queens problem: -- All the ways of extracting an element from a list. oneOf…
danidiaz
  • 26,936
  • 4
  • 45
  • 95
12
votes
1 answer

Regex Pattern Catastrophic backtracking

I have the regex shown below used in one of my old Java systems which is causing backtracking issues lately. Quite often the backtracking threads cause the CPU of the machine to hit the upper limit and it does not return back until the application…
Achilles
  • 123
  • 1
  • 1
  • 4
12
votes
3 answers

Atomic groups clarity

Consider this regex. a*b This will fail in case of aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac This takes 67 steps in debugger to fail. Now consider this regex. (?>a*)b This will fail in case of aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac This takes 133 steps in…
vks
  • 67,027
  • 10
  • 91
  • 124
12
votes
1 answer

Regex (a?)* not exponential?

I am currently looking into the problem of regular expressions which can end up running in exponential time when matched against a certain input, for example both (a*)* and (a|a)* potentially exhibit 'catastrophic backtracking' when matched against…
Jarmex
  • 679
  • 4
  • 14