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

Function color

backtracking/coloring.py:88–121  ·  view source on GitHub ↗

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], ...

(graph: list[list[int]], max_colors: int)

Source from the content-addressed store, hash-verified

86
87
88def color(graph: list[list[int]], max_colors: int) -> list[int]:
89 """
90 Wrapper function to call subroutine called util_color
91 which will either return True or False.
92 If True is returned colored_vertices list is filled with correct colorings
93
94 >>> graph = [[0, 1, 0, 0, 0],
95 ... [1, 0, 1, 0, 1],
96 ... [0, 1, 0, 1, 0],
97 ... [0, 1, 1, 0, 0],
98 ... [0, 1, 0, 0, 0]]
99
100 >>> max_colors = 3
101 >>> color(graph, max_colors)
102 [0, 1, 0, 2, 0]
103
104 >>> max_colors = 2
105 >>> color(graph, max_colors)
106 []
107 >>> color([], 2) # empty graph
108 []
109 >>> color([[0]], 1) # single node, 1 color
110 [0]
111 >>> color([[0, 1], [1, 0]], 1) # 2 nodes, 1 color (impossible)
112 []
113 >>> color([[0, 1], [1, 0]], 2) # 2 nodes, 2 colors (possible)
114 [0, 1]
115 """
116 colored_vertices = [-1] * len(graph)
117
118 if util_color(graph, max_colors, colored_vertices, 0):
119 return colored_vertices
120
121 return []

Callers

nothing calls this directly

Calls 1

util_colorFunction · 0.85

Tested by

no test coverage detected