Write regular expressions for the following languages over the alphabet - computation-theory

Write regular expressions for the following languages over the alphabet ∑ = {0, 1}:
Language of all strings which do not end with 11.
Language of all strings which do not contain the substring 01.
also
Draw Finite Automaton for each of the above described languages.

A regular expression for the first one is e + 0 + 1 + S* (00 + 01 + 10) where e is the empty string, S is the alphabet, * is the Kleene closure, + is union. This works because the language can be divided into strings of length less than two (e + 0 + 1) and strings of length at least two which do not end with 11 (this leaves endings 00, 01, and 10).
A regular expression for the second language is 1*0*. Note that we must put any 1s on the left of all 0s to avoid the substring 01, but we can have as many as either as we like.
A DFA for the first one would look like
q e q'
q0 0 q0
q0 1 q1
q1 0 q0
q1 1 q2
q2 0 q0
q2 1 q2
State q0 is initial, q0 and q1 are accepting. In state q0, you either just started or last saw a zero; your last symbol wasn't 1. In state q1, your last symbol was 1, but your second to last symbol wasn't. In state q2, you have seen two 1s in a row.
A DFA for the second would look like:
q e q'
q0 0 q1
q0 1 q0
q1 0 q1
q1 1 q2
q2 0 q2
q2 1 q2
q0 is the initial state, and q0 and q1 are accepting. q0 reads all the 0s, q1 reads all the 1s, and q2 happens if we see a 0 after we have seen a 1.

Related

Truth table of f(x1,x2,x3,x4) function from given two (4-1) multiplexers

Given two (4-1) multiplexers
How can I get the truth table of f(x1,x2,x3,x4) function??
A 4-1 multiplexer has the following general truth-table:
A1 A0 Y
0 0 I0
0 1 I1
1 0 I2
1 1 I3
The two control inputs A0 and A1 select which of the four inputs is switched through to the output.
To get your question solved, start with the left-hand multiplexer and write a truth-table for it.
In a second step write the overall truth-table by plugging in the intermediate signal values in the general table shown above.
The resulting truth-table has four input columns X1, X2, X3, X4.
There is one output column Y. Rather than using intermediate truth-tables you could figure out the output value for each of the 16 input combinations.

Logic gate whose truth is 1101

Is there a way to find a logic gate or how to make a more complex gate / bit operator from a wanted truth table
I wish to have this truth table :
0 0 = 1
0 1 = 1
1 0 = 0
1 1 = 1
There is a formal way of doing this if you cannot see it from the table.
Write the sum of products expression for the table.
If 0 0 = 1, then A'B' is in the expression.
If 0 1 = 1, then A'B is in the expression.
If 1 1 = 1, then AB is in the expression.
Then, F = A'B' + A'B + AB
Now, simplify the expression:
F = A'B' + A'B + AB
F = A'(B'+B) + AB Distribution Law
F = A'(1) + AB Complement Law
F = A' + AB Identity Law
F = A'(B + 1) + AB Annulment Law
F = A'B + A' + AB Distribution Law
F = (A' + A)B + A' Distribution Law
F = (1)B + A' Complement Law
F = B + A' Identity Law
It is not(A) or B.
You have to use a not gate and an or gate.
Alternative Solution
As pointed out in the comments, you can come up from the negated version of the function.
If 1 0 = 0, then F' = AB'
Simply, F = not(A and not(B)). If you distribute the not, then it will correspond to the same boolean expression as above.
Thanks to the comments for pointing out the shorter way.
The truth table gives exactly one possibility for 0. This is the same pattern as with the OR operator (also one possibility for 0), except that the first operand should then be flipped. So it follows that the first operand should be negated to get the desired result:
NOT(A) OR B
This is actually the implication operator: the expression is false when and only if the first operand is true and the second not:
A => B
If we represent an operator with the 4 outcome bits for operands 00, 01, 10, and 11, then this operator has code 1101 (read the output column from top to bottom). For all possible operations we have this:
Output
Expression
0000
FALSE
0001
A AND B
0010
A AND NOT(B)
0011
A
0100
NOT(A) AND B
0101
B
0110
A XOR B
0111
A OR B
1000
NOT(A OR B)
1001
A <=> B
1010
NOT B
1011
B => A
1100
NOT A
1101
A => B
1110
NOT(A AND B)
1111
TRUE
There are many alternative ways to write these expressions, but as a rule of thumb: if you need three 1-outputs, look for an OR operator where potentially one or both arguments need to be flipped. If you need one 1-output, do the same with an AND operator. If you have two 1-outputs, do the same with a XOR (or <=>) operator, unless it is a trivial case where one of the operands determines the result on its own.
if your input is X1 and X2 and you want to have a 1 as output, you can look at the combination which is easy to define: threre is only one 0 - and then invert it
the case for output 0: x1 and (not (x2) )
invert the solution (de Mogan): not (x1 and (not (x2) )) = not(x1) or x2
You need 1 x not and 1 x or
or you need 2 x not and 1 x and

Discussion about how to retrieve an i-th element in the j-th level of a binary tree algorithm

I am solving some problems from a site called codefights and the last one solved was about a binary tree in which are:
Consider a special family of Engineers and Doctors. This family has
the following rules:
Everybody has two children. The first child of an Engineer is an
Engineer and the second child is a Doctor. The first child of a Doctor
is a Doctor and the second child is an Engineer. All generations of
Doctors and Engineers start with an Engineer.
We can represent the situation using this diagram:
E
/ \
E D
/ \ / \
E D D E
/ \ / \ / \ / \
E D D E D E E D
Given the level and position of a person in the ancestor tree above,
find the profession of the person. Note: in this tree first child is
considered as left child, second - as right.
As there is some space and time restrictions, the solution can not be based on actually constructing the tree until the level required and check which element is in the position asked. So far so good. My proposed solution written in python was:
def findProfession(level, pos):
size = 2**(level-1)
shift = False
while size > 2:
if pos <= size/2:
size /= 2
else:
size /= 2
pos -= size
shift = not shift
if pos == 1 and shift == False:
return 'Engineer'
if pos == 1 and shift == True:
return 'Doctor'
if pos == 2 and shift == False:
return 'Doctor'
if pos == 2 and shift == True:
return 'Engineer'
As it solved the problem, I got access to the solutions of other used and I was astonished by this one:
def findProfession(level, pos):
return ['Engineer', 'Doctor'][bin(pos-1).count("1")%2]
Even more, I did not understand the logic behind it and so we arrived to this question. Someone could explain to me this algorithm?
Let's number the nodes of the tree in the following way:
1) the root has number 1
2) the first child of node x has number 2*x
3) the second child of node x has number 2*x+1
Now, notice that each time you go to the first child, the profession stays the same, and you add a 0 to the binary representation of the node.
And each time you go to the second child, the profession flips and you add a 1 to the binary representation.
Example: Let's find the profession of the 4th node in the 4th level (last level in the diagram you have in the question). First we start at the root with number 1, then we go to the first child with number 2 (10 binary). After that we go to the second child of 2 which is 5 (101 binary). Finally, we go to the second child of 5 which is 11 (1011 binary).
Notice that we started with only one bit equal to 1, then every 1 bit we added to the binary representation flipped the profession. So the number of times we flip a profession is equal to the (number of bits equal to 1) - 1. The parity of this amount decides the profession.
This leads us to the following solution:
X = number of bits equal to 1 in [ 2^(level-1) + pos - 1 ]
Y = (X-1) mod 2
if Y is 0 then the answer is "Engineer"
Otherwise the answer is "Doctor"
since 2^(level-1) is a power of 2, it has exactly one bit equal to 1, therefore you can write:
X = number of bits equal to 1 in [ pos-1 ]
Y = X mod 2
Which is equal to the solution you mentioned in the question.
This type of sequence is known as the Thue-Morse sequence. Using the same tree, here is a demonstration of why it gives the correct answer:
p is the 0-indexed position
b is the binary representation of p
c is the number of 1's in b
p0
E
b0
c0
/ \
p0 p1
E D
b0 b1
c0 c1
/ \ / \
p0 p1 p2 p3
E D D E
b0 b1 b10 b11
c0 c1 c1 c2
/ \ / \ / \ / \
p0 p1 p2 p3 p4 p5 p6 p7
E D D E D E E D
b0 b1 b10 b11 b100 b101 b110 b111
c0 c1 c1 c2 c1 c2 c2 c3
c is always even for Engineer and odd for Doctor. Therefore:
index = bin(pos-1).count('1') % 2
return ['Engineer', 'Doctor'][index]

In a JK Binary Counter from 0 to 9, why is the NAND gate connected to the second and fourth J-K flip flop and not the first and fourth?

In a binary counter design using 4 J-K flip-flops, that counts from 0 to 9, the flip flops are reset when the output from the 2nd flip flop NAND the 4th flipflop equals to 0. Since binary 9 is 1001, why is the NAND connected to these 2 outputs and not the first and fourth since it's the first and fourth bits that are 1s.
An image of the circuit: http://hyperphysics.phy-astr.gsu.edu/hbase/electronic/bincount.html
(second one).
The RST input on these flip-flops is interpreted asynchronously (independently of CLK). If you trigger it on binary 9 on the output, the counter will be reset to 0 immediately. To have a full clock cycle with a 9 on the output, you trigger the reset on 10 (i.e. 1010).

Why does a 4 bit adder/subtractor implement its overflow detection by looking at BOTH of the last two carry-outs?

This is the diagram we were given for class:
Why wouldn't you just use C4 in this image? If C4 is 1, then the last addition resulted in an overflow, which is what we're wondering. Why do we need to look at C3?
Overflow flag indicates an overflow condition for a signed operation.
Some points to remember in a signed operation:
MSB is always reserved to indicate sign of the number
Negative numbers are represented in 2's complement
An overflow results in invalid operation
Two's complement overflow rules:
If the sum of two positive numbers yields a negative result, the sum has overflowed.
If the sum of two negative numbers yields a positive result, the sum has overflowed.
Otherwise, the sum has not overflowed.
For Example:
**Ex1:**
0111 (carry)
0101 ( 5)
+ 0011 ( 3)
==================
1000 ( 8) ;invalid (V=1) (C3=1) (C4=0)
**Ex2:**
1011 (carry)
1001 (-7)
+ 1011 (−5)
==================
0100 ( 4) ;invalid (V=1) (C3=0) (C4=1)
**Ex3:**
1110 (carry)
0111 ( 7)
+ 1110 (−2)
==================
0101 ( 5) ;valid (V=0) (C3=1) (C4=1)
In a signed operation if the two leftmost carry bits (the ones on the far left of the top row in these examples) are both 1s or both 0s, the result is valid; if the left two carry bits are "1 0" or "0 1", a sign overflow has occurred. Conveniently, an XOR operation on these two bits can quickly determine if an overflow condition exists. (Ref:Two's complement)
Overflow vs Carry: Overflow can be considered as a two's complement form of a Carry. In a signed operation overflow flag is monitored and carry flag is ignored. Similarly in an unsigned operation carry flag is monitored and overflow flag is ignored.
Overflow for signed numbers occurs when the carry-in into the most significant bit is not equal to the carry out.
For example, working with 8 bits, 65 + 64 = 129 actually results in a overflow. This is because this is 1000 0001 in binary which is also -127 in 2's complement. If you work through this example, you can see that it is a result of the carry out not equalling the carry in.
It is possible to have a correct computation even when the carry flag is high.
Consider
1000 1000 = -120
+ 1111 1111 = -1
=(1) 10000111 = -121
There is a carry out of 1, but there has been no overflow.
I would like the give a more general answer to this question for any positive natural number of bits.
Lets call the last Carry output C1, the second to last Carry output C0, the sum sign output S0 and the signbits of A and B respectively A0 and B0.
Then the following holds:
C1 = A0 + B0 + C0
S0 = A0*B0 + A0*C0 + B0*C0
Lets now walk through the posibilities.
If C1 == 1 there are two possibilities:
if C0 == 0: A0 and B0
must both have been 1 (and thus both A en B must be negative). This
means S0 has to be 0 meaning the solution was positive while A en B
were negative => overflow
if C0 == 1: either
A en B have opposite signs, so overflow is not possible. => no overflow
A0 en B0 are both 1 (and thus A en B
must both be negative). This means S0 has to be 1 meaning the solution was negative => no overflow
If C1 == 0 there are two possibilities:
if C0 == 0: either
A0 en B0 are both 0 (and thus A en B
must both be positive). This means S0 has to be 0 meaning the solution was positive => no overflow
A en B have opposite signs => no overflow
if C0 == 1: A0 en B0 must both be 0 (and thus A en B
must both be positive) This
means S0 has to be 1 meaning the solution was negative while A en B
were positive => overflow
Hope that helps someone out there.

Resources