-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimized.java
More file actions
199 lines (171 loc) · 4.4 KB
/
Minimized.java
File metadata and controls
199 lines (171 loc) · 4.4 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import java.util.*;
public class Minimized extends DFA {
boolean [] used = new boolean[statesCount];
boolean [][] arcs = new boolean[statesCount][statesCount];
boolean [][] marked = new boolean[statesCount][statesCount];
int [] component = new int[statesCount];
List<StatePair> Q = new LinkedList<StatePair>();
Map<Integer, Set<Integer>> comp = new HashMap<Integer, Set<Integer>>();
int componentsCount = 0;
Minimized (int a, int sCount, int s0, int fCount, String[] finishState, List<String> transitions) {
super(a, sCount, s0, fCount, finishState, transitions);
}
void minimize() {
addDeadState();
initializeArcs();
step1_dfs(startState);
step2();
step3();
step4();
step5();
}
void addDeadState() {
for (int i = 0; i < statesCount; i++) {
for (int j = 0; j < alphabetLength; j++) {
StartStateSymbol s = new StartStateSymbol(i, (char)('a' + j));
if (!transitFunction.containsKey(s)) {
transitFunction.put(new StartStateSymbol(i, (char)('a' + j)), 0);
transitions.add(new Transition(i, (char)('a' + j), 0));
}
}
}
}
void initializeArcs() {
for (Transition t : transitions) {
arcs[t.start][t.target] = true;
}
}
void step1_dfs(int state) {
used[state] = true;
for (int i = 0; i < used.length; i++) {
if (!used[i] && arcs[state][i]) {
step1_dfs(i);
}
}
}
// adding state pair that different by e-symbol
void step2() {
for (int i = 0; i < statesCount; i++) {
for (int j : finalStates) {
if (!finalStates.contains(i)) {
Q.add(new StatePair(i, j));
marked[i][j] = true;
marked[j][i] = true;
}
}
}
}
//marked table filling
void step3() {
while (!Q.isEmpty()) {
StatePair pair = Q.remove(0);
for (int i = 0; i < alphabetLength; i++) {
for (Transition t : transitions) {
for (Transition t2 : transitions) {
if (pair.start == t.target && t.symbol == (char)('a' + i) &&
pair.target == t2.target && t2.symbol == (char)('a' + i)) {
int a = t.start;
int b = t2.start;
if (!marked[a][b]) {
marked[a][b] = true;
marked[b][a] = true;
Q.add(new StatePair(a, b));
}
}
}
}
}
}
}
//splitting to components
void step4() {
for (int i = 0; i < statesCount; i++) {
component[i] = -1;
}
for (int i = 0; i < statesCount; i++) {
if(!marked[0][i]) {
component[i] = 0;
}
}
for (int i = 1; i < statesCount; i++) {
if (!used[i]) {
continue;
}
if (component[i] == -1) {
Set<Integer> states = new HashSet<Integer>();
componentsCount++;
component[i] = componentsCount;
for (int j = i + 1; j < statesCount; j++) {
if (!marked[i][j]) {
component[j] = componentsCount;
states.add(j);
}
}
comp.put(componentsCount, states);
}
}
}
// building minimized DFA
void step5() {
List<Integer> newFinalStates = new ArrayList<Integer>();
for (int i = 1; i <= componentsCount; i++) {
Set<Integer> states = new HashSet<Integer>();
for (int j = 0; j < component.length; j++) {
if (component[j] == i) {
if (startState == j) {
startState = i;
}
if (finalStates.contains(j)) {
if (!newFinalStates.contains(i)) {
newFinalStates.add(i);
}
}
states.add(j);
}
}
comp.put(i, states);
}
statesCount = componentsCount;
finalStates = newFinalStates;
finalStatesCount =finalStates.size();
Set<Transition> newTransitions = new HashSet<Transition>();
for (Transition t : transitions) {
int start = 0;
int target = 0;
for (int i = 1; i <= componentsCount; i++) {
if (comp.get(i).contains(t.start)) {
start = i;
}
if (comp.get(i).contains(t.target)) {
target = i;
}
}
if (start != 0 && target != 0) {
boolean is = false;
for (Transition tb : newTransitions) {
if (tb.start == start && tb.symbol == t.symbol && tb.target == target) {
is = true;
}
}
if (!is)
newTransitions.add(new Transition(start, t.symbol, target));
}
}
transitions = newTransitions;
}
class StatePair {
int start;
int target;
StatePair(int start, int target) {
this.start = start;
this.target = target;
}
public boolean equals(StatePair o) {
return (start == o.start && target == o.target) || (start == o.target && target == o.start);
}
@Override
public int hashCode() {
return start * 1000000 + target;
}
}
}