Algorithm Search⌘K

Theoremadvanced

NP-completeness of the prime attribute problem

Also called prime attribute problem.

Deciding whether an attribute belongs to some candidate key of a schema with functional dependencies is NP-complete (Lucchesi and Osborn 1978). Finding one key is easy.

A prime attribute of a schema is an attribute that is in at least one candidate key. This page proves that deciding whether a given attribute is prime is NP-complete, a result of Lucchesi and Osborn (1978). The attribute closure algorithm tests in linear time whether a set is a superkey, and the dependency implication problem page uses it to find one key and to list all keys. The difficulty here is different: there can be very many keys, and the question is whether any of them holds the attribute. The test for third normal form asks exactly this kind of question.

Statement

PRIME ATTRIBUTE. Input: a finite set UU of attributes, a finite set FF of functional dependencies (FDs) over UU and an attribute A∈UA \in U. Question: is there a candidate key KK of UU under FF with A∈KA \in K? A candidate key is a set K⊆UK \subseteq U with K+=UK^+ = U such that no proper subset of KK has this property, where K+K^+ is the closure of KK under FF. The input size is ∣U∣|U| plus the total number of attribute occurrences in FF.

Theorem (Lucchesi and Osborn 1978). PRIME ATTRIBUTE is NP-complete.

In plain words

A key of a table is a smallest group of columns that tells you every other column. Usually a table has only a few keys, but the rules about columns can make the number of keys grow very fast with the number of columns.

The question here is simple to state: does at least one key use this column? Checking a proposed key is quick. Finding out whether one exists, among possibly very many keys, is as hard as solving a logic puzzle with true and false values in which every clause must be satisfied. So no fast general method is known, and if one existed, all such puzzles would be easy.

Some special cases are still easy, for example when each rule has one column on its left side.

Proof

Lemma 1. AA is prime if and only if there is a set X⊆U−{A}X \subseteq U - \{A\} with A∉X+A \notin X^+ and (X∪{A})+=U(X \cup \{A\})^+ = U.

Proof. Only if. Let KK be a key with A∈KA \in K, and let X=K−{A}X = K - \{A\}. Then (X∪{A})+=K+=U(X \cup \{A\})^+ = K^+ = U. If A∈X+A \in X^+, then X+⊇KX^+ \supseteq K, so X+=UX^+ = U and XX is a superkey, which contradicts the minimality of KK. So A∉X+A \notin X^+. If. X∪{A}X \cup \{A\} is a superkey, so it contains a key KK. If A∉KA \notin K, then K⊆XK \subseteq X, so X+⊇K+=U∋AX^+ \supseteq K^+ = U \ni A, a contradiction. So A∈KA \in K. □\square

Membership in NP. A certificate is the set XX of Lemma 1. Two attribute closures check it, each in time linear in the input. So PRIME ATTRIBUTE is in NP.

NP-hardness. We reduce CNF satisfiability to PRIME ATTRIBUTE. The reduction below is a direct one written for this page; the original paper may use another one. Let φ\varphi have the variables x1,…,xnx_1, \ldots, x_n and the clauses C1,…,CmC_1, \ldots, C_m. For a literal ℓ\ell write ℓˉ\bar\ell for its attribute of falsity: ℓˉ=Ni\bar\ell = N_i if ℓ=xi\ell = x_i, and ℓˉ=Pi\bar\ell = P_i if ℓ=¬xi\ell = \neg x_i. Let U={A,P1,…,Pn,N1,…,Nn}U = \{A, P_1, \ldots, P_n, N_1, \ldots, N_n\}, where PiP_i stands for "xix_i is true" and NiN_i for "xix_i is false", and let FF hold:

  • (f1) PiNi→AP_i N_i \to A for each ii;
  • (f2) {ℓˉ:ℓ∈Cj}→A\{\bar\ell : \ell \in C_j\} \to A for each clause CjC_j;
  • (f3) APi→NiA P_i \to N_i and ANi→PiA N_i \to P_i for each ii.

The size of (U,F)(U, F) is linear in the size of φ\varphi. We show that AA is prime if and only if φ\varphi is satisfiable.

If φ\varphi is satisfiable. Let β\beta satisfy φ\varphi and let XX hold PiP_i if β(xi)\beta(x_i) is true and NiN_i otherwise. No FD of (f1) applies to XX, since XX holds one of Pi,NiP_i, N_i for each ii. No FD of (f2) applies: CjC_j has a literal ℓ\ell that is true under β\beta, and then ℓˉ∉X\bar\ell \notin X. No FD of (f3) applies without AA. So X+=XX^+ = X and A∉X+A \notin X^+. From X∪{A}X \cup \{A\}, the FDs of (f3) add the missing one of Pi,NiP_i, N_i for each ii, so (X∪{A})+=U(X \cup \{A\})^+ = U. By Lemma 1, AA is prime.

If AA is prime. Take XX as in Lemma 1. Only the FDs of (f3) have PiP_i or NiN_i on the right, and each needs the other one of the pair on the left. So if XX held neither PiP_i nor NiN_i, neither would be in (X∪{A})+(X \cup \{A\})^+, which is UU. So XX holds at least one of them. It does not hold both, or (f1) would put AA in X+X^+. Let β(xi)\beta(x_i) be true exactly when Pi∈XP_i \in X. For each clause CjC_j, the FD (f2) does not apply to XX, because A∉X+A \notin X^+, so some ℓ∈Cj\ell \in C_j has ℓˉ∉X\bar\ell \notin X. If ℓ=xi\ell = x_i, then Ni∉XN_i \notin X, so Pi∈XP_i \in X and ℓ\ell is true. If ℓ=¬xi\ell = \neg x_i, then Pi∉XP_i \notin X and ℓ\ell is true. So β\beta satisfies φ\varphi. □\square

An empty clause gives the FD ∅→A\emptyset \to A, so A∈X+A \in X^+ for every XX and AA is not prime, which agrees with φ\varphi being unsatisfiable. If each clause has at most three literals, every left side has at most three attributes, so the problem stays NP-complete with left sides of size at most 3.

Example. Let φ=(x1∨x2)∧¬x1\varphi = (x_1 \lor x_2) \land \neg x_1. Then F={P1N1→A, P2N2→A, N1N2→A, P1→A}∪F = \{P_1 N_1 \to A,\ P_2 N_2 \to A,\ N_1 N_2 \to A,\ P_1 \to A\} \cup (f3). The keys are P1P2P_1 P_2, P1N2P_1 N_2, N1N2N_1 N_2 and AN1P2A N_1 P_2. The only key with AA is AN1P2A N_1 P_2, and it encodes the only satisfying assignment, x1x_1 false and x2x_2 true. For φ=x1∧¬x1\varphi = x_1 \land \neg x_1 the keys are P1P_1 and N1N_1, and AA is in neither.

Consequences

  • Non-prime is coNP-complete. Deciding whether AA is in no key is the complement, so it is coNP-complete.
  • A key of bounded size. Deciding whether there is a key with at most kk attributes is also NP-complete (Lucchesi and Osborn 1978). The reduction from vertex cover is on the dependency implication problem page.
  • Third normal form. A schema is in 3NF when, for each FD X→AX \to A of FF with XX not a superkey and A∉XA \notin X, the attribute AA is prime. A certificate of one key for each such FD shows that 3NF testing is in NP. NP-hardness of 3NF testing does not follow directly from the theorem, because a 3NF test asks many prime questions about one fixed schema. Jou and Fischer (1982) proved it separately. Testing BCNF against the given FDs needs no keys and is polynomial.
  • Rule of thumb: few keys. Lucchesi and Osborn list all keys in time polynomial in the size of the input and the number of keys. When a schema has few keys, as most real schemas do, the prime attributes are found quickly.

Easy cases. These can be decided in polynomial time.

  • AA is in every key exactly when U−{A}U - \{A\} is not a superkey. If U−{A}U - \{A\} is a superkey, it contains a key without AA. If it is not, no key avoids AA.
  • If AA is on the right side of no FD, except FDs that also have AA on the left, then AA is in every key, because no set without AA can derive it.
  • If AA is on the left side of no FD and on the right side of an FD whose left side does not contain AA, then AA is in no key. For a key K∋AK \ni A, the set K−{A}K - \{A\} derives everything that KK derives except perhaps AA, since AA helps no FD, and then it also derives AA.
  • If every FD has one attribute on its left side, draw an edge from BB to CC for each FD B→CB \to C. The closure of a set is the set of nodes it can reach. A key takes one node from each source component of the strongly connected components, so AA is prime exactly when its component has no edge entering it from another component.

Tightness

  • Left sides of size 1 are easy. The last easy case shows that the problem is in polynomial time when all left sides are single attributes. The reduction above uses left sides of size 2 in (f1) and (f3) and of the clause size in (f2).
  • Fixed schemas are easy. For a fixed number of attributes, there are 2∣U∣2^{|U|} candidate sets, and each is checked with one closure. The hardness is in the size of UU.
  • One key versus all keys. Finding some key is polynomial: start from UU and drop each attribute whose removal keeps a superkey. Whether a chosen attribute can be kept in a key is the hard part.

History

Lucchesi and Osborn (1978) studied the keys of a relation schema. They gave the algorithm that lists all keys with time polynomial in the input and the output, and proved that the prime attribute problem and the problem of a key of bounded size are NP-complete. Garey and Johnson (1979) included the prime attribute problem in their list of NP-complete problems. Jou and Fischer (1982) proved that 3NF testing is NP-complete, which shows that the design theory based on prime attributes is hard to test in the worst case, while BCNF against the given FDs is easy to test.

References

  1. Cláudio L. Lucchesi and Sylvia L. Osborn, Candidate keys for relations, Journal of Computer and System Sciences 17(2), 270 to 279 (1978), the NP-completeness of the prime attribute problem and of a key of bounded size, and the algorithm that lists all keys
  2. Jieh Hsiang Jou and Patrick C. Fischer, The complexity of recognizing 3NF relation schemes, Information Processing Letters 14(4), 187 to 190 (1982), testing whether a schema is in 3NF is NP-complete
  3. Michael R. Garey and David S. Johnson, Computers and Intractability, A Guide to the Theory of NP-Completeness, W. H. Freeman (1979), Appendix A, the list of storage and retrieval problems, the prime attribute name problem
  4. Serge Abiteboul, Richard Hull and Victor Vianu, Foundations of Databases, Addison-Wesley (1995), Chapter 11 (design and dependencies), keys, prime attributes and third normal form

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/GAQSFQ/text.yaml