Documentation
¶
Overview ¶
Package mindfa implements DFA minimization using Hopcroft's algorithm.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Minimize ¶
Minimize takes a DFA representation, minimize it and returns the groups of the states. The states in a group has same behavior.
The all of arguments represents a single DFA(before minimization). It has 0..nState states, 0..nSymbol input symbols and transitions as transition function.
transition is a function takes state and symbol and returns the destination state. The states which is included in finals are accepting state. The order of the resulting partitions is not defined, but the order of the states in a partition is ascending.
It uses Hopcroft's algorithm. The memory complexity is O(nState).
The all numbers in finals must be in [0, nState), and two same values must not appear.
Example ¶
Minimize the DFA as: https://commons.wikimedia.org/wiki/File%3ADFA_to_be_minimized.jpg#/media/File%3ADFA_to_be_minimized.jpg The result becomes: https://commons.wikimedia.org/wiki/File%3AMinimized_DFA.jpg#/media/File%3AMinimized_DFA.jpg .
package main
import (
"fmt"
"github.com/acomagu/mindfa"
)
func main() {
nState := 6
nSymbol := 2
finals := []int{2, 3, 4}
transitions := [][]int{
// 0 1
0: {1, 2}, // a
1: {0, 3}, // b
2: {4, 5}, // c
3: {4, 5}, // d
4: {4, 5}, // e
5: {5, 5}, // f
}
transitionFunc := func(state, symbol int) int { return transitions[state][symbol] }
partitions := mindfa.Minimize(nState, nSymbol, finals, transitionFunc)
fmt.Println(partitions)
}
Output: [[2 3 4] [0 1] [5]]
Example (DeterminingLeapYear) ¶
This example creates the minimum DFA inputs the digits of year(like 2 -> 0 -> 2 -> 1) and accepts if the year is a leap year.
package main
import (
"fmt"
"sort"
"github.com/acomagu/mindfa"
)
func main() {
nSymbol := 10
nState := 400
// finals becomes the list of leap years up to 400.
var finals []int
for s := 0; s < nState; s++ {
if s == 0 || (s%100 != 0 && s%4 == 0) {
finals = append(finals, s)
}
}
// e.g. 2 -> 20 -> 202 -> 21
transitions := func(state, symbol int) int {
return (state*10 + symbol) % nState
}
partitions := mindfa.Minimize(nState, nSymbol, finals, transitions)
// Mark years belonging to the same partition with identical number.
classes := make([]int, nState)
for _, p := range partitions {
for _, s := range p {
classes[s] = p[0]
}
}
checkLeapYear := func(year int) {
state := classes[0]
ds := digits(year) // digits(n) returns the slice of the digits of n.
for i := len(ds) - 1; i >= 0; i-- {
state = transitions(classes[state], ds[i])
}
// If the state is acceptable, it is a leap year.
if u := sort.SearchInts(finals, state); u < len(finals) && finals[u] == state {
fmt.Printf("%d is a leap year.\n", year)
} else {
fmt.Printf("%d is not a leap year.\n", year)
}
}
checkLeapYear(2019)
checkLeapYear(2020)
checkLeapYear(2021)
}
// digits returns the slice of the digits of n.
func digits(n int) []int {
var ans []int
for n > 0 {
ans = append(ans, n%10)
n /= 10
}
return ans
}
Output: 2019 is not a leap year. 2020 is a leap year. 2021 is not a leap year.
Types ¶
This section is empty.

