From 526ffbb5f0fdd7d3d0c67257a7c04e29d6f7bf75 Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 05:41:02 +0000 Subject: [PATCH] ocaml: phase 5.1 floyd_warshall.ml baseline (4-node APSP, dist(0,3)=9) Floyd-Warshall all-pairs shortest path with triple-nested for-loop: for k = 0 to n - 1 do for i = 0 to n - 1 do for j = 0 to n - 1 do if d.(i).(k) + d.(k).(j) < d.(i).(j) then d.(i).(j) <- d.(i).(k) + d.(k).(j) done done done Graph (4 nodes, directed): 0->1 weight 5, 0->3 weight 10, 1->2 weight 3, 2->3 weight 1 Direct edge 0->3 = 10, but path 0->1->2->3 = 5+3+1 = 9. Tests 2D array via Array.init with closure, nested .(i).(j) read + write, triple-nested for, in-place mutation under aliasing. 148 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/floyd_warshall.ml | 26 ++++++++++++++++++++++++++ plans/ocaml-on-sx.md | 8 ++++++++ 3 files changed, 35 insertions(+) create mode 100644 lib/ocaml/baseline/floyd_warshall.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 675c2dd7..dae10217 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -70,6 +70,7 @@ "fizz_classifier.ml": 540, "fizzbuzz.ml": 57, "flatten_tree.ml": 28, + "floyd_warshall.ml": 9, "lis.ml": 6, "list_ops.ml": 30, "luhn.ml": 2, diff --git a/lib/ocaml/baseline/floyd_warshall.ml b/lib/ocaml/baseline/floyd_warshall.ml new file mode 100644 index 00000000..2c642e14 --- /dev/null +++ b/lib/ocaml/baseline/floyd_warshall.ml @@ -0,0 +1,26 @@ +let inf_int = 1000000 + +let floyd n graph = + let d = Array.init n (fun i -> + Array.init n (fun j -> graph.(i).(j))) in + for k = 0 to n - 1 do + for i = 0 to n - 1 do + for j = 0 to n - 1 do + if d.(i).(k) + d.(k).(j) < d.(i).(j) then + d.(i).(j) <- d.(i).(k) + d.(k).(j) + done + done + done; + d + +;; + +let n = 4 in +let g = Array.init n (fun _ -> Array.make n inf_int) in +for i = 0 to n - 1 do g.(i).(i) <- 0 done; +g.(0).(1) <- 5; +g.(0).(3) <- 10; +g.(1).(2) <- 3; +g.(2).(3) <- 1; +let d = floyd n g in +d.(0).(3) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index b2a64fea..b564e78a 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,14 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-10 Phase 5.1 — floyd_warshall.ml baseline (all-pairs + shortest path on 4-node weighted graph, dist 0→3 = 9). Standard + O(n³) DP: for each intermediate vertex k, relax all (i,j) pairs. + Uses a 2D array implemented as `Array.init n (fun _ -> Array.make + n inf)`, exercising nested array indexing `g.(i).(j)`. Direct + edge 0→3 weighs 10; via 0→1→2→3 = 5+3+1 = 9. Tests 2D array + construction with closures, triple-nested for-loops, nested + `.(i).(j)<-` mutation. 148 baseline programs total. - 2026-05-10 Phase 5.1 — mst_kruskal.ml baseline (Kruskal MST on 5-node, 6-edge graph → MST weight 11). Sort edges by weight, greedily add edges whose endpoints are in different components