Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / moral.py @ 5cef0f13

History | View | Annotate | Download (1.61 KB)

1
# -*- coding: utf-8 -*-
2
#   Copyright (C) 2011-2019 by
3
#   Julien Klaus <julien.klaus@uni-jena.de>
4
#   All rights reserved.
5
#   BSD license.
6
#   Copyright 2016-2019 NetworkX developers.
7
#   NetworkX is distributed under a BSD license
8
#
9
# Authors: Julien Klaus <julien.klaus@uni-jena.de>
10
r"""Function for computing the moral graph of a directed graph."""
11

    
12
import networkx as nx
13
from networkx.utils import not_implemented_for
14
import itertools
15

    
16
__all__ = ['moral_graph']
17

    
18

    
19
@not_implemented_for('undirected')
20
def moral_graph(G):
21
    r"""Return the Moral Graph
22

23
        Returns the moralized graph of a given directed graph.
24

25
        Parameters
26
        ----------
27
        G : NetworkX graph
28
            Directed graph
29

30
        Returns
31
        -------
32
        H : NetworkX graph
33
            The undirected moralized graph of G
34

35
        Notes
36
        ------
37
        A moral graph is an undirected graph H = (V, E) generated from a
38
        directed Graph, where if a node has more than one parent node, edges
39
        between these parent nodes are inserted and all directed edges become
40
        undirected.
41

42
        https://en.wikipedia.org/wiki/Moral_graph
43

44
        References
45
        ----------
46
        .. [1] Wray L. Buntine. 1995. Chain graphs for learning.
47
               In Proceedings of the Eleventh conference on Uncertainty
48
               in artificial intelligence (UAI'95)
49
    """
50
    if G is None:
51
        raise ValueError("Expected NetworkX graph!")
52

    
53
    H = G.to_undirected()
54
    for preds in G.pred.values():
55
        predecessors_combinations = itertools.combinations(preds, r=2)
56
        H.add_edges_from(predecessors_combinations)
57
    return H