U
    ?h                     @   sj   d Z ddlZddlmZ dddgZejeddd Zedd	d Zejedd
d Z	dd Z
dS )zWeakly connected components.    N)not_implemented_for"number_weakly_connected_componentsweakly_connected_componentsis_weakly_connectedZ
undirectedc                 c   s:   t  }| D ]*}||kr
t t| |}|| |V  q
dS )a  Generate weakly connected components of G.

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

    Returns
    -------
    comp : generator of sets
        A generator of sets of nodes, one for each weakly connected
        component of G.

    Raises
    ------
    NetworkXNotImplemented
        If G is undirected.

    Examples
    --------
    Generate a sorted list of weakly connected components, largest first.

    >>> G = nx.path_graph(4, create_using=nx.DiGraph())
    >>> nx.add_path(G, [10, 11, 12])
    >>> [
    ...     len(c)
    ...     for c in sorted(nx.weakly_connected_components(G), key=len, reverse=True)
    ... ]
    [4, 3]

    If you only want the largest component, it's more efficient to
    use max instead of sort:

    >>> largest_cc = max(nx.weakly_connected_components(G), key=len)

    See Also
    --------
    connected_components
    strongly_connected_components

    Notes
    -----
    For directed graphs only.

    N)set
_plain_bfsupdate)Gseenvc r   a/var/www/html/venv/lib/python3.8/site-packages/networkx/algorithms/components/weakly_connected.pyr      s    0
c                 C   s   t dd t| D S )al  Returns the number of weakly connected components in G.

    Parameters
    ----------
    G : NetworkX graph
        A directed graph.

    Returns
    -------
    n : integer
        Number of weakly connected components

    Raises
    ------
    NetworkXNotImplemented
        If G is undirected.

    Examples
    --------
    >>> G = nx.DiGraph([(0, 1), (2, 1), (3, 4)])
    >>> nx.number_weakly_connected_components(G)
    2

    See Also
    --------
    weakly_connected_components
    number_connected_components
    number_strongly_connected_components

    Notes
    -----
    For directed graphs only.

    c                 s   s   | ]
}d V  qdS )   Nr   ).0Zwccr   r   r   	<genexpr>h   s     z5number_weakly_connected_components.<locals>.<genexpr>)sumr   r	   r   r   r   r   D   s    $c                 C   s.   t | dkrtdt tt| t | kS )a)  Test directed graph for weak connectivity.

    A directed graph is weakly connected if and only if the graph
    is connected when the direction of the edge between nodes is ignored.

    Note that if a graph is strongly connected (i.e. the graph is connected
    even when we account for directionality), it is by definition weakly
    connected as well.

    Parameters
    ----------
    G : NetworkX Graph
        A directed graph.

    Returns
    -------
    connected : bool
        True if the graph is weakly connected, False otherwise.

    Raises
    ------
    NetworkXNotImplemented
        If G is undirected.

    Examples
    --------
    >>> G = nx.DiGraph([(0, 1), (2, 1)])
    >>> G.add_node(3)
    >>> nx.is_weakly_connected(G)  # node 3 is not connected to the graph
    False
    >>> G.add_edge(2, 3)
    >>> nx.is_weakly_connected(G)
    True

    See Also
    --------
    is_strongly_connected
    is_semiconnected
    is_connected
    is_biconnected
    weakly_connected_components

    Notes
    -----
    For directed graphs only.

    r   z-Connectivity is undefined for the null graph.)lennxZNetworkXPointlessConceptnextr   r   r   r   r   r   k   s
    2c                 c   sj   | j }| j}t }|h}|rf|}t }|D ]8}||kr*|| |||  |||  |V  q*qdS )zwA fast BFS node generator

    The direction of the edge between nodes is ignored.

    For directed graphs only.

    N)succpredr   addr   )r	   sourceZGsuccZGpredr
   Z	nextlevelZ	thislevelr   r   r   r   r      s    
r   )__doc__Znetworkxr   Znetworkx.utils.decoratorsr   __all__Z	_dispatchr   r   r   r   r   r   r   r   <module>   s   6
&8