Closure of attributes in DBMS Theory - Time & Space Complexity
When working with database attributes, finding the closure helps us know all attributes we can get from a starting set.
We want to understand how the time needed grows as the number of attributes and rules increases.
Analyze the time complexity of the following closure calculation.
-- Given a set of attributes and functional dependencies
-- Find closure of attribute set X
closure = X
repeat
for each functional dependency (A -> B):
if A is subset of closure and B not in closure:
add B to closure
until no new attributes added
This code finds all attributes reachable from X using the given rules.
Look for loops and repeated checks.
- Primary operation: Checking each functional dependency repeatedly.
- How many times: Until no new attributes are added, potentially multiple passes over all dependencies.
As the number of attributes and dependencies grows, the checks increase.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 checks |
| 100 | About 10,000 checks |
| 1000 | About 1,000,000 checks |
Pattern observation: The work grows roughly with the square of the input size.
Time Complexity: O(n^3)
This means the time needed grows roughly with the cube of the number of attributes and dependencies.
[X] Wrong: "The closure calculation finishes in linear time because we just check each dependency once."
[OK] Correct: We often need to check dependencies multiple times as new attributes get added, so the process repeats until no changes occur.
Understanding how closure calculation scales helps you explain database normalization and dependency analysis clearly in interviews.
"What if we stored dependencies in a way that lets us quickly find which ones apply? How would that affect the time complexity?"