HEX
Server: Apache
System: Linux vps-cdc32557.vps.ovh.ca 5.15.0-156-generic #166-Ubuntu SMP Sat Aug 9 00:02:46 UTC 2025 x86_64
User: hanode (1017)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //lib/python3/dist-packages/networkx/algorithms/moral.py
# -*- coding: utf-8 -*-
#   Copyright (C) 2011-2019 by
#   Julien Klaus <julien.klaus@uni-jena.de>
#   All rights reserved.
#   BSD license.
#   Copyright 2016-2019 NetworkX developers.
#   NetworkX is distributed under a BSD license
#
# Authors: Julien Klaus <julien.klaus@uni-jena.de>
r"""Function for computing the moral graph of a directed graph."""

import networkx as nx
from networkx.utils import not_implemented_for
import itertools

__all__ = ['moral_graph']


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

        Returns the moralized graph of a given directed graph.

        Parameters
        ----------
        G : NetworkX graph
            Directed graph

        Returns
        -------
        H : NetworkX graph
            The undirected moralized graph of G

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

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

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

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