4 Computes the transitive closure of a graph implemented as a Boolean
7 Authors: Martin Clochard (École Normale Supérieure)
8 Jean-Christophe Filliâtre (CNRS)
11 module WarshallAlgorithm
17 there is a path from i to j, using only vertices smaller than k *)
18 inductive path (matrix bool) int int int =
20 forall m: matrix bool, i j k: int.
21 m.elts i j -> path m i j k
23 forall m: matrix bool, i x j k: int.
24 0 <= x < k -> path m i x k -> path m x j k -> path m i j k
27 forall m i j k1 k2. 0 <= k1 <= k2 ->
28 path m i j k1 -> path m i j k2
31 forall m k. 0 <= k -> forall i j. path m i j (k+1) ->
32 (path m i j k \/ (path m i k k /\ path m k j k))
34 let transitive_closure (m: matrix bool) : matrix bool
35 requires { m.rows = m.columns }
36 ensures { let n = m.rows in
37 forall x y: int. 0 <= x < n -> 0 <= y < n ->
38 result.elts x y <-> path m x y n }
43 invariant { forall x y. 0 <= x < n -> 0 <= y < n ->
44 t.elts x y <-> path m x y k }
46 invariant { forall x y. 0 <= x < n -> 0 <= y < n ->
52 invariant { forall x y. 0 <= x < n -> 0 <= y < n ->
54 if x < i \/ (x = i /\ y < j)
57 set t i j (get t i j || get t i k && get t k j)