From d1a4616ac423e9b8755b39e05e9713333bc54a49 Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 23:08:16 +0000 Subject: [PATCH] ocaml: phase 5.1 bipartite.ml baseline (7-node bipartite, 4 in color 0) BFS-based 2-coloring of an undirected graph: edges (undirected): 0-1 0-3 1-2 1-4 2-5 3-4 3-6 5-6 Partition: {0, 2, 4, 6} vs {1, 3, 5} (no odd cycles) Returns count of color-0 vertices (4) on success, -1 on odd-cycle detection. Tests Queue-based BFS with a source-loop wrapper for disconnected graphs, `1 - color.(u)` toggle, color-equality conflict check on already-colored neighbors. 164 baseline programs total. --- lib/ocaml/baseline/bipartite.ml | 39 ++++++++++++++++++++++++++++++++ lib/ocaml/baseline/expected.json | 1 + plans/ocaml-on-sx.md | 9 ++++++++ 3 files changed, 49 insertions(+) create mode 100644 lib/ocaml/baseline/bipartite.ml diff --git a/lib/ocaml/baseline/bipartite.ml b/lib/ocaml/baseline/bipartite.ml new file mode 100644 index 00000000..cb027281 --- /dev/null +++ b/lib/ocaml/baseline/bipartite.ml @@ -0,0 +1,39 @@ +let is_bipartite n adj = + let color = Array.make n (-1) in + let ok = ref true in + let q = Queue.create () in + for src = 0 to n - 1 do + if color.(src) = -1 then begin + color.(src) <- 0; + Queue.push src q; + while not (Queue.is_empty q) do + let u = Queue.pop q in + List.iter (fun v -> + if color.(v) = -1 then begin + color.(v) <- 1 - color.(u); + Queue.push v q + end else if color.(v) = color.(u) then + ok := false + ) adj.(u) + done + end + done; + let zeros = ref 0 in + for i = 0 to n - 1 do + if color.(i) = 0 then zeros := !zeros + 1 + done; + if !ok then !zeros else -1 + +;; + +let n = 7 in +let adj = [| + [1; 3]; + [0; 2; 4]; + [1; 5]; + [0; 4; 6]; + [1; 3]; + [2; 6]; + [3; 5] +|] in +is_bipartite n adj diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 84cee1ff..7e246220 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -12,6 +12,7 @@ "bisect.ml": 141, "bigint_add.ml": 28, "binary_heap.ml": 123456789, + "bipartite.ml": 4, "bits.ml": 21, "balance.ml": 3, "base_n.ml": 17, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index c49c7565..e79e89ab 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-10 Phase 5.1 — bipartite.ml baseline (BFS 2-coloring on + 7-node cycle-rich graph → bipartite with 4 vertices in color 0). + Edges: 0-1, 0-3, 1-2, 1-4, 2-5, 3-4, 3-6, 5-6. This forms a + bipartite graph with partition {0,2,4,6} vs {1,3,5}. Returns + count of color-0 vertices (4) on success, -1 on odd-cycle + detection. Tests Queue-based BFS with module-level state in a + loop over all sources (handles disconnected graphs), `1 - color.(u)` + binary toggle, color-equality conflict check. 164 baseline + programs total. - 2026-05-10 Phase 5.1 — egg_drop.ml baseline (worst-case trials for 2 eggs, 36 floors = 8). Classic O(e·f²) DP: dp[e][f] = 1 + min over k of max(dp[e-1][k-1], dp[e][f-k]). Closed form via