For each neighbour check if the coloring constraint is satisfied If any of the neighbours fail the constraint return False If all neighbours validate the constraint return True >>> neighbours = [0,1,0,1,0] >>> colored_vertices = [0, 2, 1, 2, 0] >>> color = 1 >>> valid_
(
neighbours: list[int], colored_vertices: list[int], color: int
)
| 8 | |
| 9 | |
| 10 | def valid_coloring( |
| 11 | neighbours: list[int], colored_vertices: list[int], color: int |
| 12 | ) -> bool: |
| 13 | """ |
| 14 | For each neighbour check if the coloring constraint is satisfied |
| 15 | If any of the neighbours fail the constraint return False |
| 16 | If all neighbours validate the constraint return True |
| 17 | |
| 18 | >>> neighbours = [0,1,0,1,0] |
| 19 | >>> colored_vertices = [0, 2, 1, 2, 0] |
| 20 | |
| 21 | >>> color = 1 |
| 22 | >>> valid_coloring(neighbours, colored_vertices, color) |
| 23 | True |
| 24 | |
| 25 | >>> color = 2 |
| 26 | >>> valid_coloring(neighbours, colored_vertices, color) |
| 27 | False |
| 28 | """ |
| 29 | # Does any neighbour not satisfy the constraints |
| 30 | return not any( |
| 31 | neighbour == 1 and colored_vertices[i] == color |
| 32 | for i, neighbour in enumerate(neighbours) |
| 33 | ) |
| 34 | |
| 35 | |
| 36 | def util_color( |