Guest User

Untitled

a guest
Dec 15th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. """
  2. Scamming the coding interview
  3. """
  4.  
  5. def color_graph(graph_adjacency, number_of_vertices):
  6.  
  7. # First vertex gets the first color
  8. vertex_to_color_map = {
  9. 0: 0
  10. }
  11.  
  12. for i in range(1, number_of_vertices):
  13.  
  14. neighbours = graph_adjacency[i]
  15. available_colors = [True for i in range(0, number_of_vertices)]
  16.  
  17. for n in neighbours:
  18. color_of_neighbour = vertex_to_color_map.get(n)
  19. if color_of_neighbour is not None:
  20. available_colors[color_of_neighbour] = False
  21.  
  22. # look for the first color that's available
  23. first_available_color = None
  24. for temp, color in enumerate(available_colors):
  25. if color:
  26. first_available_color = temp
  27. break
  28.  
  29. if first_available_color is not None:
  30. vertex_to_color_map[i] = first_available_color
  31.  
  32. return vertex_to_color_map
  33.  
  34.  
  35. if __name__ == '__main__':
  36.  
  37. graph_adjacency_list = {
  38. 0: [1, 2],
  39. 1: [0, 2, 3],
  40. 2: [0, 1, 3],
  41. 3: [1, 2, 4],
  42. 4: [3]
  43. }
  44.  
  45. max_colors_allowed = 3
  46.  
  47. vertex_to_color_map = color_graph(graph_adjacency_list, 5)
  48.  
  49. number_of_colors_used = len(set(vertex_to_color_map.values()))
  50.  
  51. print(number_of_colors_used <= max_colors_allowed)
Add Comment
Please, Sign In to add comment