# Attribute Closure Algorithm and its Utilization

Closure of a set F of FDs is the set F+ of all FDs that can be inferred from F. It is also known as complete set of Functional Dependency. It is denoted by F+.

Algorithm : Attribute Closure set

```Algorithm to compute a+, the closure of a under F
Result:= a;
while (changes to Result) do
for each B â†’ Y in F do
Begin
if B âŠ†  Result  then  Result := Result âˆª  Y
End```

Utilization of Attribute Closure –

1. Test whether attribute is superkey or not.

Example :
Let R = (A, B, C, G, H, I) and set of FD are F = { A â†’ B, A â†’ C, CG â†’ H, CG â†’ I, B â†’ H}

Find out (AG)+.

Result = {A, G}

First loop :

```A â†’ B includes B in the Result as AâŠ† Result (which is AG), so Result := Result âˆª B.
Hence Result = {A, G, B}
A â†’ C causes Result to become ABCG.
CG â†’ H causes the Result to become ABCGH.
CG â†’ I causes the Result to become ABCGHI.
B â†’ H causes Result to become ABCGHI.```

Second loop :

```A â†’ B causes the Result to be unchanged i.e. ABCGHI (B is already part of the Result).
A â†’ C causes Result to be unchanged.
CG â†’ H causes the Result to be unchanged.
CG â†’ I causes the Result to be unchanged.
B â†’ H causes Result to be unchanged.```

At the end of the second loop, the Result does not change so exit from the loop.

(AG)+ = {A, B, C, G, H, I}

Conclusion: AG is a super key as all other attributes can be determined by it.

2. Check whether Fd exists :

Example :
Let R = (A, B, C, G, H, I) and set of FD are F = { A â†’B, A â†’ C, CG â†’ H, CG â†’ I, B â†’ H}

Check HB â†’ I holds or not

Result = {H, B}

First loop :

```In A â†’ B as A âŠ† Result (which is HB) so nothing will be added.
In A â†’ C nothing added.
CG â†’ H (CG âŠ† Result (which is HB) so nothing added)

At the end of the first loop, the Result does not change so exit from the loop.

Conclusion: HB â†’ I does not hold.

3. An alternate way to find Closure of FDs (F+).

Example :
Given a relation R (A, B, C, D, E, F) and set of FDs F: {A â†’ BC, E â†’ CF, B â†’ E, CD â†’ EF}

Find out closure of {A, B}+

Step-1: Result = AB

Step-2: First loop

```Result = ABC for A â†’ BC, A âŠ† Result so Result = Result âˆª BC.
Result = ABC for Eâ†’ CF, E âˆ‰ Result so Result = Result.
Result = ABCE for B â†’ E, B âŠ†Result so Result = Result âˆª E.
Result = ABCE for CD â†’ EF, CD âˆ‰ Result so Result = Result.```

The Result before step2 is AB and after step 2 is ABCE which is different so repeat the same as step 2.

Step-3: Second loop

```Result = ABCE for A â†’ BC, A âŠ† Result so Result = Result âˆª BC.
Result = ABCEF for E â†’ CF, E âŠ† Result so Result = Result âˆª CF.
Result = ABCEF for B â†’ E, B âŠ† Result so Result = Result âˆª E.
Result = ABCEF for CD â†’ EF, CD âˆ‰ Result so Result = Result.```

The Result before step 3 is ABCE and that after step 3 is ABCEF which is different, so repeat the same as step 3.

Step-4: Third loop

```Result = ABCEF for A â†’ BC, A âŠ† Result so Result = Result âˆª BC.
Result = ABCEF for E â†’ CF, E âŠ† Result so Result = Result âˆª CF.
Result = ABCEF for B â†’ E, B âŠ† Result so Result = Result âˆª E.
Result = ABCEF for CD â†’ EF, CD âˆ‰ Result so Result = Result.```

The Result before step4 is ABCEF and after step 3 is ABCEF which is the same so stop.

So Closure of {A, B}+ is {A, B, C, E, F}.

Conclusion: For every attribute/set of attributes we can find closure. This Results in F+.