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 of attributes, a finite set of functional dependencies (FDs) over and an attribute . Question: is there a candidate key of under with ? A candidate key is a set with such that no proper subset of has this property, where is the closure of under . The input size is plus the total number of attribute occurrences in .
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. is prime if and only if there is a set with and .
Proof. Only if. Let be a key with , and let . Then . If , then , so and is a superkey, which contradicts the minimality of . So . If. is a superkey, so it contains a key . If , then , so , a contradiction. So .
Membership in NP. A certificate is the set 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 have the variables and the clauses . For a literal write for its attribute of falsity: if , and if . Let , where stands for " is true" and for " is false", and let hold:
- (f1) for each ;
- (f2) for each clause ;
- (f3) and for each .
The size of is linear in the size of . We show that is prime if and only if is satisfiable.
If is satisfiable. Let satisfy and let hold if is true and otherwise. No FD of (f1) applies to , since holds one of for each . No FD of (f2) applies: has a literal that is true under , and then . No FD of (f3) applies without . So and . From , the FDs of (f3) add the missing one of for each , so . By Lemma 1, is prime.
If is prime. Take as in Lemma 1. Only the FDs of (f3) have or on the right, and each needs the other one of the pair on the left. So if held neither nor , neither would be in , which is . So holds at least one of them. It does not hold both, or (f1) would put in . Let be true exactly when . For each clause , the FD (f2) does not apply to , because , so some has . If , then , so and is true. If , then and is true. So satisfies .
An empty clause gives the FD , so for every and is not prime, which agrees with 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 . Then (f3). The keys are , , and . The only key with is , and it encodes the only satisfying assignment, false and true. For the keys are and , and is in neither.
Consequences
- Non-prime is coNP-complete. Deciding whether 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 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 of with not a superkey and , the attribute 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.
- is in every key exactly when is not a superkey. If is a superkey, it contains a key without . If it is not, no key avoids .
- If is on the right side of no FD, except FDs that also have on the left, then is in every key, because no set without can derive it.
- If is on the left side of no FD and on the right side of an FD whose left side does not contain , then is in no key. For a key , the set derives everything that derives except perhaps , since helps no FD, and then it also derives .
- If every FD has one attribute on its left side, draw an edge from to for each FD . 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 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 candidate sets, and each is checked with one closure. The hardness is in the size of .
- One key versus all keys. Finding some key is polynomial: start from 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
- 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
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag GAQSFQ Edit this page History Report an error Markdown