Algorithm in finding combination group of number - algorithm

I'm currently stuck on a part of an application I am working on. I don't want to copy all the code and paste it here but let me go directly straight to the point with a simple example:
Suppose I have a string "abcdefg", I am trying to find an algorithm that would get all the possible grouping without exchanging the characters, for example:
abcdeg
a, b, c, d, e, f, g
ab, c, d, e, f, g
..
..
abc, def, g
..
ab, cd, efg
..
and so on...
I think the example is pretty much the point. Can anyone provide me a pseudo-code? I understand Java, C, and C++ as well, so maybe a code snippet on those language is better, but if not pseudo-code is fine and I'll try to implement it. Thanks in advance.

It's surprisingly simple. Lop off the first letter and associate a 0 or a 1 with the remaining letters. A 1 means place a comma just before the letter. A 0 means don't.
E.g. 001100 corresponds to abc,d,efg.
The notation I'm using is a simple map of a number increasing from zero expressed in binary.
So three things, (i) count integers, (ii) convert to binary, (iii) use that binary as the comma positioning rule.
The stopping condition is obvious.

Related

How to make a maze in Prolog?

For a project, I have to write a basic maze in Prolog. The only problem is that I do not know how to write a KB that represents the maze in the below picture.
This is what I currently have, but I should be able to find pats from letter to letter.
% size of maze, including barriers.
mazeSize(7,7)
barrier(1,6).
barrier(2,2).
barrier(2,3).
barrier(3,2).
barrier(3,6).
barrier(4,1).
barrier(5,4).
barrier(5,6).
barrier(6,1).
barrier(7,4).
And this is what it should look like:
Hopefully someone can help me! Thanks in advance!
Your approach is reasonable, but you would also need facts something like node_name(1, 1, a) to map between node names and coordinates. Also, a 7x7 maze will clearly not correspond to the shown 4x4 maze.
A simpler solution that would not need coordinates at all would be one that only enumerates connected nodes, but not barriers:
connection(a, b).
connection(b, f).
and so on, but not connection(b, c) for example. Note that you will probably need to express that connections are two-way, so b and a are also connected.

Prove that a specific language is not semidecidable

I have to prove that the language L = {< M >: |L(M)| <= 2016} is NOT semi-decidable. Now I thought of doing it like this:
Take a random alfabet E. Now there are an infinite number of words in E. We can only conclude that |L(M)| <= 2016 by passing every word from E* as an input to M. But because there are an infinite number of words, this means that we would have to pass an input to M an infinite number of times. But this implies that Turing Machine that performs these checks ends up in an infinite loop, and thus never returns accepts nor rejects it's input. The language L is thus not semi-decidable.
But I think that this might not be formal enough? Mainly because I just assume that the Turing Machine that checks this language will let M run on every word from E*. Is this assumption valid, or should I be more formal in this?
Consider the complement of L: the language of Turing-machine encodings whose languages contain more than 2016 strings. We can easily prove this language is semi-decidable. Given this fact, assume that L is also semi-decidable. Because L and its complement are both semi-decidable, we have an effective procedure for deciding each: run the TMs for L and its complement alternately, and one will eventually list the input.
To see that the complement of L is semi-decidable, imagine a TM that executes one step of M on every possible input, alternating moves so that each string is eventually processed by arbitrarily many moves of the TM. The order in which it would visit the inputs would be like:
e,
e, 0,
e, 0, 1,
e, 0, 1, 00,
e, 0, 1, 00, 01, ...
Clearly, any string of input will eventually be processed every possible number of times. Our machine, which is simulating M running on all possible input strings, one step at a time, will eventually find 2017 strings have cause M to halt-accept, or it will run forever. If it finds 2017 strings that works, it halt-accepts.

Part of the theorem: "A language is Turing-recognizable if and only if some enumerator enumerates it"

I need help to understand this proof.
"First we show that if we have an enumerator E that enumerates a
language A, a TM M recognizes A. The TM M works in the following way.
PROOF M = "On input w:
1.Run E. Every time that E outputs a string, compare it with w.
If w ever appears in the output of E, accept."
Clearly, M accepts those strings that appear on E's list. "
If w doesn't appear in the output of E it doesn't appear in the E's list.
What is he triyng to say?
You have to prove both parts since it includes "if and only if" phrase.
First, you should show that if there exists an enumerator E which enumerates all strings in the language L, we can construct a recognizer for this language L.
This recognizer works with an input w ( a string ) and runs the E inside. E is an enumerator which generates all strings in L one by one. If the input string is equal to one of these generated strings then ACCEPT. If this language is infinite, then the recognizer may not halt, which is not a problem for a recognizer since it is not a decider.
Second part is, if L is Turing recognizable then there must be a Turing Machine M that recognizes L. An enumerator can be constructed as follows;
for k=1,2,3...
Run M on w1,w2,w3... in parallelized for k steps
if M accepts any of the wi then print wi on the printer.
The reason why we run them in parallelized with limited steps is the same reason why we prefer depth-limited search over depth-first search. It can go through an infinite dead path on the search graph.
Your theorem has two directions: if the "if" and the "only if". The proof is for the "if" direction.
Assuming you have an enumerator E for a language L, can you construct a Turing machine M that recognizes L? Yes, you can. Just define a Turing machine M that, on input string w, checks to see if w is ever in the output of E (which may be infinite). If it is, accept. If it isn't reject.
Since E is an enumerator for L, for any w in L, E eventually outputs w before halting (if it ever halts). Thus, M halts for every string in L. If w is not in L, either M never halts, or M rejects w.
Also, for M to be a decider, not just a recognizer for L, M must always halt.

Deterministic Finite Automata pattern

I'm trying to solve this problem using Deterministic Finite Automata :
inputs: {a,b}
conditions:
a. must have exactly 2 a
b. have more than 2 b
so a correct input should be like this abbba or bbbaa or babab
now my question is, "is there a pattern to solve this things?"
Yes there is a pattern. You can take each statement and deduct pre-states from them. Then you take the cross-product of those pre-states, which will comprise the final states. In this example:
a. will yield states: 0a, 1a, 2a, 2+a (you've seen 0 a, 1 a, 2 as or more than 2 as)
b. will yield states: 0b, 1b, 2b, 2+b (you've seen 0 b, 1 b, 2 bs or more than 2 bs)
The cross product of these states result in 4x4=16 states. You'll start from {0a,0b} states. The inputs can be 3 types: a, b or something else.
From that you should be able to go. Do you need more help?
(Are we solving homework?)
Always draw such things first.
Feel free to give states any meanings. What you need here is states like: q2: (1 b, 2 a's). Draw states like this, until the accept state and connect them with lines. The accept state is qx: 2 a's 3 b's.
After reaching the accept state, if input is "b" that line goes to itself, the accept state. If the input is "a", draw a new state, that will get into an endless loop and goes into itself no matter what the input is.
(are we helping for an exam here?)

Move cubes from position A to position B in Prolog

I have the following problem: there are some towers build with cubes on a table.
a
b d
c e
------------------- <- table
Now I want to move the cubes to another situation, like this one:
c e
a b d
-------------------
The Prolog program should print the steps to get to this situation, for example: move cube a onto the table, and so on. I have the first situation represented in Prolog:
clean(t). % t is the table, you can always put things there
clean(X) :- \+ on(_,X). % X is the top element, if there is nothing above it
on(a,b). % a is on b
on(b,c). % b on c
on(d,e). % d on e
on(c,t). % c on the table
on(e,t). % and e on the table
Now my problem is to find an solution to make Prolog print the steps to the new situation. My first problem is, how to tell Prolog how the new situation looks like. I tried it with some lists, but I didnĀ“t succeed until now.
Does anyone have an idea how to solve this?
If you encode the problem state by having facts of the on/2 predicate in your database, then you can only change that state by using assert and retract to change the database (and your predicates probably have to be declared dynamic as well). This is unwieldy. A nicer solution is to pass around the problem state as a parameter to your solving predicates (and maybe hide that structure behind a wrapper predicate). This works much better with the backtracking that you're probably going to need to implement the search for the solution.

Resources