tsp_christofides_8.py 546 B

1234567891011121314151617
  1. def DrawGraph(G,color):
  2. pos = nx.spring_layout(G)
  3. nx.draw(G, pos, with_labels = True, edge_color = color) #with_labels=true is to show the node number in the output graph
  4. edge_labels = nx.get_edge_attributes(G,'length')
  5. nx.draw_networkx_edge_labels(G, pos, edge_labels = edge_labels, font_size = 11) #prints weight on all the edges
  6. return pos
  7. #main function
  8. if __name__ == "__main__":
  9. G = CreateGraph()
  10. plt.figure(1)
  11. pos = DrawGraph(G,'black')
  12. opGraph = christofedes(G, pos)
  13. plt.figure(2)
  14. pos1 = DrawGraph(opGraph,'r')
  15. plt.show()