
    9i                         S r SSKrSSKJr  / SQr\R                  S 5       r\" S5      \R                  S 5       5       r\" S5      \" S5      \R                  " S	S	S
9SS j5       5       5       r	g)z5Functions for computing and verifying regular graphs.    N)not_implemented_for)
is_regularis_k_regulark_factorc                   ^^^ [        U 5      S:X  a  [        R                  " S5      e[        R                  R	                  U 5      nU R                  5       (       d0  U R                  U5      m[        U4S jU R                   5       5      $ U R                  U5      mU4S jU R                   5       nU R                  U5      mU4S jU R                   5       n[        U5      =(       a    [        U5      $ )a  Determines whether a graph is regular.

A regular graph is a graph where all nodes have the same degree. A regular
digraph is a graph where all nodes have the same indegree and all nodes
have the same outdegree.

Parameters
----------
G : NetworkX graph

Returns
-------
bool
    Whether the given graph or digraph is regular.

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

r   zGraph has no nodes.c              3   2   >#    U  H  u  pTU:H  v   M     g 7fN ).0_dd1s      [/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/networkx/algorithms/regular.py	<genexpr>is_regular.<locals>.<genexpr>&   s     0xtq27x   c              3   2   >#    U  H  u  pTU:H  v   M     g 7fr	   r
   )r   r   r   d_ins      r   r   r   )   s     8KDAdaiKr   c              3   2   >#    U  H  u  pTU:H  v   M     g 7fr	   r
   )r   r   r   d_outs      r   r   r   +   s     ;ldauzlr   )
lennxNetworkXPointlessConceptutilsarbitrary_elementis_directeddegreeall	in_degree
out_degree)Gn1
in_regularout_regularr   r   r   s       @@@r   r   r   	   s    0 1v{))*?@@		#	#A	&B==??XXb\0qxx000{{28AKK8
R ;all;:33{#33    directedc                 B   ^ [        U4S jU R                   5       5      $ )aJ  Determines whether the graph ``G`` is a k-regular graph.

A k-regular graph is a graph where each vertex has degree k.

Parameters
----------
G : NetworkX graph

Returns
-------
bool
    Whether the given graph is k-regular.

Examples
--------
>>> G = nx.Graph([(1, 2), (2, 3), (3, 4), (4, 1)])
>>> nx.is_k_regular(G, k=3)
False

c              3   2   >#    U  H  u  pUT:H  v   M     g 7fr	   r
   )r   nr   ks      r   r   is_k_regular.<locals>.<genexpr>F   s     +($!qAv(r   )r   r   )r!   r*   s    `r   r   r   /   s    . +!((+++r%   
multigraphT)preserve_edge_attrsreturns_graphc                 R  ^^^^^ [        U4S jU R                   5       5      (       a  [        R                  " S5      eU R	                  5       n/ nU R                   GH8  u  pVTUS-  :  m[        U5       Vs/ s H  ouU4PM     snmT(       a&  [        USU-  T-
  5       Vs/ s H  ouU4PM     nn/ mOG[        SU-  SU-  T-   5       Vs/ s H  ouU4PM     nn[        USU-  5       Vs/ s H  ouU4PM     snmUR                  [        TT5      5        [        TX5   R                  5       5       H  u  n	u  pUR                  " X40 UD6  M     UR                  UUU4S jU 5       5        UR                  U5        UR                  UTUT45        GM;     [        R                  " USUS9m[        R                  " UT5      (       d  [        R                  " S5      eUR                  U4S	 jUR                   5       5        U H  u  nmnmUR!                  U5        [#        U5      nT HD  n	UR$                  U	   R                  5        H   u  pX;  d  M  UR                  " XZ40 UD6    MB     MF     UR'                  TU-   T-   5        M     U$ s  snf s  snf s  snf s  snf )
u  Compute a `k`-factor of a graph.

A `k`-factor of a graph is a spanning `k`-regular subgraph.
A spanning `k`-regular subgraph of `G` is a subgraph that contains
each node of `G` and a subset of the edges of `G` such that each
node has degree `k`.

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

k : int
    The degree of the `k`-factor.

matching_weight: string, optional (default="weight")
    Edge attribute name corresponding to the edge weight.
    If not present, the edge is assumed to have weight 1.
    Used for finding the max-weighted perfect matching.

Returns
-------
NetworkX graph
    A `k`-factor of `G`.

Examples
--------
>>> G = nx.Graph([(1, 2), (2, 3), (3, 4), (4, 1)])
>>> KF = nx.k_factor(G, k=1)
>>> KF.edges()
EdgeView([(1, 2), (3, 4)])

References
----------
.. [1] "An algorithm for computing simple k-factors.",
   Meijer, Henk, Yurai Núñez-Rodríguez, and David Rappaport,
   Information processing letters, 2009.
c              3   2   >#    U  H  u  pUT:  v   M     g 7fr	   r
   )r   r   r   r*   s      r   r   k_factor.<locals>.<genexpr>t   s     
&XTQ1q5Xr   z/Graph contains a vertex with degree less than kg       @   c              3   P   >#    U  H  nT(       a  TOT  H  o!U4v   M
     M     g 7fr	   r
   )r   uvinneris_largeouters      r   r   r1      s#     VAu8T!Q8Ts   #&T)maxcardinalityweightz7Cannot find k-factor because no perfect matching existsc              3   R   >#    U  H  oT;  d  M
  US S S2   T;  d  M  Uv   M     g 7f)Nr
   )r   ems     r   r   r1      s(     N7aqjQttWA=M7s   	'
'	')anyr   r   NetworkXUnfeasiblecopyrangeadd_edges_fromzipitemsadd_edgeremove_nodeappendmax_weight_matchingis_perfect_matchingremove_edges_fromedgesadd_nodeset_adjremove_nodes_from)r!   r*   matching_weightggadgetsnoder   icoreouter_nneighborattrscore_setr6   r7   r>   r8   s    `           @@@@r   r   r   I   sm   V 
&QXX
&&&##$UVV	AG $ %*&M2MqM2',VQZ!^'DE'D!1I'DDEE',QZVa'HI'H!1I'HDI(-fa&j(AB(A1AY(ABE 	
UE*+*-eQW]]_*E&G&hJJw2E2 +F 	
VVV	deT512+ !0 	qoNA!!!Q''##E
 	

 N177NN %, eT5	

4t9G#$66'?#8#8#:+JJt77 $; 
 	
EDL501 %, HQ 3E JBs   9J$JJ-J$)r:   )
__doc__networkxr   networkx.utilsr   __all___dispatchabler   r   r   r
   r%   r   <module>r`      s    ;  .
4 "4 "4J Z ,  !,0 Z \"d$?[ @ # ![r%   