ó
ú£Õ\c           @   sv   d  Z  d Z d d l Z d d d d g Z d d	 „ Z d d
 „ Z d d „ Z d d „ Z d d „ Z	 d d „ Z
 d S(   sB   
Maximum flow (and minimum cut) algorithms on capacitated graphs.
s'   LoÃ¯c SÃ©guin-C. <loicseguin@gmail.com>iÿÿÿÿNt   ford_fulkersont   ford_fulkerson_flowt   max_flowt   min_cutt   capacityc         C   sƒ  t  j ƒ  } | j |  ƒ i  } t  j |  ƒ rª xH|  j d t ƒ D]b } | | d k r~ | d | d k r£ | j | Œ  q£ qA | j | Œ  d | | d | d f <qA WnÏ xÌ |  j d t ƒ D]¸ } | | d k r| d | d k ru| j | Œ  | j | d | d | d ƒ quq½ | j | Œ  | j | d | d | d ƒ d | | d | d f <d | | d | d f <q½ W| | f S(   s†   Initialize an auxiliary digraph and dict of infinite capacity
    edges for a given graph G.
    Ignore edges with capacity <= 0.
    t   datai   i    i   (   t   nxt   DiGrapht   add_nodes_fromt   is_directedt   edgest   Truet   add_edge(   t   GR   t	   auxiliaryt   inf_capacity_flowst   edge(    (    sX   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/flow/maxflow.pyt   _create_auxiliary_digraph   s(    %c         C   sd  t  g  |  D] } | i  f ^ q
 ƒ } |  j ƒ  rux,|  j ƒ  D]0\ } } | j | | ƒ rT| |  | | k r¦ t d |  | | | | | | | ƒ | | | <qn|  j | | ƒ rþ | |  | | k rþ t d | | | f | | | f ƒ | | | <qnt d | | j | i  ƒ j | d ƒ |  | j | i  ƒ j | d ƒ ƒ | | | <q> |  | | | | | | <q> Wnë xè |  j ƒ  D]Ú \ } } | j | | ƒ r| |  | | k rçt |  | | | | | | | ƒ | | | <qFt | | | f | | | f ƒ | | | <n0 t |  | | | | | | | ƒ | | | <| | | | | | <q‚W| S(   s   Creates the flow dict of dicts on G corresponding to the
    auxiliary digraph H and infinite capacity edges flows
    inf_capacity_flows.
    i    (   t   dictR	   t
   edges_itert   has_edget   maxt   gett   abs(   R   t   HR   R   t   ut   flowt   v(    (    sX   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/flow/maxflow.pyt   _create_flow_dict2   s*    %6&%1!30c         C   sZ  |  j  ƒ  r t j d ƒ ‚ n  | |  k rF t j d t | ƒ ƒ ‚ n  | |  k rn t j d t | ƒ ƒ ‚ n  t |  d | ƒ\ } } d } x©t r7y t j | | | ƒ } Wn t j k
 rÅ Pn Xt t	 | d  | d ƒ ƒ } yI t
 g  | D]2 \ }	 }
 | | |	 |
 k rð | |	 |
 | ^ qð ƒ } Wn  t k
 rNt j d ƒ ‚ n X| | 7} xØ | D]Ð \ }	 }
 | |	 |
 } | | k r¼| | c | 8<| | d k rÒ| j |	 |
 ƒ qÒn | |	 |
 f c | 7<| j |
 |	 ƒ r| | |
 |	 k r0| |
 |	 | c | 7<q0q`| j |
 |	 i | | 6ƒ q`Wq Wt |  | | d | ƒ} | | f S(   sÎ  Find a maximum single-commodity flow using the Ford-Fulkerson
    algorithm.
    
    This algorithm uses Edmonds-Karp-Dinitz path selection rule which
    guarantees a running time of O(nm^2) for n nodes and m edges.


    Parameters
    ----------
    G : NetworkX graph
        Edges of the graph are expected to have an attribute called
        'capacity'. If this attribute is not present, the edge is
        considered to have infinite capacity.

    s : node
        Source node for the flow.

    t : node
        Sink node for the flow.

    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'.

    Returns
    -------
    flow_value : integer, float
        Value of the maximum flow, i.e., net outflow from the source.

    flow_dict : dictionary
        Dictionary of dictionaries keyed by nodes such that
        flow_dict[u][v] is the flow edge (u, v).

    Raises
    ------
    NetworkXError
        The algorithm does not support MultiGraph and MultiDiGraph. If
        the input graph is an instance of one of these two classes, a
        NetworkXError is raised.

    NetworkXUnbounded
        If the graph has a path of infinite capacity, the value of a 
        feasible flow on the graph is unbounded above and the function
        raises a NetworkXUnbounded.

    Examples
    --------
    >>> import networkx as nx
    >>> G = nx.DiGraph()
    >>> G.add_edge('x','a', capacity=3.0)
    >>> G.add_edge('x','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','y', capacity=2.0)
    >>> G.add_edge('e','y', capacity=3.0)
    >>> flow, F = nx.ford_fulkerson(G, 'x', 'y')
    >>> flow
    3.0
    s0   MultiGraph and MultiDiGraph not supported (yet).s   node %s not in graphR   i    iÿÿÿÿi   s-   Infinite capacity path, flow unbounded above.(   t   is_multigraphR   t   NetworkXErrort   strR   R   t   bidirectional_shortest_patht   NetworkXNoPatht   listt   zipt   mint
   ValueErrort   NetworkXUnboundedt   remove_edgeR   R   R   (   R   t   st   tR   R   R   t
   flow_valuet
   path_nodest
   path_edgesR   R   t   path_capacityt	   edge_attrt	   flow_dict(    (    sX   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/flow/maxflow.pyR    V   sL    @		3
"	c         C   s   t  |  | | d | ƒd S(   s“  Return a maximum flow for a single-commodity flow problem.

    Parameters
    ----------
    G : NetworkX graph
        Edges of the graph are expected to have an attribute called
        'capacity'. If this attribute is not present, the edge is
        considered to have infinite capacity.

    s : node
        Source node for the flow.

    t : node
        Sink node for the flow.

    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'.

    Returns
    -------
    flow_dict : dictionary
        Dictionary of dictionaries keyed by nodes such that
        flow_dict[u][v] is the flow edge (u, v).

    Raises
    ------
    NetworkXError
        The algorithm does not support MultiGraph and MultiDiGraph. If
        the input graph is an instance of one of these two classes, a
        NetworkXError is raised.

    NetworkXUnbounded
        If the graph has a path of infinite capacity, the value of a 
        feasible flow on the graph is unbounded above and the function
        raises a NetworkXUnbounded.

    Examples
    --------
    >>> import networkx as nx
    >>> G = nx.DiGraph()
    >>> G.add_edge('x','a', capacity=3.0)
    >>> G.add_edge('x','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','y', capacity=2.0)
    >>> G.add_edge('e','y', capacity=3.0)
    >>> F = nx.ford_fulkerson_flow(G, 'x', 'y')
    >>> for u, v in G.edges_iter():
    ...     print('(%s, %s) %.2f' % (u, v, F[u][v]))
    ... 
    (a, c) 2.00
    (c, y) 2.00
    (b, c) 0.00
    (b, d) 1.00
    (e, y) 1.00
    (d, e) 1.00
    (x, a) 2.00
    (x, b) 1.00
    R   i   (   R    (   R   R(   R)   R   (    (    sX   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/flow/maxflow.pyR   Ð   s    Ac         C   s   t  |  | | d | ƒd S(   s˜  Find the value of a maximum single-commodity flow.
    
    Parameters
    ----------
    G : NetworkX graph
        Edges of the graph are expected to have an attribute called
        'capacity'. If this attribute is not present, the edge is
        considered to have infinite capacity.

    s : node
        Source node for the flow.

    t : node
        Sink node for the flow.

    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'.

    Returns
    -------
    flow_value : integer, float
        Value of the maximum flow, i.e., net outflow from the source.

    Raises
    ------
    NetworkXError
        The algorithm does not support MultiGraph and MultiDiGraph. If
        the input graph is an instance of one of these two classes, a
        NetworkXError is raised.

    NetworkXUnbounded
        If the graph has a path of infinite capacity, the value of a 
        feasible flow on the graph is unbounded above and the function
        raises a NetworkXUnbounded.

    Examples
    --------
    >>> import networkx as nx
    >>> G = nx.DiGraph()
    >>> G.add_edge('x','a', capacity=3.0)
    >>> G.add_edge('x','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','y', capacity=2.0)
    >>> G.add_edge('e','y', capacity=3.0)
    >>> flow = nx.max_flow(G, 'x', 'y')
    >>> flow
    3.0
    R   i    (   R    (   R   R(   R)   R   (    (    sX   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/flow/maxflow.pyR     s    7c         C   sH   y t  |  | | d | ƒd SWn# t j k
 rC t j d ƒ ‚ n Xd S(   s  Compute the value of a minimum (s, t)-cut.

    Use the max-flow min-cut theorem, i.e., the capacity of a minimum
    capacity cut is equal to the flow value of a maximum flow.

    Parameters
    ----------
    G : NetworkX graph
        Edges of the graph are expected to have an attribute called
        'capacity'. If this attribute is not present, the edge is
        considered to have infinite capacity.

    s : node
        Source node for the flow.

    t : node
        Sink node for the flow.

    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'.

    Returns
    -------
    cutValue : integer, float
        Value of the minimum cut.
    
    Raises
    ------
    NetworkXUnbounded
        If the graph has a path of infinite capacity, all cuts have
        infinite capacity and the function raises a NetworkXError.
    
    Examples
    --------
    >>> import networkx as nx
    >>> G = nx.DiGraph()
    >>> G.add_edge('x','a', capacity = 3.0)
    >>> G.add_edge('x','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','y', capacity = 2.0)
    >>> G.add_edge('e','y', capacity = 3.0)
    >>> nx.min_cut(G, 'x', 'y')
    3.0
    R   i    s'   Infinite capacity path, no minimum cut.N(   R    R   R&   (   R   R(   R)   R   (    (    sX   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/flow/maxflow.pyR   N  s
    4(   t   __doc__t
   __author__t   networkxR   t   __all__R   R   R    R   R   R   (    (    (    sX   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/flow/maxflow.pyt   <module>   s   	$zD: