Skip to content

Commit

Permalink
#4 Graphing the board
Browse files Browse the repository at this point in the history
Sudoku board can now be represented as a graph. Only 9x9 and 4x4 are supported for now.
  • Loading branch information
kcuric committed Jan 19, 2020
1 parent 33aba63 commit 7476f05
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ verify_ssl = true
name = "pypi"

[packages]
matplotlib = "*"
networkx = "*"

[dev-packages]

Expand Down
135 changes: 133 additions & 2 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion sudoku/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from sudoku.modules.sudoku import Sudoku
from sudoku.modules.graph import Graph

def run():
sudoku = Sudoku()
sudoku.print_board((sudoku.get_solved_board(9)))
board = sudoku.get_solved_board(9)

sudoku.print_board(board)

graph = Graph()
graph.create_graph(board)
graph.show_graph()

109 changes: 109 additions & 0 deletions sudoku/modules/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import networkx as nx
import matplotlib.pyplot as plt

class Graph:

def __init__(self):
self.board = nx.Graph()
self.labels = dict()
self.display_labels = dict()

def create_graph(self, board: list):
self._add_nodes(board)
self._add_edges(board)
self._add_cluster_edges(board)

def _add_nodes(self, board: list):
'''
Adds the nodes to the board.
Parameters:
board (list): board.
'''
label = 0
for row in range(len(board)):
for col in range(len(board)):
label += 1
self.labels[(row, col)] = label
self.display_labels[label] = board[row][col]
self.board.add_node(label)

def _add_edges(self, board: list):
'''
Adds the "main" edges to the board.
Parameters:
board (list): board.
'''
label = 0
for row in range(len(board)):
for col in range(len(board)):
try:
self.board.add_edge(self.labels[row, col],
self.labels[row, col - 1])
except (IndexError, KeyError):
pass
try:
self.board.add_edge(self.labels[row, col],
self.labels[row + 1, col])
except (IndexError, KeyError):
pass
try:
self.board.add_edge(self.labels[row, col],
self.labels[row, col + 1])
except (IndexError, KeyError):
pass
try:
self.board.add_edge(self.labels[row, col],
self.labels[row - 1, col])
except (IndexError, KeyError):
pass

def _add_cluster_edges(self, board: list):
'''
Adds the edges between "clusters" to the board.
Parameters:
board (list): board.
'''
size = len(board)
divider = 2 if size % 2 == 0 else 3
num_sectors = int(size / divider)

# Find cluster beginnings
cluster_beginnings = []
for i in range(0, size, divider):
for j in range(0, size, divider):
cluster_beginnings.append((i, j))

# Iterate through cluster and add edges.
for cluster in cluster_beginnings:
current_cluster = []
for row in range(cluster[0], cluster[0] + divider):
for col in range(cluster[1], cluster[1] + divider):
current_cluster.append((row, col))

for node in current_cluster:
for _ in current_cluster:
self.board.add_edge(
self.labels[node],
self.labels[_]
)

def show_graph(self):
'''
Shows the graph.
Parameters:
board (list): board.
'''
pos = dict((v,k) for k,v in self.labels.items())
nx.draw_networkx(
self.board,
pos=pos,
with_labels=True,
node_size=800,
labels=self.display_labels
)
plt.axis('off')
plt.show()

0 comments on commit 7476f05

Please sign in to comment.