o
    i                     @   sj   d dl Z d dlZzd dlZdZW n ey   dZY nw 		dddZe ddd	d
Ze ddddZ	dS )    NTFweakc              	   C   s~  t std| }|dvrtd|sd}| jdkrtdtjj| s-tjj	| } | j
\}}||kr:td| jdkrJ|tj|| jjd	fS |d
krftj|| jjd	}tj| j| jd|| j|d n#| | j7 } tjj| sytjj	| } tjdd| j| jdddd\}}tjd| jjd	}tj|f| jjd	}	t|||	|d t|d }
|s|
S t|
t|	d|
 | |
|fS )aV  Analyzes the connected components of a sparse graph

    Args:
        csgraph (cupy.ndarray of cupyx.scipy.sparse.csr_matrix): The adjacency
            matrix representing connectivity among nodes.
        directed (bool): If ``True``, it operates on a directed graph. If
            ``False``, it operates on an undirected graph.
        connection (str): ``'weak'`` or ``'strong'``. For directed graphs, the
            type of connection to use. Nodes i and j are "strongly" connected
            only when a path exists both from i to j and from j to i.
            If ``directed`` is ``False``, this argument is ignored.
        return_labels (bool): If ``True``, it returns the labels for each of
            the connected components.

    Returns:
        tuple of int and cupy.ndarray, or int:
            If ``return_labels`` == ``True``, returns a tuple ``(n, labels)``,
            where ``n`` is the number of connected components and ``labels`` is
            labels of each connected components. Otherwise, returns ``n``.

    .. seealso:: :func:`scipy.sparse.csgraph.connected_components`
    zpylibcugraph is not available)r   strongz%connection must be 'weak' or 'strong'r      z graph should have two dimensionszgraph should be a square arrayr   )dtyper   N)offsetsindicesweights	num_verts	num_edgeslabelsF)resource_handlegraphr   r   r   r   do_expensive_check)   )size)pylibcugraph_availableRuntimeErrorlower
ValueErrorndimcupyxscipysparseisspmatrix_csr
csr_matrixshapennzcupyaranger   r   emptypylibcugraphstrongly_connected_componentsindptrTweakly_connected_componentszeros_cupy_count_componentsint_cupy_adjust_labelssort)csgraphdirected
connectionreturn_labelsmm1r   _countroot_labelsn r4   b/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/cupyx/scipy/sparse/csgraph/_traversal.pyconnected_components
   sT   






r6    z4raw I labels, raw int32 count, raw int32 root_labelsz
    int j = i;
    while (j != labels[j]) { j = labels[j]; }
    if (j != i) {
        labels[i] = j;
    } else {
        int k = atomicAdd(&count[0], 1);
        root_labels[k] = i;
    }
    r&   z&int32 n_root_labels, raw I root_labelszI labelsav  
    int cur_label = labels;
    int j_min = 0;
    int j_max = n_root_labels - 1;
    int j = (j_min + j_max) / 2;
    while (j_min < j_max) {
        if (cur_label == root_labels[j]) break;
        if (cur_label < root_labels[j]) {
            j_max = j - 1;
        } else {
            j_min = j + 1;
        }
        j = (j_min + j_max) / 2;
    }
    labels = j;
    r(   )Tr   T)
r   cupyx.scipy.sparser   r    r   ModuleNotFoundErrorr6   ElementwiseKernelr&   r(   r4   r4   r4   r5   <module>   s.    
J
