Fix comments in backtracking/coloring.py (#4857)

This commit is contained in:
DukicDev 2021-10-01 23:48:47 +02:00 committed by GitHub
parent d1e70cfa3a
commit 6341f351aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,7 @@
""" """
Graph Coloring also called "m coloring problem" Graph Coloring also called "m coloring problem"
consists of coloring given graph with at most m colors consists of coloring a given graph with at most m colors
such that no adjacent vertices are assigned same color such that no adjacent vertices are assigned the same color
Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring
""" """
@ -11,9 +11,9 @@ def valid_coloring(
neighbours: list[int], colored_vertices: list[int], color: int neighbours: list[int], colored_vertices: list[int], color: int
) -> bool: ) -> bool:
""" """
For each neighbour check if coloring constraint is satisfied For each neighbour check if the coloring constraint is satisfied
If any of the neighbours fail the constraint return False If any of the neighbours fail the constraint return False
If all neighbours validate constraint return True If all neighbours validate the constraint return True
>>> neighbours = [0,1,0,1,0] >>> neighbours = [0,1,0,1,0]
>>> colored_vertices = [0, 2, 1, 2, 0] >>> colored_vertices = [0, 2, 1, 2, 0]
@ -41,14 +41,14 @@ def util_color(
Base Case: Base Case:
1. Check if coloring is complete 1. Check if coloring is complete
1.1 If complete return True (meaning that we successfully colored graph) 1.1 If complete return True (meaning that we successfully colored the graph)
Recursive Step: Recursive Step:
2. Itterates over each color: 2. Iterates over each color:
Check if current coloring is valid: Check if the current coloring is valid:
2.1. Color given vertex 2.1. Color given vertex
2.2. Do recursive call check if this coloring leads to solving problem 2.2. Do recursive call, check if this coloring leads to a solution
2.4. if current coloring leads to solution return 2.4. if current coloring leads to a solution return
2.5. Uncolor given vertex 2.5. Uncolor given vertex
>>> graph = [[0, 1, 0, 0, 0], >>> graph = [[0, 1, 0, 0, 0],