moral_graph#
- graph_tool.generation.moral_graph(g)[git][source]#
Return the moral graph of a directed acyclic graph.
See also
graph_tool.structure.is_d_separatorTest for d-separators
graph_tool.structure.junction_treeJunction tree, built from the moral graph
Notes
The moral graph [lauritzen-local-1988] of a directed graph has the same vertices, and an undirected edge \(\{u, v\}\), for \(u \ne v\), if \(u \to v\) or \(v \to u\) is an edge of the graph, or if \(u\) and \(v\) are both parents (in-neighbors) of a common vertex, i.e. the parents of every vertex are joined (“married”). Self-loops and repeated edges are not included. It is used to turn a Bayesian network into a Markov random field, e.g. in the junction tree algorithm (see
junction_tree()). The time is \(O(V + E + \sum_v k^{\text{in}}_v{}^2)\).References
[lauritzen-local-1988]Steffen L. Lauritzen and David J. Spiegelhalter, “Local computations with probabilities on graphical structures and their application to expert systems”, Journal of the Royal Statistical Society B 50, 157-194 (1988), DOI: 10.1111/j.2517-6161.1988.tb01721.x [sci-hub, @tor]
Examples
>>> g = gt.Graph([(0, 2), (1, 2), (2, 3)]) >>> m = gt.moral_graph(g) >>> print(m.num_edges(), m.edge(0, 1) is not None) 4 True
A larger directed acyclic graph and its moral graph are drawn as follows, with the same vertex positions and the edges between the parents of a common child highlighted:
>>> g = gt.Graph([(0, 3), (1, 3), (1, 4), (2, 4), (3, 5), (4, 5), (4, 6)]) >>> m = gt.moral_graph(g) >>> pos = gt.sfdp_layout(m) >>> color = m.new_ep("string", val="#999999") >>> for e in m.edges(): ... s, t = int(e.source()), int(e.target()) ... if g.edge(s, t) is None and g.edge(t, s) is None: ... color[e] = "#e41a1c" >>> gt.graph_draw(g, pos=g.own_property(pos), vertex_text=g.vertex_index, ... output="moral_graph-dag.pdf") <...> >>> gt.graph_draw(m, pos=pos, vertex_text=m.vertex_index, ... edge_color=color, output="moral_graph-moral.pdf") <...>
Directed acyclic graph with seven vertices.#
Moral graph of the directed acyclic graph above, with the same vertex positions. The red edges join the parents of a common child, and the gray ones are the edges of the original graph without their directions.#