Professional Documents
Culture Documents
1.0 While Loop Code - Java: Diagram 1-While Loop Code (Verify Input Has The Substring "Aba" or "Bab")
1.0 While Loop Code - Java: Diagram 1-While Loop Code (Verify Input Has The Substring "Aba" or "Bab")
The language used in this assignment in Java. The loop used in this assignment is
While Loop. Refer to Diagram 1 below, this code is to verify the input has the substring
“aba” or “bab”. At the beginning of the while loop the user requires to enter either ‘a’ or ‘b’
as an input. If the users’ input is in the correct format, the input will add to the value
(variable). Whenever users enter input that does not equal to ‘a’ or ‘b’, the input will be
rejected and print an “Incorrect format” message to the user. Every time the users enter an
input, this program will check whether the value (variable) contains the “aba” or “bab”. If the
checking result is true, it will change the flag(variable) to true. When the users have done the
input, they are required to enter ‘e’ as an input to end the while loop. The program will check
if the input has the substring “aba” or “bab” according to the flag. If the flag is true, the input
has the substring “aba” or “bab”, else the input has not substring “aba” or “bab”.
Diagram 1- While Loop Code (Verify input has the substring “aba” or “bab”)
2.0 NFA
2.1 NFA Diagram
Diagram 2- NFA
Quintuple
q0 q0
F q5
q {q0,q1,q2,q3,q4,q5}
∑ {a,b,e}
In the quintuple table, ‘q0’ represents the start state, the start state in this nfa is ‘q0’.
‘F’ represents the final state, the final state of this NFA is ‘q5’. ‘q’ is the finite set of states,
there are 6 states in this NFA which are {q0, q1, q2, q3, q4, q5}. ‘∑’ is the finite input
alphabet of the NFA diagram. There are 3 input alphabets in this NFA which are {a,b,e}.
State Transition Table
Q a b e
q0 q0,q1 q0,q3 Ø
q1 Ø q2 Ø
q2 q5 Ø Ø
q3 q4 Ø Ø
q4 Ø q5 Ø
q5 q5 q5 q5
By using a transition table, it helps to understand the flow of every input. In the
transition table, it clearly states the next destination of each state in the NFA.
3.0 DFA
3.1 DFA (without simplified)
δ a b e
q0 q0q1 q0q3 qØ
qØ qØ qØ qØ
q5 q5 q5 q5
Table 3 - Transition Table for Diagram 3 - DFA (without simplified)
The transition table above shows the input flows in DFA. It helps to indicate the next
destination of each input in the DFA.
Diagram 3 - DFA (without simplified)
The diagram above (Diagram 3 - DFA (without simplified)) shows the DFA converted
from NFA (Diagram 2 - NFA).
3.2 DFA (simplified)
Referring to the (Diagram 3 - DFA (without simplified)), the DFA is too complicated
and there are lots of redundant states. Therefore a simplified DFA (Diagram 4 - DFA
(simplified)) is generated, which removes the extra states.
In the Diagram3 - DFA(without simplified), it has more than one accept state which
are state q0q3q5, state q5, state q0q2q3q5, state q0q1q5, state q5. After the input is reached in
either one of these states, it will always go to the Accept state no matter if the input is 'a', 'b',
or 'e'. Therefore, they are totally redundant and they can merge to a single state which is
called q5. State q5 will become the only Accept state in this nfa.
4.0 Regular Expression
At the Diagram 5 -Regular Expression Step1(Add Extra State q6), the state q6 is
added to the diagram. This is because in Diagram 4 - DFA (simplified), the accept state q5
has a loop to itself. By adding a new state to separate the loop and the accept state will make
the elimination process more easier.
Step 2- Eliminate state qØ
In the Diagram 7 -Regular Expression Step3(Eliminate state q0q1), the state q0q1 is
eliminated. There are 6 remaining states in the diagram which are state q0, state q6, state q5,
state q0q1q4, state q0q3 and state q0q2q3.
Step 4- Eliminate state q0q2q3
In the Diagram 9 -Regular Expression Step5(Eliminate state q0q3), the state q0q3 is
eliminated. There are 4 remaining states in the diagram which are state q0,state q6, state q5
and state q0q1q4.
Step 6- Eliminate state q0q1q4
[aa*ba]+[(aa*bb+bb*a)aa*bb(b+aa*ba)]