§
    bŠtj–  ã                   ó´   — 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dS )z,
Moody and White algorithm for k-components
é    )Údefaultdict)Úcombinations)Ú
itemgetterN)Úedmonds_karp)Únot_implemented_forÚk_componentsÚdirectedc           	      óÔ  ‡ — t          t          ¦  «        }|€t          }t          j        ‰ ¦  «        D ]?}t          |¦  «        }t          |¦  «        dk    r|d                              |¦  «         Œ@ˆ fd„t          j        ‰ ¦  «        D ¦   «         }|D ]?}t          |¦  «        }t          |¦  «        dk    r|d                              |¦  «         Œ@|D �]‚}t          |¦  «        dk    rŒt          j	        ||¬¦  «        }	|	dk    r(||	                              t          |¦  «        ¦  «         t          t          j
        ||	|¬¦  «        ¦  «        }
|	t          ||
|	¦  «        fg}|rí|d         \  }}	 t          |¦  «        }|                     |¦  «        }t          j	        ||¬¦  «        }||k    r.|dk    r(||                              t          |¦  «        ¦  «         t          t          j
        |||¬¦  «        ¦  «        }
|
r&|                     |t          ||
|¦  «        f¦  «         n$# t          $ r |                     ¦   «          Y nw xY w|°í�Œ„t!          |¦  «        S )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

    Né   c                 ó:   •— g | ]}‰                      |¦  «        ‘ŒS © )Úsubgraph)Ú.0ÚcÚGs     €új/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/networkx/algorithms/connectivity/kcomponents.pyú
<listcomp>z k_components.<locals>.<listcomp>x   s#   ø€ ÐHÐHÐH a�A—J’J˜q‘M”MÐHÐHÐHó    é   )Ú	flow_func)Úkr   éÿÿÿÿ)r   ÚlistÚdefault_flow_funcÚnxÚconnected_componentsÚsetÚlenÚappendÚbiconnected_componentsÚnode_connectivityÚall_node_cutsÚ_generate_partitionÚnextr   ÚStopIterationÚpopÚ_reconstruct_k_components)r   r   r   Ú	componentÚcompÚbicomponentsÚbicomponentÚbicompÚBr   ÚcutsÚstackÚparent_kÚ	partitionÚnodesÚCÚthis_ks   `                r   r   r      s{  ø€ õt �tÑ$Ô$€LàÐÝ%ˆ	åÔ,¨QÑ/Ô/ð )ð )ˆ	å�9‰~Œ~ˆÝˆt‰9Œ9�qŠ=ˆ=Ø˜ŒO×"Ò" 4Ñ(Ô(Ð(øØHÐHÐHÐH­2Ô+DÀQÑ+GÔ+GÐHÑHÔH€LØ#ð +ð +ˆÝ�[Ñ!Ô!ˆåˆv‰;Œ;˜Š?ˆ?Ø˜ŒO×"Ò" 6Ñ*Ô*Ð*øØð ñ ˆÝˆq‰6Œ6�QŠ;ˆ;ØÝÔ  ¨iÐ8Ñ8Ô8ˆØˆqŠ5ˆ5Ø˜ŒO×"Ò"¥3 q¡6¤6Ñ*Ô*Ð*å•BÔ$ Q¨!°yÐAÑAÔAÑBÔBˆØÕ(¨¨D°!Ñ4Ô4Ð5Ð6ˆØð 	Ø$)¨"¤IÑ!ˆX�yð
Ý˜Y™œ�Ø—J’J˜uÑ%Ô%�ÝÔ-¨a¸9ÐEÑEÔE�Ø˜HÒ$Ð$¨°!ª¨Ø  Ô(×/Ò/µ°A±´Ñ7Ô7Ð7Ý�BÔ,¨Q°&ÀIÐNÑNÔNÑOÔO�Øð QØ—L’L &Õ*=¸aÀÀvÑ*NÔ*NÐ!OÑPÔPÐPøøÝ ð ð ð Ø—	’	‘”���ðøøøð ð 	ùõ* % \Ñ2Ô2Ð2s   Å8B:H3È3IÉIc              #   ó\  ‡‡K  — t          j        ¦   «         }t          t          | ¦  «        ¦  «        Š|                     ‰¦  «         |                     ˆˆfd„t          ‰d¦  «        D ¦   «         ¦  «         t          j        |¦  «        D ]}t          j	        ˆfd„|D ¦   «         Ž V — ŒdS )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   ój   •K  — | ]-\  }}t          ‰|         ‰|         z  ¦  «        ‰k    ¯'||fV — Œ.d S ©N)r   )r   ÚuÚvr   r2   s      €€r   ú	<genexpr>z_consolidate.<locals>.<genexpr>®   sT   øè è € ð ð Ù�1�aµS¸¸q¼ÀEÈ!ÄHÑ9LÑ5MÔ5MÐQRÒ5RÐ5RˆˆAˆÐ5RÐ5RÐ5RÐ5Rðð r   r   c                 ó    •— g | ]
}‰|         ‘ŒS r   r   )r   Únr2   s     €r   r   z _consolidate.<locals>.<listcomp>²   s   ø€ Ð6Ð6Ð6 q˜% œ(Ð6Ð6Ð6r   N)
r   ÚGraphÚdictÚ	enumerateÚadd_nodes_fromÚadd_edges_fromr   r   r   Úunion)Úsetsr   r   r(   r2   s    `  @r   Ú_consolidaterD   Ÿ   sÚ   øøè è € õ 	Œ‰
Œ
€AÝ•˜4‘”Ñ!Ô!€EØ×Ò�UÑÔÐØ×Òð ð ð ð ð Ý'¨¨qÑ1Ô1ðñ ô ñ ô ð õ Ô,¨QÑ/Ô/ð 8ð 8ˆ	ÝŒiÐ6Ð6Ð6Ð6¨IÐ6Ñ6Ô6Ð7Ð7Ð7Ð7Ð7ð8ð 8r   c              #   óÄ  ‡ ‡‡‡	K  — d„ Š	g }d„ |D ¦   «         }ˆfd„‰                       ¦   «         D ¦   «         |z
  }‰                      |¦  «        }t          t          t	          j        |¦  «        ¦  «        D ]OŠ‰ˆ ˆˆ	fd„|D ¦   «         z  }t          |¦  «        ‰                      ¦   «         k     r|                     |¦  «         ŒPt          |‰dz   ¦  «        E d {V —† d S )Nc                 óF   ‡— t          ˆfd„| |         D ¦   «         ¦  «        S )Nc              3   ó    •K  — | ]}|‰v V — Œ	d S r7   r   )r   r<   r1   s     €r   r:   zE_generate_partition.<locals>.has_nbrs_in_partition.<locals>.<genexpr>·   s'   øè è € Ð3Ð3 a�1˜	�>Ð3Ð3Ð3Ð3Ð3Ð3r   ©Úany)r   Únoder1   s     `r   Úhas_nbrs_in_partitionz2_generate_partition.<locals>.has_nbrs_in_partition¶   s*   ø€ ÝÐ3Ð3Ð3Ð3¨1¨T¬7Ð3Ñ3Ô3Ñ3Ô3Ð3r   c                 ó   — h | ]	}|D ]}|’ŒŒ
S r   r   )r   Úcutr<   s      r   ú	<setcomp>z&_generate_partition.<locals>.<setcomp>º   s%   € Ð0Ð0Ð0�s¨CÐ0Ð0 q�Ð0Ð0Ð0Ð0r   c                 ó&   •— h | ]\  }}|‰k    ¯|’ŒS r   r   )r   r<   Údr   s      €r   rN   z&_generate_partition.<locals>.<setcomp>»   s"   ø€ Ð/Ð/Ð/‘4�1�a¨¨Qª¨ˆQ¨¨¨r   c                 ó.   •— h | ]} ‰‰|‰¦  «        ¯|’ŒS r   r   )r   r<   r   ÚccrK   s     €€€r   rN   z&_generate_partition.<locals>.<setcomp>¾   s.   ø€ ÐRÐRÐR Ð2GÐ2GÈÈ1ÈbÑ2QÔ2QÐR˜!ÐRÐRÐRr   r   )
Údegreer   Úmapr   r   r   r   Úorderr   rD   )
r   r.   r   Ú
componentsÚ	n_in_cutsr2   ÚHr(   rR   rK   s
   ` `     @@r   r#   r#   µ   s  øøøøè è € ð4ð 4ð 4ð €JØ0Ð0˜dÐ0Ñ0Ô0€IØ/Ð/Ð/Ð/˜1Ÿ8š8™:œ:Ð/Ñ/Ô/°)Ñ;€EØ	�
Š
�5ÑÔ€AÝ•#•rÔ.¨qÑ1Ô1Ñ2Ô2ð )ð )ˆØÐRÐRÐRÐRÐRÐR YÐRÑRÔRÑRˆ	Ýˆy‰>Œ>˜AŸGšG™IœIÒ%Ð%Ø×Ò˜iÑ(Ô(Ð(øÝ˜J¨¨A©Ñ.Ô.Ð.Ð.Ð.Ð.Ð.Ð.Ð.Ð.Ð.r   c                 ó
  ‡— i }| rt          | ¦  «        nd}t          |dd¦  «        D ]Ú}||k    r't          t          | |         |¦  «        ¦  «        ||<   Œ/|| vr*t          t          ||dz            |¦  «        ¦  «        ||<   Œ]t	          j        | |         Ž Šˆfd„||dz            D ¦   «         }|r*t          t          | |         |z   |¦  «        ¦  «        ||<   Œ´t          t          | |         |¦  «        ¦  «        ||<   ŒÛ|S )Nr   r   r   c                 óJ   •— g | ]}t          ˆfd „|D ¦   «         ¦  «        ¯|‘Œ S )c              3   ó    •K  — | ]}|‰vV — Œ	d S r7   r   )r   r<   Ú
nodes_at_ks     €r   r:   z7_reconstruct_k_components.<locals>.<listcomp>.<genexpr>Î   s(   øè è € Ð5UÐ5UÈa°a¸zÐ6IÐ5UÐ5UÐ5UÐ5UÐ5UÐ5Ur   rH   )r   r   r\   s     €r   r   z-_reconstruct_k_components.<locals>.<listcomp>Î   s<   ø€ ÐVÐVÐV˜Aµ#Ð5UÐ5UÐ5UÐ5UÐSTÐ5UÑ5UÔ5UÑ2UÔ2UÐV�aÐVÐVÐVr   )ÚmaxÚranger   rD   r   rB   )Úk_compsÚresultÚmax_kr   Úto_addr\   s        @r   r'   r'   Ä   s  ø€ Ø€FØ#Ð*�C�‰LŒLˆL¨€EÝ�5˜!˜RÑ Ô ð >ð >ˆØ�Š:ˆ:Ý�\¨'°!¬*°aÑ8Ô8Ñ9Ô9ˆF�1‰IˆIØ�gÐÐÝ�\¨&°°Q±¬-¸Ñ;Ô;Ñ<Ô<ˆF�1‰IˆIåœ G¨A¤JÐ/ˆJØVÐVÐVÐV ¨¨A©¤ÐVÑVÔVˆFØð >Ý ¥¨g°a¬j¸6Ñ.AÀ1Ñ!EÔ!EÑFÔF��q‘	�	å ¥¨g°a¬j¸!Ñ!<Ô!<Ñ=Ô=��q‘	�	Ø€Mr   c                 óv   — d„ t          |                      ¦   «         t          d¦  «        ¬¦  «        D ¦   «         S )Nc                 ó.   — i | ]\  }}|D ]
}|D ]}||“ŒŒŒS r   r   )r   r   Úcompsr)   rJ   s        r   ú
<dictcomp>z'build_k_number_dict.<locals>.<dictcomp>×   s\   € ð ð ð áˆAˆuØðð ð Øð	ð ð ð 	ˆaðð ð ð ð r   r   )Úkey)ÚsortedÚitemsr   )Úkcompss    r   Úbuild_k_number_dictrk   Ö   s>   € ðð å˜vŸ|š|™~œ~µ:¸a±=´=ÐAÑAÔAðñ ô ð r   r7   )Ú__doc__Úcollectionsr   Ú	itertoolsr   Úoperatorr   Únetworkxr   Únetworkx.algorithms.flowr   Únetworkx.utilsr   r   Ú__all__Ú_dispatchabler   rD   r#   r'   rk   r   r   r   ú<module>ru      s  ððð ð $Ð #Ð #Ð #Ð #Ð #Ø "Ð "Ð "Ð "Ð "Ð "Ø Ð Ð Ð Ð Ð à Ð Ð Ð ð 2Ð 1Ð 1Ð 1Ð 1Ð 1Ø .Ð .Ð .Ð .Ð .Ð .à Ð àÐ
€ð Ð�ZÑ Ô ØÔðF3ð F3ð F3ñ Ôñ !Ô ðF3ðR8ð 8ð 8ð,/ð /ð /ðð ð ð$ð ð ð ð r   