// Floyd-Warshall algorithm
// Let's go after another classic:
// https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
// Throughout, we represent a graph as a two-dimensional array of weights -- an
// *adjacency matrix*. Nodes of the graph are natural numbers, corresponding to
// indices within the array.
// Here is the implementation, for you to start figuring out in terms of useful proof annotations.
method AllPairsShortestPaths(ws: array2>) returns (ds: array2 >)
requires ws.Length0 == ws.Length1
ensures ds.Length0 == ds.Length1 == ws.Length0
ensures forall src, dst :: 0 <= src < ws.Length0 && 0 <= dst < ws.Length1 ==> ShortestPath(ws, src, dst, ds[src, dst])
// Nice elegant characterization of correctness, huh?
{
ds := new [ws.Length0, ws.Length1];
if ws.Length0 == 0 {
return;
}
forall u, v | 0 <= u < ws.Length0 && 0 <= v < ws.Length1 {
ds[u, v] := ws[u, v];
}
var k := 0;
while k < ws.Length0
{
var i := 0;
while i < ws.Length0
{
var j := 0;
while j < ws.Length1
{
match (ds[i, k], ds[k, j]) {
case (Some(d1), Some(d2)) => {
match ds[i, j]
case None =>
ds[i, j] := Some(d1 + d2);
case Some(d) =>
if d > d1 + d2 {
ds[i, j] := Some(d1 + d2);
} else {
}
}
case _ =>
}
j := j + 1;
}
i := i + 1;
}
k := k + 1;
}
}
// Here are all the auxiliary definitions we depend on,
// which we stepped through just before starting this exercise.
datatype Option = None | Some(value: T)
// This function computes the length (weight sum) of a *valid* path through the
// graph. It returns None when the path is invalid.
ghost function PathLength(ws: array2>, p: seq): Option
reads ws
{
if |p| <= 1 then
Some(0)
else if p[0] >= ws.Length0 || p[1] >= ws.Length1 || ws[p[0], p[1]].None? then
None
else
match PathLength(ws, p[1..])
case None => None
case Some(l) => Some(ws[p[0], p[1]].value + l)
}
// And here is what makes a path internally consistent w.r.t. a source and
// destination.
ghost predicate Path(ws: array2>, src: nat, dst: nat, p: seq)
reads ws
{
&& |p| >= 2
&& p[0] == src
&& p[|p|-1] == dst
}
// The above but with a required distance
ghost predicate PathOfDistance(ws: array2>, src: nat, dst: nat, dist: nat)
reads ws
{
exists p: seq :: Path(ws, src, dst, p) && PathLength(ws, p) == Some(dist)
}
// Now we can characterize what makes a valid shortest-path length.
// A length of None represents "infinity."
ghost predicate ShortestPath(ws: array2>, src: nat, dst: nat, dist: Option)
reads ws
{
match dist
case None => forall dist' :: !PathOfDistance(ws, src, dst, dist')
case Some(dist) =>
&& PathOfDistance(ws, src, dst, dist)
&& forall dist': nat :: PathOfDistance(ws, src, dst, dist') ==> dist' >= dist
}
// Now we redo some of the last few definitions with an extra parameter
// *maxNode*. We only allow *intermediate* nodes of paths that are *less than*
// this bound.
ghost predicate PathUsing(ws: array2>, maxNode: nat, src: nat, dst: nat, p: seq)
reads ws
{
&& |p| >= 2
&& p[0] == src
&& p[|p|-1] == dst
&& forall i :: 0 < i < |p|-1 ==> p[i] < maxNode
}
ghost predicate PathOfDistanceUsing(ws: array2>, maxNode: nat, src: nat, dst: nat, dist: nat)
reads ws
{
exists p: seq :: PathUsing(ws, maxNode, src, dst, p) && PathLength(ws, p) == Some(dist)
}
ghost predicate ShortestPathUsing(ws: array2>, maxNode: nat, src: nat, dst: nat, dist: Option)
reads ws
{
match dist
case None => forall dist' :: !PathOfDistanceUsing(ws, maxNode, src, dst, dist')
case Some(dist) =>
&& PathOfDistanceUsing(ws, maxNode, src, dst, dist)
&& forall dist': nat :: PathOfDistanceUsing(ws, maxNode, src, dst, dist') ==> dist' >= dist
}
// OK, this next type will be important for how we state key invariants. We
// always convert two-dimensional arrays into this type so that we can reason
// about them functionally.
type FuncArray = seq>>
// Here's the canonical conversion.
ghost function FuncOfArray(ds: array2>): FuncArray
reads ds
{
seq(ds.Length0, i requires 0 <= i < ds.Length0 reads ds =>
seq(ds.Length1, j requires 0 <= j < ds.Length1 reads ds =>
ds[i, j]))
// Sequence comprehensions: neat!
}
// We found that Dafny kept timing out with loop invariants expressed directly
// with quantifiers, so this recursive function function captures the same
// property.
ghost predicate Distances(ws: array2 >, ds: FuncArray, k: nat, i: nat, j: nat, src: nat, dst: nat)
requires ws.Length0 == ws.Length1
requires 0 <= k <= ws.Length0
requires 0 <= i <= ws.Length0
requires 0 <= j <= ws.Length1
requires 0 <= src <= ws.Length0
requires 0 <= dst <= ws.Length1
reads ws
decreases ws.Length0 - src, ws.Length1 - dst
{
if src == ws.Length0 then
true
else if dst == ws.Length1 then
Distances(ws, ds, k, i, j, src+1, 0)
else
0 <= src < |ds| && 0 <= dst < |ds[src]|
&& ShortestPathUsing(ws, if src < i || (src == i && dst < j) then k+1 else k, src, dst, ds[src][dst]) && Distances(ws, ds, k, i, j, src, dst+1)
}