Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/algorithms/moral.py: 58%

12 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-20 07:00 +0000

1r"""Function for computing the moral graph of a directed graph.""" 

2 

3import itertools 

4 

5import networkx as nx 

6from networkx.utils import not_implemented_for 

7 

8__all__ = ["moral_graph"] 

9 

10 

11@not_implemented_for("undirected") 

12@nx._dispatch 

13def moral_graph(G): 

14 r"""Return the Moral Graph 

15 

16 Returns the moralized graph of a given directed graph. 

17 

18 Parameters 

19 ---------- 

20 G : NetworkX graph 

21 Directed graph 

22 

23 Returns 

24 ------- 

25 H : NetworkX graph 

26 The undirected moralized graph of G 

27 

28 Raises 

29 ------ 

30 NetworkXNotImplemented 

31 If `G` is undirected. 

32 

33 Examples 

34 -------- 

35 >>> G = nx.DiGraph([(1, 2), (2, 3), (2, 5), (3, 4), (4, 3)]) 

36 >>> G_moral = nx.moral_graph(G) 

37 >>> G_moral.edges() 

38 EdgeView([(1, 2), (2, 3), (2, 5), (2, 4), (3, 4)]) 

39 

40 Notes 

41 ----- 

42 A moral graph is an undirected graph H = (V, E) generated from a 

43 directed Graph, where if a node has more than one parent node, edges 

44 between these parent nodes are inserted and all directed edges become 

45 undirected. 

46 

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

48 

49 References 

50 ---------- 

51 .. [1] Wray L. Buntine. 1995. Chain graphs for learning. 

52 In Proceedings of the Eleventh conference on Uncertainty 

53 in artificial intelligence (UAI'95) 

54 """ 

55 H = G.to_undirected() 

56 for preds in G.pred.values(): 

57 predecessors_combinations = itertools.combinations(preds, r=2) 

58 H.add_edges_from(predecessors_combinations) 

59 return H