-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom_DFA.py
54 lines (40 loc) · 1.52 KB
/
Random_DFA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import random
class Random_DFA:
def __init__(self, numb_of_states, numb_of_symbols):
transitions= {}
acceptance = []
for s in range(numb_of_states):
trans_from_s = {}
#Each state is equiprobably set to be accepting or rejecting
acceptance.append(bool(random.randrange(2)))
#evenly choose another state from [i + 1; N ] and adds a random-labeled transition
if s < numb_of_states - 1:
s_prime = random.randrange(s + 1 , numb_of_states)
a_start = random.randrange(numb_of_symbols)
trans_from_s[a_start] = s_prime
else:
a_start = None
for a in range(numb_of_symbols):
#a = str(a)
if a != a_start:
trans_from_s[a] = random.randrange(numb_of_states)
transitions[s] = trans_from_s.copy()
self.transitions = transitions
self.acceptance = acceptance
self.alphabet = ""
for a in range(numb_of_symbols):
self.alphabet += str(a)
def init_from_transacc(self, trans, acc):
self.transitions = trans
self.acceptance = acc
def accepts(self, string):
if string == '':
return self.acceptance[0]
return self.accepts_from_state(0, string)
def accepts_from_state(self, state,string):
assert string != ''
a = string[0]
next_state = self.transitions[state][a]
if len(string) == 1:
return self.acceptance[next_state]
return self.accepts_from_state(next_state, string[1:])