backtracking

Given n and k, return the kth permutation sequence

送分小仙女□ 提交于 2019-11-27 10:10:16
问题 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 sequence. For example, given n = 3, k = 4, ans = "231". There are multiple solutions out there. But all of them uses either factorial or there complexity is larger than O(n) such as O(n!). If you use factorial and find the number at the position by

Lights out game algorithm

北战南征 提交于 2019-11-27 08:59:32
It's a homework. I have to design and lights out game using backtracking description is below. The game consists of a 5-by-5 grid of lights; when the game starts, a set of these lights (random, or one of a set of stored puzzle patterns) are switched on. Pressing one of the lights will toggle it, and the four lights adjacent to it, on and off. (Diagonal neighbours are not affected.) The game provides a puzzle: given some initial configuration where some lights are on and some are off, the goal is to switch all the lights off, preferably in as few button presses as possible. My approach is go

Can you write between/3 in pure prolog?

爱⌒轻易说出口 提交于 2019-11-27 05:34:19
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 may help me with my task. I looked for an implementation in an existing Prolog system, but the implementation of between/3 for GNU Prolog is a C function, and the trick there is that it calls another C function "Pl_Create_Choice_Point" which allows it to produce additional values on backtracking. bet(N, M, K) :- N =< M, K = N. bet(N, M, K) :- N < M,

Collect all “minimum” solutions from a predicate

霸气de小男生 提交于 2019-11-27 05:28:16
Given the following facts in a database: foo(a, 3). foo(b, 2). foo(c, 4). foo(d, 3). foo(e, 2). foo(f, 6). foo(g, 3). foo(h, 2). I want to collect all first arguments that have the smallest second argument, plus the value of the second argument. First try: find_min_1(Min, As) :- setof(B-A, foo(A, B), [Min-_|_]), findall(A, foo(A, Min), As). ?- find_min_1(Min, As). Min = 2, As = [b, e, h]. Instead of setof/3 , I could use aggregate/3 : find_min_2(Min, As) :- aggregate(min(B), A^foo(A, B), Min), findall(A, foo(A, Min), As). ?- find_min_2(Min, As). Min = 2, As = [b, e, h]. NB This only gives the

How to generate all the permutations of a multiset?

给你一囗甜甜゛ 提交于 2019-11-27 04:53:17
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? ruggero Generating all the possible permutations and then discarding the repeated ones is highly inefficient. Various algorithms exist to directly generate the permutations of a multiset in lexicographical order or other kind of ordering. Takaoka's algorithm is a good example, but probably that of Aaron Williams is better http://webhome.csc.uvic.ca/~haron/CoolMulti.pdf moreover, it has been implemented in the R package ''multicool''. Btw, if you just want the

algorithm to find longest non-overlapping sequences

爱⌒轻易说出口 提交于 2019-11-27 01:41:29
问题 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 , for example (5,7) is equivalent to the sequence (5,6,7,8,9,10,11) - a list of 7 elements starting with 5. One can assume that the tuples are sorted by the start element. The output should return a non-overlapping combination of tuples that represent

generate all partitions of a set [closed]

╄→尐↘猪︶ㄣ 提交于 2019-11-26 22:42:43
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 (they can't share the same elements). For example. A = {1, 2,... n} We have the partitions: {1, 2, 3} {1, 2} {3} {1, 3} {2} {2, 3} {1} {1} {2} {3} These theoretical concepts are presented in my algorithms textbook (by the way, this subchapter is part of the "Backtracking" chapter). I am supposed to find an algorithm to generate all the partitions of

Regex Pattern Catastrophic backtracking

血红的双手。 提交于 2019-11-26 21:38:42
问题 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 is restarted. Could any one suggest a better way to rewrite this pattern or a tool which would help me to do so? Pattern: ^\[(([\p{N}]*\]\,\[[\p{N}]*)*|[\p{N}]*)\]$ Values working: [1234567],[89023432],[124534543],[4564362],[1234543],[12234567],

Prolog GNU - Univ operator? Explanation of it

做~自己de王妃 提交于 2019-11-26 20:40:17
问题 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 looks to see if another predicate is true. I don't understand what the ".." does. How would you rewrite this without the univ operator? 回答1: Univ ( =.. ) breaks up a term into a list of constituents, or constructs a term from such a list. Try: ?- f(x

Simple AlphaNumeric Regex (single spacing) without Catastrophic Backtracking

[亡魂溺海] 提交于 2019-11-26 19:12:46
I have the following REGEX expression (that works) to allow Alpha-Numeric (as well as ' and - ) and no double spacing: ^([a-zA-Z0-9'-]+\s?)*$ Due to the nested grouping, this allows Catastrophic Backtracking to happen - which is bad! How can I simplify this expression to avoid Catastrophic Backtracking?? (Ideally this wouldn't allow white-space in first and last characters either) Explanation Nested group doesn't automatically causes catastrophic backtracking. In your case, it is because your regex degenerates to the classical example of catastrophic backtracking (a*)* . Since \s in optional