sudoku.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """Generator for Sudoku graphs
  2. This module gives a generator for n-Sudoku graphs. It can be used to develop
  3. algorithms for solving or generating Sudoku puzzles.
  4. A completed Sudoku grid is a 9x9 array of integers between 1 and 9, with no
  5. number appearing twice in the same row, column, or 3x3 box.
  6. +---------+---------+---------+
  7. | | 8 6 4 | | 3 7 1 | | 2 5 9 |
  8. | | 3 2 5 | | 8 4 9 | | 7 6 1 |
  9. | | 9 7 1 | | 2 6 5 | | 8 4 3 |
  10. +---------+---------+---------+
  11. | | 4 3 6 | | 1 9 2 | | 5 8 7 |
  12. | | 1 9 8 | | 6 5 7 | | 4 3 2 |
  13. | | 2 5 7 | | 4 8 3 | | 9 1 6 |
  14. +---------+---------+---------+
  15. | | 6 8 9 | | 7 3 4 | | 1 2 5 |
  16. | | 7 1 3 | | 5 2 8 | | 6 9 4 |
  17. | | 5 4 2 | | 9 1 6 | | 3 7 8 |
  18. +---------+---------+---------+
  19. The Sudoku graph is an undirected graph with 81 vertices, corresponding to
  20. the cells of a Sudoku grid. It is a regular graph of degree 20. Two distinct
  21. vertices are adjacent if and only if the corresponding cells belong to the
  22. same row, column, or box. A completed Sudoku grid corresponds to a vertex
  23. coloring of the Sudoku graph with nine colors.
  24. More generally, the n-Sudoku graph is a graph with n^4 vertices, corresponding
  25. to the cells of an n^2 by n^2 grid. Two distinct vertices are adjacent if and
  26. only if they belong to the same row, column, or n by n box.
  27. References
  28. ----------
  29. .. [1] Herzberg, A. M., & Murty, M. R. (2007). Sudoku squares and chromatic
  30. polynomials. Notices of the AMS, 54(6), 708-717.
  31. .. [2] Sander, Torsten (2009), "Sudoku graphs are integral",
  32. Electronic Journal of Combinatorics, 16 (1): Note 25, 7pp, MR 2529816
  33. .. [3] Wikipedia contributors. "Glossary of Sudoku." Wikipedia, The Free
  34. Encyclopedia, 3 Dec. 2019. Web. 22 Dec. 2019.
  35. """
  36. import networkx as nx
  37. from networkx.exception import NetworkXError
  38. __all__ = ["sudoku_graph"]
  39. def sudoku_graph(n=3):
  40. """Returns the n-Sudoku graph. The default value of n is 3.
  41. The n-Sudoku graph is a graph with n^4 vertices, corresponding to the
  42. cells of an n^2 by n^2 grid. Two distinct vertices are adjacent if and
  43. only if they belong to the same row, column, or n-by-n box.
  44. Parameters
  45. ----------
  46. n: integer
  47. The order of the Sudoku graph, equal to the square root of the
  48. number of rows. The default is 3.
  49. Returns
  50. -------
  51. NetworkX graph
  52. The n-Sudoku graph Sud(n).
  53. Examples
  54. --------
  55. >>> G = nx.sudoku_graph()
  56. >>> G.number_of_nodes()
  57. 81
  58. >>> G.number_of_edges()
  59. 810
  60. >>> sorted(G.neighbors(42))
  61. [6, 15, 24, 33, 34, 35, 36, 37, 38, 39, 40, 41, 43, 44, 51, 52, 53, 60, 69, 78]
  62. >>> G = nx.sudoku_graph(2)
  63. >>> G.number_of_nodes()
  64. 16
  65. >>> G.number_of_edges()
  66. 56
  67. References
  68. ----------
  69. .. [1] Herzberg, A. M., & Murty, M. R. (2007). Sudoku squares and chromatic
  70. polynomials. Notices of the AMS, 54(6), 708-717.
  71. .. [2] Sander, Torsten (2009), "Sudoku graphs are integral",
  72. Electronic Journal of Combinatorics, 16 (1): Note 25, 7pp, MR 2529816
  73. .. [3] Wikipedia contributors. "Glossary of Sudoku." Wikipedia, The Free
  74. Encyclopedia, 3 Dec. 2019. Web. 22 Dec. 2019.
  75. """
  76. if n < 0:
  77. raise NetworkXError("The order must be greater than or equal to zero.")
  78. n2 = n * n
  79. n3 = n2 * n
  80. n4 = n3 * n
  81. # Construct an empty graph with n^4 nodes
  82. G = nx.empty_graph(n4)
  83. # A Sudoku graph of order 0 or 1 has no edges
  84. if n < 2:
  85. return G
  86. # Add edges for cells in the same row
  87. for row_no in range(0, n2):
  88. row_start = row_no * n2
  89. for j in range(1, n2):
  90. for i in range(j):
  91. G.add_edge(row_start + i, row_start + j)
  92. # Add edges for cells in the same column
  93. for col_no in range(0, n2):
  94. for j in range(col_no, n4, n2):
  95. for i in range(col_no, j, n2):
  96. G.add_edge(i, j)
  97. # Add edges for cells in the same box
  98. for band_no in range(n):
  99. for stack_no in range(n):
  100. box_start = n3 * band_no + n * stack_no
  101. for j in range(1, n2):
  102. for i in range(j):
  103. u = box_start + (i % n) + n2 * (i // n)
  104. v = box_start + (j % n) + n2 * (j // n)
  105. G.add_edge(u, v)
  106. return G