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
34
votes
2 answers

How to calculate time complexity of backtracking algorithm?

How to calculate time complexity for these backtracking algorithms and do they have same time complexity? If different how? Kindly explain in detail and thanks for the help. 1. Hamiltonian cycle: bool hamCycleUtil(bool graph[V][V], int…
da3m0n
  • 353
  • 1
  • 3
  • 7
32
votes
1 answer

what is AC means in leetcode, is it algorithm technique like DP?

I found from various online coding forums, there is a technique called "AC", which looks like "Dynamic Programming" or "Back tracking", but not sure what it is how to use. Any one has suggestions? Thanks Joni: As per him/her it is "Accepted". Now it…
Cast Away
  • 692
  • 7
  • 11
27
votes
7 answers

Suggested algorithms/methods for laying out labels on an image

Given an image and a set of labels attached to particular points on the image, I'm looking for an algorithm to lay out the labels to the sides of the image with certain constraints (roughly same number of labels on each side, labels roughly…
25
votes
6 answers

Sudoku solver in Java, using backtracking and recursion

I am programming a Sudoku solver in Java for a 9x9 grid. I have methods for: printing the grid initializing the board with given values testing for conflicts (if same number is in same line or 3x3 sub-grid) a method to place the digits, one by one,…
Carleton U
  • 283
  • 1
  • 3
  • 7
25
votes
7 answers

Difference between backtracking and recursion?

What is the difference between backtracking and recursion? How is this program working? void generate_all(int n) { if(n<1) printf("%s\n", ar); else{ ar[n-1]='0'; //fix (n)th bit as '0' generate_all(n-1); …
ABHISHEK WALTER
  • 261
  • 1
  • 4
  • 3
25
votes
2 answers

Using the Logic Monad in Haskell

Recently, I implemented a naïve DPLL Sat Solver in Haskell, adapted from John Harrison's Handbook of Practical Logic and Automated Reasoning. DPLL is a variety of backtrack search, so I want to experiment with using the Logic monad from Oleg…
Matt W-D
  • 1,535
  • 2
  • 18
  • 22
21
votes
2 answers

RegEx debugging

I'm debugging a Regular Expression ^(A+)*B over a string AAAC (example from rexegg.com) by two separate debugging tools I have access: regex101.com RegexBuddy v4 Below is the results (regex101 in the left side): The question I have is not mainly…
revo
  • 43,830
  • 14
  • 67
  • 109
21
votes
4 answers

Given n and k, return the kth permutation sequence

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3 ) : "123" "132" "213" "231" "312" "321" Given n and k, return the kth permutation…
explorer
  • 638
  • 1
  • 5
  • 20
20
votes
4 answers

How to optimize Knight's tour algorithm?

I code the Knight's tour algorithm in c++ using Backtracking method. But it seems too slow or stuck in infinite loop for n > 7 (bigger than 7 by 7 chessboard). The question is: What is the Time complexity for this algorithm and how can I optimize…
Kasra
  • 791
  • 1
  • 6
  • 17
19
votes
1 answer

Explain BFS and DFS in terms of backtracking

Wikipedia about Depth First Search: Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible…
hhh
  • 44,388
  • 56
  • 154
  • 251
18
votes
9 answers

algorithm to find longest non-overlapping sequences

I am trying to find the best way to solve the following problem. By best way I mean less complex. As an input a list of tuples (start,length) such: [(0,5),(0,1),(1,9),(5,5),(5,7),(10,1)] Each element represets a sequence by its start and length,…
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
  • 356
  • 2
  • 15
18
votes
3 answers

How to delete the last element from an array?

Now I'm working with the recursive backtracking,my assignment is to find the longest path in the maze,the mass is presented as the field covered with the coordinates,and the coordinates of the walls are sore in the file. I have made a parser to…
Andre Liberty
  • 538
  • 1
  • 5
  • 14
17
votes
4 answers

Difference between 'backtracking' and 'branch and bound'

In backtracking we use both bfs and dfs. Even in branch and bound we use both bfs and dfs in additional to least cost search. so when do we use backtracking and when do we use branch and bound Does using branch and bound decreases time…
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
  • 63,082
  • 19
  • 144
  • 230
1
2 3
89 90