ó
 ,µ[c           @   s´   d  Z  d d l m Z d d l Z d d l m Z d d d g Z e d ƒ e d	 ƒ d d
 „ ƒ ƒ Z	 e d ƒ e d	 ƒ d d „ ƒ ƒ Z
 e d ƒ e d	 ƒ e d d „ ƒ ƒ Z d S(   s   Bridge-finding algorithms.iÿÿÿÿ(   t   chainN(   t   not_implemented_fort   bridgest   has_bridgest   local_bridgest
   multigrapht   directedc         c   s}   t  j |  d | ƒ} t t j | ƒ ƒ } xL |  j ƒ  D]> \ } } | | f | k r7 | | f | k r7 | | f Vq7 q7 Wd S(   sÑ  Generate all bridges in a graph.

    A *bridge* in a graph is an edge whose removal causes the number of
    connected components of the graph to increase.  Equivalently, a bridge is an
    edge that does not belong to any cycle.

    Parameters
    ----------
    G : undirected graph

    root : node (optional)
       A node in the graph `G`. If specified, only the bridges in the
       connected component containing this node will be returned.

    Yields
    ------
    e : edge
       An edge in the graph whose removal disconnects the graph (or
       causes the number of connected components to increase).

    Raises
    ------
    NodeNotFound
       If `root` is not in the graph `G`.

    Examples
    --------
    The barbell graph with parameter zero has a single bridge:

    >>> G = nx.barbell_graph(10, 0)
    >>> list(nx.bridges(G))
    [(9, 10)]

    Notes
    -----
    This is an implementation of the algorithm described in _[1].  An edge is a
    bridge if and only if it is not contained in any chain. Chains are found
    using the :func:`networkx.chain_decomposition` function.

    Ignoring polylogarithmic factors, the worst-case time complexity is the
    same as the :func:`networkx.chain_decomposition` function,
    $O(m + n)$, where $n$ is the number of nodes in the graph and $m$ is
    the number of edges.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Bridge_%28graph_theory%29#Bridge-Finding_with_Chain_Decompositions
    t   rootN(   t   nxt   chain_decompositiont   setR    t   from_iterablet   edges(   t   GR   t   chainst   chain_edgest   ut   v(    (    s:   lib/python2.7/site-packages/networkx/algorithms/bridges.pyR      s
    3$c         C   s1   y t  t |  ƒ ƒ Wn t k
 r( t SXt Sd S(   s   Decide whether a graph has any bridges.

    A *bridge* in a graph is an edge whose removal causes the number of
    connected components of the graph to increase.

    Parameters
    ----------
    G : undirected graph

    root : node (optional)
       A node in the graph `G`. If specified, only the bridges in the
       connected component containing this node will be considered.

    Returns
    -------
    bool
       Whether the graph (or the connected component containing `root`)
       has any bridges.

    Raises
    ------
    NodeNotFound
       If `root` is not in the graph `G`.

    Examples
    --------
    The barbell graph with parameter zero has a single bridge::

        >>> G = nx.barbell_graph(10, 0)
        >>> nx.has_bridges(G)
        True

    On the other hand, the cycle graph has no bridges::

        >>> G = nx.cycle_graph(5)
        >>> nx.has_bridges(G)
        False

    Notes
    -----
    This implementation uses the :func:`networkx.bridges` function, so
    it shares its worst-case time complexity, $O(m + n)$, ignoring
    polylogarithmic factors, where $n$ is the number of nodes in the
    graph and $m$ is the number of edges.

    N(   t   nextR   t   StopIterationt   Falset   True(   R   R   (    (    s:   lib/python2.7/site-packages/networkx/algorithms/bridges.pyR   M   s
    1c         #   s  | t  k	 rU x|  j D]8 \ } } t |  | ƒ t |  | ƒ @s | | f Vq q WnÆ t j j |  | ƒ ‰ x® |  j D]£ \ } } t |  | ƒ t |  | ƒ @st | | h ‰  ‡  ‡ f d †  } y- t j |  | | d | ƒ} | | | f VWqt j k
 r| | t d ƒ f VqXqt qt Wd S(   s  Iterate over local bridges of `G` optionally computing the span

    A *local bridge* is an edge whose endpoints have no common neighbors.
    That is, the edge is not part of a triangle in the graph.

    The *span* of a *local bridge* is the shortest path length between
    the endpoints if the local bridge is removed.

    Parameters
    ----------
    G : undirected graph

    with_span : bool
        If True, yield a 3-tuple `(u, v, span)`

    weight : function, string or None (default: None)
        If function, used to compute edge weights for the span.
        If string, the edge data attribute used in calculating span.
        If None, all edges have weight 1.

    Yields
    ------
    e : edge
        The local bridges as an edge 2-tuple of nodes `(u, v)` or
        as a 3-tuple `(u, v, span)` when `with_span is True`.

    Examples
    --------
    A cycle graph has every edge a local bridge with span N-1.

       >>> G = nx.cycle_graph(9)
       >>> (0, 8, 8) in set(nx.local_bridges(G))
       True
    c            s,   |  ˆ  k s | ˆ  k r( ˆ |  | | ƒ Sd  S(   N(   t   None(   t   nt   nbrt   d(   t   enodest   wt(    s:   lib/python2.7/site-packages/networkx/algorithms/bridges.pyt	   hide_edgeµ   s    t   weightt   infN(	   R   R   R
   R   t   weightedt   _weight_functiont   shortest_path_lengtht   NetworkXNoPatht   float(   R   t	   with_spanR   R   R   R   t   span(    (   R   R   s:   lib/python2.7/site-packages/networkx/algorithms/bridges.pyR   †   s    %(   t   __doc__t	   itertoolsR    t   networkxR   t   networkx.utilsR   t   __all__R   R   R   R   R   (    (    (    s:   lib/python2.7/site-packages/networkx/algorithms/bridges.pyt   <module>
   s   		8		7		