CSP

 def is_safe(graph, vertex, color, c): 

# Check if the given color 'c' is safe for the 'vertex' 

for neighbor in graph[vertex]: 

if color[neighbor] == c: 

return False 

return True 

def graph_coloring(graph, num_colors, color, vertex, V): 

# Base case: If all vertices are colored 

if vertex == V: 

return True 

for c in range(1,num_colors+1): 

if is_safe(graph,vertex,color,c): 

color[vertex] =c 

if graph_coloring(graph, num_colors, color, vertex + 1, V): 

return True 

color[vertex] = 0 

return False 

def print_solution(color): 

print("Solution exists: Following are the assigned colors:") 

for c in color: 

print(c, end=" ") 

print() 

if __name__=="__main__": 

# Define the city's regions as a graph (adjacency list) 

graph = { 

0: [1, 2, 3], 

1: [0, 2], 

2: [0, 1, 3], 

3: [0, 2], 

num_colors = 3 # Number of colors available 

V= len(graph) # Number of regions in the city 

color = [0] * V # Initialize colors 

if graph_coloring(graph, num_colors, color, 0, V): 

print_solution(color) 

else: 

print("No solution exists.")

Comments

Popular posts from this blog

GLASS ELECTRODE

HIGH PERFORMANCE LIQUID CHROMATOGRAPHY

CHROMATOGRAPHY