
    wg                         d Z ddlZddlZg dZd Zd Zd ZeeedZej                  dd       Z
e
Z ej                  d	
      dd       Zej                  d        Zd Zd Zy)z+Functions for computing clustering of pairs    N)
clusteringaverage_clusteringlatapy_clusteringrobins_alexander_clusteringc                 <    t        | |z        t        | |z        z  S N)lennunvs     j/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/bipartite/cluster.pycc_dotr      s    rBw<#b2g,&&    c                 \    t        | |z        t        t        |       t        |            z  S r   )r	   maxr
   s     r   cc_maxr      $    rBw<#c"gs2w///r   c                 \    t        | |z        t        t        |       t        |            z  S r   )r	   minr
   s     r   cc_minr      r   r   )dotr   r   c           
         t         j                  j                  j                  |       st        j                  d      	 t
        |   }|| }i }|D ]j  }d}| |   D 	ch c]  }| |   D ]  }	|	  c}	}|hz
  }
|
D ]&  }	| |t        | |	         t        | |               z  }( |dkD  r|t        |
      z  }|||<   l |S # t        $ r}t        j                  d      |d}~ww xY wc c}	}w )u  Compute a bipartite clustering coefficient for nodes.

    The bipartite clustering coefficient is a measure of local density
    of connections defined as [1]_:

    .. math::

       c_u = \frac{\sum_{v \in N(N(u))} c_{uv} }{|N(N(u))|}

    where `N(N(u))` are the second order neighbors of `u` in `G` excluding `u`,
    and `c_{uv}` is the pairwise clustering coefficient between nodes
    `u` and `v`.

    The mode selects the function for `c_{uv}` which can be:

    `dot`:

    .. math::

       c_{uv}=\frac{|N(u)\cap N(v)|}{|N(u) \cup N(v)|}

    `min`:

    .. math::

       c_{uv}=\frac{|N(u)\cap N(v)|}{min(|N(u)|,|N(v)|)}

    `max`:

    .. math::

       c_{uv}=\frac{|N(u)\cap N(v)|}{max(|N(u)|,|N(v)|)}


    Parameters
    ----------
    G : graph
        A bipartite graph

    nodes : list or iterable (optional)
        Compute bipartite clustering for these nodes. The default
        is all nodes in G.

    mode : string
        The pairwise bipartite clustering method to be used in the computation.
        It must be "dot", "max", or "min".

    Returns
    -------
    clustering : dictionary
        A dictionary keyed by node with the clustering coefficient value.


    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.path_graph(4)  # path graphs are bipartite
    >>> c = bipartite.clustering(G)
    >>> c[0]
    0.5
    >>> c = bipartite.clustering(G, mode="min")
    >>> c[0]
    1.0

    See Also
    --------
    robins_alexander_clustering
    average_clustering
    networkx.algorithms.cluster.square_clustering

    References
    ----------
    .. [1] Latapy, Matthieu, Clémence Magnien, and Nathalie Del Vecchio (2008).
       Basic notions for the analysis of large two-mode networks.
       Social Networks 30(1), 31--48.
    zGraph is not bipartitez6Mode for bipartite clustering must be: dot, min or maxNg        )	nx
algorithms	bipartiteis_bipartiteNetworkXErrormodesKeyErrorsetr	   )Gnodesmodecc_funcerrccsvccnbrunbrs2s              r   r   r      s   \ ==""//2788+ }
C d3sAcF3q33qc9 	0A'#ad)S1Y//B	08#e*BA J!  D
	 4s    	C  C' 	C$	CC$bipartite_average_clustering)namec                 h    || }t        | ||      t        fd|D              t        |      z  S )u|  Compute the average bipartite clustering coefficient.

    A clustering coefficient for the whole graph is the average,

    .. math::

       C = \frac{1}{n}\sum_{v \in G} c_v,

    where `n` is the number of nodes in `G`.

    Similar measures for the two bipartite sets can be defined [1]_

    .. math::

       C_X = \frac{1}{|X|}\sum_{v \in X} c_v,

    where `X` is a bipartite set of `G`.

    Parameters
    ----------
    G : graph
        a bipartite graph

    nodes : list or iterable, optional
        A container of nodes to use in computing the average.
        The nodes should be either the entire graph (the default) or one of the
        bipartite sets.

    mode : string
        The pairwise bipartite clustering method.
        It must be "dot", "max", or "min"

    Returns
    -------
    clustering : float
       The average bipartite clustering for the given set of nodes or the
       entire graph if no nodes are specified.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.star_graph(3)  # star graphs are bipartite
    >>> bipartite.average_clustering(G)
    0.75
    >>> X, Y = bipartite.sets(G)
    >>> bipartite.average_clustering(G, X)
    0.0
    >>> bipartite.average_clustering(G, Y)
    1.0

    See Also
    --------
    clustering

    Notes
    -----
    The container of nodes passed to this function must contain all of the nodes
    in one of the bipartite sets ("top" or "bottom") in order to compute
    the correct average bipartite clustering coefficients.
    See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
    for further details on how bipartite graphs are handled in NetworkX.


    References
    ----------
    .. [1] Latapy, Matthieu, Clémence Magnien, and Nathalie Del Vecchio (2008).
        Basic notions for the analysis of large two-mode networks.
        Social Networks 30(1), 31--48.
    )r"   r#   c              3   (   K   | ]	  }|     y wr    ).0r'   r&   s     r   	<genexpr>z%average_clustering.<locals>.<genexpr>   s     %!s1v%s   )r   sumr	   )r!   r"   r#   r&   s      @r   r   r      s8    N }
AU
6C%u%%E
22r   c                     | j                         dk  s| j                         dk  ryt        |       }|dk(  ryt        |       }d|z  |z  S )u
  Compute the bipartite clustering of G.

    Robins and Alexander [1]_ defined bipartite clustering coefficient as
    four times the number of four cycles `C_4` divided by the number of
    three paths `L_3` in a bipartite graph:

    .. math::

       CC_4 = \frac{4 * C_4}{L_3}

    Parameters
    ----------
    G : graph
        a bipartite graph

    Returns
    -------
    clustering : float
       The Robins and Alexander bipartite clustering for the input graph.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.davis_southern_women_graph()
    >>> print(round(bipartite.robins_alexander_clustering(G), 3))
    0.468

    See Also
    --------
    latapy_clustering
    networkx.algorithms.cluster.square_clustering

    References
    ----------
    .. [1] Robins, G. and M. Alexander (2004). Small worlds among interlocking
           directors: Network structure and distance in bipartite graphs.
           Computational & Mathematical Organization Theory 10(1), 69–94.

          r   g      @)ordersize_threepaths_four_cycles)r!   L_3C_4s      r   r   r      sL    R 	wwy1}1
a.C
ax
q/C#Ir   c           
          d}| D ]P  }t        j                  | |   d      D ]2  \  }}|t        t        | |         t        | |         z  |hz
        z  }4 R |dz  S )Nr      r5   )	itertoolscombinationsr	   r    )r!   cyclesr'   r*   ws        r   r:   r:     sq    F 9**1Q43 	9DAqc3qt9s1Q4y0QC788F	99 A:r   c           
          d}| D ]B  }| |   D ]8  }t        | |         |hz
  D ]!  }|t        t        | |         ||hz
        z  }# : D |dz  S )Nr   r>   )r    r	   )r!   pathsr'   r*   rB   s        r   r9   r9     sr    E 11 	1A1Y!_ 1S1Y!Q/001	11 19r   )Nr   )__doc__r?   networkxr   __all__r   r   r   r   _dispatchabler   r   r   r   r:   r9   r0   r   r   <module>rI      s    1  '00 vf5 b bJ 
 56I3 7I3X . .br   