fastChainGraph

pyagrum.fastChainGraph(desc)

Build a graph from a dot-like syntax : 'A->B->C;B->E'.

'->' denotes a directed arc, '<-' a directed arc in the other direction (e.g. 'A->B<-C' means both A and C point to B), '-' an undirected edge, ';' separates independent chains. If every node token in the description is a non-negative integer, those integers are used directly as node ids ; otherwise every token – including numeric-looking ones – is used as a node name. A node name contains no space, '<' nor '>', unless it is written between single quotes ("'Visit Asia'->X", a doubled quote standing for a quote); a quoted name is never a node id.

Note

A single '-' (not '--') is used for edges on purpose : pyagrum.fastMRF already uses '--' to list the variables of a single factor (a clique), a different construct from a chain of pairwise edges. 'A--B' is therefore rejected here rather than silently misread.

Raises:
  • pyagrum.InvalidArgument – If the description is malformed (e.g. 'A<->B', 'A B->C' or 'A->'), or if the nodes are ids and the largest one leaves more than max(number of nodes, 65536) absent ids below it (e.g. '0->2000000000').

  • pyagrum.InvalidArc – If the description requires a directed arc but the target graph type does not support arcs.

  • pyagrum.InvalidEdge – If the description requires an undirected edge but the target graph type does not support edges.

Parameters:

desc (str) – the string containing the dot-like specification

Return type:

pyagrum.ChainGraph

Example

import pyagrum as gum

g = gum.fastChainGraph("A->B-C<-D")
sorted(g.arcs())
# [(0, 1), (3, 2)]
sorted(g.edges())
# [(1, 2)]