Candidate key finding using closure in DBMS Theory - Time & Space Complexity
Finding candidate keys using closure involves checking attribute combinations to see if they determine all other attributes.
We want to understand how the time needed grows as the number of attributes increases.
Analyze the time complexity of the following pseudocode for candidate key finding using closure.
FOR each subset S of attributes in relation R
Compute closure of S
IF closure of S includes all attributes in R
Mark S as a candidate key
END IF
END FOR
This code tries every possible attribute subset, computes its closure, and checks if it covers all attributes.
Look for repeated steps that take most time.
- Primary operation: Computing closure for each subset of attributes.
- How many times: Once for every subset, which grows as the number of attributes increases.
As the number of attributes (n) grows, the number of subsets grows very fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 8 subsets to check |
| 5 | 32 subsets to check |
| 10 | 1024 subsets to check |
Pattern observation: The number of subsets doubles with each added attribute, causing exponential growth.
Time Complexity: O(2^n * m)
This means the time grows exponentially with the number of attributes, where n is attributes count and m is number of functional dependencies.
[X] Wrong: "Checking only a few subsets is enough to find all candidate keys quickly."
[OK] Correct: Candidate keys can be any subset, so skipping subsets risks missing keys and leads to incorrect results.
Understanding this complexity helps you explain why candidate key discovery can be slow and shows your grasp of database fundamentals.
What if we limited the subsets to only those with a fixed size? How would the time complexity change?
Practice
Solution
Step 1: Understand closure definition
The closure of a set of attributes includes all attributes that can be functionally determined from that set using the given functional dependencies.Step 2: Relate closure to candidate keys
If the closure covers all attributes in the relation, the set is a superkey; minimal such sets are candidate keys.Final Answer:
All attributes functionally determined by the set -> Option AQuick Check:
Closure = attributes reachable from set [OK]
- Confusing closure with number of attributes
- Thinking closure is the primary key itself
- Ignoring functional dependencies in closure
{A, B}?Solution
Step 1: Recall standard closure notation
The closure of a set of attributes X is denoted as X+, meaning all attributes functionally determined by X.Step 2: Match notation to options
{A, B}+uses the correct notation{A, B}+. Other options are not standard or incorrect.Final Answer:
{A, B}+ -> Option AQuick Check:
Closure notation uses plus sign [OK]
- Using star (*) instead of plus (+)
- Writing closure as a function call
- Misplacing braces or parentheses
A -> B, B -> C, C -> D. What is the closure of {A}?Solution
Step 1: Start with attribute set {A}
Initially, closure({A}) = {A}.Step 2: Apply functional dependencies stepwise
From A -> B, add B: closure = {A, B}. Then B -> C adds C: closure = {A, B, C}. Finally, C -> D adds D: closure = {A, B, C, D}.Final Answer:
{A, B, C, D} -> Option CQuick Check:
Closure includes all reachable attributes [OK]
- Stopping after first dependency
- Ignoring transitive dependencies
- Confusing closure with original set
A -> B, B -> C. Closure of {B} is given as {B, A}. What is wrong?Solution
Step 1: Analyze given closure {B, A}
Starting with {B}, applying B -> C adds C, so closure should be {B, C}.Step 2: Check if A can be added
There is no FD that allows B to determine A, so A should not be in closure.Final Answer:
Closure should include C, not A -> Option DQuick Check:
Closure only adds attributes functionally determined [OK]
- Adding attributes not functionally dependent
- Confusing direction of dependencies
- Assuming closure includes all attributes
A -> BC, CD -> E, B -> D. Which of the following is a candidate key?Solution
Step 1: Compute closure of {A}
Start with {A}. From A -> BC, add B and C: closure = {A, B, C}. From B -> D, add D: closure = {A, B, C, D}. From CD -> E, since C and D are in closure, add E: closure = {A, B, C, D, E}.Step 2: Check minimality of {A}
Since closure({A}) includes all attributes, {A} is a superkey. It is minimal because it has only one attribute.Final Answer:
{A} -> Option BQuick Check:
Minimal set with full closure is candidate key [OK]
- Not checking transitive dependencies
- Assuming larger sets are always candidate keys
- Ignoring minimality condition
Ready to prove it?
Pass the test and this pattern moves unknown
