-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Add graph colouring #11760
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
base: master
Are you sure you want to change the base?
Add graph colouring #11760
Changes from 2 commits
677abba
cf92959
9e6f3de
2258a46
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 @@ | ||
""" | ||
Egg Dropping Problem is a well-known problem in computer science. | ||
The task is to find the minimum number of attempts required in the | ||
worst case to find the highest floor from which an egg can be dropped | ||
without breaking, given a certain number of floors and eggs. | ||
|
||
Wikipedia: https://en.wikipedia.org/wiki/Dynamic_programming#Egg_dropping_puzzle | ||
""" | ||
|
||
|
||
def egg_drop(eggs: int, floors: int) -> int: | ||
""" | ||
Calculate the minimum number of attempts required in the worst case | ||
to determine the highest floor from which an egg can be dropped | ||
without breaking it. | ||
|
||
Parameters: | ||
eggs (int): Number of eggs available. | ||
floors (int): Number of floors to test. | ||
|
||
Returns: | ||
int: Minimum number of attempts required in the worst case. | ||
|
||
Example: | ||
>>> egg_drop(2, 10) | ||
4 | ||
|
||
>>> egg_drop(3, 14) | ||
4 | ||
""" | ||
# Initialize dp table with integers | ||
dp = [[0 for _ in range(floors + 1)] for _ in range(eggs + 1)] | ||
|
||
# Fill dp table for one egg (we have to try all floors) | ||
for i in range(1, floors + 1): | ||
dp[1][i] = i | ||
|
||
# Fill the rest of the dp table | ||
for e in range(2, eggs + 1): | ||
for f in range(1, floors + 1): | ||
dp[e][f] = floors + 1 # Start with an arbitrary large integer | ||
for k in range(1, f + 1): | ||
res = 1 + max(dp[e - 1][k - 1], dp[e][f - k]) | ||
dp[e][f] = min(dp[e][f], res) | ||
|
||
return dp[eggs][floors] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
""" | ||
Graph Coloring Problem is a classic problem in graph theory. | ||
The task is to assign colors to the vertices of a graph so that no two adjacent vertices | ||
share the same color, and the number of colors used is minimized. | ||
|
||
Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring | ||
""" | ||
|
||
|
||
def is_safe(graph: list[list[int]], color: list[int], v: int, c: int) -> bool: | ||
""" | ||
Helper function to check if it is safe to color vertex `v` with color `c`. | ||
|
||
Parameters: | ||
graph (list[list[int]]): The adjacency matrix of the graph. | ||
color (list[int]): The list of colors assigned to each vertex. | ||
v (int): The vertex to check. | ||
c (int): The color to be assigned. | ||
|
||
Returns: | ||
bool: True if it's safe to assign color `c` to vertex `v`, otherwise False. | ||
|
||
Example: | ||
>>> graph = [[0, 1, 1], [1, 0, 1], [1, 1, 0]] | ||
>>> color = [-1, -1, -1] | ||
>>> is_safe(graph, color, 0, 1) | ||
True | ||
""" | ||
return all(not (graph[v][i] == 1 and color[i] == c) for i in range(len(graph))) | ||
|
||
|
||
def graph_coloring_util( | ||
graph: list[list[int]], m: int, color: list[int], v: int | ||
|
||
) -> bool: | ||
""" | ||
Utility function that uses backtracking to solve the m-coloring problem. | ||
|
||
Parameters: | ||
graph (list[list[int]]): The adjacency matrix of the graph. | ||
m (int): The maximum number of colors. | ||
color (list[int]): The list of colors assigned to each vertex. | ||
v (int): The current vertex to be colored. | ||
|
||
Returns: | ||
bool: True if all vertices are successfully colored, otherwise False. | ||
|
||
Example: | ||
>>> graph = [[0, 1, 1], [1, 0, 1], [1, 1, 0]] | ||
>>> color = [-1, -1, -1] | ||
>>> graph_coloring_util(graph, 3, color, 0) | ||
True | ||
""" | ||
if v == len(graph): | ||
return True | ||
|
||
for c in range(1, m + 1): | ||
if is_safe(graph, color, v, c): | ||
color[v] = c | ||
if graph_coloring_util(graph, m, color, v + 1): | ||
return True | ||
color[v] = -1 # Backtrack | ||
|
||
return False | ||
|
||
|
||
def graph_coloring(graph: list[list[int]], m: int) -> bool: | ||
|
||
""" | ||
Solves the m-coloring problem using backtracking. | ||
|
||
Parameters: | ||
graph (list[list[int]]): The adjacency matrix of the graph. | ||
m (int): The maximum number of colors. | ||
|
||
Returns: | ||
bool: True if the graph can be colored with `m` colors, otherwise False. | ||
|
||
Example: | ||
>>> graph = [[0, 1, 1], [1, 0, 1], [1, 1, 0]] | ||
>>> graph_coloring(graph, 3) | ||
True | ||
""" | ||
color = [-1] * len(graph) | ||
return graph_coloring_util(graph, m, color, 0) |
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.
Please provide descriptive name for the parameter:
v
Please provide descriptive name for the parameter:
c