mindfa

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2019 License: MIT Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

mindfa: The implementation of Hopcroft's algorithm in Go

GoDoc

The implementation of DFA minimization using Hopcroft's algorithm in Go. The time complexity is O(n log n) and the memory complexity is O(n)(n is the number of the states of the input DFA).

Examples

1

The demonstration minimizing the DFA:

DFA to be minimized.jpg
By Vevek - Own work, created with Inkscape, CC BY-SA 3.0

resulting as:

Minimized DFA.jpg
By Vevek - Own work, created with Inkscape, CC BY-SA 3.0

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]]

(means (c, d, e), (a, b), (f)).

Playground

2

This example creates the minimum DFA inputs the digits of year(like 2 -> 0 -> 2 -> 1) and accepts if it's a leap year.

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
transitionFunc := func(state, symbol int) int {
	return (state*10 + symbol) % nState
}

partitions := mindfa.Minimize(nState, nSymbol, finals, transitionFunc)

// 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)

Output:

2019 is not a leap year.
2020 is a leap year.
2021 is not a leap year.

Playground

Benchmark

The task is Example 2 above.

goos: linux
goarch: amd64
pkg: github.com/acomagu/mindfa
BenchmarkMinimize-8         1172            933586 ns/op            6869 B/op          8 allocs/op

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

func Minimize(nState, nSymbol int, finals []int, transition func(state, symbol int) int) [][]int

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL