Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. def color_graph(graph, colors):
  2. # Create a valid coloring for the graph
  3. # BFS
  4. to_visit = []
  5. to_visit.append(graph[0])
  6.  
  7. while to_visit:
  8. node = to_visit.pop(0)
  9. if node.color != None:
  10. continue
  11. illegal_colors = []
  12. for neighbor in node.neighbors:
  13. if neighbor == node:
  14. raise Exception()
  15. if neighbor.color != None:
  16. illegal_colors.append(neighbor.color)
  17. else:
  18. to_visit.append(neighbor)
  19. for color in colors:
  20. if color not in illegal_colors:
  21. node.color = color
  22. print(node.label + " set to " + color)
  23. break
  24.  
  25.  
  26. return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement