Ë
    D^(hë  ã                   óª   — d Z ddlmZ ddlmZ ddlmZ ddlZddl	m
Z
 ddlmZ e
ZdgZ ed	«      ej                  dd
„«       «       Zd„ Zd„ Zd„ Zd„ Zy)z,
Moody and White algorithm for k-components
é    )Údefaultdict)Úcombinations)Ú
itemgetterN)Úedmonds_karp)Únot_implemented_forÚk_componentsÚdirectedc           	      óB  — t        t        «      }|€t        }t        j                  | «      D ]0  }t        |«      }t        |«      dkD  sŒ|d   j                  |«       Œ2 t        j                  | «      D �cg c]  }| j                  |«      ‘Œ }}|D ]0  }t        |«      }t        |«      dkD  sŒ|d   j                  |«       Œ2 |D �]&  }	t        |	«      dk  rŒt        j                  |	|¬«      }
|
dkD  r||
   j                  t        |	«      «       t        t        j                  |	|
|¬«      «      }|
t        |	||
«      fg}|sŒ€|d   \  }}	 t        |«      }|	j                  |«      }t        j                  ||¬«      }||kD  r"|dkD  r||   j                  t        |«      «       t        t        j                  |||¬«      «      }|r|j                  |t        |||«      f«       |rŒ§�Œ) t!        |«      S c c}w # t        $ r |j                  «        Y Œ1w xY w)a7  Returns the k-component structure of a graph G.

    A `k`-component is a maximal subgraph of a graph G that has, at least,
    node connectivity `k`: we need to remove at least `k` nodes to break it
    into more components. `k`-components have an inherent hierarchical
    structure because they are nested in terms of connectivity: a connected
    graph can contain several 2-components, each of which can contain
    one or more 3-components, and so forth.

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

    flow_func : function
        Function to perform the underlying flow computations. Default value
        :meth:`edmonds_karp`. This function performs better in sparse graphs with
        right tailed degree distributions. :meth:`shortest_augmenting_path` will
        perform better in denser graphs.

    Returns
    -------
    k_components : dict
        Dictionary with all connectivity levels `k` in the input Graph as keys
        and a list of sets of nodes that form a k-component of level `k` as
        values.

    Raises
    ------
    NetworkXNotImplemented
        If the input graph is directed.

    Examples
    --------
    >>> # Petersen graph has 10 nodes and it is triconnected, thus all
    >>> # nodes are in a single component on all three connectivity levels
    >>> G = nx.petersen_graph()
    >>> k_components = nx.k_components(G)

    Notes
    -----
    Moody and White [1]_ (appendix A) provide an algorithm for identifying
    k-components in a graph, which is based on Kanevsky's algorithm [2]_
    for finding all minimum-size node cut-sets of a graph (implemented in
    :meth:`all_node_cuts` function):

        1. Compute node connectivity, k, of the input graph G.

        2. Identify all k-cutsets at the current level of connectivity using
           Kanevsky's algorithm.

        3. Generate new graph components based on the removal of
           these cutsets. Nodes in a cutset belong to both sides
           of the induced cut.

        4. If the graph is neither complete nor trivial, return to 1;
           else end.

    This implementation also uses some heuristics (see [3]_ for details)
    to speed up the computation.

    See also
    --------
    node_connectivity
    all_node_cuts
    biconnected_components : special case of this function when k=2
    k_edge_components : similar to this function, but uses edge-connectivity
        instead of node-connectivity

    References
    ----------
    .. [1]  Moody, J. and D. White (2003). Social cohesion and embeddedness:
            A hierarchical conception of social groups.
            American Sociological Review 68(1), 103--28.
            http://www2.asanet.org/journals/ASRFeb03MoodyWhite.pdf

    .. [2]  Kanevsky, A. (1993). Finding all minimum-size separating vertex
            sets in a graph. Networks 23(6), 533--541.
            http://onlinelibrary.wiley.com/doi/10.1002/net.3230230604/abstract

    .. [3]  Torrents, J. and F. Ferraro (2015). Structural Cohesion:
            Visualization and Heuristics for Fast Computation.
            https://arxiv.org/pdf/1503.04476v1

    é   é   )Ú	flow_func)Úkr   éÿÿÿÿ)r   ÚlistÚdefault_flow_funcÚnxÚconnected_componentsÚsetÚlenÚappendÚbiconnected_componentsÚsubgraphÚnode_connectivityÚall_node_cutsÚ_generate_partitionÚnextÚStopIterationÚpopÚ_reconstruct_k_components)ÚGr   r   Ú	componentÚcompÚcÚbicomponentsÚbicomponentÚbicompÚBr   ÚcutsÚstackÚparent_kÚ	partitionÚnodesÚCÚthis_ks                     új/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/networkx/algorithms/connectivity/kcomponents.pyr   r      s  € ôt œtÓ$€LàÐÜ%ˆ	ä×,Ñ,¨QÓ/ò )ˆ	ä�9‹~ˆÜˆt‹9�q‹=Ø˜‰O×"Ñ" 4Õ(ð	)ô
 ,.×+DÑ+DÀQÓ+GÖH a�A—J‘J˜q•MÐH€LÐHØ#ò +ˆÜ�[Ó!ˆäˆv‹;˜‹?Ø˜‰O×"Ñ" 6Õ*ð	+ð
 ó ˆÜˆq‹6�QŠ;ØÜ× Ñ  ¨iÔ8ˆØˆqŠ5Ø˜‰O×"Ñ"¤3 q£6Ô*ä”B×$Ñ$ Q¨!°yÔAÓBˆØÔ(¨¨D°!Ó4Ð5Ð6ˆÚØ$)¨"¡IÑ!ˆX�yð
Ü˜Y›�Ø—J‘J˜uÓ%�Ü×-Ñ-¨a¸9ÔE�Ø˜HÒ$¨°!ªØ  Ñ(×/Ñ/´°A³Ô7ÜœB×,Ñ,¨Q°&ÀIÔNÓO�ÙØ—L‘L &Ô*=¸aÀÀvÓ*NÐ!OÔPô ðô< % \Ó2Ð2ùòI Iøô4 !ò Ø—	‘	–ðús   Á7G=ÅBHÈHÈHc              #   óT  ‡‡K  — t        j                  «       }t        t        | «      «      Š|j	                  ‰«       |j                  ˆˆfd„t        ‰d«      D «       «       t        j                  |«      D ]'  }t        j                  |D �cg c]  }‰|   ‘Œ	 c}Ž –— Œ) yc c}w ­w)as  Merge sets that share k or more elements.

    See: http://rosettacode.org/wiki/Set_consolidation

    The iterative python implementation posted there is
    faster than this because of the overhead of building a
    Graph and calling nx.connected_components, but it's not
    clear for us if we can use it in NetworkX because there
    is no licence for the code.

    c              3   ó\   •K  — | ]#  \  }}t        ‰|   ‰|   z  «      ‰k\  sŒ||f–— Œ% y ­w©N)r   )Ú.0ÚuÚvr   r,   s      €€r/   ú	<genexpr>z_consolidate.<locals>.<genexpr>®   s8   øè ø€ ò Ù�1�a´S¸¸q¹ÀEÈ!ÁHÑ9LÓ5MÐQRÓ5RˆˆAŒñùs   ƒ,£	,r   N)
r   ÚGraphÚdictÚ	enumerateÚadd_nodes_fromÚadd_edges_fromr   r   r   Úunion)Úsetsr   r    r!   Únr,   s    `   @r/   Ú_consolidater?   Ÿ   s�   ùè ø€ ô 	�‰‹
€AÜ”˜4“Ó!€EØ×Ñ�UÔØ×Ñô Ü'¨¨qÓ1ôô ô ×,Ñ,¨QÓ/ò 8ˆ	Ü�i‰i¨IÖ6 q˜% ›(Ò6Ð7Ó7ñ8ùÚ6ùs   „B
B(ÂB#
ÂB(c              #   ó  K  — d„ }g }| j                  «       D ��ch c]  \  }}||kD  sŒ|’Œ c}}|D ��ch c]  }|D ]  }|’Œ Œ c}}z
  }| j                  |«      }	t        j                  |	«      D ]e  }
t	        |
«      }|D ]%  }|D ]  } || ||
«      sŒ|j                  |«       Œ  Œ' t        |«      | j                  «       k  sŒU|j                  |«       Œg t        ||dz   «      E d {  –—†  y c c}}w c c}}w 7 Œ­w)Nc                 ó2   ‡— t        ˆfd„| |   D «       «      S )Nc              3   ó&   •K  — | ]  }|‰v –— Œ
 y ­wr2   © )r3   r>   r+   s     €r/   r6   zE_generate_partition.<locals>.has_nbrs_in_partition.<locals>.<genexpr>·   s   øè ø€ Ò3 a�1˜	”>Ñ3ùó   ƒ)Úany)r    Únoder+   s     `r/   Úhas_nbrs_in_partitionz2_generate_partition.<locals>.has_nbrs_in_partition¶   s   ø€ ÜÓ3¨1¨T©7Ô3Ó3Ð3ó    r   )
Údegreer   r   r   r   Úaddr   Úorderr   r?   )r    r(   r   rG   Ú
componentsr>   ÚdÚcutr,   ÚHÚccr!   rF   s                r/   r   r   µ   sú   è ø€ ò4ð €JØŸ8™8›:×/‘4�1�a¨¨Q«ŠQÓ/À×2R¸ÈcÒ2RÈ²1Ð2R°1Ó2RÑR€EØ	�
‰
�5Ó€AÜ×%Ñ% aÓ(ò )ˆÜ˜“Gˆ	Øò 	(ˆCØò (�Ù(¨¨D°"Õ5Ø—M‘M $Õ'ñ(ð	(ô ˆy‹>˜AŸG™G›IÓ%Ø×Ñ˜iÕ(ð)ô ˜J¨¨A©Ó.×.Ñ.ùó 0ùÓ2Rð /ús8   ‚C?›C1©C1­C?µC7ÁAC?Â1C?Ã%C?Ã+C=Ã,C?c                 óä  ‡— i }t        | «      }t        t        d|dz   «      «      D ]Á  }||k(  rt        t	        | |   |«      «      ||<   Œ$|| vrt        t	        ||dz      |«      «      ||<   ŒGt        j                  | |   Ž Š||dz      D �cg c]  }t        ˆfd„|D «       «      sŒ|‘Œ }}|rt        t	        | |   |z   |«      «      ||<   Œ§t        t	        | |   |«      «      ||<   ŒÃ |S c c}w )Nr   c              3   ó&   •K  — | ]  }|‰v–— Œ
 y ­wr2   rC   )r3   r>   Ú
nodes_at_ks     €r/   r6   z,_reconstruct_k_components.<locals>.<genexpr>Ñ   s   øè ø€ Ò5UÈa°a¸zÔ6IÑ5UùrD   )ÚmaxÚreversedÚranger   r?   r   r<   rE   )Úk_compsÚresultÚmax_kr   r#   Úto_addrS   s         @r/   r   r   Ç   s÷   ø€ Ø€FÜ�‹L€EÜ”e˜A˜u q™yÓ)Ó*ò >ˆØ�Š:Üœ\¨'°!©*°aÓ8Ó9ˆF�1ŠIØ�gÑÜœ\¨&°°Q±©-¸Ó;Ó<ˆF�1ŠIäŸ™ G¨A¡JÐ/ˆJØ!'¨¨A©¡ÖV˜A´#Ó5UÐSTÔ5UÕ2U’aÐVˆFÐVÙÜ ¤¨g°a©j¸6Ñ.AÀ1Ó!EÓF��q’	ä ¤¨g°a©j¸!Ó!<Ó=��q’	ð>ð €Mùò Ws   ÂC-Â'C-c                 óˆ   — i }t        | j                  «       t        d«      ¬«      D ]  \  }}|D ]  }|D ]  }|||<   Œ	 Œ Œ |S )Nr   )Úkey)ÚsortedÚitemsr   )ÚkcompsrX   r   Úcompsr"   rF   s         r/   Úbuild_k_number_dictra   Ù   sY   € Ø€FÜ˜6Ÿ<™<›>¬z¸!«}Ô=ò !‰ˆˆ5Øò 	!ˆDØò !�Ø ��t’ñ!ñ	!ð!ð €MrH   r2   )Ú__doc__Úcollectionsr   Ú	itertoolsr   Úoperatorr   Únetworkxr   Únetworkx.algorithms.flowr   Únetworkx.utilsr   r   Ú__all__Ú_dispatchabler   r?   r   r   ra   rC   rH   r/   ú<module>rk      sl   ðñõ $Ý "Ý ã õ 2Ý .à Ð àÐ
€ñ �ZÓ Ø×ÑòF3ó ó !ðF3òR8ò,/ò$ó$rH   