Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. class Graph_Coloring:
  2.  
  3. colors = []
  4.  
  5. nodes = [0, 1, 2, 3]
  6.  
  7. graph = {0: [1, 2],
  8. 1: [0, 2, 3],
  9. 2: [0, 1, 3],
  10. 3: [1, 2]
  11. }
  12.  
  13. solution = {}
  14.  
  15. def __init__():
  16. ch = 'a'
  17. while True:
  18. Graph_Coloring.colors.append(ch)
  19. print("Colors: "+str(len(Graph_Coloring.colors)))
  20. satisfied = Graph_Coloring.main()
  21.  
  22. if satisfied == True:
  23. print(Graph_Coloring.solution)
  24. exit()
  25. else:
  26. print("Cannot do coloring with "+str(len(Graph_Coloring.colors))+" colors")
  27. ch = chr(ord(ch) + 1)
  28.  
  29. def check(node, color):
  30. for neighbor in Graph_Coloring.graph.get(node):
  31. color_of_neighbor =Graph_Coloring.solution.get(neighbor)
  32. if color_of_neighbor == color:
  33. return False
  34.  
  35. return True
  36.  
  37. def set_node_color(node):
  38. for color in Graph_Coloring.colors:
  39. if Graph_Coloring.check(node, color):
  40. return color
  41.  
  42. def main():
  43. for node in Graph_Coloring.nodes:
  44. if Graph_Coloring.set_node_color(node) == None:
  45. return False
  46. else:
  47. Graph_Coloring.solution[node] = Graph_Coloring.set_node_color(node)
  48. return True
  49.  
  50. Graph_Coloring.__init__()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement