MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / valid_coloring

Function valid_coloring

backtracking/coloring.py:10–33  ·  view source on GitHub ↗

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
)

Source from the content-addressed store, hash-verified

8
9
10def 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
36def util_color(

Callers 1

util_colorFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected