diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index f83a769d..d3f2cbbf 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -101,6 +101,7 @@ "merge_intervals.ml": 12, "merge_sort.ml": 44, "merge_two.ml": 441, + "min_cost_path.ml": 12, "module_use.ml": 3, "monotonic.ml": 4, "newton_sqrt.ml": 1414, diff --git a/lib/ocaml/baseline/min_cost_path.ml b/lib/ocaml/baseline/min_cost_path.ml new file mode 100644 index 00000000..0e385c2e --- /dev/null +++ b/lib/ocaml/baseline/min_cost_path.ml @@ -0,0 +1,31 @@ +let h = 4 +let w = 4 + +let cost = [| + [| 1; 3; 1; 2 |]; + [| 1; 5; 1; 3 |]; + [| 4; 2; 1; 4 |]; + [| 1; 6; 2; 3 |] +|] + +let min_cost_path () = + let dp = Array.init h (fun _ -> Array.make w 0) in + dp.(0).(0) <- cost.(0).(0); + for j = 1 to w - 1 do + dp.(0).(j) <- dp.(0).(j - 1) + cost.(0).(j) + done; + for i = 1 to h - 1 do + dp.(i).(0) <- dp.(i - 1).(0) + cost.(i).(0) + done; + for i = 1 to h - 1 do + for j = 1 to w - 1 do + let a = dp.(i - 1).(j) in + let b = dp.(i).(j - 1) in + dp.(i).(j) <- (if a < b then a else b) + cost.(i).(j) + done + done; + dp.(h - 1).(w - 1) + +;; + +min_cost_path () diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 935dd1d9..e8ec7f72 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,16 @@ _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 — min_cost_path.ml baseline (min-cost path + through 4×4 cost grid, top-left to bottom-right with moves + right/down only, optimal = 12). Standard 2D DP: dp[i][j] = min + of (dp[i-1][j], dp[i][j-1]) + cost[i][j]. Cost matrix yields + optimal path 1→1→2→1→1→1→2→3 = 12 (visiting (0,0),(1,0),(2,1), + (2,2),(2,3),(3,3)? actually the path is row-by-row). Tests + nested 2D arrays via Array.init + Array.make, double-nested + for-loops with branched edges (first row, first column, then + general case), `dp.(i-1).(j)` 2-D index read + `dp.(i).(j)<-` + 2-D write. 161 baseline programs total. - 2026-05-10 Phase 5.1 — topo_dfs.ml baseline (DFS-based topo sort on the same 6-node DAG as topo_sort.ml, digit-fingerprint 24135). Cons each node onto the order list AFTER recursing on all its