## An example of a deterministic finite automaton that accepts only binary numbers that are multiples of 3. The state q0 is both the start state and an accept state. For example, the string "1001" leads to the state sequence q0, q1, q2, q1, q0, and is hence accepted.Please Follow the Video and Then go for the Code for Better Understanding.

``````
dfa={0:{'0':0,'1':1},
1:{'0':2,'1':0},
2:{'0':1,'1':2}}

def accepts(delta,start,final,sigma):
state=start
for input in sigma:
state=dfa[state][input]
return state in final
print(accepts(dfa,0,{0},'1001'))
``````
``` ```

# Nondeterministic finite automaton:

## To perform this Operation Please install this module just type in CMD: pip install automata-lib

``````
from automata.fa.nfa import NFA
nfa = NFA(
states={'q0', 'q1', 'q2'},
input_symbols={'1', '0'},
transitions={
'q0': {'1': {'q1'}},
# Use '' as the key name for empty string (lambda/epsilon) transitions
'q1': {'1': {'q1'}, '': {'q2'}},
'q2': {'0': {'q0'}}
},
initial_state='q0',
final_states={'q1'}
)
``````
``` ```

# PushDown Automaton:

## A PDA is formally defined as a 7-tuple: where

## Example:DPDA which which matches zero or more '0's, followed by the same number of '1's (accepting by final state)

``````
from automata.pda.dpda import DPDA
dpda = DPDA(
states={'q0', 'q1', 'q2', 'q3'},
input_symbols={'0', '1'},
stack_symbols={'0', '1'},
transitions={
'q0': {
'0': {'0': ('q1', ('1', '0'))}  # transition pushes '1' to stack
},
'q1': {
'0': {'1': ('q1', ('1', '1'))},
'1': {'1': ('q2', '')}  # transition pops from stack
},
'q2': {
'1': {'1': ('q2', '')},
'': {'0': ('q3', ('0',))}  # transition does not change stack
}
},
initial_state='q0',
initial_stack_symbol='0',
final_states={'q3'},
acceptance_mode='final_state'
)
```
```

# Turing Machine:

## Example:DTM which matches all strings beginning with '0's, and followed by the same number of '1's

``````
from automata.tm.dtm import DTM
dtm = DTM(
states={'q0', 'q1', 'q2', 'q3', 'q4'},
input_symbols={'0', '1'},
tape_symbols={'0', '1', 'x', 'y', '.'},
transitions={
'q0': {
'0': ('q1', 'x', 'R'),
'y': ('q3', 'y', 'R')
},
'q1': {
'0': ('q1', '0', 'R'),
'1': ('q2', 'y', 'L'),
'y': ('q1', 'y', 'R')
},
'q2': {
'0': ('q2', '0', 'L'),
'x': ('q0', 'x', 'R'),
'y': ('q2', 'y', 'L')
},
'q3': {
'y': ('q3', 'y', 'R'),
'.': ('q4', '.', 'R')
}
},
initial_state='q0',
blank_symbol='.',
final_states={'q4'}
)

``` ```