1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* SPDX-License-Identifier: MIT
* Copyright (c) 2023 - 2026. The DeepCausality Authors and Contributors. All Rights Reserved.
*/
//! Meek orientation rules (Meek 1995) for completing a PDAG to its CPDAG.
//!
//! Repeatedly orients undirected edges that are *compelled* — forced by the
//! two structural constraints "no new unshielded collider" and "no directed
//! cycle" — until a fixpoint is reached. The result is the unique maximally
//! oriented graph (CPDAG) for the input pattern.
//!
//! Rules R1–R3 are implemented; R4 is **deliberately omitted**, for parity with
//! the reference. R4 is only required to complete a graph that already carries
//! background-knowledge orientations (orientations not arising from
//! v-structures). The Python BRCD reference calls
//! `graphical_models.PDAG.to_complete_pdag` (uhlerlab, MIT —
//! <https://github.com/uhlerlab/graphical_models>), whose completion applies
//! Meek R1–R3 only; this port is therefore at parity with the exact code the
//! reference runs. For a pattern of a DAG, R1–R3 are provably complete
//! (Meek 1995, Theorem 3). Should a future change need to maximally orient a
//! graph carrying background knowledge, R4 would be added then — but that is a
//! deliberate divergence from the reference, not a defect of this port.
//!
//! Each undirected edge `a — b` below is oriented into `a → b` when a rule fires.
use ;
use BTreeSet;
/// Completes a PDAG to its CPDAG in place by closing under Meek rules R1–R3.
///
/// The graph's directed arcs and undirected edges are read through `MixedGraph`'s
/// projection accessors; compelled undirected edges are oriented via `orient`.
/// Undirected edges that no rule forces remain undirected. Terminates because
/// every pass that changes anything orients at least one undirected edge, and
/// the undirected set only shrinks.
///
/// Completion assumes the input is an extendable PDAG. For a contradictory
/// (non-extendable) input where a rule compels *both* directions of an edge,
/// completion picks one and proceeds rather than signalling — consistency is
/// deferred to the caller's validity pass
/// ([`crate::brcd::brcd_validity::is_valid_configuration`]), which rejects the result
/// if completion produced a cycle or a new unshielded collider.
/// True if any of Meek R1–R3 compels the undirected edge `a — b` to `a → b`.
/// R1 (no new collider): there is `k → a` with `k` not adjacent to `b`.
///
/// Orienting `b → a` would make `k → a ← b` a new unshielded collider.
/// R2 (no cycle): there is a directed 2-path `a → k → b`.
///
/// Orienting `b → a` would close the cycle `a → k → b → a`.
/// R3 (no new collider, via two R2 steps): there are `k1 → b`, `k2 → b` with
/// `a — k1`, `a — k2` undirected and `k1`, `k2` non-adjacent.