ó
|£*^c           @   s�   d  Z  d d d d d g Z d d l Z d d l m Z m Z d d	 l m Z d
 e	 d „ Z
 d
 d „ Z e Z d
 e	 d „ Z d
 d „ Z d S(   s6   
Computes minimum spanning tree of a weighted graph.

t   kruskal_mstt   minimum_spanning_edgest   minimum_spanning_treet   prim_mst_edgest   prim_mstiÿÿÿÿN(   t   heappopt   heappush(   t   countt   weightc   	      #   sÅ   d d l  m } |  j ƒ  r. t j d ƒ ‚ n  | ƒ  } t |  j d t ƒ d ‡  f d †  ƒ} x` | D]X \ } } } | | | | k re | rŸ | | | f Vn | | f V| j | | ƒ qe qe Wd S(   sØ  Generate edges in a minimum spanning forest of an undirected
    weighted graph.

    A minimum spanning tree is a subgraph of the graph (a tree)
    with the minimum sum of edge weights.  A spanning forest is a
    union of the spanning trees for each connected component of the graph.

    Parameters
    ----------
    G : NetworkX Graph

    weight : string
       Edge data key to use for weight (default 'weight').

    data : bool, optional
       If True yield the edge data along with the edge.

    Returns
    -------
    edges : iterator
       A generator that produces edges in the minimum spanning tree.
       The edges are three-tuples (u,v,w) where w is the weight.

    Examples
    --------
    >>> G=nx.cycle_graph(4)
    >>> G.add_edge(0,3,weight=2) # assign weight 2 to edge 0-3
    >>> mst=nx.minimum_spanning_edges(G,data=False) # a generator of MST edges
    >>> edgelist=list(mst) # make a list of the edges
    >>> print(sorted(edgelist))
    [(0, 1), (1, 2), (2, 3)]

    Notes
    -----
    Uses Kruskal's algorithm.

    If the graph edges do not have a weight attribute a default weight of 1
    will be used.

    Modified code from David Eppstein, April 2006
    http://www.ics.uci.edu/~eppstein/PADS/
    iÿÿÿÿ(   t	   UnionFinds6   Mimimum spanning tree not defined for directed graphs.t   datat   keyc            s   |  d j  ˆ  d ƒ S(   Ni   i   (   t   get(   t   t(   R   (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/mst.pyt   <lambda>O   s    N(	   t   networkx.utilsR	   t   is_directedt   nxt   NetworkXErrort   sortedt   edgest   Truet   union(	   t   GR   R
   R	   t   subtreesR   t   ut   vt   d(    (   R   sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/mst.pyR      s    1	'c         C   s¾   t  j t  j |  d | d t ƒƒ } t | ƒ t |  ƒ k r} | j g  |  j ƒ  j ƒ  D] \ } } | d k rU | ^ qU ƒ n  x( | D]  } |  j | j	 ƒ  | j | <q„ W|  j
 j	 ƒ  | _
 | S(   sÊ  Return a minimum spanning tree or forest of an undirected
    weighted graph.

    A minimum spanning tree is a subgraph of the graph (a tree) with
    the minimum sum of edge weights.

    If the graph is not connected a spanning forest is constructed.  A
    spanning forest is a union of the spanning trees for each
    connected component of the graph.

    Parameters
    ----------
    G : NetworkX Graph

    weight : string
       Edge data key to use for weight (default 'weight').

    Returns
    -------
    G : NetworkX Graph
       A minimum spanning tree or forest.

    Examples
    --------
    >>> G=nx.cycle_graph(4)
    >>> G.add_edge(0,3,weight=2) # assign weight 2 to edge 0-3
    >>> T=nx.minimum_spanning_tree(G)
    >>> print(sorted(T.edges(data=True)))
    [(0, 1, {}), (1, 2, {}), (2, 3, {})]

    Notes
    -----
    Uses Kruskal's algorithm.

    If the graph edges do not have a weight attribute a default weight of 1
    will be used.
    R   R
   i    (   R   t   GraphR   R   t   lent   add_nodes_fromt   degreet   itemst   nodet   copyt   graph(   R   R   t   Tt   nR   (    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/mst.pyR   Y   s    &$Ac         c   sž  |  j  ƒ  r t j d ƒ ‚ n  t } t } |  j ƒ  } t ƒ  } xX| r™| j d ƒ } g  } | g }	 xP |  j | ƒ D]? \ } }
 | | |  | |
 j	 | d ƒ t
 | ƒ | |
 f ƒ qv WxÚ | r•| | ƒ \ } } } }
 |
 |	 k rì q¼ n  |	 j |
 ƒ | j |
 ƒ x_ |  j |
 ƒ D]N \ }
 } | |	 k r| | |  |
 | j	 | d ƒ t
 | ƒ |
 | f ƒ qqW| r‡| |
 |  | |
 f Vq¼ | |
 f Vq¼ WqB Wd S(   so  Generate edges in a minimum spanning forest of an undirected
    weighted graph.

    A minimum spanning tree is a subgraph of the graph (a tree)
    with the minimum sum of edge weights.  A spanning forest is a
    union of the spanning trees for each connected component of the graph.

    Parameters
    ----------
    G : NetworkX Graph

    weight : string
       Edge data key to use for weight (default 'weight').

    data : bool, optional
       If True yield the edge data along with the edge.

    Returns
    -------
    edges : iterator
       A generator that produces edges in the minimum spanning tree.
       The edges are three-tuples (u,v,w) where w is the weight.

    Examples
    --------
    >>> G=nx.cycle_graph(4)
    >>> G.add_edge(0,3,weight=2) # assign weight 2 to edge 0-3
    >>> mst=nx.prim_mst_edges(G,data=False) # a generator of MST edges
    >>> edgelist=list(mst) # make a list of the edges
    >>> print(sorted(edgelist))
    [(0, 1), (1, 2), (2, 3)]

    Notes
    -----
    Uses Prim's algorithm.

    If the graph edges do not have a weight attribute a default weight of 1
    will be used.
    s6   Mimimum spanning tree not defined for directed graphs.i    i   N(   R   R   R   R   R   t   nodesR   t   popR   R   t   nextt   appendt   remove(   R   R   R
   t   pushR'   R&   t   cR   t   frontiert   visitedR   t   Wt   _t   w(    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/mst.pyR   Œ   s2    )			7	:c         C   s¾   t  j t  j |  d | d t ƒƒ } t | ƒ t |  ƒ k r} | j g  |  j ƒ  j ƒ  D] \ } } | d k rU | ^ qU ƒ n  x( | D]  } |  j | j	 ƒ  | j | <q„ W|  j
 j	 ƒ  | _
 | S(   sº  Return a minimum spanning tree or forest of an undirected
    weighted graph.

    A minimum spanning tree is a subgraph of the graph (a tree) with
    the minimum sum of edge weights.

    If the graph is not connected a spanning forest is constructed.  A
    spanning forest is a union of the spanning trees for each
    connected component of the graph.

    Parameters
    ----------
    G : NetworkX Graph

    weight : string
       Edge data key to use for weight (default 'weight').

    Returns
    -------
    G : NetworkX Graph
       A minimum spanning tree or forest.

    Examples
    --------
    >>> G=nx.cycle_graph(4)
    >>> G.add_edge(0,3,weight=2) # assign weight 2 to edge 0-3
    >>> T=nx.prim_mst(G)
    >>> print(sorted(T.edges(data=True)))
    [(0, 1, {}), (1, 2, {}), (2, 3, {})]

    Notes
    -----
    Uses Prim's algorithm.

    If the graph edges do not have a weight attribute a default weight of 1
    will be used.
    R   R
   i    (   R   R   R   R   R   R   R   R    R!   R"   R#   (   R   R   R$   R%   R   (    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/mst.pyR   Õ   s    '$A(   t   __doc__t   __all__t   networkxR   t   heapqR   R   t	   itertoolsR   R   R   R   R    R   R   (    (    (    sk   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/mst.pyt   <module>   s   	A0I