Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/algorithms/distance_regular.py: 30%
43 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"""
2=======================
3Distance-regular graphs
4=======================
5"""
7import networkx as nx
8from networkx.utils import not_implemented_for
10from .distance_measures import diameter
12__all__ = [
13 "is_distance_regular",
14 "is_strongly_regular",
15 "intersection_array",
16 "global_parameters",
17]
20@nx._dispatch
21def is_distance_regular(G):
22 """Returns True if the graph is distance regular, False otherwise.
24 A connected graph G is distance-regular if for any nodes x,y
25 and any integers i,j=0,1,...,d (where d is the graph
26 diameter), the number of vertices at distance i from x and
27 distance j from y depends only on i,j and the graph distance
28 between x and y, independently of the choice of x and y.
30 Parameters
31 ----------
32 G: Networkx graph (undirected)
34 Returns
35 -------
36 bool
37 True if the graph is Distance Regular, False otherwise
39 Examples
40 --------
41 >>> G = nx.hypercube_graph(6)
42 >>> nx.is_distance_regular(G)
43 True
45 See Also
46 --------
47 intersection_array, global_parameters
49 Notes
50 -----
51 For undirected and simple graphs only
53 References
54 ----------
55 .. [1] Brouwer, A. E.; Cohen, A. M.; and Neumaier, A.
56 Distance-Regular Graphs. New York: Springer-Verlag, 1989.
57 .. [2] Weisstein, Eric W. "Distance-Regular Graph."
58 http://mathworld.wolfram.com/Distance-RegularGraph.html
60 """
61 try:
62 intersection_array(G)
63 return True
64 except nx.NetworkXError:
65 return False
68def global_parameters(b, c):
69 """Returns global parameters for a given intersection array.
71 Given a distance-regular graph G with integers b_i, c_i,i = 0,....,d
72 such that for any 2 vertices x,y in G at a distance i=d(x,y), there
73 are exactly c_i neighbors of y at a distance of i-1 from x and b_i
74 neighbors of y at a distance of i+1 from x.
76 Thus, a distance regular graph has the global parameters,
77 [[c_0,a_0,b_0],[c_1,a_1,b_1],......,[c_d,a_d,b_d]] for the
78 intersection array [b_0,b_1,.....b_{d-1};c_1,c_2,.....c_d]
79 where a_i+b_i+c_i=k , k= degree of every vertex.
81 Parameters
82 ----------
83 b : list
85 c : list
87 Returns
88 -------
89 iterable
90 An iterable over three tuples.
92 Examples
93 --------
94 >>> G = nx.dodecahedral_graph()
95 >>> b, c = nx.intersection_array(G)
96 >>> list(nx.global_parameters(b, c))
97 [(0, 0, 3), (1, 0, 2), (1, 1, 1), (1, 1, 1), (2, 0, 1), (3, 0, 0)]
99 References
100 ----------
101 .. [1] Weisstein, Eric W. "Global Parameters."
102 From MathWorld--A Wolfram Web Resource.
103 http://mathworld.wolfram.com/GlobalParameters.html
105 See Also
106 --------
107 intersection_array
108 """
109 return ((y, b[0] - x - y, x) for x, y in zip(b + [0], [0] + c))
112@not_implemented_for("directed", "multigraph")
113@nx._dispatch
114def intersection_array(G):
115 """Returns the intersection array of a distance-regular graph.
117 Given a distance-regular graph G with integers b_i, c_i,i = 0,....,d
118 such that for any 2 vertices x,y in G at a distance i=d(x,y), there
119 are exactly c_i neighbors of y at a distance of i-1 from x and b_i
120 neighbors of y at a distance of i+1 from x.
122 A distance regular graph's intersection array is given by,
123 [b_0,b_1,.....b_{d-1};c_1,c_2,.....c_d]
125 Parameters
126 ----------
127 G: Networkx graph (undirected)
129 Returns
130 -------
131 b,c: tuple of lists
133 Examples
134 --------
135 >>> G = nx.icosahedral_graph()
136 >>> nx.intersection_array(G)
137 ([5, 2, 1], [1, 2, 5])
139 References
140 ----------
141 .. [1] Weisstein, Eric W. "Intersection Array."
142 From MathWorld--A Wolfram Web Resource.
143 http://mathworld.wolfram.com/IntersectionArray.html
145 See Also
146 --------
147 global_parameters
148 """
149 # test for regular graph (all degrees must be equal)
150 degree = iter(G.degree())
151 (_, k) = next(degree)
152 for _, knext in degree:
153 if knext != k:
154 raise nx.NetworkXError("Graph is not distance regular.")
155 k = knext
156 path_length = dict(nx.all_pairs_shortest_path_length(G))
157 diameter = max(max(path_length[n].values()) for n in path_length)
158 bint = {} # 'b' intersection array
159 cint = {} # 'c' intersection array
160 for u in G:
161 for v in G:
162 try:
163 i = path_length[u][v]
164 except KeyError as err: # graph must be connected
165 raise nx.NetworkXError("Graph is not distance regular.") from err
166 # number of neighbors of v at a distance of i-1 from u
167 c = len([n for n in G[v] if path_length[n][u] == i - 1])
168 # number of neighbors of v at a distance of i+1 from u
169 b = len([n for n in G[v] if path_length[n][u] == i + 1])
170 # b,c are independent of u and v
171 if cint.get(i, c) != c or bint.get(i, b) != b:
172 raise nx.NetworkXError("Graph is not distance regular")
173 bint[i] = b
174 cint[i] = c
175 return (
176 [bint.get(j, 0) for j in range(diameter)],
177 [cint.get(j + 1, 0) for j in range(diameter)],
178 )
181# TODO There is a definition for directed strongly regular graphs.
182@not_implemented_for("directed", "multigraph")
183@nx._dispatch
184def is_strongly_regular(G):
185 """Returns True if and only if the given graph is strongly
186 regular.
188 An undirected graph is *strongly regular* if
190 * it is regular,
191 * each pair of adjacent vertices has the same number of neighbors in
192 common,
193 * each pair of nonadjacent vertices has the same number of neighbors
194 in common.
196 Each strongly regular graph is a distance-regular graph.
197 Conversely, if a distance-regular graph has diameter two, then it is
198 a strongly regular graph. For more information on distance-regular
199 graphs, see :func:`is_distance_regular`.
201 Parameters
202 ----------
203 G : NetworkX graph
204 An undirected graph.
206 Returns
207 -------
208 bool
209 Whether `G` is strongly regular.
211 Examples
212 --------
214 The cycle graph on five vertices is strongly regular. It is
215 two-regular, each pair of adjacent vertices has no shared neighbors,
216 and each pair of nonadjacent vertices has one shared neighbor::
218 >>> G = nx.cycle_graph(5)
219 >>> nx.is_strongly_regular(G)
220 True
222 """
223 # Here is an alternate implementation based directly on the
224 # definition of strongly regular graphs:
225 #
226 # return (all_equal(G.degree().values())
227 # and all_equal(len(common_neighbors(G, u, v))
228 # for u, v in G.edges())
229 # and all_equal(len(common_neighbors(G, u, v))
230 # for u, v in non_edges(G)))
231 #
232 # We instead use the fact that a distance-regular graph of diameter
233 # two is strongly regular.
234 return is_distance_regular(G) and diameter(G) == 2