ocaml: phase 5.1 topo_dfs.ml baseline (DFS topo sort, fingerprint 24135)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s

DFS topological sort — recurse on out-edges first, prepend after:

  let rec dfs v =
    if not visited.(v) then begin
      visited.(v) <- true;
      List.iter dfs adj.(v);
      order := v :: !order
    end

Same 6-node DAG as iter 230's Kahn's-algorithm baseline:
  0 -> {1, 2}
  1 -> {3}
  2 -> {3, 4}
  3 -> {5}
  4 -> {5}
  5

DFS order: [0; 2; 4; 1; 3; 5]
Horner fold: 0->0->2->24->241->2413->24135.

Complementary to topo_sort.ml (Kahn's BFS); tests recursive DFS
with no explicit stack + List.fold_left as a horner reduction.

160 baseline programs total.
This commit is contained in:
2026-05-10 22:27:18 +00:00
parent bcb7db2ea4
commit 5384ff6c42
3 changed files with 36 additions and 0 deletions

View File

@@ -143,6 +143,7 @@
"tarjan_scc.ml": 4,
"subset_sum.ml": 8,
"tic_tac_toe.ml": 1,
"topo_dfs.ml": 24135,
"topo_sort.ml": 6,
"word_freq.ml": 8,
"xor_cipher.ml": 601,

View File

@@ -0,0 +1,26 @@
let n = 6
let adj = [|
[1; 2];
[3];
[3; 4];
[5];
[5];
[]
|]
let visited = Array.make n false
let order = ref []
let rec dfs v =
if not visited.(v) then begin
visited.(v) <- true;
List.iter dfs adj.(v);
order := v :: !order
end
;;
for v = 0 to n - 1 do
dfs v
done;
List.fold_left (fun acc v -> acc * 10 + v) 0 !order