a
    Oˆc=  ã                   @   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 | ¡}|  ¡ s6|  |¡‰ t‡ fdd„| jD ƒƒS |  |¡‰t‡fdd„| jD ƒƒ}|  |¡‰t‡fdd„| jD ƒƒ}|o€|S d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   s   | ]\}}ˆ |kV  qd S ©N© ©Ú.0Ú_Úd)Úd1r   ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/networkx/algorithms/regular.pyÚ	<genexpr>   ó    zis_regular.<locals>.<genexpr>c                 3   s   | ]\}}ˆ |kV  qd S r   r   r   )Úd_inr   r   r      r   c                 3   s   | ]\}}ˆ |kV  qd S r   r   r   )Úd_outr   r   r   !   r   N)ÚnxÚutilsZarbitrary_elementZis_directedÚdegreeÚallZ	in_degreeZ
out_degree)ÚGZn1Z
in_regularZout_regularr   )r   r   r   r   r      s    


r   Z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   Z
multigraphÚweightc                    s&  ddl m}m} G ‡ fdd„dƒ}G dd„ dƒ}t‡fdd„| jD ƒƒrRt d	¡‚|  ¡ ‰ g }tˆ jƒD ]D\}}	ˆ|	d
 k rŒ|ˆ|	|ˆ ƒ}
n|ˆ|	|ˆ ƒ}
|
 	¡  | 
|
¡ qh|ˆ d|d�}|ˆ |ƒsÐt d¡‚ˆ  ¡ D ]4}||vrØ|d |d f|vrØˆ  |d |d ¡ qØ|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                    s   g | ]}ˆ |f‘qS r   r   ©r	   Úx©Únoder   r   Ú
<listcomp>a   r   z;k_factor.<locals>.LargeKGadget.__init__.<locals>.<listcomp>c                    s   g | ]}ˆ|ˆ  f‘qS r   r   r   ©r   r    r   r   r!   b   r   )ÚoriginalÚgr   r   ÚrangeÚouter_verticesÚcore_vertices©Úselfr   r   r    r$   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 |¤Ž q2| jD ]}| jD ]}| j  ||¡ qdqZ| j  	| j¡ d S r   )
r$   r#   ÚlistÚkeysÚvaluesÚzipr&   Úadd_edger'   Úremove_node)r)   Úadj_viewZ	neighborsÚ
edge_attrsÚouterÚneighborÚcorer   r   r   Úreplace_noded   s    ÿ

z+k_factor.<locals>.LargeKGadget.replace_nodec                    s|   | j  | j¡ | jD ]J}| j | }t| ¡ ƒD ].\}}|| jvr.| j j| j|fi |¤Ž  qq.qˆ  | j¡ ˆ  | j¡ d S r   )	r$   Úadd_noder#   r&   r+   Úitemsr'   r/   Úremove_nodes_from©r)   r3   r1   r4   r2   ©r$   r   r   Úrestore_nodeq   s    


z+k_factor.<locals>.LargeKGadget.restore_nodeN©Ú__name__Ú
__module__Ú__qualname__r*   r6   r<   r   r;   r   r   ÚLargeKGadgetZ   s   	rA   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                    s   g | ]}ˆ |f‘qS r   r   r   r   r   r   r!   ƒ   r   z;k_factor.<locals>.SmallKGadget.__init__.<locals>.<listcomp>c                    s   g | ]}ˆ|ˆ  f‘qS r   r   r   r"   r   r   r!   „   r   c                    s   g | ]}ˆ|d ˆ   f‘qS )é   r   r   r"   r   r   r!   …   r   )r#   r   r   r$   r%   r&   Úinner_verticesr'   r(   r   r"   r   r*   }   s    z'k_factor.<locals>.SmallKGadget.__init__c                 S   s�   | j | j }t| j| jt| ¡ ƒƒD ]2\}}\}}| j  ||¡ | j j||fi |¤Ž q$| jD ]}| jD ]}| j  ||¡ qhq^| j  	| j¡ d S r   )
r$   r#   r.   r&   rC   r+   r8   r/   r'   r0   )r)   r1   r3   Úinnerr4   r2   r5   r   r   r   r6   ‡   s    ÿ

z+k_factor.<locals>.SmallKGadget.replace_nodec                 S   sŠ   | j  | j¡ | jD ]F}| j | }| ¡ D ].\}}|| jvr*| j j| j|fi |¤Ž  qq*q| j  | j¡ | j  | j¡ | j  | j¡ d S r   )	r$   r7   r#   r&   r8   r'   r/   r9   rC   r:   r   r   r   r<   “   s    


z+k_factor.<locals>.SmallKGadget.restore_nodeNr=   r   r   r   r   ÚSmallKGadget|   s   
rE   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)Zmaxcardinalityr   z7Cannot find k-factor because no perfect matching existsé   )Znetworkx.algorithms.matchingr   r   Úanyr   r   ZNetworkXUnfeasibleÚcopyr+   r6   ÚappendÚedgesZremove_edger<   )r   r   Zmatching_weightr   r   rA   rE   Zgadgetsr    r   ZgadgetZmatchingZedger   )r$   r   r   r   8   s0     "$

ÿr   )r   )	Ú__doc__Znetworkxr   Znetworkx.utilsr   Ú__all__r   r   r   r   r   r   r   Ú<module>   s   
