atlas.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. """
  2. Generators for the small graph atlas.
  3. """
  4. import gzip
  5. import os
  6. import os.path
  7. from itertools import islice
  8. import networkx as nx
  9. __all__ = ["graph_atlas", "graph_atlas_g"]
  10. #: The total number of graphs in the atlas.
  11. #:
  12. #: The graphs are labeled starting from 0 and extending to (but not
  13. #: including) this number.
  14. NUM_GRAPHS = 1253
  15. #: The absolute path representing the directory containing this file.
  16. THIS_DIR = os.path.dirname(os.path.abspath(__file__))
  17. #: The path to the data file containing the graph edge lists.
  18. #:
  19. #: This is the absolute path of the gzipped text file containing the
  20. #: edge list for each graph in the atlas. The file contains one entry
  21. #: per graph in the atlas, in sequential order, starting from graph
  22. #: number 0 and extending through graph number 1252 (see
  23. #: :data:`NUM_GRAPHS`). Each entry looks like
  24. #:
  25. #: .. sourcecode:: text
  26. #:
  27. #: GRAPH 6
  28. #: NODES 3
  29. #: 0 1
  30. #: 0 2
  31. #:
  32. #: where the first two lines are the graph's index in the atlas and the
  33. #: number of nodes in the graph, and the remaining lines are the edge
  34. #: list.
  35. #:
  36. #: This file was generated from a Python list of graphs via code like
  37. #: the following::
  38. #:
  39. #: import gzip
  40. #: from networkx.generators.atlas import graph_atlas_g
  41. #: from networkx.readwrite.edgelist import write_edgelist
  42. #:
  43. #: with gzip.open('atlas.dat.gz', 'wb') as f:
  44. #: for i, G in enumerate(graph_atlas_g()):
  45. #: f.write(bytes(f'GRAPH {i}\n', encoding='utf-8'))
  46. #: f.write(bytes(f'NODES {len(G)}\n', encoding='utf-8'))
  47. #: write_edgelist(G, f, data=False)
  48. #:
  49. ATLAS_FILE = os.path.join(THIS_DIR, "atlas.dat.gz")
  50. def _generate_graphs():
  51. """Sequentially read the file containing the edge list data for the
  52. graphs in the atlas and generate the graphs one at a time.
  53. This function reads the file given in :data:`.ATLAS_FILE`.
  54. """
  55. with gzip.open(ATLAS_FILE, "rb") as f:
  56. line = f.readline()
  57. while line and line.startswith(b"GRAPH"):
  58. # The first two lines of each entry tell us the index of the
  59. # graph in the list and the number of nodes in the graph.
  60. # They look like this:
  61. #
  62. # GRAPH 3
  63. # NODES 2
  64. #
  65. graph_index = int(line[6:].rstrip())
  66. line = f.readline()
  67. num_nodes = int(line[6:].rstrip())
  68. # The remaining lines contain the edge list, until the next
  69. # GRAPH line (or until the end of the file).
  70. edgelist = []
  71. line = f.readline()
  72. while line and not line.startswith(b"GRAPH"):
  73. edgelist.append(line.rstrip())
  74. line = f.readline()
  75. G = nx.Graph()
  76. G.name = f"G{graph_index}"
  77. G.add_nodes_from(range(num_nodes))
  78. G.add_edges_from(tuple(map(int, e.split())) for e in edgelist)
  79. yield G
  80. def graph_atlas(i):
  81. """Returns graph number `i` from the Graph Atlas.
  82. For more information, see :func:`.graph_atlas_g`.
  83. Parameters
  84. ----------
  85. i : int
  86. The index of the graph from the atlas to get. The graph at index
  87. 0 is assumed to be the null graph.
  88. Returns
  89. -------
  90. list
  91. A list of :class:`~networkx.Graph` objects, the one at index *i*
  92. corresponding to the graph *i* in the Graph Atlas.
  93. See also
  94. --------
  95. graph_atlas_g
  96. Notes
  97. -----
  98. The time required by this function increases linearly with the
  99. argument `i`, since it reads a large file sequentially in order to
  100. generate the graph [1]_.
  101. References
  102. ----------
  103. .. [1] Ronald C. Read and Robin J. Wilson, *An Atlas of Graphs*.
  104. Oxford University Press, 1998.
  105. """
  106. if not (0 <= i < NUM_GRAPHS):
  107. raise ValueError(f"index must be between 0 and {NUM_GRAPHS}")
  108. return next(islice(_generate_graphs(), i, None))
  109. def graph_atlas_g():
  110. """Returns the list of all graphs with up to seven nodes named in the
  111. Graph Atlas.
  112. The graphs are listed in increasing order by
  113. 1. number of nodes,
  114. 2. number of edges,
  115. 3. degree sequence (for example 111223 < 112222),
  116. 4. number of automorphisms,
  117. in that order, with three exceptions as described in the *Notes*
  118. section below. This causes the list to correspond with the index of
  119. the graphs in the Graph Atlas [atlas]_, with the first graph,
  120. ``G[0]``, being the null graph.
  121. Returns
  122. -------
  123. list
  124. A list of :class:`~networkx.Graph` objects, the one at index *i*
  125. corresponding to the graph *i* in the Graph Atlas.
  126. See also
  127. --------
  128. graph_atlas
  129. Notes
  130. -----
  131. This function may be expensive in both time and space, since it
  132. reads a large file sequentially in order to populate the list.
  133. Although the NetworkX atlas functions match the order of graphs
  134. given in the "Atlas of Graphs" book, there are (at least) three
  135. errors in the ordering described in the book. The following three
  136. pairs of nodes violate the lexicographically nondecreasing sorted
  137. degree sequence rule:
  138. - graphs 55 and 56 with degree sequences 001111 and 000112,
  139. - graphs 1007 and 1008 with degree sequences 3333444 and 3333336,
  140. - graphs 1012 and 1213 with degree sequences 1244555 and 1244456.
  141. References
  142. ----------
  143. .. [atlas] Ronald C. Read and Robin J. Wilson,
  144. *An Atlas of Graphs*.
  145. Oxford University Press, 1998.
  146. """
  147. return list(_generate_graphs())