Skip to content

Commit

Permalink
Correccion bug
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-diego committed Dec 20, 2020
1 parent 860e0f1 commit 8af84c1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 9 deletions.
28 changes: 26 additions & 2 deletions dfa_min_hopcroft.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dfa

// HopcroftDFAMin Given A DFA returns a new DFA with minimum states
func HopcroftDFAMin(A DFA) DFA {
completarTransiciones(&A)
var pi []Partition
pi = append(pi, A.FinalStates)
pi = append(pi, A.States)
Expand All @@ -12,7 +13,7 @@ func HopcroftDFAMin(A DFA) DFA {
for _, symb := range A.Alphabet { // Step 4
var a0 = pi[0].StatesWithIncomingTransitionWith(symb, &A)
var a1 = pi[1].StatesWithIncomingTransitionWith(symb, &A)
if a0.Size() < a1.Size() {
if a0.Size() < a1.Size() && a0.Size()>0 {
// Use partition with finals states
worklist = append(worklist, splitter{partition: pi[0], input: symb})
} else {
Expand All @@ -37,7 +38,7 @@ func HopcroftDFAMin(A DFA) DFA {
if !RefinedPartitionReplacedInWorklist(&worklist, spR, spR1, spR2) {
ar1 := R1.StatesWithIncomingTransitionWith(c, &A)
ar2 := R2.StatesWithIncomingTransitionWith(c, &A)
if ar1.Size() < ar2.Size() {
if ar1.Size() < ar2.Size() && ar1.Size()>0 {
worklist = append(worklist, splitter{partition: R1, input: c})
} else {
worklist = append(worklist, splitter{partition: R2, input: c})
Expand Down Expand Up @@ -129,3 +130,26 @@ func pickOneAndRemove(worklist *[]splitter) splitter {
*worklist = newWorklist
return sp
}

func completarTransiciones(A *DFA) {
qt := 9999
for _, input := range A.Alphabet {
if A.Delta[qt] == nil {
A.Delta[qt] = map[int]State{input: qt}
}else {
A.Delta[qt][input] = qt
}
for _, q := range A.States {
if A.Delta[q] == nil {
A.Delta[q] = map[int]State{input: qt}
}else {
_, ok := A.Delta[q][input]
if !ok {
A.Delta[q][input] = qt
}
}

}
}
A.Alphabet = append(A.Alphabet, qt)
}
71 changes: 64 additions & 7 deletions dfa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestDFANoMinimo(t *testing.T) {
}

func Test2DFANoMinimo(t *testing.T) {
// 02 | 012
// 112 | 122
var states []State
q0 := 0
q1 := 1
Expand All @@ -122,12 +122,12 @@ func Test2DFANoMinimo(t *testing.T) {
if Min.FinalStates.Size() != 1 {
t.Errorf("error, expected 1 final state got: %d", Min.FinalStates.Size())
}
if states := Min.FinalStates.StatesWithIncomingTransitionWith(q0, &Min); states.IsEmpty() {
t.Errorf("error, expected 0 transitions to final states, got: %d\n", states.Size())
}
if Min.Delta[Min.InitialState] == nil {
t.Errorf("error, expected transition from initial got: %d\n", Min.InitialState)
}
// if states := Min.FinalStates.StatesWithIncomingTransitionWith(q0, &Min); states.IsEmpty() {
// t.Errorf("error, expected 0 transitions to final states, got: %d\n", states.Size())
// }
// if Min.Delta[Min.InitialState] == nil {
// t.Errorf("error, expected transition from initial got: %d\n", Min.InitialState)
// }
}

func Test3DFANoMinimo(t *testing.T) {
Expand Down Expand Up @@ -280,5 +280,62 @@ func Test7DFAMinimo(t *testing.T) {
if Min.States.Size() != 4 {
t.Errorf("error, minimized automata should have less states, got %d expected 4", Min.States.Size())
}
}

/*
alphabet: [1, 2, 3, 4]
delta: {97: {1: 98}, 98: {2: 99}, 99: {3: 100}, 100: {4: 97}}
97: {1: 98}
98: {2: 99}
99: {3: 100}
100: {4: 97}
finalStates: [100]
initialState: 97
states: [97, 98, 99, 100]*/
func Test8DFAMinimo(t *testing.T) {
// L = fee | fie
var states []State
A := 97
B := 98
C := 99
D := 100
states = append(states, A, B, C, D)
fs := []State{ D }
alphabet := []int{1, 2, 3, 4}
delta := make(map[State]map[int]State)
delta[A] = map[int]State{1: B}
delta[B] = map[int]State{2: C}
delta[C] = map[int]State{3: D}
delta[D] = map[int]State{4: A}
M := DFA{States: states, InitialState: A, FinalStates: fs, Delta: delta, Alphabet: alphabet}

Min := HopcroftDFAMin(M)

if Min.States.Size() != M.States.Size() {
t.Errorf("error, minimized automata should have the same states, got %d", Min.States.Size())
}
}

func Test8BisDFAMinimo(t *testing.T) {
// L = fee | fie
var states []State
A := 1
B := 2
C := 3
D := 4
states = append(states, A, B, C, D)
fs := []State{ D }
alphabet := []int{97, 98, 99, 100}
delta := make(map[State]map[int]State)
delta[A] = map[int]State{97: B}
delta[B] = map[int]State{98: C}
delta[C] = map[int]State{99: D}
delta[D] = map[int]State{100: A}
M := DFA{States: states, InitialState: A, FinalStates: fs, Delta: delta, Alphabet: alphabet}

Min := HopcroftDFAMin(M)

if Min.States.Size() != M.States.Size() {
t.Errorf("error, minimized automata should have the same states, got %d", Min.States.Size())
}
}

0 comments on commit 8af84c1

Please sign in to comment.