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.

1349 questions
11
votes
2 answers

When should regex backtracking controls, like (*PRUNE), be used?

Some regex engines support backtracking-related verbs: (*PRUNE), (*SKIP), (?{doSomeCode();})*, etc. I already know what these verbs do, from Reference - What does this regex mean?. I'm inclined to think that these verbs are somewhat esoteric, or at…
Laurel
  • 5,522
  • 11
  • 26
  • 49
11
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
  • 63,206
  • 9
  • 78
  • 110
10
votes
3 answers

Using recursion and backtracking to generate all possible combinations

I'm trying to implement a class that will generate all possible unordered n-tuples or combinations given a number of elements and the size of the combination. In other words, when calling this: NTupleUnordered unordered_tuple_generator(3, 5,…
Lechuzza
  • 113
  • 1
  • 1
  • 7
10
votes
2 answers

Minimum number of clicks to solve Flood-It-like puzzle

I have grid N × M in which each cell is coloured with one colour. When the player clicks on any cell of the grid of colour α, the cell in the top-leftmost corner of the grid, of colour β, receives the colour α, but not only it: all those cells…
Felipe
  • 635
  • 5
  • 20
10
votes
3 answers

generate all partitions of a set

For a set of the form A = {1, 2, 3, ..., n}. It is called partition of the set A, a set of k<=n elements which respect the following theorems: a) the union of all the partitions of A is A b) the intersection of 2 partitions of A is the empty set…
cristid9
  • 924
  • 14
  • 23
10
votes
1 answer

When is recursive backtracking appropriate?

I'm making a SudokuSolver for a class, and I'm having trouble with the solve method. My current solution uses recursive backtracking (I think). Assignment Requirements int solve() -- tries to solve the puzzle using the strategy described above.…
Eva
  • 3,792
  • 4
  • 35
  • 60
9
votes
3 answers

Simplified Travelling Salesman in Prolog

I've looked through the similar questions but can't find anything that's relevant to my problem. I'm struggling to find an algorithm or set of 'loops' that will find a path from CityA to CityB, using a database…
g.a.kilby
  • 93
  • 1
  • 3
9
votes
2 answers

gdb disassemble: show function offsets in base 16

When disassembling functions, gdb will display memory addresses in base 16, but offsets in base 10. Example: (gdb) disassemble unregister_sysctl_table Dump of assembler code for function unregister_sysctl_table: 0x00037080 <+0>: push %ebp …
Lucian Adrian Grijincu
  • 2,242
  • 2
  • 17
  • 17
9
votes
5 answers

Recursive function to match a string against a wildcard pattern

So I've been trying to solve this assignment whole day, just can't get it. The following function accepts 2 strings, the 2nd (not 1st) possibly containing *'s (asterisks). An * is a replacement for a string (empty, 1 char or more), it can appear…
George Kagan
  • 5,207
  • 8
  • 44
  • 49
9
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
  • 93
  • 1
  • 3
9
votes
5 answers

How to generate all the permutations of a multiset?

A multi-set is a set in which all the elements may not be unique.How to enumerate all the possible permutations among the set elements?
piyukr
  • 581
  • 1
  • 10
  • 17
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,152
  • 13
  • 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
  • 52,327
  • 12
  • 74
  • 110
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
  • 32,521
  • 18
  • 64
  • 139
8
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
  • 9,379
  • 18
  • 67
  • 140
1 2
3
89 90