ó
 ,µ[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 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
    ------
    exc:`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, c, 'bipartite')
    >>> print(G.nodes[0]['bipartite'])
    1
    >>> print(G.nodes[1]['bipartite'])
    0
    iÿÿÿÿNc            s(   ˆ j  j ˆ  j |  ƒ ˆ  j |  ƒ g ƒ S(   N(   t   chaint   from_iterablet   predecessorst
   successors(   t   v(   t   Gt	   itertools(    sB   lib/python2.7/site-packages/networkx/algorithms/bipartite/basic.pyt	   neighbors=   s    i    i   s   Graph is not bipartite.(   t   is_directedR   R   t   lent   popt   nxt   NetworkXErrort   appendt   updatet   dictt   fromkeyst   isolates(   R   R   R   t   nt   queueR
   t   ct   w(    (   R   R   sB   lib/python2.7/site-packages/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   (    (    sB   lib/python2.7/site-packages/networkx/algorithms/bipartite/basic.pyR    X   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(    (    sB   lib/python2.7/site-packages/networkx/algorithms/bipartite/basic.pyR   q   s    c         C   s²   |  j  ƒ  r t j } n	 t j } | d k	 rL t | ƒ } t |  ƒ | } n\ | |  ƒ sp d } t j | ƒ ‚ n  t |  ƒ } d „  | j ƒ  Dƒ } d „  | j ƒ  Dƒ } | | f S(   s=  Returns bipartite node sets of graph G.

    Raises an exception if the graph is not bipartite or if the input
    graph is disconnected and thus more than one valid solution exists.
    See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
    for further details on how bipartite graphs are handled in NetworkX.

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

    top_nodes : container

      Container with all nodes in one bipartite node set. If not supplied
      it will be computed. But if more than one solution exists an exception
      will be raised.

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

    Raises
    ------
    AmbiguousSolution : Exception

      Raised if the input bipartite graph is disconnected and no container
      with all nodes in one bipartite set is provided. When determining
      the nodes in each bipartite set more than one valid solution is
      possible if the input graph is disconnected.

    NetworkXError: Exception

      Raised if the input graph is not bipartite.

    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

    s:   Disconnected graph: Ambiguous solution for bipartite sets.c         S   s"   h  |  ] \ } } | r | ’ q S(    (    (   t   .0R   t   is_top(    (    sB   lib/python2.7/site-packages/networkx/algorithms/bipartite/basic.pys	   <setcomp>Ð   s   	 c         S   s"   h  |  ] \ } } | s | ’ q S(    (    (   R'   R   R(   (    (    sB   lib/python2.7/site-packages/networkx/algorithms/bipartite/basic.pys	   <setcomp>Ñ   s   	 N(	   R   R   t   is_weakly_connectedt   is_connectedt   NoneR   t   AmbiguousSolutionR   t   items(   R   t	   top_nodesR*   R%   R&   t   msgR   (    (    sB   lib/python2.7/site-packages/networkx/algorithms/bipartite/basic.pyR   ‘   s    3	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 node 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

    Notes
    -----
    The container of nodes passed as argument must contain all nodes
    in one of the two bipartite node sets to avoid ambiguity in the
    case of disconnected graphs.
    See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
    for further details on how bipartite graphs are handled in NetworkX.

    See Also
    --------
    color
    i    g        g       @(   R   R   t   number_of_edgesR   t   float(   t   BR"   R   t   mt   nbt   ntt   d(    (    sB   lib/python2.7/site-packages/networkx/algorithms/bipartite/basic.pyR   Õ   s    &
	c         C   s>   t  | ƒ } t  |  ƒ | } |  j | | ƒ |  j | | ƒ f S(   s¯  Return the degrees of the two node sets in the bipartite graph B.

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

    nodes: list or container
      Nodes in one node 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)
    >>> dict(degX)
    {0: 2, 1: 2, 2: 2}

    Notes
    -----
    The container of nodes passed as argument must contain all nodes
    in one of the two bipartite node sets to avoid ambiguity in the
    case of disconnected graphs.
    See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
    for further details on how bipartite graphs are handled in NetworkX.

    See Also
    --------
    color, density
    (   R   t   degree(   R2   R"   t   weightt   bottomt   top(    (    sB   lib/python2.7/site-packages/networkx/algorithms/bipartite/basic.pyR   	  s    )(   t   __doc__t   networkxR   t   joint
   __author__t   __all__R   R    R   R+   R   R   R   (    (    (    sB   lib/python2.7/site-packages/networkx/algorithms/bipartite/basic.pyt   <module>   s   			@		 D	4