0% found this document useful (0 votes)
84 views

COMPT Assignment v2

This document contains: 1) The design of a NFA with 10 states and transition table to recognize the regular expression "while(αα*<5)" 2) The conversion of the NFA to an equivalent DFA 3) The generation of the regular expression by removing states from the DFA one by one until only the initial and final states remain 4) A code snippet using a while loop to iterate a function if the input is below 5 5) Analysis of the best-case and worst-case time complexities of the code snippet 6) Discussion on how adding another loop would increase the complexity from O(n) to O(n2) 7) Explanations of the

Uploaded by

pmadoh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

COMPT Assignment v2

This document contains: 1) The design of a NFA with 10 states and transition table to recognize the regular expression "while(αα*<5)" 2) The conversion of the NFA to an equivalent DFA 3) The generation of the regular expression by removing states from the DFA one by one until only the initial and final states remain 4) A code snippet using a while loop to iterate a function if the input is below 5 5) Analysis of the best-case and worst-case time complexities of the code snippet 6) Discussion on how adding another loop would increase the complexity from O(n) to O(n2) 7) Explanations of the

Uploaded by

pmadoh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Name: Low Le Hao

TP number:
Module Code: CT113-3-2-COMT
Lecturer:
Contents
Part A ...................................................................................................................................................... 3
i) Design a Non-deterministic Finite Automata (NFA).................................................................. 3
ii) Quintuple and Transition Table ................................................................................................. 3
iii) Convert NFA to DFA................................................................................................................ 5
iv) Generate Regular Expression ................................................................................................. 6
Part B .................................................................................................................................................... 12
i) Code snippet ............................................................................................................................. 12
ii) Best-Case & Worst-Case Scenario............................................................................................ 13
iii) New Complexity ................................................................................................................... 13
iv) Computability ....................................................................................................................... 14
v) Decidability ............................................................................................................................... 14
References ............................................................................................................................................ 14
Part A
i) Design a Non-deterministic Finite Automata (NFA)

ii) Quintuple and Transition Table

NFA Quintuple:

Q = {A, B, C, D, E, F, G, H, I, J, K}

∑ = {w, h, i, l, e, (, α, <, 5, ) }

First State = {A}

Final State = {K}


Transition Table

δ w h i l e ( α < 5 )
A B Ø Ø Ø Ø Ø Ø Ø Ø Ø
B Ø C Ø Ø Ø Ø Ø Ø Ø Ø
C Ø Ø D Ø Ø Ø Ø Ø Ø Ø
D Ø Ø Ø E Ø Ø Ø Ø Ø Ø
E Ø Ø Ø Ø F Ø Ø Ø Ø Ø
F Ø Ø Ø Ø Ø G Ø Ø Ø Ø
G Ø Ø Ø Ø Ø Ø H Ø Ø Ø
H Ø Ø Ø Ø Ø Ø H I Ø Ø
I Ø Ø Ø Ø Ø Ø Ø Ø J Ø
J Ø Ø Ø Ø Ø Ø Ø Ø Ø K
K Ø Ø Ø Ø Ø Ø Ø Ø Ø Ø
iii) Convert NFA to DFA

Diagram: DFA Graph


iv) Generate Regular Expression
Generating regular expression for DFA, the reject state is removed because regular
expression does not accept rejection state. The graph below is DFA graph without rejection
state.

Diagram: DFA graph that removed rejected state

Diagram: DFA after renamed initial state

State A is the initial state in the DFA graph above, therefore state A is renamed to q0, which
represents the new initial state. There is only one initial state and without any incoming state,
so one new initial state is enough.
Diagram: DFA after renamed final state

Final state also needed to be identified, there is also only one final state and no other outgoing
state, so one new renamed final state is enough.

Remove state one by one to generate regular expression which will only left initial and final
state.

Step 1: Remove state B

Diagram: DFA that removed state B


Step 2: Remove state C

Diagram: DFA with removed state C

Step 3: Remove state D

Diagram: DFA with removed state D


Step 4: Remove state E

Diagram: DFA with removed state E

Step 5: Remove state F

Diagram: DFA with removed state F


Step 6: Remove state G

Diagram: DFA with removed state G

Step 7: Remove state H

Diagram: DFA with removed state H


Step 8: Remove state I

Diagram: DFA with removed state I

Step 9: Remove state J

Diagram: DFA with removed state J


Regular Expression is generated after removed the intermediate state and left the initial and
final state.

Diagram: DFA with removed all states (except initial and final state)

The regular expression is shown as the diagram above.

while(αα*<5)

Part B
i) Code snippet

The sample code function receives integer input and using while loop to iterate the function if
the input is below 5 and print out the output. If the input is below 5, it will be increasing
through the while loop is break by the input becoming above 5.
ii) Best-Case & Worst-Case Scenario

Time Complexity Time Complexity


(if (aa = 0)) (if (aa >= 5)
 5  1
 5  0
 5  0

O[15] O[1]

The worst-case scenario is when aa is equal to 0, which has time complexity of 15. For the
best-case scenario is when aa is greater than or equal to 5, which has time complexity of 1.
This is because if aa is equal to 0, it has to iterate the while loop for 5 time, and if aa is
greater than or equal to 5, it only run through the Boolean parameter in while loop and return
false to exit loop.

iii) New Complexity

O(n)

O(n2)
O(n2)

O(n)

The complexity of this function is significantly increase. Another while loop is inside the
while loop make the inner while loop time complexity increases to power of 2 because each
outer loop is iterated, the inner loop has to restart and loop again. Therefore, the whole
function time complexity is increase.
iv) Computability
Computability is the limit of computer to solve a problem. A function is defined as
computable if the function can be form by finite automata. Therefore, the code snippet state
above is computable, able to perform finite automata. The code above also passed the
computational standard as complete, mechanistic, and deterministic as the code given correct
output even in identical input.

v) Decidability
An algorithm or function can be defined as decidable or undecidable. Which decidable is the
machine accepts and halts on every input string, and undecidable is the machine do not have
any solution and turn into infinite loop. As the code snippet above is decidable, as given the
correct output with two different scenario which are worse case and best-case scenario and
receiving the correct output.

References
tutorialspoint, n.d. tutorialspoint. [Online]
Available at: https://www.tutorialspoint.com/java/java_while_loop.htm
[Accessed 7 March 2019].

You might also like