I am working on an end of chapter question regarding how many different Moore machines there are with n states. Where n = number of states, m = number of input letters, and q = number of output characters, is it correct that there are n*q^m possible machines? My reasoning is that for each state, each input has the possibility to lead to one of the give output characters.
A Moore machine consists of:
set of states S (n)
start state s0
input alphabet Sigma (m)
output alphabet A (q)
transition function (S x Sigma -> S)
output function (S -> A)
The number of states and input/output-characters is given.
For the start state there are n possibilities.
For the transition function, there are |S| ^ (|S| * |Sigma|) = n^(n*m) different variants.
Finally, there are |A| ^ |S| = q^n output functions
This yields in total n^(n*m+1) * q^n different Moore machines.
Related
I have written an algorithm that given a list of words, must check each unique combination of four words in that list of words (regardless of order).
The number of combinations to be checked, x, can be calculated using the binomial coefficient i.e. x = n!/(r!(n-r)!) where n is the total number of words in the list and r is the number of words in each combination, which in my case is always 4, therefore the function is x = n!/(4!(n-4)!) = n!/(24(n-4)!). Therefore as the number of total words, n, increases the number of combinations to be checked, x, therefore increases factorially right?
What has thrown me is that WolframAlpha was able to rewrite this function as x = (n^4)/24 − (n^3)/4 + (11.n^2)/24 − n/4, so now it would appear to grow polynomially as n grows? So which is it?!
Here is a graph to visualise the growth of the function (the letter x is switched to an l)
For a fixed value of r, this function is O(n^r). In your case, r = 4, it is O(n^4). This is because most of the terms in the numerator are canceled out by the denominator:
n!/(4!(n-4)!)
= n(n-1)(n-2)(n-3)(n-4)(n-5)(n-6)...(3)(2)(1)
-------------------------------------------
4!(n-4)(n-5)(n-6)...(3)(2)(1)
= n(n-1)(n-2)(n-3)
----------------
4!
This is a 4th degree polynomial in n.
I have this number x and i wanted to find all numbers which are relatively prime to it.
my code so far:
For i = 1 To x-1
if [number n is relatively prime to x] Then
ListBox1.Items.Add(x)
End If
Next
Thanks in advance
Two numbers are relatively prime if their greatest common divisor is 1. VB doesn't have the GCD function built-in, but the algorithm is simple enough (and about 2300 years old!):
function gcd(m, n)
while n > 0
m, n = n, m%n
return m
Note that m and n are assigned simultaneously. I'll leave it to you to complete the VB implementation. You might be interested in googling for the totient of a number and the list of its totatives, which is what you are calculating.
Assuming you want only numbers that are smaller than x, which are coprime with it - you could also take a generative approach, running a special kind of a sieve. When the multiples of each prime are generated, you'd see if that sequence "hits" your upper limit x or misses it, and mark all the numbers in it as non-coprimes if it does hit x.
Or in "pseudocode" (with Haskell syntax :) ),
coprimes n = go( [1..n-1], [2..n-1]) where
go( xs, [] ) = xs -- ' no more numbers to sieve - return xs
go( xs, p:ks ) = -- ' p is first in candidates, ks is the rest
let ms = [p, 2*p .. n-1] -- ' p's multiples
in
go( if ( (mod n p) == 0 ) -- ' is n a multiple of p ?
then (xs\\ms) -- ' yes: remove p's multiples
else xs, -- ' no: possible coprimes
ks\\ms ) -- ' candidates to sieve
Haskell's set difference \\ is very inefficient with unordered list representation of sets, but you would naturally encode this efficiently, on top of mutable arrays, in VB.
What is the minimum number of states needed in a DFA to accept the strings having '1' as 5th symbol from right? Strings are defined over the alphabet {0,1}.
The Myhill-Nerode theorem is a useful tool for solving these sorts of problems.
The idea is to build up a set of equivalence classes of strings, using the idea of "distinguishing extensions". Consider two strings x and y. If there exists a string z
such that exactly one of xz and yz is in the language, then z is a distinguishing extension,
and x and y must belong to different equivalence classes. Each equivalence class maps to a different state in the minimal DFA.
For the language you've described, let x and y be any pair of different 5-character strings
over {0,1}. If they differ at position n (counting from the right, starting at 1), then any string z with length 5-n will be a distinguishing extension: if x has a 0 at position n,
and y has a 1 at position n, then xz is rejected and yz is accepted. This gives 25 = 32
equivalence classes.
If s is a string with length k < 5 characters, it belongs to the same equivalence class
as 0(5-k)s (i.e. add 0-padding to the left until it's 5 characters long).
If s is a string with length k > 5 characters, its equivalence class is determined by its final 5 characters.
Therefore, all strings over {0,1} fall into one of the 32 equivalence classes described above, and by the Myhill-Nerode theorem, the minimal DFA for this language has 32 states.
No of state will be 2^n where n is nth symbol from right
So 2^5=32 will be no of states
Let L = {a^f(m) | m >= 1 } where f: Z^+ -> Z^+ is monotone increasing and complies that for all element n in Z^+ there is an m belonging to Z^+ such that f(m+1) - f(m) >= n.
Is it possible to prove that L is a regular language?
Let f(x) = 2^x. For any positive n, f(n+1) - f(n) >= n.
L = {a^f(m)} is not regular. Consider the strings a^(2^x + 1). After an FA processes such a string, the smallest string which leads to an accepting state is a^(2^x - 1), having length 2^x - 1. Therefore, a separate state will be needed for every value of x. Since there are infinitely many values of x (positive integers), no FA exists to recognize L; ergo, L is not a regular language.
Definition of a language L with alphabet { a } is given as following
L = { ank | k > 0 ; and n is a positive integer constant }
What is the number of states needed in a DFA to recognize L?
In my opinion it should be k+1 but I am not sure.
The language L can be recognized by a DFA with n+1 states.
Observe that the length of any string in L is congruent to 0 mod n.
Label n of the states with integers 0, 1, 2, ... n-1, representing each possible remainder. An additional state, S, is the start state. S has a single transition, to state 1. If the machine is currently in state i, on input it moves to state (i+1) mod n. State 0 is
the only accepting state. (If the empty string were part of L, we could eliminate S and make state 0 the start state).
Suppose there were a DFA with fewer than n+1 states that still recognized L. Consider the sequence of states S0, S1, ... Sn encountered while processing the string an. Sn must be an accepting state, since an is in L. But since there are fewer than n+1 distinct states in this DFA, by the pigeonhole principle there must have been some state that was visited at least twice. Removing that loop gives another path (and another accepted string), with length < n, from S0 to Sn. But L contains no strings shorter than n, contradicting our assumption. Therefore no DFA with fewer than n+1 states recognizes L.