Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/generators/triads.py: 54%
13 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-20 07:00 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-20 07:00 +0000
1# See https://github.com/networkx/networkx/pull/1474
2# Copyright 2011 Reya Group <http://www.reyagroup.com>
3# Copyright 2011 Alex Levenson <alex@isnotinvain.com>
4# Copyright 2011 Diederik van Liere <diederik.vanliere@rotman.utoronto.ca>
5"""Functions that generate the triad graphs, that is, the possible
6digraphs on three nodes.
8"""
9import networkx as nx
10from networkx.classes import DiGraph
12__all__ = ["triad_graph"]
14#: Dictionary mapping triad name to list of directed edges in the
15#: digraph representation of that triad (with nodes 'a', 'b', and 'c').
16TRIAD_EDGES = {
17 "003": [],
18 "012": ["ab"],
19 "102": ["ab", "ba"],
20 "021D": ["ba", "bc"],
21 "021U": ["ab", "cb"],
22 "021C": ["ab", "bc"],
23 "111D": ["ac", "ca", "bc"],
24 "111U": ["ac", "ca", "cb"],
25 "030T": ["ab", "cb", "ac"],
26 "030C": ["ba", "cb", "ac"],
27 "201": ["ab", "ba", "ac", "ca"],
28 "120D": ["bc", "ba", "ac", "ca"],
29 "120U": ["ab", "cb", "ac", "ca"],
30 "120C": ["ab", "bc", "ac", "ca"],
31 "210": ["ab", "bc", "cb", "ac", "ca"],
32 "300": ["ab", "ba", "bc", "cb", "ac", "ca"],
33}
36@nx._dispatch(graphs=None)
37def triad_graph(triad_name):
38 """Returns the triad graph with the given name.
40 Each string in the following tuple is a valid triad name::
42 ('003', '012', '102', '021D', '021U', '021C', '111D', '111U',
43 '030T', '030C', '201', '120D', '120U', '120C', '210', '300')
45 Each triad name corresponds to one of the possible valid digraph on
46 three nodes.
48 Parameters
49 ----------
50 triad_name : string
51 The name of a triad, as described above.
53 Returns
54 -------
55 :class:`~networkx.DiGraph`
56 The digraph on three nodes with the given name. The nodes of the
57 graph are the single-character strings 'a', 'b', and 'c'.
59 Raises
60 ------
61 ValueError
62 If `triad_name` is not the name of a triad.
64 See also
65 --------
66 triadic_census
68 """
69 if triad_name not in TRIAD_EDGES:
70 raise ValueError(
71 f'unknown triad name "{triad_name}"; use one of the triad names'
72 " in the TRIAD_NAMES constant"
73 )
74 G = DiGraph()
75 G.add_nodes_from("abc")
76 G.add_edges_from(TRIAD_EDGES[triad_name])
77 return G