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
11
votes
3 answers

Maximize minimum distance between arrays

Lets say that you are given n sorted arrays of numbers and you need to pick one number from each array such that the minimum distance between the n chosen elements is maximized. Example: arrays: [0, 500] [100, 350] [200] 2<=n<=10 and every array…
11
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,491
  • 2
  • 18
  • 18
11
votes
2 answers

Finding the numbers from a set which give the minimum amount of waste

A set is passed to this method below, and a length of a bar is also passed in. The solution should output the numbers from the set which give the minimum amount of waste if certain numbers from the set were removed from the bar length. So, bar…
Newb
  • 111
  • 2
11
votes
2 answers

Prolog backtracking VS Rete backtracking

In my class I have been taught the Prolog backtracking algorithm and the Rete forprop algorithm, but I have also been told that Rete can be used to do backprop. How does that work? In which ways is it similar / different to Prolog backtracking? For…
Jsevillamol
  • 2,425
  • 2
  • 23
  • 46
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,965
  • 14
  • 31
  • 57
11
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
  • 4,397
  • 5
  • 43
  • 65
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
  • 687
  • 5
  • 21
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
  • 1,070
  • 1
  • 17
  • 37
10
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,913
  • 8
  • 46
  • 50
10
votes
6 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
  • 631
  • 3
  • 12
  • 18
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
  • 1
  • 3
9
votes
1 answer

MongoDB $regex query and potential exploits

We have a REST API for querying records in a MongoDB. Very simple, something along the following: GET /api/items?q=foo During development, it was convenient to allow regular expressions as the query q. We would simply pass the query parameter to a…
qqilihq
  • 10,794
  • 7
  • 48
  • 89
9
votes
6 answers

Why is this called backtracking?

I have read in Wikipedia and have also Googled it, but I cannot figure out what "Backtracking Algorithm" means. I saw this solution from "Cracking the Code Interviews" and wonder why is this a backtracking algorithm?
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
9
votes
3 answers

What is the "unit" of pcre.backtrack_limit?

I'm encountering an issue where preg_replace() with a complicated regular expression causes an error (PREG_BACKTRACK_LIMIT_ERROR) due to pcre.backtrack_limit being too low, which is set to 1,000,000 by default. I set this to 10,000,000, and it works…
laketuna
  • 3,832
  • 14
  • 59
  • 104