ó žÃÒYc@sËdZdZdgZddlmZmZmZddlmZm Z ddl Z ddl m Z yddlmZWnek rŠnXy eZWnek r¨nXe d ƒd d d d „ƒZdS(s< Minimum cost flow algorithms on directed connected graphs. s'Loïc Séguin-C. tnetwork_simplexiÿÿÿÿ(tchaintislicetrepeat(tceiltsqrtN(tnot_implemented_for(tizipt undirectedtdemandtcapacitytweightc!sD t|ƒdkr$tjdƒ‚nt|ƒ‰d„tˆƒDƒ}gˆD]}|j|j|dƒ^qM}tdƒ‰ xHtˆ|ƒD]7\}}t |ƒˆ krŽtjd|fƒ‚qŽqŽW|j ƒ} g‰g‰| rðg} ni} g‰g‰| s|j dt ƒ} n|j dt dt ƒ} ‡‡ fd†| Dƒ} x¦t| ƒD]˜\} ‰ˆj |ˆdƒˆj |ˆd ƒ| r«| j ˆd ƒn| | ˆd <ˆj ˆd jˆˆ ƒƒˆj ˆd jˆdƒƒq[WxHt| ˆƒD]7\‰}t |ƒˆ krtjd ˆfƒ‚qqW| s`tj|dt ƒ} ntj|dt dt ƒ} xM| D]E‰t ˆd jˆdƒƒˆ kr‚tjd ˆd fƒ‚q‚q‚Wt|ƒdkrïtjd ƒ‚nxBt| ˆƒD]1\‰}|dkrÿtjdˆfƒ‚qÿqÿW| sRtj|dt ƒ} ntj|dt dt ƒ} xG| D]?‰ˆd jˆˆ ƒdkrttjdˆd fƒ‚qtqtWtˆƒ}x]t|ƒD]O\}}|dkrˆj d ƒˆj |ƒqЈj |ƒˆj d ƒqÐWdttt‡ fd†ˆDƒƒtd„ˆDƒƒgd„|Dƒƒƒpud ‰ˆjtˆ|ƒƒˆjtˆ|ƒƒt| ƒ‰tttdˆƒd„|Dƒƒƒ‰g|D]}|dkr÷ˆnˆ ^q߉tttd |ƒd+gƒƒ‰ttˆˆ|ƒƒ‰tttd |ƒ|d gƒƒ‰tttd |ƒd dgƒƒ‰ ttd |ƒƒ‰ttt|ƒ|d gƒƒ‰ ‡‡‡‡‡fd†‰‡‡‡‡‡fd†}‡‡fd†‰ ‡‡fd†‰‡ ‡fd†}‡‡‡fd†‰‡‡‡fd†}‡‡fd†}‡ ‡ fd†‰‡‡ ‡ ‡‡‡fd†}‡‡ ‡ ‡‡‡fd†}‡‡ ‡ ‡‡‡fd†}‡‡‡‡fd †}x÷|ƒD]ì\} }}|| ||ƒ\}}|||ƒ\}}}|||ˆ||ƒƒ| |krôˆ||krx||}}n|j| ƒ|j|ƒkr¦||}}n|||ƒ||ƒ|| ||ƒ|| ||ƒqôqôWt‡fd!†t| dƒDƒƒrtjd"ƒ‚nt‡‡fd#†tˆƒDƒƒsrt‡‡ ‡fd$†tj|dt ƒDƒƒr„tjd%ƒ‚nˆˆ3td&„tˆˆƒDƒƒ}d'„ˆDƒ‰ ‡ fd(†} ‡fd)†ˆDƒ‰‡fd*†ˆDƒ‰| s7 x$tˆˆˆƒD]‰| ˆƒq W|j dt ƒ} nBx'tˆˆ| ˆƒD]‰| ˆƒqM W|j dt dt ƒ} x¾| D]¶‰ˆdˆd krÎ ˆd jˆˆ ƒdkr6 | ˆd d,ƒq6 q€ ˆd jˆdƒ}|dkr | ˆd d-ƒq€ ˆd ˆ}|||7}| ˆd |fƒq€ W|ˆ fS(.sËFind a minimum cost flow satisfying all demands in digraph G. This is a primal network simplex algorithm that uses the leaving arc rule to prevent cycling. G is a digraph with edge costs and capacities and in which nodes have demand, i.e., they want to send or receive some amount of flow. A negative demand means that the node wants to send flow, a positive demand means that the node want to receive flow. A flow on the digraph G satisfies all demand if the net flow into each node is equal to the demand of that node. Parameters ---------- G : NetworkX graph DiGraph on which a minimum cost flow satisfying all demands is to be found. demand : string Nodes of the graph G are expected to have an attribute demand that indicates how much flow a node wants to send (negative demand) or receive (positive demand). Note that the sum of the demands should be 0 otherwise the problem in not feasible. If this attribute is not present, a node is considered to have 0 demand. Default value: 'demand'. capacity : string Edges of the graph G are expected to have an attribute capacity that indicates how much flow the edge can support. If this attribute is not present, the edge is considered to have infinite capacity. Default value: 'capacity'. weight : string Edges of the graph G are expected to have an attribute weight that indicates the cost incurred by sending one unit of flow on that edge. If not present, the weight is considered to be 0. Default value: 'weight'. Returns ------- flowCost : integer, float Cost of a minimum cost flow satisfying all demands. flowDict : dictionary Dictionary of dictionaries keyed by nodes such that flowDict[u][v] is the flow edge (u, v). Raises ------ NetworkXError This exception is raised if the input graph is not directed, not connected or is a multigraph. NetworkXUnfeasible This exception is raised in the following situations: * The sum of the demands is not zero. Then, there is no flow satisfying all demands. * There is no flow satisfying all demand. NetworkXUnbounded This exception is raised if the digraph G has a cycle of negative cost and infinite capacity. Then, the cost of a flow satisfying all demands is unbounded below. Notes ----- This algorithm is not guaranteed to work if edge weights or demands are floating point numbers (overflows and roundoff errors can cause problems). As a workaround you can use integer numbers by multiplying the relevant edge attributes by a convenient constant factor (eg 100). See also -------- cost_of_flow, max_flow_min_cost, min_cost_flow, min_cost_flow_cost Examples -------- A simple example of a min cost flow problem. >>> import networkx as nx >>> G = nx.DiGraph() >>> G.add_node('a', demand=-5) >>> G.add_node('d', demand=5) >>> G.add_edge('a', 'b', weight=3, capacity=4) >>> G.add_edge('a', 'c', weight=6, capacity=10) >>> G.add_edge('b', 'd', weight=1, capacity=9) >>> G.add_edge('c', 'd', weight=2, capacity=5) >>> flowCost, flowDict = nx.network_simplex(G) >>> flowCost 24 >>> flowDict # doctest: +SKIP {'a': {'c': 1, 'b': 4}, 'c': {'d': 1}, 'b': {'d': 4}, 'd': {}} The mincost flow algorithm can also be used to solve shortest path problems. To find the shortest path between two nodes u and v, give all edges an infinite capacity, give node u a demand of -1 and node v a demand a 1. Then run the network simplex. The value of a min cost flow will be the distance between u and v and edges carrying positive flow will indicate the path. >>> G=nx.DiGraph() >>> G.add_weighted_edges_from([('s', 'u' ,10), ('s' ,'x' ,5), ... ('u', 'v' ,1), ('u' ,'x' ,2), ... ('v', 'y' ,1), ('x' ,'u' ,3), ... ('x', 'v' ,5), ('x' ,'y' ,2), ... ('y', 's' ,7), ('y' ,'v' ,6)]) >>> G.add_node('s', demand = -1) >>> G.add_node('v', demand = 1) >>> flowCost, flowDict = nx.network_simplex(G) >>> flowCost == nx.shortest_path_length(G, 's', 'v', weight='weight') True >>> sorted([(u, v) for u in flowDict for v in flowDict[u] if flowDict[u][v] > 0]) [('s', 'x'), ('u', 'v'), ('x', 'u')] >>> nx.shortest_path(G, 's', 'v', weight = 'weight') ['s', 'x', 'u', 'v'] It is possible to change the name of the attributes used for the algorithm. >>> G = nx.DiGraph() >>> G.add_node('p', spam=-4) >>> G.add_node('q', spam=2) >>> G.add_node('a', spam=-2) >>> G.add_node('d', spam=-1) >>> G.add_node('t', spam=2) >>> G.add_node('w', spam=3) >>> G.add_edge('p', 'q', cost=7, vacancies=5) >>> G.add_edge('p', 'a', cost=1, vacancies=4) >>> G.add_edge('q', 'd', cost=2, vacancies=3) >>> G.add_edge('t', 'q', cost=1, vacancies=2) >>> G.add_edge('a', 't', cost=2, vacancies=4) >>> G.add_edge('d', 'w', cost=3, vacancies=4) >>> G.add_edge('t', 'w', cost=4, vacancies=1) >>> flowCost, flowDict = nx.network_simplex(G, demand='spam', ... capacity='vacancies', ... weight='cost') >>> flowCost 37 >>> flowDict # doctest: +SKIP {'a': {'t': 4}, 'd': {'w': 2}, 'q': {'d': 1}, 'p': {'q': 2, 'a': 2}, 't': {'q': 1, 'w': 1}, 'w': {}} References ---------- .. [1] Z. Kiraly, P. Kovacs. Efficient implementation of minimum-cost flow algorithms. Acta Universitatis Sapientiae, Informatica 4(1):67--118. 2012. .. [2] R. Barr, F. Glover, D. Klingman. Enhancement of spanning tree labeling procedures for network optimization. INFOR 17(1):16--34. 1979. isgraph has no nodescSsi|]\}}||“qS(((t.0titu((s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pys Âs tinfsnode %r has infinite demandtdatatkeysc3sE|];}|d|dkr|djˆˆƒdkr|VqdS(iiiÿÿÿÿN(tget(R te(R R(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pys ×siiiÿÿÿÿsedge %r has infinite weightstotal node demand is not zerosedge %r has negative capacityic3s!|]}|ˆkr|VqdS(N((R R(R(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pys scss|]}t|ƒVqdS(N(tabs(R tc((s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pys scss|]}t|ƒVqdS(N(R(R td((s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pys scss|]}t|ƒVqdS(N(R(R R((s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pys scs;ˆ|ˆˆ|ˆˆ|}ˆ|dkr6|S| S(s.Return the reduced cost of an edge i. i((R R(tCtStTtpitx(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pyt reduced_cost's"c 3s9ˆdkrdStttˆƒƒƒ}ˆ|d|}d}d}xì||kr4||}|ˆkr}t||ƒ}n(|ˆ8}tt|ˆƒt|ƒƒ}|}t|dˆƒ}ˆ|ƒ}|dkrâ|d7}qIˆ|dkr ˆ|}ˆ|} nˆ|}ˆ|} ||| fVd}qIWdS(s6Yield entering edges until none can be found. iNitkey(tintRRtrangeRtmin( tBtMtmtftltedgesR Rtptq(RRRRR(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pytfind_entering_edges-s0           cs½ˆ|}ˆ|}x¢tr¸x$||krCˆ|}ˆ|}q Wx$||krjˆ|}ˆ|}qGW||kr||kr®ˆ|}ˆ|}ˆ|}ˆ|}qµ|SqqWdS(sWFind the lowest common ancestor of nodes p and q in the spanning tree. N(tTrue(R'R(tsize_ptsize_q(tparenttsize(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pyt find_apexXs            csT|g}g}x8||krI|jˆ|ƒˆ|}|j|ƒqW||fS(sVReturn the nodes and edges on the path from node p to its ancestor w. (tappend(R'twtWntWe(tedgeR-(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pyt trace_pathns  csˆ||ƒ}ˆ||ƒ\}}|jƒ|jƒ|j|ƒˆ||ƒ\}}|d=||7}||7}||fS(sÀReturn the nodes and edges on the cycle containing edge i == (p, q) when the latter is added to the spanning tree. The cycle is oriented in the direction from p to q. iÿÿÿÿ(treverseR0(R R'R(R1R2R3tWnRtWeR(R/R5(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pyt find_cyclezs     cs(ˆ||kr ˆ|ˆ|Sˆ|S(seReturn the residual capacity of an edge i in the direction away from its endpoint p. ((R R'(RtUR(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pytresidual_capacityŒscsgttt|ƒt|ƒƒd‡fd†ƒ\}}ˆ||krPˆ|nˆ|}|||fS(sEReturn the leaving edge in a cycle represented by Wn and We. Rcs ˆ|ŒS(N((ti_p(R;(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pyt–s(R tziptreversed(R2R3tjtstt(RRR;(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pytfind_leaving_edge’s$csWxPt||ƒD]?\}}ˆ||kr?ˆ|c|7(R2R3R$R R'(RR(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pyt augment_flowšsc3s5|Vˆ|}x||kr0ˆ|}|VqWdS(s;Yield the nodes in the subtree rooted at a node p. N((R'R%(tlasttnext(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pyt trace_subtree£s   cs²ˆ|}ˆ|}ˆ|}ˆ|}dˆ|R(R(t ancestorsR'R+tlast_ptprev_qtlast_qt next_last_q(R4RERFR-RMR.(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pyt make_rootÃs:  (                    cs²ˆ|}ˆ|}ˆ|}ˆ|}|ˆ|<|ˆ|<|ˆ|<|ˆ|<|ˆ|<|ˆ|!ss"no flow satisfies all node demandsc3s#|]}ˆ|dˆkVqdS(iN((R R (tfaux_infR(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pys $sc3sG|]=}|djˆˆƒˆko>|djˆdƒdkVqdS(iÿÿÿÿiN(R(R R(R RR (s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pys %ss+negative cycle with infinite capacity foundcss|]\}}||VqdS(N((R RR((s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pys /scSsi|]}i|“qS(((R tn((s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pys 0s cstˆ|d}xM|dd!D]>}y||}Wqtk rYi}|||<|}qXqW|d||d?sc3s|]}ˆ|VqdS(N((R RB(R^(s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pys @sN(i(i(tlentnxt NetworkXErrortlistt enumeratetnodesRtfloatR>Rt is_multigraphR&R*R0tselfloop_edgestsumtNetworkXUnfeasibletmaxRtextendRRHRtindextanytNetworkXUnbounded(!tGR R R tIRtDR'tbt multigraphtKtER&R RRYRR)R9RCRDRNRTRVRWR(R2R3R@RARBt flow_costR]((RR^RRR:R RR4RXR/R\RRERFR-RRMRR;R.R5RGR Rs}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pyRsŸ ,   ! "!      " (,!%$"+   %    &%    (t__doc__t __author__t__all__t itertoolsRRRtmathRRtnetworkxR`tnetworkx.utilsRRR>t ImportErrortxrangeRt NameErrorR(((s}/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/flow/networksimplex.pyts