PDAG#
- class causalbase.graph.PDAG(num_nodes)[source]#
A partially directed acyclic graph over integer nodes
0..num_nodes-1.A PDAG carries two kinds of edges: directed edges
u -> vand undirected edges{u, v}. At most one edge (of either kind, in either orientation) is stored between any pair of nodes.- Parameters:
- num_nodesint
Number of nodes in the graph. Nodes are addressed by the integer indices
0, 1, ..., num_nodes - 1.
- Attributes:
- num_nodesint
Number of nodes in the graph.
Notes
Unlike
DAG,add_directeddoes not check for cycles. A directed cycle surfaces only whento_dag()finds no consistent extension.- add_directed(u, v)[source]#
Add the directed edge u -> v.
Adding an edge that already exists (same orientation) is a no-op. No cycle checking is performed; consistency is enforced only by
to_dag().- Parameters:
- uint
Source node index.
- vint
Target node index.
- Raises:
- ValueError
If u == v (self-loop), if the opposite directed edge v -> u already exists, or if the undirected edge {u, v} already exists – at most one edge is allowed between any pair of nodes.
- add_undirected(u, v)[source]#
Add the undirected edge {u, v}.
Adding an edge that already exists is a no-op.
- Parameters:
- uint
One endpoint index.
- vint
Other endpoint index.
- Raises:
- ValueError
If u == v (self-loop), or if a directed edge already exists between u and v (in either orientation) – at most one edge is allowed between any pair of nodes.
- orient(u, v)[source]#
Orient the undirected edge {u, v} as the directed edge u -> v.
- Parameters:
- uint
Node the oriented edge points from.
- vint
Node the oriented edge points to.
- Raises:
- KeyError
If {u, v} is not an undirected edge of the graph.
- remove_edge(u, v)[source]#
Remove the edge between u and v, of either kind and orientation.
- Parameters:
- uint
One endpoint index.
- vint
Other endpoint index.
- Raises:
- KeyError
If there is no edge (directed either way, or undirected) between u and v.
- adjacency()[source]#
Return the boolean adjacency matrix of the graph.
An undirected edge {u, v} sets both A[u, v] and A[v, u]; a directed edge u -> v sets only A[u, v].
- Returns:
- np.ndarray
A copy of the cached boolean adjacency matrix.
- copy()[source]#
Return an independent deep copy of this graph.
- Returns:
- PDAG
A new PDAG with the same nodes and edges, sharing no mutable state with the original.