
    ިsg                         d Z ddlZddlmZ g dZej                  d        Z ed      ej                  d               Z ed       ed       ej                  d	d	
      dd                     Z	y)z5Functions for computing and verifying regular graphs.    N)not_implemented_for)
is_regularis_k_regulark_factorc                    t        |       dk(  rt        j                  d      t        j                  j	                  |       }| j                         s/| j                  |      t        fd| j                  D              S | j                  |      t        fd| j                  D              }| j                  |      t        fd| j                  D              }|xr |S )a  Determines whether the graph ``G`` is a regular graph.

    A regular graph is a graph where each vertex has the same degree. A
    regular digraph is a graph where the indegree and outdegree of each
    vertex are equal.

    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   .   K   | ]  \  }}|k(    y wN ).0_dd1s      N/var/www/html/venv/lib/python3.12/site-packages/networkx/algorithms/regular.py	<genexpr>zis_regular.<locals>.<genexpr>&   s     0tq!270   c              3   .   K   | ]  \  }}|k(    y wr	   r
   )r   r   r   d_ins      r   r   zis_regular.<locals>.<genexpr>)   s     ;tq!;r   c              3   .   K   | ]  \  }}|k(    y wr	   r
   )r   r   r   d_outs      r   r   zis_regular.<locals>.<genexpr>+   s     >A%1*>r   )
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;q{{;;
R >>>)k)    directedc                 @    t        fd| j                  D              S )a  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   .   K   | ]  \  }}|k(    y wr	   r
   )r   nr   ks      r   r   zis_k_regular.<locals>.<genexpr>F   s     +$!QqAv+r   )r   r   )r    r)   s    `r   r   r   /   s    . +!((+++r$   
multigraphT)preserve_edge_attrsreturns_graphc                    ddl m}m}  G fdd      } G d d      }t        fd| j                  D              rt        j                  d      | j                         g }t        j                        D ]E  \  }}	|	d	z  k  r ||	|      }
n ||	|      }
|
j                          |j                  |
       G  |d
|      } ||      st        j                  d      j                         D ],  }||vs|d   |d   f|vsj                  |d   |d          . |D ]  }
|
j                           S )u  Compute a k-factor of G

    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 vertex of G and a subset of the edges of G such that each
    vertex has degree k.

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

    matching_weight: string, optional (default='weight')
       Edge data key corresponding to the edge weight.
       Used for finding the max-weighted perfect matching.
       If key not found, uses 1 as weight.

    Returns
    -------
    G2 : NetworkX graph
        A k-factor of G

    Examples
    --------
    >>> G = nx.Graph([(1, 2), (2, 3), (3, 4), (4, 1)])
    >>> G2 = nx.k_factor(G, k=1)
    >>> G2.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.
    r   )is_perfect_matchingmax_weight_matchingc                   $    e Zd Zd Zd Z fdZy)k_factor.<locals>.LargeKGadgetc                     || _         || _        || _        || _        t	        |      D cg c]  }||f c}| _        t	        ||z
        D cg c]	  }|||z   f c}| _        y c c}w c c}w r	   )originalgr)   r   rangeouter_verticescore_verticesselfr)   r   noder4   xs         r   __init__z'k_factor.<locals>.LargeKGadget.__init__t   sg     DMDFDF DK6;Fm"DD!9"DD>CFQJ>O!P4V"4!PD #E!Ps   A$A)c                    | j                   | j                     }t        |j                               }t        |j	                               }t        | j                  ||      D ]$  \  }}} | j                   j                  ||fi | & | j                  D ]/  }| j                  D ]  }| j                   j                  ||         1 | j                   j                  | j                         y r	   )
r4   r3   listkeysvalueszipr6   add_edger7   remove_node)r9   adj_view	neighbors
edge_attrsouterneighborcores          r   replace_nodez+k_factor.<locals>.LargeKGadget.replace_node}   s    vvdmm,HX]]_-Ihoo/0J/2##Y
0 ?+x  x>:>? ** 1!00 1EFFOOD%011 FFt}}-r$   c                    | j                   j                  | j                         | j                  D ]j  }| j                   |   }t	        |j                               D ]=  \  }}|| j                  vs | j                   j                  | j                  |fi |  j l j                  | j                         j                  | j                         y r	   )	r4   add_noder3   r6   r>   itemsr7   rB   remove_nodes_from)r9   rG   rD   rH   rF   r4   s        r   restore_nodez+k_factor.<locals>.LargeKGadget.restore_node   s    FFOODMM*,, 66%=,01A,B (Hjt'9'99'xN:N  3 34 2 23r$   N__name__
__module____qualname__r<   rJ   rO   )r4   s   r   LargeKGadgetr1   s   s    	Q	.		4r$   rT   c                       e Zd Zd Zd Zd Zy)k_factor.<locals>.SmallKGadgetc                 ,   || _         || _        || _        || _        t	        |      D cg c]  }||f c}| _        t	        |      D cg c]	  }|||z   f c}| _        t	        |      D cg c]  }||d|z  z   f c}| _        y c c}w c c}w c c}w )N   )r3   r)   r   r4   r5   r6   inner_verticesr7   r8   s         r   r<   z'k_factor.<locals>.SmallKGadget.__init__   s     DMDF DKDF6;Fm"DD!9"DD?DV}"M!D!f*#5"MDBG(!KQ4QZ"8!KD #E"M!Ks   B
B-Bc                    | j                   | j                     }t        | j                  | j                  t        |j                                     D ]C  \  }}\  }}| j                   j                  ||        | j                   j                  ||fi | E | j                  D ]/  }| j                  D ]  }| j                   j                  ||         1 | j                   j                  | j                         y r	   )
r4   r3   rA   r6   rY   r>   rM   rB   r7   rC   )r9   rD   rG   innerrH   rF   rI   s          r   rJ   z+k_factor.<locals>.SmallKGadget.replace_node   s    vvdmm,H8;##T%8%8$x~~?O:P9 ?4u4x u-x>:>	?
 ** 1!00 1EFFOOD%011 FFt}}-r$   c                    | j                   j                  | j                         | j                  D ]a  }| j                   |   }|j	                         D ]=  \  }}|| j
                  vs | j                   j                  | j                  |fi |  a c | j                   j                  | j                         | j                   j                  | j                         | j                   j                  | j
                         y r	   )	r4   rL   r3   r6   rM   r7   rB   rN   rY   )r9   rG   rD   rH   rF   s        r   rO   z+k_factor.<locals>.SmallKGadget.restore_node   s    FFOODMM*,, 66%=,4NN,< (Hjt'9'99'xN:N FF$$T%8%89FF$$T%8%89FF$$T%7%78r$   NrP   r
   r$   r   SmallKGadgetrV      s    	L
	.
	9r$   r]   c              3   .   K   | ]  \  }}|k    y wr	   r
   )r   r   r   r)   s      r   r   zk_factor.<locals>.<genexpr>   s     
&TQ1q5
&r   z/Graph contains a vertex with degree less than kg       @T)maxcardinalityweightz7Cannot find k-factor because no perfect matching exists   )networkx.algorithms.matchingr.   r/   anyr   r   NetworkXUnfeasiblecopyr>   rJ   appendedgesremove_edgerO   )r    r)   matching_weightr.   r/   rT   r]   gadgetsr:   r   gadgetmatchingedger4   s    `           @r   r   r   I   sV   P V 4  4D!9 !9H 
&QXX
&&##$UVV	A GQXX fv|!!VT15F!!VT15Fv #1T/RH q(+##E
 	
 	 ,xT!Wd1g$6h$FMM$q'47+,   Hr$   )r`   )
__doc__networkxr   networkx.utilsr   __all___dispatchabler   r   r   r
   r$   r   <module>rs      s    ;  .
4 "* "*J Z ,  !,0 Z \"d$?K @ # !Kr$   