Sub topic 12
Computing Attribute Closure
This chapter assumes you know what a functional dependency (FD) is, as covered in the previous chapter. It also assumes you know the meaning of superkey and candidate key, and that you can read basic Python. The goal is to compute the closure of a set of attributes by hand and in code, and to use it to test FD implication and superkeys.
What the closure is
Given a relation schema R, a set F of FDs, and a set of attributes X, the attribute closure X+ is the set of all attributes A of R such that F logically implies X -> A. In plain terms, X+ holds every attribute you can reach starting from X by repeatedly applying the FDs in F.
Three facts follow directly from the definition:
- X is always a subset of X+, by reflexivity.
- X -> Y holds under F exactly when Y is a subset of X+.
- X is a superkey of R exactly when X+ contains every attribute of R.
The closure turns questions about logical implication into a simple set computation. This is why GATE questions on keys, FD implication, and equivalence of FD sets all reduce to it.
The algorithm
- Set result = X.
- Look for an FD L -> Rt in F where L is a subset of result and Rt is not yet a subset of result.
- If you find one, add Rt to result.
- Repeat from step 2 until a full pass over F adds nothing.
- Return result.
The loop ends because result only grows and R is finite. Each pass either adds at least one attribute or stops, so there are at most |R| productive passes.
Worked example by hand
Let R(A, B, C, D, E) with F = { A -> B, B -> C, CD -> E }. Compute {A, D}+.
| Step | FD used | Left side in result? | result after step |
|---|---|---|---|
| 0 | none | not applicable | {A, D} |
| 1 | A -> B | yes, A is present | {A, B, D} |
| 2 | B -> C | yes, B is present | {A, B, C, D} |
| 3 | CD -> E | yes, C and D are present | {A, B, C, D, E} |
| 4 | no FD adds anything | not applicable | stop |
So {A, D}+ = {A, B, C, D, E}, which is all of R. Therefore {A, D} is a superkey. Neither A+ = {A, B, C} nor D+ = {D} is all of R, so no proper subset of {A, D} is a superkey. This makes {A, D} a candidate key.
Note that the order in which you scan the FDs does not change the final answer. It only changes how many passes you need. If the FDs had been listed as CD -> E, B -> C, A -> B, the first pass would add only B, and a second pass would add C, and a third would add E.
Implementation
def closure(attrs, fds):
result = set(attrs)
changed = True
while changed:
changed = False
for lhs, rhs in fds:
if lhs <= result and not rhs <= result:
result |= rhs
changed = True
return result
fds = [({'A'}, {'B'}), ({'B'}, {'C'}), ({'C', 'D'}, {'E'})]
print(sorted(closure({'A', 'D'}, fds))) # ['A', 'B', 'C', 'D', 'E']Each FD is a pair of sets (lhs, rhs). The test lhs <= result is the subset check, and result |= rhs is the union. The not rhs <= result guard prevents setting changed when the FD adds nothing, which is what lets the loop terminate.
Using the closure
Testing whether an FD is implied
To test whether F implies X -> Y, compute X+ under F and check whether Y is a subset of it. You do not need Armstrong's axioms or a derivation.
Example: with F = { A -> B, B -> C, CD -> E }, does F imply AD -> E? We computed {A, D}+ above and it contains E, so yes. Does F imply A -> E? A+ = {A, B, C}, which lacks E, so no.
Testing whether two FD sets are equivalent
F and G are equivalent if every FD in G is implied by F and every FD in F is implied by G. For each FD X -> Y in G, compute X+ under F and check that Y is inside. Then do the same with the roles swapped. This is a common GATE question type.
Testing for a superkey
Compute X+ and compare it with the full attribute set. This is the basis of candidate key search in the next chapter.
Edge cases and common errors
- Empty left side. An FD with an empty left side, written {} -> A, means A is constant. Since the empty set is a subset of every set, the algorithm applies it on the first pass. The code above handles this correctly.
- Composite left sides. An FD such as CD -> E fires only when both C and D are in result. Having one of them is not enough. Students who add E after seeing only C make the most frequent mistake in this topic.
- Stopping early. After adding attributes, an FD that was skipped earlier may now apply. Always make another full pass until nothing changes.
- Confusing X+ with the set of FDs. The closure of an attribute set is a set of attributes. The closure F+ of an FD set is the set of all implied FDs, which can be exponentially large. You never compute F+ directly in practice.
- Attributes that never appear on a right side. If an attribute appears on no right side of any FD, it can only be in X+ if it is in X. Such attributes must be part of every candidate key. This shortcut saves time in the next chapter.
- Splitting right sides. An FD A -> BC behaves the same as A -> B and A -> C. The algorithm works with either form, because adding the whole right side at once is equivalent to adding its parts one by one.
Cost
Each pass scans |F| FDs, and each check is a subset test over at most |R| attributes. There are at most |R| productive passes, so a direct implementation runs in O(|R| · |F| · |R|) time. A linear-time version exists. It keeps, for each FD, a counter of left-side attributes not yet in result, and a queue of newly added attributes. For GATE-sized problems the simple loop is enough.
A second example with a trap
Let R(A, B, C, D) with F = { AB -> C, C -> D, D -> A }. Compute B+ and {B, C}+.
- B+: no FD has a left side contained in {B}. AB -> C needs A as well. So B+ = {B}.
- {B, C}+: start with {B, C}. C -> D adds D, giving {B, C, D}. D -> A adds A, giving {A, B, C, D}. AB -> C adds nothing new. So {B, C}+ = R, and {B, C} is a superkey.
Notice that A was reached only after two steps of chaining. Checking only the FDs whose left sides lie in the starting set would have missed it.
Exercise
Let R(A, B, C, D, E, F) with F = { A -> B, BC -> D, D -> E, E -> A, F -> C }. Compute {D, F}+ and say whether {D, F} is a superkey of R. Does F imply DF -> B?
Show answer
Start with {D, F}. D -> E adds E, giving {D, E, F}. E -> A adds A, giving {A, D, E, F}. A -> B adds B, giving {A, B, D, E, F}. F -> C adds C, giving {A, B, C, D, E, F}. BC -> D adds nothing new. So {D, F}+ = {A, B, C, D, E, F}, which is all of R, and {D, F} is a superkey. Since B is in the closure, F implies DF -> B.
Exercise
Under F = { A -> B, B -> C, CD -> E }, show that A -> E is not implied, and name the smallest attribute that you could add to the left side to make the FD hold.
Show answer
A+ = {A, B, C}, which does not contain E, so A -> E is not implied. Adding D gives {A, D}+ = {A, B, C, D, E}, which contains E, so AD -> E holds. The attribute to add is D.