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.
7
8"""
9
10import networkx as nx
11from networkx.classes import DiGraph
12
13__all__ = ["triad_graph"]
14
15#: Dictionary mapping triad name to list of directed edges in the
16#: digraph representation of that triad (with nodes 'a', 'b', and 'c').
17TRIAD_EDGES = {
18 "003": [],
19 "012": ["ab"],
20 "102": ["ab", "ba"],
21 "021D": ["ba", "bc"],
22 "021U": ["ab", "cb"],
23 "021C": ["ab", "bc"],
24 "111D": ["ac", "ca", "bc"],
25 "111U": ["ac", "ca", "cb"],
26 "030T": ["ab", "cb", "ac"],
27 "030C": ["ba", "cb", "ac"],
28 "201": ["ab", "ba", "ac", "ca"],
29 "120D": ["bc", "ba", "ac", "ca"],
30 "120U": ["ab", "cb", "ac", "ca"],
31 "120C": ["ab", "bc", "ac", "ca"],
32 "210": ["ab", "bc", "cb", "ac", "ca"],
33 "300": ["ab", "ba", "bc", "cb", "ac", "ca"],
34}
35
36
37@nx._dispatchable(graphs=None, returns_graph=True)
38def triad_graph(triad_name):
39 """Returns the triad graph with the given name.
40
41 Each string in the following tuple is a valid triad name::
42
43 (
44 "003",
45 "012",
46 "102",
47 "021D",
48 "021U",
49 "021C",
50 "111D",
51 "111U",
52 "030T",
53 "030C",
54 "201",
55 "120D",
56 "120U",
57 "120C",
58 "210",
59 "300",
60 )
61
62 Each triad name corresponds to one of the possible valid digraph on
63 three nodes.
64
65 Parameters
66 ----------
67 triad_name : string
68 The name of a triad, as described above.
69
70 Returns
71 -------
72 :class:`~networkx.DiGraph`
73 The digraph on three nodes with the given name. The nodes of the
74 graph are the single-character strings 'a', 'b', and 'c'.
75
76 Raises
77 ------
78 ValueError
79 If `triad_name` is not the name of a triad.
80
81 See also
82 --------
83 triadic_census
84
85 """
86 if triad_name not in TRIAD_EDGES:
87 raise ValueError(
88 f'unknown triad name "{triad_name}"; use one of the triad names'
89 " in the TRIAD_NAMES constant"
90 )
91 G = DiGraph()
92 G.add_nodes_from("abc")
93 G.add_edges_from(TRIAD_EDGES[triad_name])
94 return G