transitiveClosure function

Set<(Logic, Logic)> transitiveClosure(
  1. List<(Logic, Logic)> implications
)

Computes the transitive closure of a list of implications

Uses Warshall's algorithm, as described at http://www.cs.hope.edu/~cusack/Notes/Notes/DiscreteMath/Warshall.pdf.

Implementation

Set<(Logic, Logic)> transitiveClosure(List<(Logic, Logic)> implications) {
  final Set<(Logic, Logic)> fullImplications = implications.toSet();
  final literals = fullImplications
      .map((i) => <Logic>{i.$1, i.$2})
      .fold(<dynamic>{}, (s, e) => s.union(e));

  for (final k in literals) {
    for (final i in literals) {
      if (fullImplications.contains((i, k))) {
        for (final j in literals) {
          if (fullImplications.contains((k, j))) {
            fullImplications.add((i, j));
          }
        }
      }
    }
  }

  return fullImplications;
}