antecedent-graph 0.5.0

Causal graph types (DAG, ADMG, CPDAG, PAG, temporal), separation queries, and traversal workspaces for the Antecedent engine; start with the `antecedent` crate
Documentation
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! Temporal PAG over lagged nodes.
//!
//! SPDX-License-Identifier: MIT OR Apache-2.0

#![allow(clippy::many_single_char_names)]

use std::sync::Arc;

use antecedent_core::{Lag, VariableId};

use crate::error::GraphError;
use crate::marked_storage::{self, AdjEntry};
use crate::pag::Pag;
use crate::types::{DenseNodeId, Endpoint, MarkedEdge, MiddleMark, NodeRef};
use crate::workspace::GraphWorkspace;

/// Temporal PAG: lagged nodes with ancestral-graph marks including circles.
#[derive(Clone, Debug)]
pub struct TemporalPag {
    nodes: Vec<NodeRef>,
    adj: Vec<Vec<AdjEntry>>,
}

impl TemporalPag {
    /// Empty temporal PAG.
    #[must_use]
    pub fn empty() -> Self {
        Self { nodes: Vec::new(), adj: Vec::new() }
    }

    /// Node count.
    #[must_use]
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// Whether empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }

    /// Nodes in dense order.
    #[must_use]
    pub fn nodes(&self) -> &[NodeRef] {
        &self.nodes
    }

    /// Add a lagged node.
    ///
    /// # Errors
    ///
    /// Non-lagged or capacity.
    pub fn add_node(&mut self, node: NodeRef) -> Result<DenseNodeId, GraphError> {
        match node {
            NodeRef::Lagged { .. } => {}
            _ => {
                return Err(GraphError::InvalidEndpoints {
                    message: "TemporalPag accepts only Lagged nodes",
                });
            }
        }
        let id = u32::try_from(self.nodes.len()).map_err(|_| GraphError::TooManyNodes)?;
        self.nodes.push(node);
        self.adj.push(Vec::new());
        Ok(DenseNodeId::from_raw(id))
    }

    /// Add `variable` at `lag`.
    ///
    /// # Errors
    ///
    /// Capacity.
    pub fn add_lagged(
        &mut self,
        variable: VariableId,
        lag: Lag,
    ) -> Result<DenseNodeId, GraphError> {
        self.add_node(NodeRef::Lagged { variable, lag })
    }

    fn validate_node(&self, id: DenseNodeId) -> Result<(), GraphError> {
        if id.as_usize() >= self.node_count() {
            return Err(GraphError::UnknownNode { id: id.raw() });
        }
        Ok(())
    }

    /// Insert a marked edge with temporal constraints (no future→past directed).
    ///
    /// # Errors
    ///
    /// Unknown nodes, duplicates, temporal violations, cycles.
    pub fn insert_marked(&mut self, edge: MarkedEdge) -> Result<(), GraphError> {
        self.validate_node(edge.a)?;
        self.validate_node(edge.b)?;
        if edge.a == edge.b {
            return Err(GraphError::InvalidEndpoints { message: "TemporalPag rejects self-loops" });
        }
        if let (
            NodeRef::Lagged { variable: v1, lag: l1 },
            NodeRef::Lagged { variable: v2, lag: l2 },
        ) = (self.nodes[edge.a.as_usize()], self.nodes[edge.b.as_usize()])
        {
            if v1 == v2 && l1 == l2 && l1.is_contemporaneous() {
                return Err(GraphError::ContemporaneousSelfEdge { variable: v1 });
            }
            // Directed edge must not point future → past (smaller lag = closer to present).
            if let Some((from, to)) = edge.parent_child() {
                if let (NodeRef::Lagged { lag: lf, .. }, NodeRef::Lagged { lag: lt, .. }) =
                    (self.nodes[from.as_usize()], self.nodes[to.as_usize()])
                {
                    // Lag is non-negative steps into the past; edge from larger lag to smaller
                    // lag goes past→present (allowed). from lag < to lag means future→past.
                    if lf.raw() < lt.raw() {
                        return Err(GraphError::InvalidEndpoints {
                            message: "TemporalPag rejects future-to-past directed edges",
                        });
                    }
                }
            }
        }
        marked_storage::insert_marked_finish(&mut self.adj, edge)
    }

    /// Directed insert.
    ///
    /// # Errors
    ///
    /// See [`Self::insert_marked`].
    pub fn insert_directed(
        &mut self,
        from: DenseNodeId,
        to: DenseNodeId,
    ) -> Result<(), GraphError> {
        self.insert_marked(MarkedEdge::directed(from, to))
    }

    /// Circle-arrow insert with empty middle mark.
    ///
    /// # Errors
    ///
    /// See [`Self::insert_marked`].
    pub fn insert_circle_arrow(
        &mut self,
        from: DenseNodeId,
        to: DenseNodeId,
    ) -> Result<(), GraphError> {
        self.insert_circle_arrow_with_middle(from, to, MiddleMark::Empty)
    }

    /// Circle-arrow insert with an LPCMCI middle mark (typically [`MiddleMark::Left`] for lagged).
    ///
    /// # Errors
    ///
    /// See [`Self::insert_marked`].
    pub fn insert_circle_arrow_with_middle(
        &mut self,
        from: DenseNodeId,
        to: DenseNodeId,
        middle: MiddleMark,
    ) -> Result<(), GraphError> {
        self.insert_marked(MarkedEdge {
            a: from,
            b: to,
            at_a: Endpoint::Circle,
            at_b: Endpoint::Arrow,
            middle,
        })
    }

    /// Circle-circle contemporaneous insert with middle mark (typically [`MiddleMark::Unknown`]).
    ///
    /// # Errors
    ///
    /// See [`Self::insert_marked`].
    pub fn insert_circle_circle_with_middle(
        &mut self,
        a: DenseNodeId,
        b: DenseNodeId,
        middle: MiddleMark,
    ) -> Result<(), GraphError> {
        let (lo, hi) = if a.raw() <= b.raw() { (a, b) } else { (b, a) };
        self.insert_marked(MarkedEdge {
            a: lo,
            b: hi,
            at_a: Endpoint::Circle,
            at_b: Endpoint::Circle,
            middle,
        })
    }

    /// Whether edge exists.
    #[must_use]
    pub fn has_edge(&self, a: DenseNodeId, b: DenseNodeId) -> bool {
        self.edge_between(a, b).is_some()
    }

    /// Edge between nodes.
    #[must_use]
    pub fn edge_between(&self, a: DenseNodeId, b: DenseNodeId) -> Option<MarkedEdge> {
        marked_storage::edge_between(&self.adj, a, b)
    }

    /// Neighbors.
    pub fn neighbors(
        &self,
        id: DenseNodeId,
    ) -> impl Iterator<Item = (DenseNodeId, Endpoint, Endpoint)> + '_ {
        marked_storage::neighbors(&self.adj, id)
    }

    /// Set marks on existing edge.
    ///
    /// When the new marks form a definite directed edge, rejects orientations that
    /// would create a directed cycle (same check as [`crate::pag::Pag::set_marks`]).
    /// On cycle, previous marks are restored and [`GraphError::Cycle`] is returned.
    ///
    /// # Errors
    ///
    /// Missing edge or directed cycle after orientation.
    pub fn set_marks(
        &mut self,
        a: DenseNodeId,
        b: DenseNodeId,
        at_a: Endpoint,
        at_b: Endpoint,
    ) -> Result<(), GraphError> {
        self.validate_node(a)?;
        self.validate_node(b)?;
        let Some(previous) = self.edge_between(a, b) else {
            return Err(GraphError::UnknownNode { id: a.raw() });
        };
        marked_storage::set_marks_finish(&mut self.adj, a, b, at_a, at_b, previous)
    }

    /// Mark an existing edge as an `x-x` conflict ([`Endpoint::Conflict`]–[`Endpoint::Conflict`]).
    ///
    /// # Errors
    ///
    /// Missing edge or unknown nodes.
    pub fn mark_conflict(&mut self, a: DenseNodeId, b: DenseNodeId) -> Result<(), GraphError> {
        self.set_marks(a, b, Endpoint::Conflict, Endpoint::Conflict)
    }

    /// Set / merge the LPCMCI middle mark on an existing edge.
    ///
    /// # Errors
    ///
    /// Missing edge.
    pub fn apply_middle(
        &mut self,
        a: DenseNodeId,
        b: DenseNodeId,
        update: MiddleMark,
    ) -> Result<(), GraphError> {
        self.validate_node(a)?;
        self.validate_node(b)?;
        let Some(e) = self.edge_between(a, b) else {
            return Err(GraphError::UnknownNode { id: a.raw() });
        };
        marked_storage::set_middle(&mut self.adj, a, b, e.middle.apply(update))
    }

    /// Replace the middle mark (no merge).
    ///
    /// # Errors
    ///
    /// Missing edge.
    pub fn set_middle(
        &mut self,
        a: DenseNodeId,
        b: DenseNodeId,
        middle: MiddleMark,
    ) -> Result<(), GraphError> {
        self.validate_node(a)?;
        self.validate_node(b)?;
        if self.edge_between(a, b).is_none() {
            return Err(GraphError::UnknownNode { id: a.raw() });
        }
        marked_storage::set_middle(&mut self.adj, a, b, middle)
    }

    /// Middle mark between `a` and `b`, if adjacent.
    #[must_use]
    pub fn middle_between(&self, a: DenseNodeId, b: DenseNodeId) -> Option<MiddleMark> {
        self.edge_between(a, b).map(|e| e.middle)
    }

    /// Remove an edge (both adjacency halves).
    ///
    /// # Errors
    ///
    /// Unknown nodes.
    pub fn remove_edge(&mut self, a: DenseNodeId, b: DenseNodeId) -> Result<(), GraphError> {
        self.validate_node(a)?;
        self.validate_node(b)?;
        if self.edge_between(a, b).is_none() {
            return Err(GraphError::UnknownNode { id: a.raw() });
        }
        marked_storage::remove_edge(&mut self.adj, a, b);
        Ok(())
    }

    /// Force all middle marks to [`MiddleMark::Empty`] (LPCMCI finalization).
    pub fn clear_middle_marks(&mut self) {
        for list in &mut self.adj {
            for e in list.iter_mut() {
                e.middle = MiddleMark::Empty;
            }
        }
    }

    /// Directed reachability.
    #[must_use]
    pub fn reaches_directed(&self, from: DenseNodeId, to: DenseNodeId) -> bool {
        let mut ws = GraphWorkspace::default();
        self.reaches_directed_with(&mut ws, from, to)
    }

    /// Directed reachability reusing a caller-owned workspace.
    #[must_use]
    pub fn reaches_directed_with(
        &self,
        ws: &mut GraphWorkspace,
        from: DenseNodeId,
        to: DenseNodeId,
    ) -> bool {
        marked_storage::reaches_directed(&self.adj, ws, from, to)
    }

    /// View as static [`Pag`] for algorithms that only need adjacency/marks.
    ///
    /// Nodes become `Static(VariableId::from_raw(dense))` — only for algorithm reuse.
    #[must_use]
    pub fn as_static_pag_for_alg(&self) -> Pag {
        let n = u32::try_from(self.node_count()).expect("node fit");
        let mut p = Pag::with_variables(n);
        for i in 0..self.node_count() {
            let a = DenseNodeId::from_raw(u32::try_from(i).expect("node fit"));
            for e in &self.adj[i] {
                if e.neighbor.raw() < a.raw() {
                    continue;
                }
                let edge = MarkedEdge {
                    a,
                    b: e.neighbor,
                    at_a: e.at_self,
                    at_b: e.at_neighbor,
                    middle: e.middle,
                };
                let _ = p.insert_marked(edge);
            }
        }
        p
    }

    /// Convert to a [`TemporalDag`] when every edge is definite-directed (no circles,
    /// undirected, bidirected, or conflict marks).
    ///
    /// # Errors
    ///
    /// [`GraphError::InvalidEndpoints`] when ambiguous marks remain, or insert failure.
    pub fn try_into_temporal_dag(&self) -> Result<crate::TemporalDag, GraphError> {
        use crate::TemporalDag;
        for i in 0..self.node_count() {
            let a = DenseNodeId::from_raw(u32::try_from(i).expect("node fit"));
            for e in &self.adj[i] {
                if e.neighbor.raw() < a.raw() {
                    continue;
                }
                let edge = MarkedEdge {
                    a,
                    b: e.neighbor,
                    at_a: e.at_self,
                    at_b: e.at_neighbor,
                    middle: e.middle,
                };
                if edge.parent_child().is_none() {
                    return Err(GraphError::InvalidEndpoints {
                        message: "cannot complete TemporalPag to TemporalDag while \
                                  circle/undirected/bidirected/conflict marks remain",
                    });
                }
            }
        }
        let mut dag = TemporalDag::empty();
        for node in &self.nodes {
            dag.add_node(*node)?;
        }
        for i in 0..self.node_count() {
            let a = DenseNodeId::from_raw(u32::try_from(i).expect("node fit"));
            for e in &self.adj[i] {
                if e.neighbor.raw() < a.raw() {
                    continue;
                }
                let edge = MarkedEdge {
                    a,
                    b: e.neighbor,
                    at_a: e.at_self,
                    at_b: e.at_neighbor,
                    middle: e.middle,
                };
                if let Some((from, to)) = edge.parent_child() {
                    dag.insert_directed(from, to)?;
                }
            }
        }
        Ok(dag)
    }

    /// Definite-status paths (delegates via static projection of marks).
    ///
    /// # Errors
    ///
    /// Unknown nodes.
    pub fn definite_status_paths(
        &self,
        x: DenseNodeId,
        y: DenseNodeId,
        max_paths: usize,
        max_len: usize,
    ) -> Result<crate::pag::DefiniteStatusPathSearch, GraphError> {
        self.as_static_pag_for_alg().definite_status_paths(x, y, max_paths, max_len)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use antecedent_core::Lag;

    #[test]
    fn rejects_future_to_past_directed() {
        let mut g = TemporalPag::empty();
        let past = g.add_lagged(VariableId::from_raw(0), Lag::from_raw(2)).unwrap();
        let present = g.add_lagged(VariableId::from_raw(0), Lag::from_raw(0)).unwrap();
        // present -> past is future to past
        assert!(g.insert_directed(present, past).is_err());
        // past -> present ok
        g.insert_directed(past, present).unwrap();
    }

    #[test]
    fn allows_circle_arrow() {
        let mut g = TemporalPag::empty();
        let a = g.add_lagged(VariableId::from_raw(0), Lag::from_raw(1)).unwrap();
        let b = g.add_lagged(VariableId::from_raw(1), Lag::from_raw(0)).unwrap();
        g.insert_circle_arrow(a, b).unwrap();
        assert!(g.has_edge(a, b));
    }

    #[test]
    fn set_marks_rejects_directed_cycle_and_restores() {
        // c → b → a with a o→ c; completing a → c would cycle (c already reaches a).
        let mut g = TemporalPag::empty();
        let a = g.add_lagged(VariableId::from_raw(0), Lag::CONTEMPORANEOUS).unwrap();
        let b = g.add_lagged(VariableId::from_raw(1), Lag::CONTEMPORANEOUS).unwrap();
        let c = g.add_lagged(VariableId::from_raw(2), Lag::CONTEMPORANEOUS).unwrap();
        g.insert_directed(c, b).unwrap();
        g.insert_directed(b, a).unwrap();
        g.insert_circle_arrow(a, c).unwrap();
        let err = g.set_marks(a, c, Endpoint::Tail, Endpoint::Arrow).unwrap_err();
        assert!(matches!(err, GraphError::Cycle { .. }));
        let e = g.edge_between(a, c).unwrap();
        let (at_a, at_c) = if e.a == a { (e.at_a, e.at_b) } else { (e.at_b, e.at_a) };
        assert!(matches!(at_a, Endpoint::Circle));
        assert!(matches!(at_c, Endpoint::Arrow));
    }

    #[test]
    fn try_into_temporal_dag_requires_definite_directed() {
        let mut g = TemporalPag::empty();
        let a = g.add_lagged(VariableId::from_raw(0), Lag::from_raw(1)).unwrap();
        let b = g.add_lagged(VariableId::from_raw(1), Lag::CONTEMPORANEOUS).unwrap();
        g.insert_circle_arrow(a, b).unwrap();
        assert!(g.try_into_temporal_dag().is_err());
        g.set_marks(a, b, Endpoint::Tail, Endpoint::Arrow).unwrap();
        let dag = g.try_into_temporal_dag().unwrap();
        assert_eq!(dag.node_count(), 2);
        assert!(dag.children(a).iter().any(|c| *c == b));
    }
}

/// Review artifact for a discovered temporal PAG (pending circle marks).
#[derive(Clone, Debug)]
pub struct TemporalPagReview {
    /// Proposed PAG.
    pub graph: TemporalPag,
    /// Edges that still have at least one circle endpoint `(a,b)` with `a.raw() <= b.raw()`.
    pub pending_circles: Arc<[(DenseNodeId, DenseNodeId)]>,
    /// Algorithm id.
    pub algorithm: Arc<str>,
}

impl TemporalPagReview {
    /// Build review listing all circle-bearing edges.
    #[must_use]
    pub fn from_pag(graph: TemporalPag, algorithm: impl Into<Arc<str>>) -> Self {
        let mut pending = Vec::new();
        for i in 0..graph.node_count() {
            let a = DenseNodeId::from_raw(u32::try_from(i).expect("node fit"));
            for (b, at_a, at_b) in graph.neighbors(a) {
                if b.raw() < a.raw() {
                    continue;
                }
                if matches!(at_a, Endpoint::Circle) || matches!(at_b, Endpoint::Circle) {
                    pending.push((a, b));
                }
            }
        }
        Self { graph, pending_circles: Arc::from(pending), algorithm: algorithm.into() }
    }

    /// Whether no circle marks remain.
    #[must_use]
    pub fn is_complete(&self) -> bool {
        self.pending_circles.is_empty()
    }
}