Wiki Agenda Contact Version française

Find the shortest path in a directed graph using BFS

Problem 5 of the second verified software competition.

The ZIP file below contains both the source code, the Why3 proof session file and the Coq scripts of the proofs made in Coq. The Why3 source code is then displayed, followed by a summary of the proofs.


Authors: Jean-Christophe Filliâtre

Topics: Graph Algorithms

Tools: Why3

References: The 2nd Verified Software Competition

see also the index (by topic, by tool, by reference, by year)


download ZIP archive
(* The 2nd Verified Software Competition (VSTTE 2012)
   https://sites.google.com/site/vstte2012/compet

   Problem 5:
   Shortest path in a graph using breadth-first search *)

theory Graph

  use int.Int
  use export set.Fset

  type vertex

  val predicate eq (x y: vertex)
    ensures { result <-> x = y }

  function succ vertex : set vertex

  (* there is a path of length n from v1 to v2 *)
  inductive path (v1 v2: vertex) (n: int) =
    | path_empty:
        forall v: vertex. path v v 0
    | path_succ:
        forall v1 v2 v3: vertex, n: int.
        path v1 v2 n -> mem v3 (succ v2) -> path v1 v3 (n+1)

  (* path length is non-negative *)
  lemma path_nonneg:
    forall v1 v2: vertex, n: int. path v1 v2 n -> n >= 0

  (* a non-empty path has a last but one node *)
  lemma path_inversion:
    forall v1 v3: vertex, n: int. n >= 0 ->
    path v1 v3 (n+1) ->
    exists v2: vertex. path v1 v2 n /\ mem v3 (succ v2)

  (* a path starting in a set S that is closed under succ ends up in S *)
  lemma path_closure:
    forall s: set vertex.
    (forall x: vertex. mem x s ->
       forall y: vertex. mem y (succ x) -> mem y s) ->
    forall v1 v2: vertex, n: int. path v1 v2 n ->
    mem v1 s -> mem v2 s

  predicate shortest_path (v1 v2: vertex) (n: int) =
    path v1 v2 n /\
    forall m: int. m < n -> not (path v1 v2 m)

end

module BFS

  use int.Int
  use Graph
  clone impset.Impset as B with type elt = vertex
  use ref.Refint

  exception Found int

  (* global invariant *)
  predicate inv (s t: vertex) (visited current next: set vertex) (d: int) =
    (* current *)
    subset current visited /\
    (forall x: vertex. mem x current -> shortest_path s x d) /\
    (* next *)
    subset next visited /\
    (forall x: vertex. mem x next -> shortest_path s x (d + 1)) /\
    (* visited *)
    (forall x: vertex, m: int. path s x m -> m <= d -> mem x visited) /\
    (forall x: vertex. mem x visited ->
       exists m: int. path s x m /\ m <= d+1) /\
    (* nodes at distance d+1 are either in next or not yet visited *)
    (forall x: vertex. shortest_path s x (d + 1) ->
       mem x next \/ not (mem x visited)) /\
    (* target t *)
    (mem t visited -> mem t current \/ mem t next)

  (* visited\current\next is closed under succ *)
  predicate closure (visited current next: set vertex) (x: vertex) =
    mem x visited -> not (mem x current) -> not (mem x next) ->
    forall y: vertex. mem y (succ x) -> mem y visited

  function (!!) (s: B.t) : set vertex = B.contents s

  val pick_succ (v: vertex) : B.t
    ensures { !!result = succ v }

  (* function fill_next fills set next with the unvisited successors of v *)
  let fill_next (s t v: vertex) (visited current next: B.t) (d:ref int)
    requires { inv s t !!visited !!current !!next !d /\
      shortest_path s v !d /\
      (forall x: vertex. x<> v -> closure !!visited !!current !!next x) }
    ensures { inv s t !!visited !!current !!next !d /\
      subset (succ v) !!visited  /\
      (forall x: vertex. closure !!visited !!current !!next x) }
  = let acc = pick_succ v in
    while not (B.is_empty acc) do
      invariant {
        inv s t !!visited !!current !!next !d /\
        subset !!acc (succ v) /\
        subset (diff (succ v) !!acc) !!visited /\
        (forall x: vertex. x <> v -> closure !!visited !!current !!next x)
      }
      variant { cardinal !!acc }
      let w = B.choose_and_remove acc in
      if not (B.mem w visited) then begin
        B.add w visited;
        B.add w next
      end
    done

  let bfs (s: vertex) (t: vertex)
    ensures { forall d: int. not (path s t d) }
    raises { Found r -> shortest_path s t r }
    diverges (* the set of reachable nodes may be infinite *)
  = let visited = B.empty () in
    let current = ref (B.empty ()) in
    let next    = ref (B.empty ()) in
    B.add s visited;
    B.add s !current;
    let d = ref 0 in
    while not (B.is_empty !current) do
      invariant {
        inv s t !!visited !! !current !! !next !d /\
        (is_empty !! !current -> is_empty !! !next) /\
        (forall x: vertex. closure !!visited !! !current !! !next x) /\
        0 <= !d
      }
      let v = B.choose_and_remove !current in
      if eq v t then raise (Found !d);
      fill_next s t v visited !current !next d;
      if B.is_empty !current then begin
        (current.contents, next.contents) <- (!next, B.empty ());
        incr d
      end
    done;
    assert { not (mem t !!visited) }

end

Why3 Proof Results for Project "vstte12_bfs"

Theory "vstte12_bfs.Graph": fully verified

ObligationsAlt-Ergo 2.0.0CVC4 1.5
path_nonneg------
induction_pr
path_nonneg.00.00---
path_nonneg.1---0.00
path_inversion------
inversion_pr
path_inversion.00.00---
path_inversion.10.00---
path_closure------
induction_pr
path_closure.00.00---
path_closure.1---0.01

Theory "vstte12_bfs.BFS": fully verified

ObligationsAlt-Ergo 2.0.0CVC4 1.5Coq 8.7.1Eprover 2.0Spass 3.7Z3 4.6.0
VC fill_next------------------
split_goal_right
VC fill_next.00.02---------------
VC fill_next.10.01---------------
VC fill_next.20.02---------------
VC fill_next.3------------------
introduce_premises
VC fill_next.3.0------------------
inline_goal
VC fill_next.3.0.0------------------
split_goal_right
VC fill_next.3.0.0.00.03---------------
VC fill_next.3.0.0.10.01---------------
VC fill_next.3.0.0.20.03---------------
VC fill_next.3.0.0.30.04---------------
VC fill_next.3.0.0.40.01---------------
VC fill_next.3.0.0.50.76---------------
VC fill_next.3.0.0.60.01---------------
VC fill_next.3.0.0.70.02---------------
VC fill_next.3.0.0.80.01---------------
VC fill_next.3.0.0.90.11---------------
VC fill_next.3.0.0.10---------0.33------
VC fill_next.40.01---------------
VC fill_next.50.36---------------
VC fill_next.60.09---------------
VC bfs------------------
split_goal_right
VC bfs.00.19---------------
VC bfs.10.00---------------
VC bfs.20.01---------------
VC bfs.3------------------
split_goal_right
VC bfs.3.00.02---------------
VC bfs.3.10.01---------------
VC bfs.3.2------------------
introduce_premises
VC bfs.3.2.0------------------
inline_goal
VC bfs.3.2.0.0---------0.05------
VC bfs.4------------------
split_goal_right
VC bfs.4.0------------------
introduce_premises
VC bfs.4.0.0------------------
inline_goal
VC bfs.4.0.0.0------------------
split_goal_right
VC bfs.4.0.0.0.00.01---------------
VC bfs.4.0.0.0.10.01---------------
VC bfs.4.0.0.0.20.02---------------
VC bfs.4.0.0.0.30.02---------------
VC bfs.4.0.0.0.4------------------
inline_all
VC bfs.4.0.0.0.4.0------------------
remove real,bool,tuple0,unit,ref,( * ),empty,add,remove,union,inter,diff,choose,cardinal1,eq,contents,mem,cardinal,o,o1,visited1,visited2,current2,visited3,Assoc,Unit_def_l,Unit_def_r,Inv_def_l,Inv_def_r,Comm,Assoc1,Mul_distr_l,Mul_distr_r,Comm1,Unitary,NonTrivialRing,Refl,Trans,Antisymm,Total,ZeroLessOne,CompatOrderAdd,CompatOrderMult,extensionality,subset_spec,subset_refl,subset_trans,empty_def,add_spec,remove_spec,add_remove,remove_add,subset_remove,union_spec,inter_spec,diff_spec,subset_diff,choose_spec,cardinal_nonneg,cardinal_empty,cardinal_add,cardinal_remove,cardinal_subset,subset_eq,cardinal1,eq_spec,path_nonneg,path_inversion,path_closure,mem_spec,cardinal_spec,H1,H2,H3,H5,H7,H9,H10,H11,H12,H13,H14,H16,H17,H18,H19,H20,H21,H22,H23
VC bfs.4.0.0.0.4.0.00.120.02---------0.04
VC bfs.4.0.0.0.50.02---------------
VC bfs.4.0.0.0.60.00---------------
VC bfs.4.0.0.0.70.02---------------
VC bfs.4.10.00---------------
VC bfs.4.2------------------
introduce_premises
VC bfs.4.2.0------------------
inline_goal
VC bfs.4.2.0.0---------0.100.05---
VC bfs.4.30.01---------------
VC bfs.50.01---------------
VC bfs.60.02---------------
VC bfs.7------0.54---------