On this page
Package fd provides functional dependency primitives including closure computation, minimal cover, candidate keys, and BCNF decomposition for audit.
#internal/fd
#internal/fd
Package fd provides functional dependency primitives including closure computation, minimal cover, candidate keys, and BCNF decomposition for audit.
#FuncDep
type FuncDep structFuncDep represents a functional dependency X -> Y.
#Component
type Component structComponent represents a relation produced by BCNF decomposition.
#Closure
func Closure(attrs []string, fds []FuncDep) []stringClosure computes the attribute closure of attrs under fds using Armstrong's axioms. Returns the closure set (sorted for determinism).
#MinimalCover
func MinimalCover(fds []FuncDep) []FuncDepMinimalCover computes the minimal (canonical) cover of a set of functional dependencies. Does not merge cyclic equivalences (e.g., A->B, B->A). This is a known limitation, not a bug — the algorithm is correct for non-cyclic FD sets.
#CandidateKeys
func CandidateKeys(allAttrs []string, fds []FuncDep) [][]stringCandidateKeys finds all minimal superkeys of allAttrs under fds. Returns candidate keys sorted for determinism (each key sorted internally).
#IsSuperkey
func IsSuperkey(attrs []string, allAttrs []string, fds []FuncDep) boolIsSuperkey returns true if Closure(attrs, fds) contains all of allAttrs.
#IsPrime
func IsPrime(attr string, candidateKeys [][]string) boolIsPrime returns true if attr appears in any candidate key.
#BCNFDecompose
func BCNFDecompose(name string, allAttrs []string, fds []FuncDep) []ComponentBCNFDecompose decomposes a relation into BCNF components. Returns one Component per BCNF sub-relation, with names derived from the original name (e.g., "orders_1", "orders_2").
#IsLosslessJoin
func IsLosslessJoin(r1, r2 []string, allAttrs []string, fds []FuncDep) boolIsLosslessJoin checks whether decomposing into r1 and r2 is lossless under the given FDs. The decomposition is lossless if the closure of (r1 intersect r2) contains all of r1 or all of r2.
#PreservesDependencies
func PreservesDependencies(original []FuncDep, components []Component) (preserved bool, lost []FuncDep)PreservesDependencies checks whether the projected FDs in the given components preserve all original FDs. Returns true if all are preserved, along with any lost FDs (with multi-attribute dependents merged).
#ArmstrongRelation
func ArmstrongRelation(allAttrs []string, fds []FuncDep, violating FuncDep) []map[string]stringArmstrongRelation generates a small counterexample table showing redundancy for a specific FD X->A in relation to the full attribute set. Two rows agree on X (and A, since X determines A) but differ on other attributes, making the redundancy visible. Cap at 10 rows.
#FormatRelation
func FormatRelation(allAttrs []string, rows []map[string]string) stringFormatRelation formats rows as a text table for diagnostic output.
#FuncDep.String
func (f FuncDep) String() stringString formats the FD as "{A, B} -> {C, D}".