ó
|£*^c           @   s|   d  Z  d d l Z d j d d g ƒ Z d d d d	 d
 d g Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d d „ Z d S(   sR   
==========================
Bipartite Graph Algorithms
==========================
iÿÿÿÿNs   
s%   Jordi Torrents <jtorrents@milnou.net>s%   Aric Hagberg <aric.hagberg@gmail.com>t   is_bipartitet   is_bipartite_node_sett   colort   setst   densityt   degreesc            s8  ˆ  j  ƒ  r- d d l ‰ ‡  ‡ f d †  } n	 ˆ  j } i  } xÓ ˆ  D]Ë } | | k sC t ˆ  | ƒ d k rq qC n  | g } d | | <x‡ | r| j ƒ  } d | | } x` | | ƒ D]R } | | k rï | | | | k rt j d ƒ ‚ qq´ | | | <| j | ƒ q´ Wq‡ WqC W| j t	 j
 t j ˆ  ƒ d ƒ ƒ | S(   sí  Returns a two-coloring of the graph.

    Raises an exception if the graph is not bipartite.

    Parameters
    ----------
    G : NetworkX graph

    Returns
    -------
    color : dictionary
       A dictionary keyed by node with a 1 or 0 as data for each node color.

    Raises
    ------
    NetworkXError if the graph is not two-colorable.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.path_graph(4)
    >>> c = bipartite.color(G)
    >>> print(c)
    {0: 1, 1: 0, 2: 1, 3: 0}

    You can use this to set a node attribute indicating the biparite set:

    >>> nx.set_node_attributes(G, 'bipartite', c)
    >>> print(G.node[0]['bipartite'])
    1
    >>> print(G.node[1]['bipartite'])
    0
    iÿÿÿÿNc            s(   ˆ j  j ˆ  j |  ƒ ˆ  j |  ƒ g ƒ S(   N(   t   chaint   from_iterablet   predecessors_itert   successors_iter(   t   v(   t   Gt	   itertools(    sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/basic.pyt	   neighbors<   s    i    i   s   Graph is not bipartite.(   t   is_directedR   t   neighbors_itert   lent   popt   nxt   NetworkXErrort   appendt   updatet   dictt   fromkeyst   isolates(   R   R   R   t   nt   queueR
   t   ct   w(    (   R   R   sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/basic.pyR      s*    "	"	
	
"c         C   s.   y t  |  ƒ t SWn t j k
 r) t SXd S(   sG   Returns True if graph G is bipartite, False if not.

    Parameters
    ----------
    G : NetworkX graph

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.path_graph(4)
    >>> print(bipartite.is_bipartite(G))
    True

    See Also
    --------
    color, is_bipartite_node_set
    N(   R   t   TrueR   R   t   False(   R   (    (    sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/basic.pyR    V   s
    
c         C   s|   t  | ƒ } xi t j |  ƒ D]X } t | ƒ \ } } | j | ƒ rR | j | ƒ pm | j | ƒ om | j | ƒ s t Sq Wt S(   sù  Returns True if nodes and G/nodes are a bipartition of G.

    Parameters
    ----------
    G : NetworkX graph

    nodes: list or container
      Check if nodes are a one of a bipartite set.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.path_graph(4)
    >>> X = set([1,3])
    >>> bipartite.is_bipartite_node_set(G,X)
    True

    Notes
    -----
    For connected graphs the bipartite sets are unique.  This function handles
    disconnected graphs.
    (   t   setR   t   connected_component_subgraphsR   t   issubsett
   isdisjointR   R   (   R   t   nodest   St   CCt   Xt   Y(    (    sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/basic.pyR   n   s    c            sN   t  |  ƒ ‰  t ‡  f d †  ˆ  Dƒ ƒ } t ‡  f d †  ˆ  Dƒ ƒ } | | f S(   sõ  Returns bipartite node sets of graph G.

    Raises an exception if the graph is not bipartite.

    Parameters
    ----------
    G : NetworkX graph

    Returns
    -------
    (X,Y) : two-tuple of sets
       One set of nodes for each part of the bipartite graph.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.path_graph(4)
    >>> X, Y = bipartite.sets(G)
    >>> list(X)
    [0, 2]
    >>> list(Y)
    [1, 3]

    See Also
    --------
    color
    c         3   s   |  ] } ˆ  | r | Vq d  S(   N(    (   t   .0R   (   R   (    sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/basic.pys	   <genexpr>«   s    c         3   s   |  ] } ˆ  | s | Vq d  S(   N(    (   R(   R   (   R   (    sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/basic.pys	   <genexpr>¬   s    (   R   R   (   R   R&   R'   (    (   R   sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/basic.pyR   Ž   s    c         C   s…   t  |  ƒ } t j |  ƒ } t  | ƒ } | | } | d k rF d } n; |  j ƒ  rm | d t | | ƒ } n | t | | ƒ } | S(   s	  Return density of bipartite graph B.

    Parameters
    ----------
    G : NetworkX graph

    nodes: list or container
      Nodes in one set of the bipartite graph.

    Returns
    -------
    d : float
       The bipartite density

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.complete_bipartite_graph(3,2)
    >>> X=set([0,1,2])
    >>> bipartite.density(G,X)
    1.0
    >>> Y=set([3,4])
    >>> bipartite.density(G,Y)
    1.0

    See Also
    --------
    color
    i    g        g       @(   R   R   t   number_of_edgesR   t   float(   t   BR#   R   t   mt   nbt   ntt   d(    (    sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/basic.pyR   ¯   s    
	c         C   s>   t  | ƒ } t  |  ƒ | } |  j | | ƒ |  j | | ƒ f S(   sU  Return the degrees of the two node sets in the bipartite graph B.

    Parameters
    ----------
    G : NetworkX graph

    nodes: list or container
      Nodes in one set of the bipartite graph.

    weight : string or None, optional (default=None)
       The edge attribute that holds the numerical value used as a weight.
       If None, then each edge has weight 1.
       The degree is the sum of the edge weights adjacent to the node.

    Returns
    -------
    (degX,degY) : tuple of dictionaries
       The degrees of the two bipartite sets as dictionaries keyed by node.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.complete_bipartite_graph(3,2)
    >>> Y=set([3,4])
    >>> degX,degY=bipartite.degrees(G,Y)
    >>> degX
    {0: 2, 1: 2, 2: 2}

    See Also
    --------
    color, density
    (   R   t   degree(   R+   R#   t   weightt   bottomt   top(    (    sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/basic.pyR   Ú   s    !(   t   __doc__t   networkxR   t   joint
   __author__t   __all__R   R    R   R   R   t   NoneR   (    (    (    sw   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/basic.pyt   <module>   s   			>		 	!	+