Skip to content

Instantly share code, notes, and snippets.

@utgwkk
Created July 11, 2018 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utgwkk/41543ab519df0a9dfcc134f5d361bbdc to your computer and use it in GitHub Desktop.
Save utgwkk/41543ab519df0a9dfcc134f5d361bbdc to your computer and use it in GitHub Desktop.
Calculating closure of functional dependencies
fds = {
('A', 'B'),
('A', 'G'),
('B', 'C'),
('C', 'D'),
('BC', 'E'),
('CG', 'A'),
}
start = 'B'
output = {start}
while True:
used = set()
for lhs, rhs in fds:
lhsset = set(lhs)
if lhsset <= output:
output.add(rhs)
used.add((lhs, rhs))
fds -= used
if not (fds and used):
break
print(sorted(list(output)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment