expanders.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. """Provides explicit constructions of expander graphs.
  2. """
  3. import itertools
  4. import networkx as nx
  5. __all__ = ["margulis_gabber_galil_graph", "chordal_cycle_graph", "paley_graph"]
  6. # Other discrete torus expanders can be constructed by using the following edge
  7. # sets. For more information, see Chapter 4, "Expander Graphs", in
  8. # "Pseudorandomness", by Salil Vadhan.
  9. #
  10. # For a directed expander, add edges from (x, y) to:
  11. #
  12. # (x, y),
  13. # ((x + 1) % n, y),
  14. # (x, (y + 1) % n),
  15. # (x, (x + y) % n),
  16. # (-y % n, x)
  17. #
  18. # For an undirected expander, add the reverse edges.
  19. #
  20. # Also appearing in the paper of Gabber and Galil:
  21. #
  22. # (x, y),
  23. # (x, (x + y) % n),
  24. # (x, (x + y + 1) % n),
  25. # ((x + y) % n, y),
  26. # ((x + y + 1) % n, y)
  27. #
  28. # and:
  29. #
  30. # (x, y),
  31. # ((x + 2*y) % n, y),
  32. # ((x + (2*y + 1)) % n, y),
  33. # ((x + (2*y + 2)) % n, y),
  34. # (x, (y + 2*x) % n),
  35. # (x, (y + (2*x + 1)) % n),
  36. # (x, (y + (2*x + 2)) % n),
  37. #
  38. def margulis_gabber_galil_graph(n, create_using=None):
  39. r"""Returns the Margulis-Gabber-Galil undirected MultiGraph on `n^2` nodes.
  40. The undirected MultiGraph is regular with degree `8`. Nodes are integer
  41. pairs. The second-largest eigenvalue of the adjacency matrix of the graph
  42. is at most `5 \sqrt{2}`, regardless of `n`.
  43. Parameters
  44. ----------
  45. n : int
  46. Determines the number of nodes in the graph: `n^2`.
  47. create_using : NetworkX graph constructor, optional (default MultiGraph)
  48. Graph type to create. If graph instance, then cleared before populated.
  49. Returns
  50. -------
  51. G : graph
  52. The constructed undirected multigraph.
  53. Raises
  54. ------
  55. NetworkXError
  56. If the graph is directed or not a multigraph.
  57. """
  58. G = nx.empty_graph(0, create_using, default=nx.MultiGraph)
  59. if G.is_directed() or not G.is_multigraph():
  60. msg = "`create_using` must be an undirected multigraph."
  61. raise nx.NetworkXError(msg)
  62. for x, y in itertools.product(range(n), repeat=2):
  63. for u, v in (
  64. ((x + 2 * y) % n, y),
  65. ((x + (2 * y + 1)) % n, y),
  66. (x, (y + 2 * x) % n),
  67. (x, (y + (2 * x + 1)) % n),
  68. ):
  69. G.add_edge((x, y), (u, v))
  70. G.graph["name"] = f"margulis_gabber_galil_graph({n})"
  71. return G
  72. def chordal_cycle_graph(p, create_using=None):
  73. """Returns the chordal cycle graph on `p` nodes.
  74. The returned graph is a cycle graph on `p` nodes with chords joining each
  75. vertex `x` to its inverse modulo `p`. This graph is a (mildly explicit)
  76. 3-regular expander [1]_.
  77. `p` *must* be a prime number.
  78. Parameters
  79. ----------
  80. p : a prime number
  81. The number of vertices in the graph. This also indicates where the
  82. chordal edges in the cycle will be created.
  83. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  84. Graph type to create. If graph instance, then cleared before populated.
  85. Returns
  86. -------
  87. G : graph
  88. The constructed undirected multigraph.
  89. Raises
  90. ------
  91. NetworkXError
  92. If `create_using` indicates directed or not a multigraph.
  93. References
  94. ----------
  95. .. [1] Theorem 4.4.2 in A. Lubotzky. "Discrete groups, expanding graphs and
  96. invariant measures", volume 125 of Progress in Mathematics.
  97. Birkhäuser Verlag, Basel, 1994.
  98. """
  99. G = nx.empty_graph(0, create_using, default=nx.MultiGraph)
  100. if G.is_directed() or not G.is_multigraph():
  101. msg = "`create_using` must be an undirected multigraph."
  102. raise nx.NetworkXError(msg)
  103. for x in range(p):
  104. left = (x - 1) % p
  105. right = (x + 1) % p
  106. # Here we apply Fermat's Little Theorem to compute the multiplicative
  107. # inverse of x in Z/pZ. By Fermat's Little Theorem,
  108. #
  109. # x^p = x (mod p)
  110. #
  111. # Therefore,
  112. #
  113. # x * x^(p - 2) = 1 (mod p)
  114. #
  115. # The number 0 is a special case: we just let its inverse be itself.
  116. chord = pow(x, p - 2, p) if x > 0 else 0
  117. for y in (left, right, chord):
  118. G.add_edge(x, y)
  119. G.graph["name"] = f"chordal_cycle_graph({p})"
  120. return G
  121. def paley_graph(p, create_using=None):
  122. r"""Returns the Paley $\frac{(p-1)}{2}$ -regular graph on $p$ nodes.
  123. The returned graph is a graph on $\mathbb{Z}/p\mathbb{Z}$ with edges between $x$ and $y$
  124. if and only if $x-y$ is a nonzero square in $\mathbb{Z}/p\mathbb{Z}$.
  125. If $p \equiv 1 \pmod 4$, $-1$ is a square in $\mathbb{Z}/p\mathbb{Z}$ and therefore $x-y$ is a square if and
  126. only if $y-x$ is also a square, i.e the edges in the Paley graph are symmetric.
  127. If $p \equiv 3 \pmod 4$, $-1$ is not a square in $\mathbb{Z}/p\mathbb{Z}$ and therefore either $x-y$ or $y-x$
  128. is a square in $\mathbb{Z}/p\mathbb{Z}$ but not both.
  129. Note that a more general definition of Paley graphs extends this construction
  130. to graphs over $q=p^n$ vertices, by using the finite field $F_q$ instead of $\mathbb{Z}/p\mathbb{Z}$.
  131. This construction requires to compute squares in general finite fields and is
  132. not what is implemented here (i.e `paley_graph(25)` does not return the true
  133. Paley graph associated with $5^2$).
  134. Parameters
  135. ----------
  136. p : int, an odd prime number.
  137. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  138. Graph type to create. If graph instance, then cleared before populated.
  139. Returns
  140. -------
  141. G : graph
  142. The constructed directed graph.
  143. Raises
  144. ------
  145. NetworkXError
  146. If the graph is a multigraph.
  147. References
  148. ----------
  149. Chapter 13 in B. Bollobas, Random Graphs. Second edition.
  150. Cambridge Studies in Advanced Mathematics, 73.
  151. Cambridge University Press, Cambridge (2001).
  152. """
  153. G = nx.empty_graph(0, create_using, default=nx.DiGraph)
  154. if G.is_multigraph():
  155. msg = "`create_using` cannot be a multigraph."
  156. raise nx.NetworkXError(msg)
  157. # Compute the squares in Z/pZ.
  158. # Make it a set to uniquify (there are exactly (p-1)/2 squares in Z/pZ
  159. # when is prime).
  160. square_set = {(x**2) % p for x in range(1, p) if (x**2) % p != 0}
  161. for x in range(p):
  162. for x2 in square_set:
  163. G.add_edge(x, (x + x2) % p)
  164. G.graph["name"] = f"paley({p})"
  165. return G