
    hk                        S r SSKrSSKJr  SSKJr  / SQr\" S5      \R                  S 5       5       r	\" S5      \R                  S	 5       5       r
\" S5      \R                  S
 5       5       r\" S5      \R                  S 5       5       rS rg)zConnected components.    N)not_implemented_for   )arbitrary_element)number_connected_componentsconnected_componentsis_connectednode_connected_componentdirectedc              #      #    [        5       n[        U 5      nU  H+  nX1;  d  M
  [        XU5      nUR                  U5        Uv   M-     g7f)ax  Generate connected components.

Parameters
----------
G : NetworkX graph
   An undirected graph

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

Raises
------
NetworkXNotImplemented
    If G is directed.

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

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

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

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

To create the induced subgraph of each component use:

>>> S = [G.subgraph(c).copy() for c in nx.connected_components(G)]

See Also
--------
strongly_connected_components
weakly_connected_components

Notes
-----
For undirected graphs only.

N)setlen
_plain_bfsupdate)Gseennvcs        Z/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/components/connected.pyr   r      sC     ` 5DAA=1#AKKNG	 s
   A
&A
c                 8    [        S [        U 5       5       5      $ )a  Returns the number of connected components.

Parameters
----------
G : NetworkX graph
   An undirected graph.

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

Raises
------
NetworkXNotImplemented
    If G is directed.

Examples
--------
>>> G = nx.Graph([(0, 1), (1, 2), (5, 6), (3, 4)])
>>> nx.number_connected_components(G)
3

See Also
--------
connected_components
number_weakly_connected_components
number_strongly_connected_components

Notes
-----
For undirected graphs only.

c              3   &   #    U  H  nS v   M	     g7f   N ).0ccs     r   	<genexpr>.number_connected_components.<locals>.<genexpr>n   s     32Rq2   )sumr   )r   s    r   r   r   I   s    J 3/2333    c           
          [        U 5      nUS:X  a  [        R                  " S5      e[        S [	        X[        U 5      5       5       5      [        U 5      :H  $ )a  Returns True if the graph is connected, False otherwise.

Parameters
----------
G : NetworkX Graph
   An undirected graph.

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

Raises
------
NetworkXNotImplemented
    If G is directed.

Examples
--------
>>> G = nx.path_graph(4)
>>> print(nx.is_connected(G))
True

See Also
--------
is_strongly_connected
is_weakly_connected
is_semiconnected
is_biconnected
connected_components

Notes
-----
For undirected graphs only.

r   z-Connectivity is undefined for the null graph.c              3   &   #    U  H  nS v   M	     g7fr   r   )r   nodes     r   r   is_connected.<locals>.<genexpr>   s     DCTqCr   )r   nxNetworkXPointlessConceptr    r   r   r   r   s     r   r   r   q   sT    N 	AAAv));
 	
 DZ.?.BCDDANNr!   c                 .    [        U [        U 5      U5      $ )a-  Returns the set of nodes in the component of graph containing node n.

Parameters
----------
G : NetworkX Graph
   An undirected graph.

n : node label
   A node in G

Returns
-------
comp : set
   A set of nodes in the component of G containing node n.

Raises
------
NetworkXNotImplemented
    If G is directed.

Examples
--------
>>> G = nx.Graph([(0, 1), (1, 2), (5, 6), (3, 4)])
>>> nx.node_connected_component(G, 0)  # nodes of component that contains node 0
{0, 1, 2}

See Also
--------
connected_components

Notes
-----
For undirected graphs only.

)r   r   r(   s     r   r	   r	      s    L aQ##r!   c                     U R                   nU1nU/nU(       a]  Un/ nU HJ  nX7    H,  nX;  d  M
  UR                  U5        UR                  U5        M.     [        U5      U:X  d  MH  Us  $    U(       a  M]  U$ )zA fast BFS node generator)_adjaddappendr   )	r   r   sourceadjr   	nextlevel	thislevelr   ws	            r   r   r      sz    
&&C8DI
		AV=HHQK$$Q'  4yA~  ) Kr!   )__doc__networkxr&   networkx.utils.decoratorsr   utilsr   __all___dispatchabler   r   r   r	   r   r   r!   r   <module>r9      s      9 & Z 4  !4n Z #4  !#4L Z *O  !*OZ Z $$  !$$Nr!   