-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
feat(backtracking): add m-coloring algorithm #13275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
eacebd7
21ef5e1
2df824d
913c90a
16b6d78
d0909dc
152c0c0
7647b47
80cfe21
5dba8a1
ecab4cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| from typing import List | ||
|
Check failure on line 1 in backtracking/m_coloring_problem.py
|
||
|
|
||
|
|
||
| def is_safe(node: int, color: int, graph: List[List[int]], n: int, col: List[int]) -> bool: | ||
|
Check failure on line 4 in backtracking/m_coloring_problem.py
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
| """ | ||
| Check if it is safe to assign a color to a node. | ||
|
|
||
| >>> is_safe(0, 1, [[0,1],[1,0]], 2, [0,1]) | ||
| False | ||
| >>> is_safe(0, 2, [[0,1],[1,0]], 2, [0,1]) | ||
| True | ||
| """ | ||
| return all(not (graph[node][k] == 1 and col[k] == color) for k in range(n)) | ||
|
|
||
|
|
||
| def solve(node: int, col: List[int], m: int, n: int, graph: List[List[int]]) -> bool: | ||
|
Check failure on line 16 in backtracking/m_coloring_problem.py
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: |
||
| """ | ||
| Recursively try to color the graph using at most m colors. | ||
|
|
||
| >>> solve(0, [0]*3, 3, 3, [[0,1,0],[1,0,1],[0,1,0]]) | ||
| True | ||
| >>> solve(0, [0]*3, 2, 3, [[0,1,0],[1,0,1],[0,1,0]]) | ||
| False | ||
| """ | ||
| if node == n: | ||
| return True | ||
| for c in range(1, m + 1): | ||
| if is_safe(node, c, graph, n, col): | ||
| col[node] = c | ||
| if solve(node + 1, col, m, n, graph): | ||
| return True | ||
| col[node] = 0 | ||
| return False | ||
|
|
||
|
|
||
| def graph_coloring(graph: List[List[int]], m: int, n: int) -> bool: | ||
|
Check failure on line 36 in backtracking/m_coloring_problem.py
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: |
||
| """ | ||
| Determine if the graph can be colored with at most m colors. | ||
|
|
||
| >>> graph_coloring([[0,1,1],[1,0,1],[1,1,0]], 3, 3) | ||
| True | ||
| >>> graph_coloring([[0,1,1],[1,0,1],[1,1,0]], 2, 3) | ||
| False | ||
| """ | ||
| col = [0] * n | ||
| return solve(0, col, m, n, graph) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
backtracking/m_coloring_problem.py, please provide doctest for the functionis_safePlease provide descriptive name for the parameter:
n