assignment_prob_hungarian_7.py 684 B

1234567891011121314151617181920
  1. def DrawGraph(B):
  2. l, r = nx.bipartite.sets(B)
  3. pos = {}
  4. # Update position for node from each group
  5. pos.update((node, (1, index)) for index, node in enumerate(l))
  6. pos.update((node, (2, index)) for index, node in enumerate(r))
  7. nx.draw(B, pos, with_labels = True) #with_labels=true is to show the node number in the output graph
  8. edge_labels = dict([((u, v), d['length']) for u, v, d in B.edges(data = True)])
  9. nx.draw_networkx_edge_labels(B, pos, edge_labels = edge_labels, label_pos = 0.2, font_size = 11) #prints weight on all the edges
  10. return pos
  11. #main function
  12. if __name__ == "__main__":
  13. B, cost = CreateGraph();
  14. pos = DrawGraph(B)
  15. hungarian(B, pos, cost)
  16. plt.show()