Skip to content

Commit 52205e7

Browse files
committed
Correccion de la funcion AddTransitions que no verificaba que Delta[q][c] fuera nil. Corrijo tests, agrego descripcion de los lenguajes de los automatas en algunos tests
1 parent 5ce66d8 commit 52205e7

File tree

2 files changed

+65
-18
lines changed

2 files changed

+65
-18
lines changed

dfa_min_hopcroft.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,21 @@ func HopcroftDFAMin(A DFA) DFA {
8080

8181
func AddTransitions(DeltaMin *map[State]map[int]State, A *DFA, pi []Partition, i int) {
8282
for _, c := range A.Alphabet {
83-
particionDelEstado := particionDeEstado(&pi, A.Delta[pi[i][0]][c])
84-
(*DeltaMin)[i] = map[int]State{c: particionDelEstado}
83+
transitionState, ok := A.Delta[pi[i][0]][c]
84+
if !ok {
85+
continue
86+
}
87+
partitionOfState := getPartitionOfState(&pi, transitionState)
88+
if (*DeltaMin)[i] == nil {
89+
(*DeltaMin)[i] = map[int]State{c: partitionOfState}
90+
}else {
91+
(*DeltaMin)[i][c] = partitionOfState
92+
}
8593
}
94+
8695
}
8796

88-
func particionDeEstado(pi *[]Partition, q State) int {
97+
func getPartitionOfState(pi *[]Partition, q State) int {
8998
for i, part := range *pi {
9099
if part.Includes(q) {
91100
return i

dfa_test.go

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package DFA
22

3-
import testing "testing"
3+
import (
4+
testing "testing"
5+
)
46

57
func TestTrivial(t *testing.T) {
68
q0 := 0
@@ -52,7 +54,7 @@ func TestDFAMinimo(t *testing.T) {
5254
Min := HopcroftDFAMin(M)
5355

5456
if (Min.States.Size() != M.States.Size()) {
55-
t.Errorf("the minimized dfa should have the same number of states, expected 6 have %d", Min.States.Size())
57+
t.Errorf("the minimized dfa should have the same number of states, expected 6 got %d", Min.States.Size())
5658
}
5759
}
5860

@@ -90,12 +92,13 @@ func TestDFANoMinimo(t *testing.T) {
9092
Min := HopcroftDFAMin(M)
9193

9294
if (Min.States.Size() == M.States.Size()) {
93-
t.Errorf("the minimized dfa should have less states, expected 4, have %d", Min.States.Size())
95+
t.Errorf("the minimized dfa should have less states, expected 4, got %d", Min.States.Size())
9496
}
9597
}
9698

9799

98100
func Test2DFANoMinimo(t *testing.T) {
101+
// 02 | 012
99102
var states []State
100103
q0 := 0
101104
q1 := 1
@@ -115,20 +118,21 @@ func Test2DFANoMinimo(t *testing.T) {
115118
Min := HopcroftDFAMin(M)
116119

117120
if Min.States.Size() != 4 {
118-
t.Errorf("error, expected 4 have %d states", Min.States.Size())
121+
t.Errorf("error, expected 4 got %d states", Min.States.Size())
119122
}
120123
if Min.FinalStates.Size() != 1 {
121-
t.Errorf("error, expected 1 final state have: %d", Min.FinalStates.Size())
124+
t.Errorf("error, expected 1 final state got: %d", Min.FinalStates.Size())
125+
}
126+
if states := Min.FinalStates.StatesWithIncomingTransitionWith(q0, &Min); states.IsEmpty() {
127+
t.Errorf("error, expected 0 transitions to final states, got: %d\n", states.Size())
128+
}
129+
if Min.Delta[Min.InitialState] == nil {
130+
t.Errorf("error, expected transition from initial got: %d\n", Min.InitialState)
122131
}
123-
// if Min.Delta[Min.FinalStates[0]] == nil {
124-
// t.Errorf("Final: %d \n %#v\n", Min.FinalStates[0], Min.Delta[Min.FinalStates[0]])
125-
// }
126-
// if Min.Delta[Min.InitialState] != nil {
127-
// t.Errorf("Initial: %d\n %#v\n", Min.InitialState, Min.Delta[Min.InitialState])
128-
// }
129132
}
130133

131134
func Test3DFANoMinimo(t *testing.T) {
135+
// a ( b | c*)
132136
var states []State
133137
q0 := 0
134138
q1 := 1
@@ -138,23 +142,57 @@ func Test3DFANoMinimo(t *testing.T) {
138142
fs := []State{ q1, q2, q3 }
139143
alphabet := []int{0, 1, 2}
140144
delta := make(map[State]map[int]State)
141-
delta[q0] = map[int]State{1: q1}
145+
delta[q0] = map[int]State{0: q1}
142146
delta[q1] = map[int]State{1: q2, 2: q3}
143147
delta[q2] = map[int]State{1: q2, 2: q3}
144148
delta[q3] = map[int]State{1: q2, 2: q3}
149+
145150
M := DFA{States: states, InitialState: q1, FinalStates: fs, Delta: delta, Alphabet: alphabet}
146151
Min := HopcroftDFAMin(M)
147152

148153
if Min.States.Size() != 2 {
149-
t.Errorf("error, expected 2 have %d states", Min.States.Size())
154+
t.Errorf("error, expected 2 got %d states", Min.States.Size())
150155
}
151156
if Min.FinalStates.Size() != 1 {
152-
t.Errorf("error, expected 1 final state have: %d", Min.FinalStates.Size())
157+
t.Errorf("error, expected 1 final state got: %d", Min.FinalStates.Size())
153158
}
154159
for _, c := range Min.Alphabet {
155160
statesWithIncomingTransitions := Min.FinalStates.StatesWithIncomingTransitionWith(c, &Min)
156161
if statesWithIncomingTransitions.Size() == 0 {
157-
t.Errorf("error, expected transition with %d to final state %d", c, Min.FinalStates[0])
162+
t.Errorf("error, expected transition with %d to final state %d\n", c, Min.FinalStates[0])
158163
}
159164
}
165+
}
166+
167+
168+
func Test4DFANoMinimo(t *testing.T) {
169+
var states []State
170+
A := 0
171+
B := 1
172+
C := 2
173+
D := 3
174+
E := 4
175+
F := 5
176+
G := 6
177+
H := 7
178+
states = append(states, A, B, C, D, E, F, G, H)
179+
fs := []State{ C }
180+
alphabet := []int{ 0, 1 }
181+
delta := make(map[State]map[int]State)
182+
delta[A] = map[int]State{0: B, 1: F}
183+
delta[B] = map[int]State{0: G, 1: C}
184+
delta[C] = map[int]State{0: A, 1: C}
185+
delta[D] = map[int]State{0: C, 1: G}
186+
delta[E] = map[int]State{0: H, 1: F}
187+
delta[F] = map[int]State{0: C, 1: G}
188+
delta[G] = map[int]State{0: G, 1: E}
189+
delta[H] = map[int]State{0: G, 1: C}
190+
M := DFA{States: states, InitialState: A, FinalStates: fs, Delta: delta, Alphabet: alphabet}
191+
Min := HopcroftDFAMin(M)
192+
if Min.States.Size() != 5 {
193+
t.Errorf("error, expected 5 states got %d", Min.States.Size())
194+
}
195+
if Min.FinalStates.Size() != 1 {
196+
t.Errorf("error, expected 1 final state got %d", Min.FinalStates.Size())
197+
}
160198
}

0 commit comments

Comments
 (0)