ó
ú£Õ\c           @   sª   d  Z  d d l Z d d l m Z d j d d g ƒ Z d d d	 d
 d d d g Z d d d d „ Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z d S(   sR   
==========================
Bipartite Graph Algorithms
==========================
iÿÿÿÿN(   t   counts   
s%   Jordi Torrents <jtorrents@milnou.net>s%   Aric Hagberg <aric.hagberg@gmail.com>t   is_bipartitet   is_bipartite_node_sett   colort   setst   densityt   degreest   biadjacency_matrixt   weightc         C   s  y d d l  } Wn  t k
 r2 t d d ƒ ‚ n X| d k r^ t t |  ƒ t | ƒ ƒ } n  t t | t ƒ  ƒ ƒ } t t | t ƒ  ƒ ƒ } | j t	 | ƒ t	 | ƒ f d | ƒ} xS | D]K }	 xB |  |	 j
 ƒ  D]0 \ }
 } | j | d ƒ | | |	 | |
 f <qÓ Wq¼ W| j | ƒ S(   s²  Return the biadjacency matrix of the bipartite graph G.

    Let `G = (U, V, E)` be a bipartite graph with node sets
    `U = u_{1},...,u_{r}` and `V = v_{1},...,v_{s}`. The biadjacency
    matrix [1] is the `r` x `s` matrix `B` in which `b_{i,j} = 1`
    if, and only if, `(u_i, v_j) \in E`. If the parameter `weight` is
    not `None` and matches the name of an edge attribute, its value is
    used instead of 1.

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

    row_order : list of nodes
       The rows of the matrix are ordered according to the list of nodes.

    column_order : list, optional
       The columns of the matrix are ordered according to the list of nodes.
       If column_order is None, then the ordering of columns is arbitrary.

    weight : string or None, optional (default='weight')
       The edge data key used to provide each value in the matrix.
       If None, then each edge has weight 1.

    dtype : NumPy data type, optional
        A valid single NumPy data type used to initialize the array.
        This must be a simple type such as int or numpy.float64 and
        not a compound data type (see to_numpy_recarray)
        If None, then the NumPy default is used.

    Returns
    -------
    B : numpy matrix
      Biadjacency matrix representation of the bipartite graph G.

    Notes
    -----
    No attempt is made to check that the input graph is bipartite.

    For directed bipartite graphs only successors are considered as neighbors.
    To obtain an adjacency matrix with ones (or weight values) for both
    predecessors and successors you have to generate two biadjacency matrices
    where the rows of one of them are the columns of the other, and then add
    one to the transpose of the other.

    See Also
    --------
    to_numpy_matrix
    adjacency_matrix

    References
    ----------
    [1] http://en.wikipedia.org/wiki/Adjacency_matrix#Adjacency_matrix_of_a_bipartite_graph
    iÿÿÿÿNs"   adjacency_matrix() requires numpy s   http://scipy.org/t   dtypei   (   t   numpyt   ImportErrort   Nonet   listt   sett   dictt   zipR    t   zerost   lent   itemst   gett   asmatrix(   t   Gt	   row_ordert   column_orderR   R	   t   npt   rowt   colt   Mt   ut   vt   d(    (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/basic.pyR      s    9',c            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(   R   (   R   t	   itertools(    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/basic.pyt	   neighbors…   s    i    i   s   Graph is not bipartite.(   t   is_directedR$   t   neighbors_iterR   t   popt   nxt   NetworkXErrort   appendt   updateR   t   fromkeyst   isolates(   R   R%   R   t   nt   queueR   t   ct   w(    (   R   R$   s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/basic.pyR   a   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   (    (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/basic.pyR   Ÿ   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.
    (   R   R)   t   connected_component_subgraphsR   t   issubsett
   isdisjointR4   R3   (   R   t   nodest   St   CCt   Xt   Y(    (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/basic.pyR   ·   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/   (   R1   (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/basic.pys	   <genexpr>ô   s    c         3   s   |  ] } ˆ  | s | Vq d  S(   N(    (   R=   R/   (   R1   (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/basic.pys	   <genexpr>õ   s    (   R   R   (   R   R;   R<   (    (   R1   s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/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   BR8   R/   t   mt   nbt   ntR   (    (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/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@   R8   R   t   bottomt   top(    (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/basic.pyR   #  s    !c         C   s:   d d l  m } y d d  l } Wn | d ƒ ‚ n Xd  S(   Niÿÿÿÿ(   t   SkipTests   NumPy not available(   t   noseRG   R
   (   t   moduleRG   R
   (    (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/basic.pyt   setup_moduleJ  s
    (   t   __doc__t   networkxR)   R$   R    t   joint
   __author__t   __all__R   R   R   R   R   R   R   R   RJ   (    (    (    s[   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/basic.pyt   <module>   s(   		G	>		 	!	+'