0% found this document useful (0 votes)
4 views25 pages

Batch-15 Sec-J Toc

The document discusses the theory of computation, focusing on Deterministic Finite Automata (DFA) and Non-Deterministic Finite Automata (NFA), including their definitions, differences, and conversion methods. It provides a problem statement to construct an NFA, convert it to a DFA, and minimize the DFA, along with a code implementation for these processes. The conclusion emphasizes the importance of the minimized DFA in recognizing specific binary patterns for applications in various computational fields.

Uploaded by

Saketh Ram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views25 pages

Batch-15 Sec-J Toc

The document discusses the theory of computation, focusing on Deterministic Finite Automata (DFA) and Non-Deterministic Finite Automata (NFA), including their definitions, differences, and conversion methods. It provides a problem statement to construct an NFA, convert it to a DFA, and minimize the DFA, along with a code implementation for these processes. The conclusion emphasizes the importance of the minimized DFA in recognizing specific binary patterns for applications in various computational fields.

Uploaded by

Saketh Ram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

THEORY OF

COMPUTATION
SUBMITTED BY: BATCH-15
231FA04438
231FA04543
231FA04577
SUBMITTED TO:
231FA04G12
MR.SIMHADRI CHINNA
GOPI
INTRODUCTION:
ABOUT DFA :
• WHAT IS DFA?
• DFA stands for Deterministic Finite Automaton, a mathematical model that
accepts or rejects strings of characters. It's a type of finite-state machine,
which means it has a limited number of possible states.
• It has 5 tuples and they are:
• Q-finite set of states
• Σ -input symbols
• q0-initial state
• F-final state
• δ: Q × Σ → Q
INTRODUCTION:
ABOUT NFA :

• WHAT IS NFA?
• NFA could refer to Non-Deterministic Finite Automata, Net Foreign Assets, National
Food Authority, National Futures Association, or No Further Action.
• It has 5 tuples as DFA but differ at transition diagram and they are:
• Q-finite set of states
• Σ -input symbols
• q0-initial state
• F-final state
• δ: Q × Σ →
DIFFERENCE BETWEEN DFA AND NFA :

DFA: NFA :
• DFA stands for Deterministic Finite • NFA stands for Nondeterministic
Automata. Finite Automata.
• For each symbolic representation of the • No need to specify how does the NFA
alphabet, there is only one state
react according to some symbol.
transition in DFA.
• NFA can use Empty String transition.
• DFA cannot use Empty String transition.
• All DFA are NFA.
• Not all NFA are DFA.
PROBLEM STATEMENT:

• a) Construct an NFA that accepts the set of all


strings over {0,1} that start with 0 or 1 and
end with 10 or 01.

• b) Describe the procedure of converting NFA


to DFA with a suitable example.

• c) Design the minimized DFA for the below


DFA with a reduced number of states
SOLUTION:

• (a). Constructing the NFA:

 Starting condition: The string must begin with either 0 or 1.


 Ending condition: The string must end with 10 or 01.

• Language:

• {1101,001,1110,1001,1010…}
State Input 0 Input 1
• States: q0 {q0,q1} {q0,q3}
q1 q2 q1
 q0​moves to q1​on 0 and to q2 on 1. q2 φ φ
q3 φ q4
 q1 stays on 0, but moves to q3 on 1 (starting 10). q4 q3 φ
 q2 stays on 1, but moves to q4 on 0 (starting 01).
 q2 and q4 are final states
• b)conversion for NFA to DFA:
• PROCEDURE:
• i . state the given diagram as NFA
• ii . construct the transition table for the NFA transition diagram
• iii . Construct the DFA transition table from the NFA table
• iv . Finally construct the DFA from the DFA transition table
• Given NFA diagram is:
NFA transition
State
table Input 0 Input 1
q0 {q0,q1} {q0,q3}
q1 q2 q1
 q2
q3
φ
φ
φ
q4
q4 q3 φ

DFA transition
table
State Input 0 Input 1
-> q0 q0 q1 q0 q3
q0 q1
q0 q3
q0 q1 q2
q0 q1 q4
q0 q3
q0 q3

q0 q1 q0 q1 q2 q0 q3
q3*
q0 q1 q0 q1 q2 q0 q3
q4*
• C)Minimization of the above DFA:
• PROCEDURE:
• i . Check weather it has the dead states or unreachable states
• ii . Transition table
• iii . 0-equivalence
• iv . 1-equivalence
• v . Repeat the step 4 until the verification as same
• vi . Finally construct the minimized DFA
• Given DFA diagram is:
• It has no any dead states.
State Input 0 Input 1

• After solving the 0-equivalence,1-equivalence and 2-equivalence


-> q0 q0 q1 q0 q3
q0 q1 q0 q1 q2 q0 q3
• of the obtained DFA , the minimized DFA is obtained q0 q3 q0 q1 q4 q0 q3
q0 q1 q0 q1 q2 q0 q3
q3*
• The minimized DFA’s transition diagram as follows: q0 q1 q0 q1 q2 q0 q3
q4*
CODE FOR THE ABOVE SOLUTION IS:

• from collections import defaultdict


• def transition_table():
• table = {
• 'State': ['q0', 'q1', 'q2'],
• '0': ['q0', '0', '0'],
• '1': ['0', 'q1', '0']
• }
• return table
• def print_transition_table(table):
• print("\nTransition Table:")
• print(f"{'State':<5} {'0':<10} {'1':<10}")
• for i in range(len(table['State'])):
• print(f"{table['State'][i]:<5} {table['0'][i]:<10} {table['1'][i]:<10}")

• def is_accepted_by_nfa(input_string):
• states = {
• 'q0': {'0': ['q0'], '1': ['q0', 'q1']},
• 'q1': {'0': ['q2'], '1': ['q1']},
• 'q2': {'0': ['q0'], '1': ['q0']}
• }
• current_states = ['q0']
• for char in input_string:
• next_states = []
• for state in current_states:
• if char in states[state]:
• next_states.extend(states[state][char])
• current_states = list(set(next_states))
• accepting_states = ['q1', 'q2'] # Accepting states for "01" and "10"
• return any(state in accepting_states for state in current_states)
• def print_transition_diagram():
• print("\nTransition Diagram:")
• print("q0 --0--> q0")
• print("q0 --1--> q1")
• print("q1 --0--> q2")
• print("q1 --1--> q1")
• print("q2 --0--> q0")
• print("q2 --1--> q0")
• print("Accepting States: q1 (ends with 10), q2 (ends with 01)")

• def nfa_to_dfa(nfa):
• dfa = {}
• start_state = frozenset(['q0'])
• dfa[start_state] = {}
• unmarked_states = [start_state]
• dfa_states = {start_state: 'q0'}

• while unmarked_states:
• current = unmarked_states.pop()
• for symbol in ['0', '1']:
• next_states = set()
• for state in current:
• if state in nfa and symbol in nfa[state]:
• next_states.update(nfa[state][symbol])
• if next_states:
• next_state = frozenset(next_states)
• dfa[current][symbol] = next_state
• if next_state not in dfa:
• dfa[next_state] = {}
• unmarked_states.append(next_state)
• dfa_states[next_state] = f'q{len(dfa_states)}'
• return dfa, dfa_states
• def print_dfa_transition_table(dfa, dfa_states):
• print("\nDFA Transition Table:")
• print(f"{'State':<5} {'0':<10} {'1':<10}")
• for state in dfa:
• state_name = dfa_states[state]
• transitions = dfa[state]
• print(f"{state_name:<5} {dfa_states.get(transitions.get('0'), 'None'):<10}
{dfa_states.get(transitions.get('1'), 'None'):<10}")
• def is_accepted_by_dfa(dfa, dfa_states, input_string):
• current_state = frozenset(['q0'])
• for char in input_string:
• if char in dfa[current_state]:
• current_state = dfa[current_state][char]
• else:
• return False
• accepting_states = {frozenset(['q1']), frozenset(['q2'])} # Accepting states for
"01" and "10"
• return current_state in accepting_states
• def minimize_dfa(dfa, dfa_states):
• accepting_states = {state for state in dfa if dfa_states[state] in ['q1', 'q2']}
• non_accepting_states = {state for state in dfa if dfa_states[state] not in ['q1',
'q2']}
• partitions = [accepting_states, non_accepting_states]
• while True:
• new_partitions = []
• for part in partitions:
• transition_map = defaultdict(set)
• for state in part:
• key = tuple(dfa[state].get(symbol) for symbol in ['0', '1'])
• transition_map[key].add(state)
• new_partitions.extend(transition_map.values())
• if len(new_partitions) ==
• break
• partitions = new_partitions
• minimized_dfa = {}
• minimized_dfa_states = {}
• state_mapping = {}
• for i, part in enumerate(partitions):
• representative = next(iter(part))
• state_mapping.update({state: representative for state in part})
• minimized_dfa[representative] = {}
• minimized_dfa_states[representative] = f'q{i
• for state in minimized_dfa:
• for symbol in ['0', '1']:
• if state in dfa and symbol in dfa[state]:
• next_state = dfa[state][symbol]
• return minimized_dfa, minimized_dfa_states
• def print_minimized_dfa_transition_table(minimized_dfa, minimized_dfa_states):
• print("\nMinimized DFA Transition Table:")
• print(f"{'State':<5} {'0':<10} {'1':<10}")
• for state in minimized_dfa:
• state_name = minimized_dfa_states[state]
• transitions = minimized_dfa[state]
• print(f"{state_name:<5} {minimized_dfa_states.get(transitions.get('0'),
'None'):<10} {minimized_dfa_states.get(transitions.get('1'), 'None'):<10}")
• def main():
• nfa = {
• 'q0': {'0': ['q0'], '1': ['q0', 'q1']},
• 'q1': {'0': ['q2'], '1': ['q1']},
• 'q2': {'0': ['q0'], '1': ['q0']}
• }
• user_input = input("Enter a binary string: ")
• table = transition_table()
• print_transition_table(table)
• print_transition_diagram()
• if is_accepted_by_nfa(user_input):
• print("\nThe string is accepted by the NFA.")
• else:
• print("\nThe string is not accepted by the NFA.")
• dfa, dfa_states = nfa_to_dfa(nfa)
• print_dfa_transition_table(dfa, dfa_states)
• if is_accepted_by_dfa(dfa, dfa_states, user_input):
• print("\nThe string is accepted by the DFA.")
• else:
• print("\nThe string is not accepted by the DFA.")
• minimized_dfa, minimized_dfa_states = minimize_dfa(dfa, dfa_states)
• print_minimized_dfa_transition_table(minimized_dfa,
minimized_dfa_states)
• if is_accepted_by_dfa(minimized_dfa, minimized_dfa_states, user_input):
• print("\nThe string is accepted by the minimized DFA.")
• else:
• print("\nThe string is not accepted by the minimized DFA.")
• if __name__ == "__main__":
• main()
OUTPUT:
CONCLUSION:

• This solution outlines the process of constructing, converting, and minimizing finite automata for
recognizing strings that start with 0 or 1 and end with 10 or 01.

 NFA was first created to track multiple transitions.

 DFA was derived using subset construction to remove non-determinism.

 Minimization was applied to merge redundant states and optimize efficiency.

• The final minimized DFA efficiently recognizes the required binary patterns, which is essential for
applications in lexical analysis, pattern matching, and formal language processing.

You might also like