o
    ˷e=                     @   sV   d Z ddlZddlmZ g dZdd Zeddd	 Zede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                    s   t j| }|  s| | t fdd| jD S | |tfdd| jD }| |tfdd| jD }|o@|S )ar  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.

    c                 3       | ]	\}} |kV  qd S N .0_d)d1r   R/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/algorithms/regular.py	<genexpr>       zis_regular.<locals>.<genexpr>c                 3   r   r   r   r	   )d_inr   r   r      r   c                 3   r   r   r   r	   )d_outr   r   r   !   r   )nxutilsarbitrary_elementis_directeddegreeall	in_degree
out_degree)Gn1
in_regularout_regularr   )r   r   r   r   r      s   


r   directedc                    s   t  fd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.

    c                 3   s    | ]	\}}| kV  qd S r   r   )r
   nr   kr   r   r   5   r   zis_k_regular.<locals>.<genexpr>)r   r   )r   r"   r   r!   r   r   %   s   r   
multigraphweightc                    s$  ddl m}m} G  fddd}G dd d}tfdd| jD r)td	|   g }t jD ]"\}}	|	d
 k rF||	| }
n||	| }
|
	  |
|
 q4| d|d}| |shtd  D ]}||vr|d |d f|vr |d |d  ql|D ]}
|
  q S )u0  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

    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                       s(   e Zd Zdd Zdd Z fddZdS )zk_factor.<locals>.LargeKGadgetc                    sR   | _ || _|| _ | _fddt D | _ fddt | D | _d S )Nc                       g | ]} |fqS r   r   r
   xnoder   r   
<listcomp>a       z;k_factor.<locals>.LargeKGadget.__init__.<locals>.<listcomp>c                       g | ]}|  fqS r   r   r(   r   r+   r   r   r,   b       )originalgr"   r   rangeouter_verticescore_verticesselfr"   r   r+   r2   r   r/   r   __init__[   s   "z'k_factor.<locals>.LargeKGadget.__init__c                 S   s   | j | j }t| }t| }t| j||D ]\}}}| j j||fi | q| jD ]}| jD ]	}| j || q2q-| j 	| j d S r   )
r2   r1   listkeysvalueszipr4   add_edger5   remove_node)r7   adj_view	neighbors
edge_attrsouterneighborcorer   r   r   replace_noded   s   

z+k_factor.<locals>.LargeKGadget.replace_nodec                    s|   | j | j | jD ]%}| j | }t| D ]\}}|| jvr.| j j| j|fi |  nqq
 | j  | j d S r   )	r2   add_noder1   r4   r9   itemsr5   r=   remove_nodes_fromr7   rB   r?   rC   rA   r2   r   r   restore_nodeq   s   


z+k_factor.<locals>.LargeKGadget.restore_nodeN__name__
__module____qualname__r8   rE   rK   r   rJ   r   r   LargeKGadgetZ   s    	rP   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )zk_factor.<locals>.SmallKGadgetc                    sh   | _ || _ | _|| _fddt D | _ fddt D | _ fddt|D | _d S )Nc                    r'   r   r   r(   r*   r   r   r,      r-   z;k_factor.<locals>.SmallKGadget.__init__.<locals>.<listcomp>c                    r.   r   r   r(   r/   r   r   r,      r0   c                    s   g | ]
}|d    fqS )   r   r(   r/   r   r   r,      s    )r1   r"   r   r2   r3   r4   inner_verticesr5   r6   r   r/   r   r8   }   s   z'k_factor.<locals>.SmallKGadget.__init__c                 S   s   | j | j }t| j| jt| D ]\}}\}}| j || | j j||fi | q| jD ]}| jD ]	}| j || q4q/| j 	| j d S r   )
r2   r1   r<   r4   rR   r9   rG   r=   r5   r>   )r7   r?   rB   innerrC   rA   rD   r   r   r   rE      s   

z+k_factor.<locals>.SmallKGadget.replace_nodec                 S   s   | j | j | jD ]#}| j | }| D ]\}}|| jvr,| j j| j|fi |  nqq
| j | j | j | j | j | j d S r   )	r2   rF   r1   r4   rG   r5   r=   rH   rR   rI   r   r   r   rK      s   


z+k_factor.<locals>.SmallKGadget.restore_nodeNrL   r   r   r   r   SmallKGadget|   s    
rT   c                 3   s    | ]	\}}| k V  qd S r   r   r	   r!   r   r   r      r   zk_factor.<locals>.<genexpr>z/Graph contains a vertex with degree less than kg       @T)maxcardinalityr$   z7Cannot find k-factor because no perfect matching exists   )networkx.algorithms.matchingr%   r&   anyr   r   NetworkXUnfeasiblecopyr9   rE   appendedgesremove_edgerK   )r   r"   matching_weightr%   r&   rP   rT   gadgetsr+   r   gadgetmatchingedger   )r2   r"   r   r   8   s2    "$


r   )r$   )	__doc__networkxr   networkx.utilsr   __all__r   r   r   r   r   r   r   <module>   s    
