k_centers_problem_3.py 602 B

12345678910111213141516171819
  1. def DrawGraph(G, centers):
  2. pos = nx.spring_layout(G)
  3. color_map = ['blue'] * len(G.nodes())
  4. #all the center nodes are marked with 'red'
  5. for c in centers:
  6. color_map[c] = 'red'
  7. nx.draw(G, pos, node_color = color_map, with_labels = True) #with_labels=true is to show the node number in the output graph
  8. edge_labels = nx.get_edge_attributes(G, 'length')
  9. nx.draw_networkx_edge_labels(G, pos, edge_labels = edge_labels, font_size = 11) #prints weight on all the edges
  10. #main function
  11. if __name__ == "__main__":
  12. G,n = CreateGraph()
  13. centers = k_centers(G, n)
  14. DrawGraph(G, centers)
  15. plt.show()