Quite confused with this problem here regarding finite automata
Consider the finite automaton
M=({q0,q1}, {0}, {((q0,0),q1), ((q1,0),q0)}, q0, {q1}).
If the alphabet is {0,1}, what happens to the machine if the input string contains 1? Modify the FA so that the problem is resolved.
I tried to change the transition function ((q0, 0),q1)) into ((q0,1),q1)), which from my understanding would make the finite automata correct. Professor says its still wrong. Any help with this?
As of now your automata accepts strings that has an odd number of zeroes.
because 2 0's will take you back to the same start state again and q_1
is only on 0 away from q_0. But this dfa still remains incomplete , as there is no mapping for delta (q_0,1) , delta(q_1,1) So , in-case you encounter a 1, you need to clearly stay in the same place, i.e q0 or q1 since this does not change the number of 0's you have seen up until then
Related
Waring: This task was given by my professor who is 80 y/o and nobody understands what he sometimes wants, I do not expect more less standard approach to this problem, not just because the problem is difficult, but because my professor is old-school ex-ussr crazy guy ;) (he likes to make easy things complicated, just to explain why this is posted here)
This task is pure theory one, but I do not know how to formalize it with words
Problem:
9 bits binary code is given on input, we have to print "0" in output
if amount of bits with value "1" are two times less than amount of
bits with value "0", if this condition is false that we have to print
"1" in output.
What I proposed in my description is to introduce a counter, and then count bits that have value 1, then make an output based on this counter, but I was claimed to be an idiot and I was told that there's the way without the counter and I choose the hardest way. Does someone know the better way to determine what to output?
Thanks in advance, and sorry if description looks messy
As the TM reads the input bits, the state number must capture the number of bits seen, from 0 through to 9, so that we can recognize when we get to the end, and the number of 1 bits seen, with the relevant cases being 0, 1, 2, 3, and >=4.
There are less than 10*5=50 states required to encode all the relevant possibilities. When the machine enters one of the states indicating that 9 input bits have been seen, it writes a 0 if it indicates that 3 1s have been seen, or 1 otherwise, then stops.
Note that we didn't need to use the tape for storage -- the input language is regular so it can be decided with a finite state machine and unbounded storage is unnecessary.
While Matt is correct, you can generalize this problem to arbitrary input sizes using storage.
Go to the beginning of the tape
Move forward looking for an unmarked 1. Mark it.
If you can't find an unmarked 1, go to step 7.
Go back to the beginning of the tape
Look for an unmarked 0. Mark it.
If you can't find an unmarked 0, go to step 9.
Look for another unmarked 0. Mark it.
If you can't find an unmarked 0, go to step 9.
Go to step 1
Go to the beginning of your input.
Look for an unmarked 0.
If you don't find one, output 0. Halt.
Output 1. Halt.
This will work for any input size. Intuitively, we're looking for 2 0s for every 1 in the input, making sure there are twice as many 0 bits as 1 bits.
First of all I'm not sure how to name this problem. If anyone have better idea feel free to change it or tell that I do so.
Let's say I have two strings s1, s2 containing '+' and '-', which means positive and negative charge.
s1 is our begin input, s2 is pattern we want to get from s1. Our only operation is that we can change charge into opposite. But when we do so not only chosen charge is being changed but also charges next to one that we choose (left and right, besides first and last character since one of them do not have left and other right).
When it's not possible to get from s1 to s2.
How to find minimum amount of charge changes to transform from s1 to s2.
I believe the only one is when we have string length of 2 and in total amount '+'(or '-') is odd. For instance
in:"+-"
pattern:"++"
otherwise it's possible, but proof would be appreciated. As point 2 I have no idea, any hints are welcome.
Your intuition for when the problem is solvable isn't quite right. Half of all instances are insoluble whenever n = 2 (mod 3). One way to see this is by doing a few steps of reducing the appropriate system of equations (mod 2). Another way to see that there's some redundancy is to see that flipping the first, fourth, seventh, ... (n-1)st affects exactly the same set of characters as flipping the second, fifth, eight, ... nth.
As for an algorithm for solving these problems: There are two possible choices for the first flip. Once you've decided whether to flip around the first character, the value of the first character tells you whether you need to flip around the second character. Then the value of the second character tells you whether to flip around the third character. And so forth. So just try both possibilities. If neither one works, the problem's insoluble; if one works, report it; if both work, report the one that required fewer flips.
I trying to create a DFA that can recognize strings with alphabet {a,b,c} where a and c appear a even number of times and where b appears an uneven number of times.
I am wondering that this may only be expressed with other mathods such as turing machine or context-free languages.
You might find it fun to think of the solution.
The way I would go about constructing such a machine is as follows.
Make eight states. Each state represents a possible 3 tuple combination. The start state is a state representing the combination where all three are even. If a is the first character in the input, then you would to a state that represents an odd number of a's and even number of b's and c's. The accept state is where a and c are even, and b is odd.
This is possible using a DFA simply have a state for each of the combinations of an odd number of a's, b's and c's. So if you're in the state with even # of a's, odd # of b's and an even # of c's then you can accept. You can also define simple transitions for any of the other cases. So naively this can be done with 8 states.
I'm trying to solve this problem on SPOJ : http://www.spoj.pl/problems/EDIT/
I'm trying to get a decent recursive description of the algorithm, but I'm failing as my thoughts keep spinning in circles! Can you guys help me out with this one? I'll try to describe what approach I'm trying to solve this.
Basically I want to solve a problem of size j-i where i is the starting index and j is the ending index. Now, there should be two cases. If j-i is even then both the starting and the ending letters have to be the same case, and they have to be the opposite case when j-i is odd. I also want to reduce the problem of a lower size (j-i-1 or j-i-2), but I feel that if I know a solution to a smaller problem, then constructing a solution of a just bigger problem should also take into account the starting and ending letter cases of the smaller problem. This is exactly where I'm getting confused. Can you guys put my thoughts on the right track?
I think recursion is not the best way to go with this problem. It can be solved quite fast if we take a different approach!
Let us consider binary strings. Say an uppercase char is 1 and a lowercase one is 0. For example
AaAaB -> 10101
ABaa -> 1100
a -> 0
a "correct" alternating chain is either 10101010.. or 010101010..
We call the minimum number of substitutions required to change one string into the other the Hamming distance between the strings. What we have to find is the minimum Hamming distance between the input binary string and one of the two alternating chains of the same length.
It's not difficult: we XOR each string and then count the number of 1s. (link). For example, let's consider the following string: ABaa.
We convert it in binary:
ABaa -> 1100
We generate the only two alternating chains of length 4:
1010
0101
We XOR them with the input:
1100 XOR 1010 = 0101
1100 XOR 0101 = 1010
We count the 1s in the results and take the minimum. In this case, it's 2.
I coded this procedure in Java with some minor optimization (buffered I/O, no real need to generate the alternating chains) and it got accepted: (0.60 seconds one).
Given any string s of length n, there are only two possible "alternating chain".
This 2 variants can be defined sequentially by settings the first letter state (if first is upper then second is lower, third is upper...).
A simple linear algorithm would be to make 2 simple assumptions about the first letter:
First letter is UpperCase
First letter is LowerCase
For each assumption, run a simple edit distance algorithm and you are done.
You can do it recursively, but you'll need to pass and return a lot of state information between functions, which I think is not worthwhile when this problem can be solved by a simple loop.
As the others say, there are two possible "desired result" strings: one starts with an uppercase letter (let's call it result_U) and one starts with a lowercase letter (result_L). We want the smaller of EditDistance(input, result_U) and EditDistance(input, result_L).
Also observe that, to calculate EditDistance(input, result_U), we do not need to generate result_U, we just need to scan input 1 character at a time, and each character that is not the expected case will need 1 edit to make it the correct case, i.e. adds 1 to the edit distance. Ditto for EditDistance(input, result_L).
Also, we can combine the two loops so that we scan input only once. In fact, this can be done while reading each input string.
A naive approach would look like this:
Pseudocode:
EditDistance_U = 0
EditDistance_L = 0
Read a character
To arrive at result_U, does this character need editing?
Yes => EditDistance_U += 1
No => Do nothing
To arrive at result_L, does this character need editing?
Yes => EditDistance_L += 1
No => Do nothing
Loop until end of string
EditDistance = min(EditDistance_U, EditDistance_L)
There are obvious optimizations that can be done to the above also, but I'll leave it to you.
Hint 1: Do we really need 2 conditionals in the loop? How are they related to each other?
Hint 2: What is EditDistance_U + EditDistance_L?
I don't even know if a solution exists or not. Here is the problem in detail. You are a program that is accepting an infinitely long stream of characters (for simplicity you can assume characters are either 1 or 0). At any point, I can stop the stream (let's say after N characters were passed through) and ask you if the string received so far is a palindrome or not. How can you do this using less sub-linear space and/or time.
Yes. The answer is about two-thirds of the way down http://rjlipton.wordpress.com/2011/01/12/stringology-the-real-string-theory/
EDIT: Some people have asked me to summarize the result, in case the link dies. The link gives some details about a proof of the following theorem: There is a multi-tape Turing machine that can recognize initial non-trivial palindromes in real-time. (A summary, also provided by the article linked: Suppose the machine has read x1, x2, ..., xk of the input. Then it has only constant time to decide if x1, x2, ..., xk is a palindrome.)
A multitape Turing machine is just one with several side-by-side tapes that it can read and write to; in a very specific sense it is exactly equivalent to a standard Turing machine.
A real-time computation is one in which a Turing machine must read a character from input at least once every M steps (for some bounded constant M). It is readily seen that any real-time algorithm should be linear-time, then.
There is a paper on the proof which is around 10 pages which is available behind an institutional paywall here which I will not repost elsewhere. You can contact the author for a more detailed explanation if you'd like; I just had read this recently and realized it was more or less what you were looking for.
You could use a rolling hash, or more rolling hashes for accuracy. Incrementally compute the hash of the characters read so far, in the order they were read, and in reverse order of reading.
If your hash function is x*3^(k-1)+x*3^(k-2)+...+x*3^0 for example, where x is a character you read, this is how you'd do it:
hLeftRight = 0
hRightLeft = 0
k = 0
repeat until there are numbers in the stream
x = stream.Get()
hLeftRight = 3*hLeftRight + x.Value
hRightLeft = hRightLeft + 3^k*x.Value
if (x.QueryPalindrome = true)
yield hLeftRight == hRightLeft
k = k + 1
Obviously you'd have to calculate the hashes modulo something, probably a prime or a power of two. And of course, this could lead to false positives.
Round 2
As I see it, with each new character, there are three cases:
Character breaks potential symmetry, for example, aab -> aabc
Character extends the middle, for example aab -> aabb
Character continues symmetry, for example aab->aaba
Assume you have a pointer that tracks down the string and points to the last character that continued a potential palindrome.
(I am going to use parenthesis to indicate a pointed at character)
Lets say you are starting with aa(b) and get an:
'a' (case 3), you move the pointer to
the left and check if it's an 'a' (it
is). You now have a(a)b.
'c' (case 1), you are not expecting a 'c', in this case you start back at the beginning and you now have aab(c).
The really tricky case is 2, because somehow you have to know that the character you just got isn't affecting symmetry, it is just extending the middle. For this, you have to hold an additional pointer that tracks where the plateau's (middle's) edge lies. For example, you have (b)baabb and you just got another 'b', in this case you have to know to reset the pointer to the base of the middle plateau here: bbaa(b)bb. Since we are going for constant time, you have to hold a pointer here to begin with (you can't afford the time to search for the plateau's edge). Now if you get another 'b', you know that you are still on the edge of that plateau and you keep the pointer where it is, so bbaa(b)bb -> bbaa(b)bbb. Now, if you get an 'a', you know that the 'b's are not part of the extended middle and you reset both pointers (The tracking pointer and the edge pointer) so you now have bbaabbbb((a)).
With these three cases, I think all bases are covered. If you ever want to check if the current string is a palindrome, check if the first pointer (not the plateau's edge pointer) is at index 0.
This might help you:
http://arxiv.org/pdf/1308.3466v1.pdf
If you store the last $k$ many input symbols you can easily find palindromes up to a length of $k$.
If you use the algorithms of the paper you can find the midpoints of palindromes and an length estimate of its length.