Algorithms on graphs
The module pyagrum.graph gathers the algorithms on graphs: one function per algorithm, whatever the
kind of graph. It is imported explicitly:
import pyagrum.graph as gg
A worked example: from a DAG to its Markov equivalence class and back.
import pyagrum as gum
import pyagrum.graph as gg
dag = gum.fastDAG("A->B->C;D->C;C->E")
gg.topologicalOrder(dag)
# [3, 0, 1, 2, 4]
gg.ancestors(dag, "C")
# {0, 1, 3}
# the class of the DAG: its CPDAG, a graph with arcs (compelled) and edges (reversible)
cpdag = gg.cpdag(dag)
sorted(cpdag.arcs()), sorted(cpdag.edges())
# ([(1, 2), (2, 4), (3, 2)], [(0, 1)])
gg.isChordal(cpdag) # the chain components of a CPDAG are chordal
# True
# the same d-separations in the DAG and in its class
gg.separation(dag, "A", "D"), gg.separation(cpdag, "A", "D")
# (True, True)
gg.separation(dag, "A", "D", "E"), gg.separation(cpdag, "A", "D", "E")
# (False, False)
# back to a DAG of the class
sorted(gg.consistentExtension(cpdag).arcs())
# [(0, 1), (1, 2), (2, 4), (3, 2)]
A topological order of the arcs of a graph (family A): every node comes after its parents. |
|
Whether the arcs of a graph (family A) contain a directed cycle. |
|
The ancestors of nodes (family A, or a marked graph): the nodes with a directed path to one of them. |
|
The descendants of nodes (family A, or a marked graph): the nodes with a directed path from one of them. |
|
A shortest directed path between two nodes (family A). |
|
Whether a directed path exists between two nodes (family A). |
|
Whether the edges of a graph (family E) contain a cycle. |
|
Whether the edges of a graph (family E) form a chordal graph: every cycle of length 4 or more has a chord. |
|
An ordering of the nodes by lexicographic breadth-first search on the edges of a graph (family E). |
|
An ordering of the nodes by maximum cardinality search on the edges of a graph (family E): each node has the largest number of already visited neighbours. |
|
Whether an ordering of all the nodes is a perfect elimination ordering of the edges of a graph (family E): the neighbours of each node that come after it form a clique. |
|
A shortest path of edges between two nodes (family E). |
|
Whether a graph with arcs and edges (family AE) has a partially directed cycle: a cycle made of arcs (followed in their direction) and edges, with at least one arc. |
|
A shortest path made of arcs (followed in their direction) and edges between two nodes (family AE). |
|
A degeneracy (smallest-last) ordering of a graph with arcs and edges (family AE), whatever their orientation. |
|
The anterior nodes of nodes (family AE, or a marked graph): the nodes with a path made of arcs (followed in their direction) and edges to one of them; in a marked graph, of edges |
|
Whether a marked graph (family K) is an ancestral graph: no circle, no node anterior to one of its parents or spouses, and no parent or spouse for a node with an undirected edge. |
|
Whether an ancestral graph (family K) is maximal: two non adjacent nodes are always m-separated by some set of nodes. |
|
The CPDAG (completed PDAG, or essential graph) of the Markov equivalence class of a DAG. |
|
A consistent extension of a partially directed graph: a DAG with the same skeleton, the same arcs and the same v-structures. |
|
The closure of a PDAG under the orientation rules of Meek, if the PDAG has a consistent extension. |
|
A DAG orienting all the edges of a PDAG, preferring the extensions with the smallest parameter count. |
|
Whether two sets of nodes are separated by a third one, with the criterion of the class of the graph. |