Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions backtracking/coloring.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,23 @@ def color(graph: list[list[int]], max_colors: int) -> list[int]:
return colored_vertices

return []


if __name__ == "__main__":
# Example graph represented as an adjacency matrix
graph_example = [
[0, 1, 0, 0, 0],
[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0],
[0, 0, 1, 0, 1],
[0, 1, 0, 1, 0],
]

max_colors_example = 3
coloring_result = color(graph_example, max_colors_example)
if coloring_result:
print(
f"Graph can be colored with {max_colors_example} colors: {coloring_result}"
)
else:
print(f"Graph cannot be colored with {max_colors_example} colors.")