Professional Documents
Culture Documents
Ai Assignment - 8 Pycode
Ai Assignment - 8 Pycode
Ai Assignment - 8 Pycode
segments = list(graph.keys())
colors = ['R', 'G', 'B']
segment = segments[segment_index]
for color in colors:
if is_safe(segment, color, color_assignment):
color_assignment[segment] = color
if graph_coloring(segment_index + 1, color_assignment):
return True # Continue with the next segment
color_assignment[segment] = None # Backtrack
if graph_coloring(0, color_assignment):
print("Solution found: Segment colors are as follows:")
for segment, color in color_assignment.items():
print(f"Segment {segment} is colored {color}")
else:
print("No solution exists.")