Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Why3
why3
Commits
d7eef731
Commit
d7eef731
authored
Oct 19, 2013
by
Andrei Paskevich
Browse files
add a variation of Kadane's algorithm to examples/maximum_subarray
parent
10f8d879
Changes
2
Hide whitespace changes
Inline
Side-by-side
examples/maximum_subarray.mlw
View file @
d7eef731
...
...
@@ -7,6 +7,7 @@
Authors: Jean-Christophe Filliâtre (CNRS)
Guillaume Melquiond (Inria)
Andrei Paskevich (U-PSUD)
*)
module Spec
...
...
@@ -194,3 +195,40 @@ module Algo4
!ms
end
(* A slightly different variation of Kadane's algorithm *)
module Algo5
use import int.Int
use import ref.Refint
use export array.Array
use export array.ArraySum
(*
[| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |]
......|###### maxsum #######|..............
............................. |## curmax ##
*)
let maximum_subarray (a: array int): int
ensures { forall l h: int. 0 <= l <= h <= length a -> sum a l h <= result }
ensures { exists l h: int. 0 <= l <= h <= length a /\ sum a l h = result }
=
let maxsum = ref 0 in
let curmax = ref 0 in
let ghost lo = ref 0 in
let ghost hi = ref 0 in
let ghost cl = ref 0 in
for i = 0 to a.length - 1 do
invariant { forall l : int. 0 <= l <= i -> sum a l i <= !curmax }
invariant { 0 <= !cl <= i /\ sum a !cl i = !curmax }
invariant { forall l h: int. 0 <= l <= h <= i -> sum a l h <= !maxsum }
invariant { 0 <= !lo <= !hi <= i /\ sum a !lo !hi = !maxsum }
curmax += a[i];
if !curmax < 0 then begin curmax := 0; cl := i+1 end;
if !curmax > !maxsum then begin maxsum := !curmax; lo := !cl; hi := i+1 end
done;
!maxsum
end
examples/maximum_subarray/why3session.xml
View file @
d7eef731
This source diff could not be displayed because it is too large. You can
view the blob
instead.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment