Closure of Functional Dependency

This program is in Maxima programming language. It finds the closure of functional dependencies.


presentin(lis1,lis2):= block([temp,count:0,present:true],
for item in lis1 do ( if(present=true) then
      if(not member(item,lis2)) then present:false
          else present:true),present)$



fd(fds,key):= block([temp:[],sent,final:[],right,prev,ttemp],
fds:append([[key,key]],fds),
sent:first(fds),
right:second(first(fds)),
while(right#flatten(fds) and fds#[]) do (
ttemp:map(lambda([s],last(s)),fds),
if(sent#[]) then fds:delete(sent,fds),
if(fds#[]) then (
right:unique(append(first(sent),right)),
sent:sublist(fds,lambda([s],is(first(s)=second(sent)))),
if(sent=[]) then sent:sublist(fds,lambda([s],presentin(first(s),right))),
if(sent#[]) then sent:first(sent) else fds:[],
if (sent#[]) then for item in sent do (
    right:unique(flatten(append((item),right))),
    if(prev#right) then (prev:right,
    final:append([right],final)),
    if(sent=[]) then (fds:[],return())
))),(reverse(final)))$


Usage:
fdd:[[[A,B],[C]],[[D,E],[B]],[[C,D],[E]],[[B],[K,R]]]$
/* means AB->C,DE->B,CD->E,B->KR   */

fd(fdd,[A,D,E]);    /*[A,D,E] is the set of candidate keys to check if it can reach all attributes */
=> [[A,D,E],[A,B,D,E],[A,B,D,E,K,R],[A,B,C,D,E,K,R]]

fd(fdd,[A,B,E]);
=> [[A,B,E],[A,B,C,E],[A,B,C,E,K,R]]

fd(fdd,[E,K]);
=> [ ]

No comments:

Post a Comment

Disqus for http://programathing.blogspot.in/