Advertisement
Guest User

Untitled

a guest
May 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. # ejemplo coloreado de grafos con networkx
  2. # gabriel sanchez
  3.  
  4. import networkx as nx
  5. import matplotlib.pyplot as plt
  6. import itertools
  7.  
  8. G = nx.Graph()
  9.  
  10. colores_dict = {0:'pink', 1: 'blue', 2: 'green', 3:'red', 4:'orange', 5:'yellow'}
  11. colores = []
  12.  
  13. G.add_node(1)
  14. G.add_node(2)
  15. G.add_node(3)
  16. G.add_node(4)
  17.  
  18. G.add_edge(1,2)
  19. G.add_edge(2,3)
  20. G.add_edge(1,3)
  21. G.add_edge(3,4)
  22. G.add_edge(1,4)
  23.  
  24. def coloreado_voraz(grafo):
  25. colores = {}
  26. nodos = grafo.nodes()
  27. for u in nodos:
  28. colores_vecinos = {colores[v] for v in grafo[u] if v in colores}
  29. for color in itertools.count():
  30. if color not in colores_vecinos:
  31. break
  32. colores[u] = color
  33. return colores
  34.  
  35. coloreado = coloreado_voraz(G)
  36.  
  37. for i,j in coloreado.items():
  38. colores.append(colores_dict[coloreado[i]])
  39. nx.draw(G,node_color = colores,with_labels = True)
  40.  
  41. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement