From 872302ede18b73ea9bab817f333a8e9dc4e3fd3c Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 04:51:15 +0000 Subject: [PATCH] ocaml: phase 5.1 topo_sort.ml baseline (6-node DAG, all 6 ordered) Kahn's algorithm BFS topological sort: let topo_sort n adj = let in_deg = Array.make n 0 in for i = 0 to n - 1 do List.iter (fun j -> in_deg.(j) <- in_deg.(j) + 1) adj.(i) done; let q = Queue.create () in for i = 0 to n - 1 do if in_deg.(i) = 0 then Queue.push i q done; let count = ref 0 in while not (Queue.is_empty q) do let u = Queue.pop q in count := !count + 1; List.iter (fun v -> in_deg.(v) <- in_deg.(v) - 1; if in_deg.(v) = 0 then Queue.push v q ) adj.(u) done; !count Graph: 0->{1,2}; 1->{3}; 2->{3,4}; 3->{5}; 4->{5}; 5. Acyclic, so all 6 nodes can be ordered. Tests Queue.{create,push,pop,is_empty}, mutable array via closure. 144 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/topo_sort.ml | 32 ++++++++++++++++++++++++++++++++ plans/ocaml-on-sx.md | 8 ++++++++ 3 files changed, 41 insertions(+) create mode 100644 lib/ocaml/baseline/topo_sort.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index ebee2b32..13c5866c 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -129,6 +129,7 @@ "tail_factorial.ml": 479001600, "subset_sum.ml": 8, "tic_tac_toe.ml": 1, + "topo_sort.ml": 6, "word_freq.ml": 8, "xor_cipher.ml": 601, "zerosafe.ml": 28, diff --git a/lib/ocaml/baseline/topo_sort.ml b/lib/ocaml/baseline/topo_sort.ml new file mode 100644 index 00000000..e4d0296c --- /dev/null +++ b/lib/ocaml/baseline/topo_sort.ml @@ -0,0 +1,32 @@ +let topo_sort n adj = + let in_deg = Array.make n 0 in + for i = 0 to n - 1 do + List.iter (fun j -> in_deg.(j) <- in_deg.(j) + 1) adj.(i) + done; + let q = Queue.create () in + for i = 0 to n - 1 do + if in_deg.(i) = 0 then Queue.push i q + done; + let count = ref 0 in + while not (Queue.is_empty q) do + let u = Queue.pop q in + count := !count + 1; + List.iter (fun v -> + in_deg.(v) <- in_deg.(v) - 1; + if in_deg.(v) = 0 then Queue.push v q + ) adj.(u) + done; + !count + +;; + +let n = 6 in +let adj = [| + [1; 2]; + [3]; + [3; 4]; + [5]; + [5]; + [] +|] in +topo_sort n adj diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 9338f7cc..d5e6dca9 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 — topo_sort.ml baseline (Kahn's algorithm + topological sort of a 6-node DAG → all 6 vertices ordered). + Standard BFS approach: compute in-degrees, seed queue with zero- + indegree nodes, pop and decrement neighbours. Graph: 0→{1,2}; + 1→{3}; 2→{3,4}; 3→{5}; 4→{5}; 5. A valid topological order is + 0,1,2,3,4,5. Returns count of sortable nodes (6 means acyclic). + Tests Queue.{create,push,pop,is_empty}, List.iter with closure + capturing `in_deg`, mutable array. 144 baseline programs total. - 2026-05-10 Phase 5.1 — knapsack.ml baseline (0/1 knapsack DP, cap=8 with values [|6;10;12;15;20|] and weights [|1;2;3;4;5|] → max value 36). 1D rolling DP: outer loop over items, inner