#P1237. 冗余依赖

冗余依赖

Description

When designing tables in a relational database, the term "functional dependency" (FD) is used to describe relationships between attributes. A functional dependency describes the relationship between the values of attributes in one set and those in another set. The notation XYX \to Y is used to mean that once the attributes in set XX are assigned values, the attributes in set YY are determined. For example, in a data table with attributes "Social Security Number" (SS), "Name" (NN), "Address" (AA), and "Phone" (PP), where each person has a unique value of SS, the attributes NN, AA, and PP are determined by SS. This is written as S{N,A,P}S \to \{N, A, P\}.

Write a program to find all redundant dependencies in a set of dependencies. A dependency is redundant if it can be derived from the other dependencies in the set. For example, if the set includes ABA \to B, BCB \to C, and ACA \to C, then the third dependency is redundant because CC can be derived from the first two dependencies (AA determines BB, and BB determines CC). Among ABA \to B, BCB \to C, CAC \to A, ACA \to C, CBC \to B, and BAB \to A, all dependencies are redundant.

You are required to write a program to identify the redundant dependencies from the given set.

Input Format

The first line contains an integer nn not exceeding 100100, which is the number of functional dependencies in the file.

From the second line onward, each line contains one functional dependency, and all dependencies are distinct. Each line consists of two non-empty attribute lists separated by the characters -\verb!-! and >\verb!>!. Each list consists solely of uppercase letters. There are no spaces or tabs in any FD line. No trivial dependencies (such as AAA \to A) will appear. Although the FDs are not explicitly numbered in the file, their order corresponds to indices 11 through nn.

Output Format

Output each redundant dependency on its own line, along with a sequence of other dependencies that shows it is redundant. The format is $\texttt{FD}\ x\ \texttt{is redundant using FDs:}\ p_1\ p_2 \cdots p_k$ where xx is the index of the redundant dependency, and p1,p2,,pkp_1, p_2, \cdots, p_k is a sequence of dependency indices that proves xx is redundant.

If multiple sequences of functional dependencies can be used to show a dependency is redundant, output the shortest proof sequence.

If no redundant dependencies are present among the given FDs, output No redundant FDs.

3
A->BD
BD->C
A->C

FD 3 is redundant using FDs: 1 2

6
P->RST
VRT->SQP
PS->T
Q->TR
QS->P
SR->V

FD 3 is redundant using FDs: 1
FD 5 is redundant using FDs: 4 6 2

Hint

Explanation for Sample 1

Dependency 33 is redundant because ACA \to C can be derived from the first two dependencies A{B,D}A \to \{B, D\} and {B,D}C\{B, D\} \to C.

Translated by ChatGPT 5