ó
ú£Õ\c           @   sˆ   d  Z  d d l Z d j d d g ƒ Z d d d d	 d
 d g Z e d „ Z e d „ Z d „  Z	 e
 d „ Z d d „ Z d d „ Z d S(   s7   One-mode (unipartite) projections of bipartite graphs.
iÿÿÿÿNs   
s%   Aric Hagberg <aric.hagberg@gmail.com>s%   Jordi Torrents <jtorrents@milnou.net>t   projectt   projected_grapht   weighted_projected_grapht&   collaboration_weighted_projected_grapht    overlap_weighted_projected_grapht    generic_weighted_projected_graphc   	         s«  ˆ  j  ƒ  r t j d ƒ ‚ n  ˆ  j ƒ  rT t } | rE t j ƒ  } q{ t j ƒ  } n' t } | ro t j ƒ  } n t j	 ƒ  } | j
 j ˆ  j
 ƒ | j ‡  f d †  | Dƒ ƒ xù | D]ñ ‰ t ‡  f d †  ˆ  ˆ Dƒ ƒ t ˆ g ƒ } | r†xµ | D] } | r"t ˆ  ˆ ƒ t ˆ  j | ƒ @} n t ˆ  ˆ ƒ t ˆ  | ƒ @} x< | D]4 } | j ˆ | | ƒ sG| j ˆ | d | ƒqGqGWqò Wq² | j ‡ f d †  | Dƒ ƒ q² W| S(   s‰  Returns the projection of B onto one of its node sets.

    Returns the graph G that is the projection of the bipartite graph B 
    onto the specified nodes. They retain their attributes and are connected
    in G if they have a common neighbor in B.

    Parameters
    ----------
    B : NetworkX graph 
      The input graph should be bipartite. 

    nodes : list or iterable
      Nodes to project onto (the "bottom" nodes).

    multigraph: bool (default=False)
       If True return a multigraph where the multiple edges represent multiple
       shared neighbors.  They edge key in the multigraph is assigned to the
       label of the neighbor.

    Returns
    -------
    Graph : NetworkX graph or multigraph
       A graph that is the projection onto the given nodes.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> B = nx.path_graph(4)
    >>> G = bipartite.projected_graph(B, [1,3]) 
    >>> print(G.nodes())
    [1, 3]
    >>> print(G.edges())
    [(1, 3)]
    
    If nodes `a`, and `b` are connected through both nodes 1 and 2 then
    building a multigraph results in two edges in the projection onto 
    [`a`,`b`]:

    >>> B = nx.Graph()
    >>> B.add_edges_from([('a', 1), ('b', 1), ('a', 2), ('b', 2)])
    >>> G = bipartite.projected_graph(B, ['a', 'b'], multigraph=True)
    >>> print(G.edges(keys=True))
    [('a', 'b', 1), ('a', 'b', 2)]

    Notes
    ------
    No attempt is made to verify that the input graph B is bipartite.
    Returns a simple graph that is the projection of the bipartite graph B
    onto the set of nodes given in list nodes.  If multigraph=True then
    a multigraph is returned with an edge for every shared neighbor.

    Directed graphs are allowed as input.  The output will also then
    be a directed graph with edges if there is a directed path between
    the nodes.

    The graph and node properties are (shallow) copied to the projected graph.

    See Also
    --------
    is_bipartite, 
    is_bipartite_node_set, 
    sets, 
    weighted_projected_graph,
    collaboration_weighted_projected_graph,
    overlap_weighted_projected_graph,
    generic_weighted_projected_graph
    s   not defined for multigraphsc         3   s"   |  ] } | ˆ  j  | f Vq d  S(   N(   t   node(   t   .0t   n(   t   B(    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pys	   <genexpr>g   s    c         3   s&   |  ] } ˆ  | D] } | Vq q d  S(   N(    (   R   t   nbrt   v(   R	   (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pys	   <genexpr>i   s    t   keyc         3   s   |  ] } ˆ  | f Vq d  S(   N(    (   R   R   (   t   u(    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pys	   <genexpr>t   s    (   t   is_multigrapht   nxt   NetworkXErrort   is_directedt   Truet   MultiDiGrapht   DiGrapht   Falset
   MultiGrapht   Grapht   grapht   updatet   add_nodes_fromt   sett   predt   has_edget   add_edget   add_edges_from(	   R	   t   nodest
   multigrapht   directedt   Gt   nbrs2R   t   linkst   l(    (   R	   R   s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pyR      s2    D-$$!c            sW  ˆ  j  ƒ  r t j d ƒ ‚ n  ˆ  j ƒ  rB ˆ  j } t j ƒ  } n ˆ  j } t j ƒ  } | j j	 ˆ  j ƒ | j
 ‡  f d †  | Dƒ ƒ t t ˆ  ƒ t | ƒ ƒ } x­ | D]¥ } t ˆ  | ƒ } t ‡  f d †  | Dƒ ƒ t | g ƒ } xc | D][ }	 t | |	 ƒ }
 | |
 @} | s%t | ƒ } n t | ƒ | } | j | |	 d | ƒqð Wqª W| S(   sã  Returns a weighted projection of B onto one of its node sets.

    The weighted projected graph is the projection of the bipartite
    network B onto the specified nodes with weights representing the
    number of shared neighbors or the ratio between actual shared
    neighbors and possible shared neighbors if ratio=True [1]_. The
    nodes retain their attributes and are connected in the resulting graph
    if they have an edge to a common node in the original graph.

    Parameters
    ----------
    B : NetworkX graph 
        The input graph should be bipartite. 

    nodes : list or iterable
        Nodes to project onto (the "bottom" nodes).

    ratio: Bool (default=False)
        If True, edge weight is the ratio between actual shared neighbors 
        and possible shared neighbors. If False, edges weight is the number 
        of shared neighbors.

    Returns
    -------
    Graph : NetworkX graph 
       A graph that is the projection onto the given nodes.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> B = nx.path_graph(4)
    >>> G = bipartite.weighted_projected_graph(B, [1,3])
    >>> print(G.nodes())
    [1, 3]
    >>> print(G.edges(data=True))
    [(1, 3, {'weight': 1})]
    >>> G = bipartite.weighted_projected_graph(B, [1,3], ratio=True)
    >>> print(G.edges(data=True))
    [(1, 3, {'weight': 0.5})]
    
    Notes
    ------
    No attempt is made to verify that the input graph B is bipartite.
    The graph and node properties are (shallow) copied to the projected graph.

    See Also
    --------
    is_bipartite, 
    is_bipartite_node_set, 
    sets, 
    collaboration_weighted_projected_graph,
    overlap_weighted_projected_graph,
    generic_weighted_projected_graph
    projected_graph 

    References
    ----------
    .. [1] Borgatti, S.P. and Halgin, D. In press. "Analyzing Affiliation 
        Networks". In Carrington, P. and Scott, J. (eds) The Sage Handbook 
        of Social Network Analysis. Sage Publications.
    s   not defined for multigraphsc         3   s"   |  ] } | ˆ  j  | f Vq d  S(   N(   R   (   R   R   (   R	   (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pys	   <genexpr>¾   s    c         3   s&   |  ] } ˆ  | D] } | Vq q d  S(   N(    (   R   R
   R   (   R	   (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pys	   <genexpr>Â   s    t   weight(   R   R   R   R   R   R   t   adjR   R   R   R   t   floatt   lenR   R   (   R	   R    t   ratioR   R#   t   n_topR   t   unbrsR$   R   t   vnbrst   commonR'   (    (   R	   s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pyR   w   s*    >		)
c      	      s]  ˆ  j  ƒ  r t j d ƒ ‚ n  ˆ  j ƒ  rB ˆ  j } t j ƒ  } n ˆ  j } t j ƒ  } | j j	 ˆ  j ƒ | j
 ‡  f d †  | Dƒ ƒ xÏ | D]Ç } t ˆ  | ƒ } t ‡  f d †  | Dƒ ƒ t | g ƒ } x… | D]} } t | | ƒ } | | @}	 t g  |	 D]4 }
 t ˆ  |
 ƒ d k rþ d t ˆ  |
 ƒ d ^ qþ ƒ } | j | | d | ƒqÔ WqŽ W| S(   s7  Newman's weighted projection of B onto one of its node sets.

    The collaboration weighted projection is the projection of the
    bipartite network B onto the specified nodes with weights assigned
    using Newman's collaboration model [1]_:

    .. math::
        
        w_{v,u} = \sum_k \frac{\delta_{v}^{w} \delta_{w}^{k}}{k_w - 1}

    where `v` and `u` are nodes from the same bipartite node set,
    and `w` is a node of the opposite node set. 
    The value `k_w` is the degree of node `w` in the bipartite
    network and `\delta_{v}^{w}` is 1 if node `v` is
    linked to node `w` in the original bipartite graph or 0 otherwise.
 
    The nodes retain their attributes and are connected in the resulting
    graph if have an edge to a common node in the original bipartite
    graph.

    Parameters
    ----------
    B : NetworkX graph 
      The input graph should be bipartite. 

    nodes : list or iterable
      Nodes to project onto (the "bottom" nodes).

    Returns
    -------
    Graph : NetworkX graph 
       A graph that is the projection onto the given nodes.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> B = nx.path_graph(5)
    >>> B.add_edge(1,5)
    >>> G = bipartite.collaboration_weighted_projected_graph(B, [0, 2, 4, 5])
    >>> print(G.nodes())
    [0, 2, 4, 5]
    >>> for edge in G.edges(data=True): print(edge)
    ... 
    (0, 2, {'weight': 0.5})
    (0, 5, {'weight': 0.5})
    (2, 4, {'weight': 1.0})
    (2, 5, {'weight': 0.5})
    
    Notes
    ------
    No attempt is made to verify that the input graph B is bipartite.
    The graph and node properties are (shallow) copied to the projected graph.

    See Also
    --------
    is_bipartite, 
    is_bipartite_node_set, 
    sets, 
    weighted_projected_graph,
    overlap_weighted_projected_graph,
    generic_weighted_projected_graph,
    projected_graph 

    References
    ----------
    .. [1] Scientific collaboration networks: II. 
        Shortest paths, weighted networks, and centrality, 
        M. E. J. Newman, Phys. Rev. E 64, 016132 (2001).
    s   not defined for multigraphsc         3   s"   |  ] } | ˆ  j  | f Vq d  S(   N(   R   (   R   R   (   R	   (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pys	   <genexpr>  s    c         3   s&   |  ] } ˆ  | D] } | Vq q d  S(   N(    (   R   R
   R   (   R	   (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pys	   <genexpr>  s    i   g      ð?R'   (   R   R   R   R   R   R   R(   R   R   R   R   R   t   sumR*   R   (   R	   R    R   R#   R   R-   R$   R   R.   R/   R   R'   (    (   R	   s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pyR   Í   s$    F		)
Gc            sh  ˆ  j  ƒ  r t j d ƒ ‚ n  ˆ  j ƒ  rB ˆ  j } t j ƒ  } n ˆ  j } t j ƒ  } | j j	 ˆ  j ƒ | j
 ‡  f d †  | Dƒ ƒ xÚ | D]Ò } t ˆ  | ƒ } t ‡  f d †  | Dƒ ƒ t | g ƒ } x | D]ˆ } t | | ƒ }	 | rt t | |	 @ƒ ƒ t | |	 Bƒ }
 n/ t t | |	 @ƒ ƒ t t | ƒ t |	 ƒ ƒ }
 | j | | d |
 ƒqÔ WqŽ W| S(   s¶  Overlap weighted projection of B onto one of its node sets.

    The overlap weighted projection is the projection of the bipartite 
    network B onto the specified nodes with weights representing 
    the Jaccard index between the neighborhoods of the two nodes in the
    original bipartite network [1]_: 

    .. math::
        
        w_{v,u} = \frac{|N(u) \cap N(v)|}{|N(u) \cup N(v)|}

    or if the parameter 'jaccard' is False, the fraction of common 
    neighbors by minimum of both nodes degree in the original 
    bipartite graph [1]_:
    
    .. math::

        w_{v,u} = \frac{|N(u) \cap N(v)|}{min(|N(u)|,|N(v)|)}
    
    The nodes retain their attributes and are connected in the resulting
    graph if have an edge to a common node in the original bipartite graph.

    Parameters
    ----------
    B : NetworkX graph 
        The input graph should be bipartite. 

    nodes : list or iterable
        Nodes to project onto (the "bottom" nodes).

    jaccard: Bool (default=True)

    Returns
    -------
    Graph : NetworkX graph 
       A graph that is the projection onto the given nodes.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> B = nx.path_graph(5)
    >>> G = bipartite.overlap_weighted_projected_graph(B, [0, 2, 4])
    >>> print(G.nodes())
    [0, 2, 4]
    >>> print(G.edges(data=True))
    [(0, 2, {'weight': 0.5}), (2, 4, {'weight': 0.5})]
    >>> G = bipartite.overlap_weighted_projected_graph(B, [0, 2, 4], jaccard=False)
    >>> print(G.edges(data=True))
    [(0, 2, {'weight': 1.0}), (2, 4, {'weight': 1.0})]
    
    Notes
    ------
    No attempt is made to verify that the input graph B is bipartite.
    The graph and node properties are (shallow) copied to the projected graph.

    See Also
    --------
    is_bipartite, 
    is_bipartite_node_set, 
    sets, 
    weighted_projected_graph,
    collaboration_weighted_projected_graph,
    generic_weighted_projected_graph,
    projected_graph 

    References
    ----------
    .. [1] Borgatti, S.P. and Halgin, D. In press. Analyzing Affiliation 
        Networks. In Carrington, P. and Scott, J. (eds) The Sage Handbook 
        of Social Network Analysis. Sage Publications.
    
    s   not defined for multigraphsc         3   s"   |  ] } | ˆ  j  | f Vq d  S(   N(   R   (   R   R   (   R	   (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pys	   <genexpr>y  s    c         3   s&   |  ] } ˆ  | D] } | Vq q d  S(   N(    (   R   R
   R   (   R	   (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pys	   <genexpr>|  s    R'   (   R   R   R   R   R   R   R(   R   R   R   R   R   R)   R*   t   minR   (   R	   R    t   jaccardR   R#   R   R-   R$   R   R.   R'   (    (   R	   s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pyR   '  s&    I		)'/c            s3  ˆ  j  ƒ  r t j d ƒ ‚ n  | d k r6 d „  } n  ˆ  j ƒ  rZ ˆ  j } t j ƒ  } n ˆ  j } t j ƒ  } | j	 j
 ˆ  j	 ƒ | j ‡  f d †  | Dƒ ƒ x | D]… } t ˆ  | ƒ } t ‡  f d †  | Dƒ ƒ t | g ƒ } xC | D]; } t | | ƒ }	 | | |	 ƒ }
 | j | | d |
 ƒqì Wq¦ W| S(   sF  Weighted projection of B with a user-specified weight function.

    The bipartite network B is projected on to the specified nodes
    with weights computed by a user-specified function.  This function
    must accept as a parameter the neighborhood sets of two nodes and
    return an integer or a float.

    The nodes retain their attributes and are connected in the resulting graph 
    if they have an edge to a common node in the original graph.

    Parameters
    ----------
    B : NetworkX graph 
        The input graph should be bipartite. 

    nodes : list or iterable
        Nodes to project onto (the "bottom" nodes).

    weight_function: function
        This function must accept as a parameters two sets,
        the neighborhoods of two nodes, and return an integer or a float.
        The default function computes the number of shared neighbors.

    Returns
    -------
    Graph : NetworkX graph 
       A graph that is the projection onto the given nodes.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> def jaccard(unbrs, vnbrs):
    ...     return float(len(unbrs & vnbrs)) / len(unbrs | vnbrs)
    ... 
    >>> def shared(unbrs, vnbrs):
    ...     return len(unbrs & vnbrs)
    ... 
    >>> B = nx.path_graph(5)
    >>> G = bipartite.generic_weighted_projected_graph(B, [0, 2, 4], weight_function=jaccard)
    >>> print(G.nodes())
    [0, 2, 4]
    >>> print(G.edges(data=True))
    [(0, 2, {'weight': 0.5}), (2, 4, {'weight': 0.5})]
    >>> G = bipartite.generic_weighted_projected_graph(B, [0, 2, 4], weight_function=shared)
    >>> print(G.nodes())
    [0, 2, 4]
    >>> print(G.edges(data=True))
    [(0, 2, {'weight': 1}), (2, 4, {'weight': 1})]
    
    Notes
    ------
    No attempt is made to verify that the input graph B is bipartite.
    The graph and node properties are (shallow) copied to the projected graph.

    See Also
    --------
    is_bipartite, 
    is_bipartite_node_set, 
    sets, 
    weighted_projected_graph,
    collaboration_weighted_projected_graph,
    overlap_weighted_projected_graph,
    projected_graph 

    s   not defined for multigraphsc         S   s   t  |  | @ƒ S(   N(   R*   (   R-   R.   (    (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pyt   <lambda>Ë  t    c         3   s"   |  ] } | ˆ  j  | f Vq d  S(   N(   R   (   R   R   (   R	   (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pys	   <genexpr>Ó  s    c         3   s&   |  ] } ˆ  | D] } | Vq q d  S(   N(    (   R   R
   R   (   R	   (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pys	   <genexpr>Ö  s    R'   N(   R   R   R   t   NoneR   R   R   R(   R   R   R   R   R   R   (   R	   R    t   weight_functionR   R#   R   R-   R$   R   R.   R'   (    (   R	   s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pyR   †  s&    B		)c         C   s   t  |  | ƒ S(   N(   R   (   R	   R    t   create_using(    (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pyR    Ý  s    (   t   __doc__t   networkxR   t   joint
   __author__t   __all__R   R   R   R   R   R   R5   R   R    (    (    (    s`   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/bipartite/projection.pyt   <module>   s   		cV	Z_W