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)]

topologicalOrder()

A topological order of the arcs of a graph (family A): every node comes after its parents.

hasDirectedCycle()

Whether the arcs of a graph (family A) contain a directed cycle.

ancestors()

The ancestors of nodes (family A, or a marked graph): the nodes with a directed path to one of them.

descendants()

The descendants of nodes (family A, or a marked graph): the nodes with a directed path from one of them.

directedPath()

A shortest directed path between two nodes (family A).

hasDirectedPath()

Whether a directed path exists between two nodes (family A).

hasUndirectedCycle()

Whether the edges of a graph (family E) contain a cycle.

isChordal()

Whether the edges of a graph (family E) form a chordal graph: every cycle of length 4 or more has a chord.

lexBFS()

An ordering of the nodes by lexicographic breadth-first search on the edges of a graph (family E).

maximumCardinalitySearch()

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.

isPerfectEliminationOrdering()

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.

undirectedPath()

A shortest path of edges between two nodes (family E).

hasPartiallyDirectedCycle()

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.

mixedOrientedPath()

A shortest path made of arcs (followed in their direction) and edges between two nodes (family AE).

degeneracyOrdering()

A degeneracy (smallest-last) ordering of a graph with arcs and edges (family AE), whatever their orientation.

anteriors()

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 -> (pointing towards them) and -.

isAncestral()

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.

isMaximal()

Whether an ancestral graph (family K) is maximal: two non adjacent nodes are always m-separated by some set of nodes.

cpdag()

The CPDAG (completed PDAG, or essential graph) of the Markov equivalence class of a DAG.

consistentExtension()

A consistent extension of a partially directed graph: a DAG with the same skeleton, the same arcs and the same v-structures.

meekClosure()

The closure of a PDAG under the orientation rules of Meek, if the PDAG has a consistent extension.

orientAsDAG()

A DAG orienting all the edges of a PDAG, preferring the extensions with the smallest parameter count.

separation()

Whether two sets of nodes are separated by a third one, with the criterion of the class of the graph.