From 90ba37ecc8c6568788966786dc55fbca6e38aec4 Mon Sep 17 00:00:00 2001 From: giles Date: Mon, 11 May 2026 04:04:42 +0000 Subject: [PATCH] ocaml: phase 5.1 activity_select.ml baseline (greedy non-overlap = 4) Activity selection by earliest end time. Manual bubble sort over (start, end) tuple array, then sweep accepting whenever the next start >= last_end. intervals: (1,4) (3,5) (0,6) (5,7) (3,8) (5,9) (6,10) (8,11) (8,12) (2,13) (12,14) selection: (1,4) -> (5,7) -> (8,11) -> (12,14) = 4 activities Tests double-loop bubble sort on tuple array with let-pattern destructure for swap-key extraction, in-place swap of tuple cells. 192 baseline programs total. --- lib/ocaml/baseline/activity_select.ml | 31 +++++++++++++++++++++++++++ lib/ocaml/baseline/expected.json | 1 + plans/ocaml-on-sx.md | 9 ++++++++ 3 files changed, 41 insertions(+) create mode 100644 lib/ocaml/baseline/activity_select.ml diff --git a/lib/ocaml/baseline/activity_select.ml b/lib/ocaml/baseline/activity_select.ml new file mode 100644 index 00000000..77bb6114 --- /dev/null +++ b/lib/ocaml/baseline/activity_select.ml @@ -0,0 +1,31 @@ +let max_nonoverlap intervals = + let arr = Array.of_list intervals in + let n = Array.length arr in + let sorted = Array.make n (0, 0) in + for i = 0 to n - 1 do + sorted.(i) <- arr.(i) + done; + for i = 0 to n - 1 do + for j = 0 to n - 2 - i do + let (_, e1) = sorted.(j) in + let (_, e2) = sorted.(j + 1) in + if e1 > e2 then begin + let t = sorted.(j) in + sorted.(j) <- sorted.(j + 1); + sorted.(j + 1) <- t + end + done + done; + let count = ref 0 in + let last_end = ref (-1000000) in + for i = 0 to n - 1 do + let (s, e) = sorted.(i) in + if s >= !last_end then begin + count := !count + 1; + last_end := e + end + done; + !count +;; + +max_nonoverlap [(1, 4); (3, 5); (0, 6); (5, 7); (3, 8); (5, 9); (6, 10); (8, 11); (8, 12); (2, 13); (12, 14)] diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 3df8f632..b98d1d25 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -1,5 +1,6 @@ { "abundant.ml": 21, + "activity_select.ml": 4, "ackermann.ml": 125, "adler32.ml": 300286872, "anagram_check.ml": 2, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index bb6f2b7c..1b017130 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,15 @@ _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-11 Phase 5.1 — activity_select.ml baseline (greedy + earliest-end-time activity selection on 11 intervals → max + non-overlapping 4). Sort intervals by end time (bubble sort to + avoid relying on List.sort with tuple comparator), then sweep + and accept whenever start ≥ last_end. Optimal selection: + (1,4) → (5,7) → (8,11) → (12,14) = 4 activities. Tests double- + loop bubble sort on tuple array with let-pattern destructure + for swap key extraction, in-place swap of tuple cells via temp. + 192 baseline programs total. - 2026-05-11 Phase 5.1 — count_paths_dag.ml baseline (count source-to-sink paths in the same 6-node DAG as topo_sort.ml, paths 0→5 = 3). Topological sort via Kahn's (BFS), then relax