
    Cii                        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	dS )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                            |           }|                                 s5|                     |          t          fd| j        D                       S |                     |          fd| j        D             }| 	                    |          fd| j	        D             }t          |          ot          |          S )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   *   K   | ]\  }}|k    V  d S N ).0_dd1s      a/var/www/html/bet.cuttalo.com/ml/venv/lib/python3.11/site-packages/networkx/algorithms/regular.py	<genexpr>zis_regular.<locals>.<genexpr>&   s+      00tq!27000000    c              3   *   K   | ]\  }}|k    V  d S r	   r
   )r   r   r   d_ins      r   r   zis_regular.<locals>.<genexpr>)   s+      88DAqdai888888r   c              3   *   K   | ]\  }}|k    V  d S r	   r
   )r   r   r   d_outs      r   r   zis_regular.<locals>.<genexpr>+   s+      ;;dauz;;;;;;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 1vv{{)*?@@@		#	#A	&	&B==?? 4XXb\\0000qx000000{{28888AK888
R  ;;;;al;;;:33{#3#33r   directedc                 D    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    V  d S 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_graphweightc                 &   t          fd| j        D                       rt          j        d          |                                 }g }| j        D ]I\  }|dz  k    fdt          |          D             r%fdt          |d|z  z
            D             }g nDfdt          d|z  d|z  z             D             }fdt          |d|z            D             |                    t                               t          |                                                   D ]\  }\  }}	 |j	        ||fi |	 |                    fd	|D                        |
                               |                    |f           Kt          j        |d
|          t          j        |          st          j        d          |                    fd|j        D                        |D ]\  }|                               t#          |          }
D ]<}|j        |                                         D ]\  }}	||
vr |j	        |fi |	  n=|                    |z   z              |S )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   *   K   | ]\  }}|k     V  d S r	   r
   )r   r   r   r(   s      r   r   zk_factor.<locals>.<genexpr>t   s+      
&
&TQ1q5
&
&
&
&
&
&r   z/Graph contains a vertex with degree less than kg       @c                     g | ]}|fS r
   r
   r   inodes     r   
<listcomp>zk_factor.<locals>.<listcomp>   s    222q$222r   c                     g | ]}|fS r
   r
   r0   s     r   r3   zk_factor.<locals>.<listcomp>   s    EEE!T1IEEEr      c                     g | ]}|fS r
   r
   r0   s     r   r3   zk_factor.<locals>.<listcomp>   s    III!T1IIIIr   c                     g | ]}|fS r
   r
   r0   s     r   r3   zk_factor.<locals>.<listcomp>   s    BBB1dAYBBBr   c              3   2   K   | ]}rnD ]}||fV  	d S r	   r
   )r   uvinneris_largeouters      r   r   zk_factor.<locals>.<genexpr>   s=      VVA8TuVV!!QVVVVVVVr   T)maxcardinalityr,   z7Cannot find k-factor because no perfect matching existsc              3   >   K   | ]}|v|d d d         v|V  d S )Nr
   )r   ems     r   r   zk_factor.<locals>.<genexpr>   s?      NNaaqjjQtttWA=M=M=M=M=M=MNNr   )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gadgetsr   coreouter_nneighborattrscore_setr;   r<   rB   r2   r=   s    `         @@@@@r   r   r   I   s4   V 
&
&
&
&QX
&
&
&&& W#$UVVV	AG  3 3f$ 3222E&MM222 	CEEEEuVQZ!^'D'DEEEDEEIIIIuQZVa'H'HIIIDBBBBfa&j(A(ABBBE 	
UE**+++*-eQtW]]__*E*E 	3 	3&G&hAJw22E2222 	
VVVVVVVVVVVV	deT512222 	qoNNNA!!Q'' 
#E
 
 	

 NNNN17NNNNNN %, 2 2 eT5	

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