1^3^n for n>=1 turing machine - computation-theory

I want to make a turing machine which accept strings of 1's of length power of 3. 111, 111111111, 111111111111111111111111111, so on. But I am unable to make algorithm for this. So far I am able to make machine which accepts length of multiple of 3. Kindly help me

As with any programming task, your goal is to break the task into smaller problems you can solve, solve those, and put the pieces together to answer the harder problem. There are lots of ways to do this, generally, and you just have to find one that makes sense to you. Then, and only then, should you worry about getting a "better", "faster" program.
What makes a number a power of three?
the number one is a power of three (3^0)
if a number is a power of three, three times that number is a power of three (x = 3^k => 3x = 3^(k+1)).
We can "reverse" the direction of 2 above to give a recursive, rather than an inductive, definition: a number is a power of three if it is divisible by three and the number divided by three is a power of three: (3 | x && x / 3 = 3^k => x = 3^(k+1)).
This suggests a Turing machine that works as follows:
Check to see if the tape has a single one. If so, halt-accept.
Otherwise, divide by three, reset the tape head to the beginning, and start over.
How can we divide by three? Well, we can count a one and then erase two ones after it; provided that we end up erasing twice as many ones as we counted, we will have exactly one third the original number of ones. If we run out of ones to erase, however, we know the number isn't divisible by three, and we can halt-reject.
This is our design. Now is the time for implementation. We will have two phases: the first phase will check for the case of a single one; the other phase will divide by three and reset the tape head. When we divide, we will erase ones by introducing a new tape symbol, B, which we can distinguish from the blank cell #. This will be important so we can remember where our input begins and ends.
Q T | Q' T' D
----------+-----------------
// phase one: check for 3^0
----------+-----------------
q0 # | h_r # S // empty tape, not a power of three
q0 1 | q1 1 R // see the required one
q0 B | h_r B S // unreachable configuration; invalid tape
q1 # | h_a # L // saw a single one; 1 = 3^0, accept
q1 1 | q2 1 L // saw another one; must divide by 3
q1 B | q1 B R // ignore previously erased ones
----------+-----------------
// prepare for phase two
----------+-----------------
q2 # | q3 # R // reached beginning of tape
q2 1 | q2 1 L // ignore tape and move left
q2 B | q2 B L // ignore tape and move left
----------------------------
// phase two: divide by 3
----------------------------
q3 # | q6 # L // dividing completed successfully
q3 1 | q4 1 R // see the 1st one
q3 B | q3 B R // ignore previously erased ones
q4 # | h_r # S // dividing fails; missing 2nd one
q4 1 | q5 B R // see the 2nd one
q4 B | q4 B R // ignore previously erased ones
q5 # | h_r # S // dividing fails; missing 3rd one
q5 1 | q3 B R // see the 3rd one
q5 B | q5 B R // ignore previously erased one
----------+-----------------
// prepare for phase one
----------+-----------------
q6 # | q0 # R // reached beginning of tape
q6 1 | q6 1 L // ignore tape and move left
q6 B | q6 B L // ignore tape and move left
There might be some bugs in that but I think the idea should be basically sound.

Check the length of the input (111, 111111111,...) say strLen.
Check that the result of log(strLen) to base 3 is equal to an interger.
i.e,in code:
bool is_integer(float k)
{
return std::floor(k) == k;
}
if(is_integer(log(strLen)/log(3))){
//Then accept the string as it's length is a power of 3.
}

Related

A specific push down automaton for a language (PDA)

I'm wondering how could I design a pushdown automaton for this specific language.
I can't solve this..
L2 = { u ∈ {a, b}∗ : 3 ∗ |u|a = 2 ∗ |u|b + 1 }
So the number of 'a's multiplied by 3 is equals to number of 'b's multiplied by 2 and added 1.
The grammar corresponding to that language is something like:
S -> ab | ba |B
B -> abB1 | baB1 | aB1b | bB1a | B1ab | B1ba
B1 -> aabbbB1 | baabbB1 | [...] | aabbb | baabb | [...]
S generates the base case (basically strings with #a = 1 = #b) or B
B generates the base case + B1 (in every permutation)
B1 adds 2 'a' and 3 'b' to the base case (in fact if you keep adding this number of 'a' and 'b' the equation 3#a = 2#b + 1 will always be true!). I didn't finish writing B1, basically you need to add every permutation of 2 'a' and 3 'b'. I think you'll be able to do it on your own :)
When you're finished with the grammar, designing the PDA is simple. More info here.
3|u|a = 2|u|b + 1 <=> 3|u|a - 2|u|b = 1
The easiest way to design a PDA for this is to implement this equation directly.
For any string x, let f(x) = 3|x|a - 2|x|b. Then design a PDA such that, after processing any string x:
The stack depth is always equal to abs( floor( f(x)/3 ) );
The symbol on the top of the stack (if any), reflects the sign of floor( f(x)/3 ). You only need 2 kinds of stack symbols
The current state number = f(x) mod 3. Of course you only need 3 states.
From the state number and the symbol on top of the stack, you can detect when f(x) = 1, and at that condition the PDA accepts x as a string in the language.

Pushdown automata to accept the following language

I need to construct a pushdown automation for the following language: L = {a^n b^m | 2n>=m }
Can someone help me with it?
There are two approaches that come to mind:
try to write a PDA from scratch using the stack in a sensible way
write a CFG and then rely on the construction that shows PDAs accept the languages of all CFGs
To take approach 1, recognize that PDAs are like NFAs that can push symbols onto a stack and pop them off. If we want 2n >= m, that means we want the number of a's to be at least half the number of b's. That is, we want at least one a for every two b's. That means if we push all the a's on the stack, we need to read no more than two b's for every a on the stack. This suggests a PDA that works like this:
1. read all the a's, pushing into the stack
2. read b's, popping an a for every two b's you see
3. if you run out of stack but still have b's to process, reject
4. if you run out of input at the same time or sooner than you run out of stack, accept
In terms of states and transitions:
Q S E Q' S'
q0 Z a q0 aZ // these transitions read all the a's and
q0 a a q0 aa // push them onto the stack
q0 a b q1 a // these transitions read all the b's and
q1 a b q2 - // pop a's off the stack for every complete
q2 a b q1 a // pair of b's we see. if we run out of a's
// it will crash and reject
q0 Z - q3 Z // these transitions just let us guess at any
q0 a - q3 a // point that we have read all the input and
q1 Z - q3 Z // go to the accepting state. note that if
q1 a - q3 a // we guessed wrong the PDA will crash there
q2 Z - q3 Z // since we have no transitions defined.
q2 a - q3 a // crashing rejects the input.
Here, the accept condition is being in state q3 with no more stack.
To do the second option, you'd write a CFG like this:
S -> aSbb | aS | e
Then your PDA could do the following:
push S onto the stack
pop from the stack and push onto the stack one of the productions for S
if you pop a terminal off the stack, consume the nonterminal from input
if you pop a nonterminal off the stack, replace it with some production for that nonterminal
This nondeterministically generates every possible derivation according to the CFG. If the input is a string in the language, then one of these derivations will consume all the input and leave the stack empty, which is the halt/accept condition.
We need to construct a pushdown automation for the following language: L = {a^n b^m | 2n>=m }
The given condition is 2n >= m, that means we want the number of a's to be at least half the number of b's. That is, we want at least one a for every two b's. That means if we push all the a's on the stack, we need to read no more than two b's for every a on the stack. This suggests a PDA that works like this:
read all the a's, pushing into the stack
read b's, popping an a for every two b's you see
if you run out of stack but still have b's to process, reject
if you run out of input at the same time or sooner than you run out of stack, accept.
The PDA for the problem is as follows −
Transition functions
The transition functions are as follows −
Step 1: δ(q0, a, Z) = (q0, aZ)
Step 2: δ(q0, a, a) = (q0, aa)
Step 3: δ(q0, b, a) = (q1, a)
Step 4: δ(q1, b, a) = (q2, ε)
Step 5: δ(q2, b, a) = (q1, a)
Step 6: δ(q2,b, Z) = dead state.
Step 7: δ(q2,ε, Z) = (qf, Z)

Odd DFA for Language

Can somebody please help me draw a NFA that accepts this language:
{ w | the length of w is 6k + 1 for some k ≥ 0 }
I have been stuck on this problem for several hours now. I do not understand where the k comes into play and how it is used in the diagram...
{ w | the length of w is 6k + 1 for some k ≥ 0 }
We can use the Myhill-Nerode theorem to constructively produce a provably minimal DFA for this language. This is a useful exercise. First, a definition:
Two strings w and x are indistinguishable with respect to a language L iff: (1) for every string y such that wy is in L, xy is in L; (2) for every string z such that xz is in L, wz is in L.
The insight in Myhill-Nerode is that if two strings are indistinguishable w.r.t. a regular language, then a minimal DFA for that language will see to it that the machine ends up in the same state for either string. Indistinguishability is reflexive, symmetric and transitive so we can define equivalence classes on it. Those equivalence classes correspond directly to the set of states in the minimal DFA. Now, to find the equivalence classes for our language. We consider strings of increasing length and see for each one whether it's indistinguishable from any of the strings before it:
e, the empty string, has no strings before it. We need a state q0 to correspond to the equivalence class this string belongs to. The set of strings that can come after e to reach a string in L is L itself; also written c(c^6)*
c, any string of length one, has only e before it. These are not, however, indistinguishable; we can add e to c to get ce = c, a string in L, but we cannot add e to e to get a string in L, since e is not in L. We therefore need a new state q1 for the equivalence class to which c belongs. The set of strings that can come after c to reach a string in L is (c^6)*.
It turns out we need a new state q2 here; the set of strings that take cc to a string in L is ccccc(c^6)*. Show this.
It turns out we need a new state q3 here; the set of strings that take ccc to a string in L is cccc(c^6)*. Show this.
It turns out we need a new state q4 here; the set of strings that take cccc to a string in L is ccc(c^6)*. Show this.
It turns out we need a new state q5 here; the set of strings that take ccccc to a string in L is cc(c^6)*. Show this.
Consider the string cccccc. What strings take us to a string in L? Well, c does. So does c followed by any string of length 6. Interestingly, this is the same as L itself. And we already have an equivalence class for that: e could also be followed by any string in L to get a string in L. cccccc and e are indistinguishable. What's more: since all strings of length 6 are indistinguishable from shorter strings, we no longer need to keep checking longer strings. Our DFA is guaranteed to have one the states q0 - q5 we have already identified. What's more, the work we've done above defines the transitions we need in our DFA, the initial state and the accepting states as well:
The DFA will have a transition on symbol c from state q to state q' if x is a string in the equivalence class corresponding to q and xc is a string in the equivalence class corresponding to q';
The initial state will be the state corresponding to the equivalence class to which e, the empty string, belongs;
A state q is accepting if any string (hence all strings) belonging to the equivalence class corresponding to the language is in the language; alternatively, if the set of strings that take strings in the equivalence class to a string in L includes e, the empty string.
We may use the notes above to write the DFA in tabular form:
q x q'
-- -- --
q0 c q1 // e + c = c
q1 c q2 // c + c = cc
q2 c q3 // cc + c = ccc
q3 c q4 // ccc + c = cccc
q4 c q5 // cccc + c = ccccc
q5 c q0 // ccccc + c = cccccc ~ e
We have q0 as the initial state and the only accepting state is q1.
Here's a NFA which goes 6 states forward then if there is one more character it stops on the final state. Otherwise it loops back non-deterministcally to the start and past the final state.
(Start) S1 -> S2 -> S3 -> S5 -> S6 -> S7 (Final State) -> S8 - (loop forever)
^ |
^ v |_|
|________________________| (non deterministically)

Meaning of [->1] in system verilog property definition

I haven't been able to find the meaning of the [->] expression. I am familiar with using ranges as [a:b] or sequences such as EVENT1|->EVENT2, but not with the one I mentioned at the beginning.
The context is an assert property that ensures that clk1 is always toggling when reset is low.
#(clk2) disable iff(rst) !$stable(clk1)[->1];
Any idea on what [->] really means?
Thanks in advance!
[->N] means "non-consecutive exact repetition". a[->N] means N non-consecutive repetitions of a, with the match occurring exactly after the Nth repetition of a.
So, for example, a ##1 b[->2] ##1 c will match
_
a _______/ \_____________________
| _ _
b _________________/ \___/ \_____
| _
c _________________________/ \___
| |
| |
match |<----------------->|
but not
_
a _______/ \___________________________
_ _
b _________________/ \___/ \___________
_
c _______________________________/ \___
[=N] means "non-consecutive repetition". a[=N] means N non-consecutive repetitions of a, with the match occurring any time after the Nth repetition of a.
So, for example, a ##1 b[=2] ##1 c would match the second pattern, above.
There are basically 3 operators like this :
Consecutive Repetition [*n] -
(a ##1 b[*2] ##1 c) = (a ##1 b ##1 b ##1 c)
This means that, a should be asserted, after that b should be asserted on consecutive 2 clock ticks, following assertion of c in the next clock tick.
Goto Repetition [->n] -
(a ##1 b[->2] ##1 c) = (a ##1 ((!b[*0:$] ##1 b)[*2]) ##1 c)
This means that, a should be asserted, after that b should be asserted on 2 clock ticks, but not consecutively. And as soon as b is asserted on 2 clock ticks, on the next clock tick c should get asserted
Nonconsecutive Repetition [=n] -
(a ##1 b[=2] ##1 c) = (a ##1 ((!b[*0:$] ##1 b)[*2]) ##1 !b[*0:$] ##1 c)
This means that, a should be asserted, after that b should be asserted on 2 clock ticks, but not consecutively. And as soon as b is asserted on 2 clock ticks, c should get asserted, before b again gets asserted 3rd time.
Remember, difference between [->n] & [=n] operator, is related to the
last operand "c". In [->2], c should be asserted, on the next clock
tick, as soon as b is asserted twice (non consecutively). But in [=2],
c should be asserted, before b gets asserted 3rd time (non
consecutively)
There are basically 3 repetition operators available in system verilog as stated and explained in other answers.
The code
#(clk2) disable iff(rst) !$stable(clk1)[->1];
can be interpreted as follows:
'Disable' the assertion property if 'rst' is high. If not so, check if the 'clk1' is 'not' stable for '1' clk2 clock changes(either from high to low or low to high), not necessarily on successive clk2 clock changes.
which means that the assertion:
will get disabled(i.e neither pass nor fail) if rst is 1.
will pass if clk1 is not stable(remains in the state it is, i.e either high or low) for 1 clk2 clock change. But it is not necessary that clk1 has to be unstable on successive clk2 clock changes.
will fail otherwise.
It in the suggests that you want to check if clk1 is operating at a higher frequency than clk2.

stable matching

I'm reading a algorithm book in my lesure time. Here is a question I have my own answer but not quite sure. What's your opinion? Thanks!
Question:
There are 2 television network company, let as assume A and B, each are planning the TV progame schedule in n time slots of a day. Each of them are putting their n programs in those slots. While each program have a rate based on the popularity in the past year, and these rate are distinct to each other. The company win a slot when its show have a higher rate then its opponent's. Is there a schedule matching that A made a schedule S and B made a schedule T, and (S, T) is stable that neither network can unilaterally change its own schedule and win more time slots.
There is no stable matching, unless one station has all its programs contiguous in the ratings (ie. the other station has no program which is rated better than one program on the first station but worse than another on the first station).
Proof
Suppose a station can improve its score, and the result is a stable matching. But then the other station could improve its own score by reversing the rearrangement. So it wasn't a stable matching. Contradiction.
Therefore a stable matching can not be reached by a station improving its score.
Therefore a stable matching can't be made worse (for either station), because then the lower state could be improved to a stable matching (which I just showed was not allowed).
Therefore every program rearrangement of a stable matching must give equal scores to both stations.
The only program sets which can't have scores changed by rearrangement are the ones where one of the stations' programs are contiguous in the ratings. (Proof left to reader)
Solution in Haskell:
hasStable :: Ord a => [a] -> [a] -> Bool
hasStable x y =
score x y + score y x == 0
-- score is number of slots we win minus number of slots they win
-- in our best possible response schedule
score :: Ord a => [a] -> [a] -> Integer
score others mine =
scoreSorted (revSort others) (revSort mine)
where
-- revSort is sort from biggest to smallest
revSort = reverse . sort
scoreSorted :: Ord a => [a] -> [a] -> Integer
scoreSorted (o:os) (m:ms)
| m > o =
-- our best show is better then theirs
-- we use it to beat theirs and move on
1 + score os ms
| otherwise =
-- their best show is better then ours
-- we match it with our worst show
-- so we could make use of our big guns
-1 + score os (m : ms)
scoreSorted _ _ = 0 -- there are no shows left
> hasStable [5,3] [4,2]
False
> hasStable [5,2] [3,4]
True
My own answer is no stable matching. Supposing there are only 2 time slots.
A have program p1(5.0) p2(3.0);
B have program p3(4.0) p4(2.0);
The schedule of A includes:
S1: p1, p2
S2: p2, p1
The schedule of B includes:
T1: p3, p4
T2: p4, p3
So the matching include:
(S1, T1)(S1, T2)(S2, T1)(S2, T2)
while the results are
(S1, T1) - (p1, p3)(p2, p4) 2:0 - not stable, becuase B can change its schedule to T2 and the result is : (S1, T2) - (p1, p4)(p2, p3) 1:0
Vise versa and so does the other matching.
Let each TV channel having 2 shows.
TV-1:
Show1 has a rating of 20 pts.
show2 has a rating of 40 pts.
TV-2:
Show1 has a rating of 30 pts.
Show2 has a rating of 50 pts.
Then it clearly shows the matching is unstable.

Resources