-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Graph coloring #1921
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
Merged
Merged
Graph coloring #1921
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
457af3b
add skeleton code
CSenshi 46127ee
add doctests
CSenshi 2d821c8
add mainc function pseudo code and tests (ToDo: write Implementation)
CSenshi 15585f1
typo fixes
CSenshi b60da84
implement algorithm
CSenshi aeecd32
add type checking
CSenshi 0dbcf67
add wikipedia link
CSenshi d097d27
typo fix
CSenshi d6723ae
update range syntax
CSenshi bf04f4d
change indexed iteration checking to any()
CSenshi de52538
fix: swap import and documentation sections
CSenshi 87be0fc
fix: change return none to return empty list
CSenshi b994349
remove unnecessary import (Union)
CSenshi e331656
change: remove returning boolean indicating problem was solved or not
CSenshi 04b72cb
remove unnecessary import (Tuple)
CSenshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
""" | ||
Graph Coloring also called "m coloring problem" | ||
consists of coloring given graph with at most m colors | ||
such that no adjacent vertices are assigned same color | ||
|
||
Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring | ||
""" | ||
from typing import List | ||
|
||
|
||
def valid_coloring( | ||
neighbours: List[int], colored_vertices: List[int], color: int | ||
) -> bool: | ||
""" | ||
For each neighbour check if coloring constraint is satisfied | ||
If any of the neighbours fail the constraint return False | ||
If all neighbours validate constraint return True | ||
|
||
>>> neighbours = [0,1,0,1,0] | ||
>>> colored_vertices = [0, 2, 1, 2, 0] | ||
|
||
>>> color = 1 | ||
>>> valid_coloring(neighbours, colored_vertices, color) | ||
True | ||
|
||
>>> color = 2 | ||
>>> valid_coloring(neighbours, colored_vertices, color) | ||
False | ||
""" | ||
# Does any neighbour not satisfy the constraints | ||
return not any( | ||
neighbour == 1 and colored_vertices[i] == color | ||
for i, neighbour in enumerate(neighbours) | ||
) | ||
|
||
|
||
def util_color( | ||
graph: List[List[int]], max_colors: int, colored_vertices: List[int], index: int | ||
) -> bool: | ||
""" | ||
Pseudo-Code | ||
|
||
Base Case: | ||
1. Check if coloring is complete | ||
1.1 If complete return True (meaning that we successfully colored graph) | ||
|
||
Recursive Step: | ||
2. Itterates over each color: | ||
Check if current coloring is valid: | ||
2.1. Color given vertex | ||
2.2. Do recursive call check if this coloring leads to solving problem | ||
2.4. if current coloring leads to solution return | ||
2.5. Uncolor given vertex | ||
|
||
>>> graph = [[0, 1, 0, 0, 0], | ||
... [1, 0, 1, 0, 1], | ||
... [0, 1, 0, 1, 0], | ||
... [0, 1, 1, 0, 0], | ||
... [0, 1, 0, 0, 0]] | ||
>>> max_colors = 3 | ||
>>> colored_vertices = [0, 1, 0, 0, 0] | ||
>>> index = 3 | ||
|
||
>>> util_color(graph, max_colors, colored_vertices, index) | ||
True | ||
|
||
>>> max_colors = 2 | ||
>>> util_color(graph, max_colors, colored_vertices, index) | ||
False | ||
""" | ||
|
||
# Base Case | ||
if index == len(graph): | ||
return True | ||
|
||
# Recursive Step | ||
for i in range(max_colors): | ||
if valid_coloring(graph[index], colored_vertices, i): | ||
# Color current vertex | ||
colored_vertices[index] = i | ||
# Validate coloring | ||
if util_color(graph, max_colors, colored_vertices, index + 1): | ||
return True | ||
# Backtrack | ||
colored_vertices[index] = -1 | ||
return False | ||
|
||
|
||
def color(graph: List[List[int]], max_colors: int) -> List[int]: | ||
""" | ||
Wrapper function to call subroutine called util_color | ||
which will either return True or False. | ||
If True is returned colored_vertices list is filled with correct colorings | ||
|
||
>>> graph = [[0, 1, 0, 0, 0], | ||
... [1, 0, 1, 0, 1], | ||
... [0, 1, 0, 1, 0], | ||
... [0, 1, 1, 0, 0], | ||
... [0, 1, 0, 0, 0]] | ||
|
||
>>> max_colors = 3 | ||
>>> color(graph, max_colors) | ||
[0, 1, 0, 2, 0] | ||
|
||
>>> max_colors = 2 | ||
>>> color(graph, max_colors) | ||
[] | ||
""" | ||
colored_vertices = [-1] * len(graph) | ||
|
||
if util_color(graph, max_colors, colored_vertices, 0): | ||
return colored_vertices | ||
|
||
return [] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.