backtracking

Have you used the Perl 5.10 backtracking control verbs in your regexes yet?

Have you used the Perl 5.10 backtracking control verbs in your regexes yet? And what problems did they help you accomplish? Just as background: I have done some fiddling, but I can't get any really useful results. As a comparison, when I started getting what the (?> grouping did, it started showing up more in my regexes. I liked the...

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 solutions I found just checked each new queen against all the previous ones. Something like thi...

Comparison of algorithmic approaches to the N queens problem

I wanted to evaluate performance comparisons for various approaches to solving the N queens problem. Mainly AI metaheuristics based algorithms like simulated annealing, tabu search and genetic algorithm etc compared to exact methods(like backtracking). Is there any code available for study? A lot of real-world optimization problems like ...

What OCaml libraries are there for lazy list handling?

What OCaml libraries are out there that provide lazy list handling? I am looking for something along these lines: type 'a lazy_list = (*'*) | Nil | Cons of 'a * 'a lazy_list lazy_t let from f = let rec gen n = lazy ( match f n with | Some x -> Cons (x, gen (n + 1)) | None ->...

Sudoku Solver by Backtracking question update

Assuming a two dimensional array holding a 9x9 sudoku grid, where is my solve function breaking down? I'm trying to solve this using a simple backtracking approach. Thanks! bool solve(int grid[9][9]) { int i,j,k; bool isSolved = false; if(!isSolved(grid)) isSolved = false; if(isSolved) return isSolved; for(i=0; i<9; i++...

Regular Expression, Back reference or alternate construct...

I am trying to write a RegEx in .Net to capture the whole function from a list of function that look something like this. public string Test1() { string result = null; foreach(var item in Entity.EntityProperties) { result +=string.Format("inner string with bracket{0}", "test"); } return result; } public string Test5() { return str...

Backtracking in Erlang

First of all sorry for my English. I would like to use a backtracking algorithm in Erlang. It would serve as a guessing to solve partially filled sudokus. A 9x9 sudoku is stored as a list of 81 elements, where every element stores the possible number which can go into that cell. For a 4x4 sudoku my initial solution looks like this: [[1...

Backtracking recursion question

There is a bag that can take X kilogram. You will get an array of stuff and their weight. Print true and the each weight of the stuff and false if there is no answer Example: for X=20 array {4,9,1,15,7,12,3} print true and 4 1 15 (4+1+15=20) ...

"Crypt Kicker Problem" (Programming Challeneges)

Hello, "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 insecure method of encrypting text is to permute the letters of the alphabet. In o...

Parsec: backtracking not working

I am trying to parse F# type syntax. I started writing an [F]Parsec grammar and ran into problems, so I simplified the grammar down to this: type ::= identifier | type -> type identifier ::= [A-Za-z0-9.`]+ After running into problems with FParsec, I switched to Parsec, since I have a full chapter of a book dedicated to explaining it. ...

How to Solve N-Queens Problem in Scheme?

Hi, I'm stuck on the extended exercise 28.2 of How to Design Programs. Here is the link to the question: http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-35.html#node_chap_28 I used a vector of true or false values to represent the board instead of using a list. This is what I've got which doesn't work: #lang Scheme (define-struc...

Backtracking Problem

Let's say I have the following Prolog knowledge base: likes( john, mary ). likes( john, emma ). likes( john, ashley ). If I compose the following C code: #include... term_t tv; term_t tu; term_t goal_term; functor_t goal_functor; int main( int argc, char** argv ) { argv[ 0 ] = "libpl.dll"; PL_initialise( 1, argv ); PlCa...

backtracking in haskell

I have to traverse a matrix and say how many "characteristic areas" of each type it has. A characteristic area is defined as a zone where elements of value n or >n are adjacent. For example, given the matrix: 0 1 2 2 0 1 1 2 0 3 0 0 There's a single characteristic area of type 1 which is equal to the original matrix: 0 1 2 2 0 1...

Stopping Backtracking

Is there any way in C/C++ to stop a backtracking algorithm after finding the first solution without exiting the program. I want my function to immediately exit the function,not to quit every level of recurrsion one by one stating return. ...

Explain BFS and DFS in terms of backtracking

Wikipedia about DFS 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 along each branch before backtracking. So is BFS? "an algorithm that choose a starting ...

Prolog, fail and do not backtrack.

Is there any build-in predicate in SWI-Prolog that will always fail AND prevent machine from backtracking - it is stop the program from executing immediately (this is not what fail/0 does)? I could use cuts, but I don't like them. Doing something like !, fail is not a problem for me, but in order to accomplish what I want, I would hav...

can backtracking be avoided when trying to minimize the rectangle space that encloses rectangles of different integer shapes?

The abstraction of my problem is that in a cartesian plane there are a lot of rectangles. These rectangles have known integer sizes and must have integer coordinates(their abscissas are known and fixed, only their ordinates may vary). The problem is to find those ordinates for which the smallest rectangle that contains all the given rec...

A recursive backtracking problem

Ok, the real problem is slightly more complicated because it uses some classes that I've written instead of Strings, but it can be imagined like this: If you have a list of 700ish 3-letter words, how can I find every combination that can be formed by linking 5 of these words together by their first and last letters, i.e., CAT TOW WAR RAD...

Array backtracking

Hi, There is an array of numbers, the number on each cell display how many setps does you need to go (left or right), for exmpple int[2]=4 that means that you have to go 4 steps or to int [-2] or to int[6] you have to return true if you went trough all the cell and did not went "out" of the array and false if not. Thanks a lot ...

Backtracking a balancing group in a greedy repetition may cause imbalance?

As a generically brewed example for the purpose of this question, my intent is to match some number of a's, then an equal number of b's, plus one more b. Examine the two patterns exhibited in this snippet (also on ideone.com): var r1 = new Regex(@"(?xn) (?<A> a)+ (?<B-A> b)+ (?(A)(?!)) b "); var r2 = new Regex(@"(?xn) (?<...