DM559/DM545 – Linear and Integer Programming
Max Flow in NetworkxWe import the
In [1]:
import networkx as nx
print "Networkx versions is",nx.__version__
Network definition
In [2]:
G = nx.DiGraph()
G.add_edge('s','a', capacity=3.0)
G.add_edge('s','b', capacity=1.0)
G.add_edge('a','c', capacity=3.0)
G.add_edge('b','c', capacity=5.0)
G.add_edge('b','d', capacity=4.0)
G.add_edge('d','e', capacity=2.0)
G.add_edge('c','t', capacity=2.0)
G.add_edge('e','t', capacity=3.0)
Network drawing
In [3]:
%matplotlib inline
pos=nx.spectral_layout(G)
nx.draw(G, pos=pos, with_labels=True)
nx.draw_networkx_edge_labels(G,pos,edge_labels={e:G.edge[e[0]][e[1]]["capacity"] for e in G.edges()})
Out[3]:
Max flow
In [4]:
flow_value, flow_dict = nx.maximum_flow(G, 's', 't')
print flow_value
print flow_dict
|