§
    bŠtj
  ã                   ó\   — d Z ddlmZ ddlmZ ddlZddlmZ ddgZ	dd	„Z
dd
„Zdd„Zd„ ZdS )zB
Cuthill-McKee ordering of graph nodes to produce sparse matrices
é    )Údeque)Ú
itemgetterNé   )Úarbitrary_elementÚcuthill_mckee_orderingÚreverse_cuthill_mckee_orderingc              #   óŠ   K  — t          j        | ¦  «        D ]+}t          |                      |¦  «        |¦  «        E d{V —† Œ,dS )aÝ  Generate an ordering (permutation) of the graph nodes to make
    a sparse matrix.

    Uses the Cuthill-McKee heuristic (based on breadth-first search) [1]_.

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

    heuristic : function, optional
      Function to choose starting node for RCM algorithm.  If None
      a node from a pseudo-peripheral pair is used.  A user-defined function
      can be supplied that takes a graph object and returns a single node.

    Returns
    -------
    nodes : generator
       Generator of nodes in Cuthill-McKee ordering.

    Examples
    --------
    >>> from networkx.utils import cuthill_mckee_ordering
    >>> G = nx.path_graph(4)
    >>> rcm = list(cuthill_mckee_ordering(G))
    >>> A = nx.adjacency_matrix(G, nodelist=rcm)

    Smallest degree node as heuristic function:

    >>> def smallest_degree(G):
    ...     return min(G, key=G.degree)
    >>> rcm = list(cuthill_mckee_ordering(G, heuristic=smallest_degree))


    See Also
    --------
    reverse_cuthill_mckee_ordering

    Notes
    -----
    The optimal solution the bandwidth reduction is NP-complete [2]_.


    References
    ----------
    .. [1] E. Cuthill and J. McKee.
       Reducing the bandwidth of sparse symmetric matrices,
       In Proc. 24th Nat. Conf. ACM, pages 157-172, 1969.
       http://doi.acm.org/10.1145/800195.805928
    .. [2]  Steven S. Skiena. 1997. The Algorithm Design Manual.
       Springer-Verlag New York, Inc., New York, NY, USA.
    N)ÚnxÚconnected_componentsÚ connected_cuthill_mckee_orderingÚsubgraph)ÚGÚ	heuristicÚcs      úP/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/networkx/utils/rcm.pyr   r      s`   è è € õj Ô$ QÑ'Ô'ð Nð NˆÝ3°A·J²J¸q±M´MÀ9ÑMÔMÐMÐMÐMÐMÐMÐMÐMÐMðNð Nó    c                 óX   — t          t          t          | |¬¦  «        ¦  «        ¦  «        S )aÿ  Generate an ordering (permutation) of the graph nodes to make
    a sparse matrix.

    Uses the reverse Cuthill-McKee heuristic (based on breadth-first search)
    [1]_.

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

    heuristic : function, optional
      Function to choose starting node for RCM algorithm.  If None
      a node from a pseudo-peripheral pair is used.  A user-defined function
      can be supplied that takes a graph object and returns a single node.

    Returns
    -------
    nodes : generator
       Generator of nodes in reverse Cuthill-McKee ordering.

    Examples
    --------
    >>> from networkx.utils import reverse_cuthill_mckee_ordering
    >>> G = nx.path_graph(4)
    >>> rcm = list(reverse_cuthill_mckee_ordering(G))
    >>> A = nx.adjacency_matrix(G, nodelist=rcm)

    Smallest degree node as heuristic function:

    >>> def smallest_degree(G):
    ...     return min(G, key=G.degree)
    >>> rcm = list(reverse_cuthill_mckee_ordering(G, heuristic=smallest_degree))


    See Also
    --------
    cuthill_mckee_ordering

    Notes
    -----
    The optimal solution the bandwidth reduction is NP-complete [2]_.

    References
    ----------
    .. [1] E. Cuthill and J. McKee.
       Reducing the bandwidth of sparse symmetric matrices,
       In Proc. 24th Nat. Conf. ACM, pages 157-72, 1969.
       http://doi.acm.org/10.1145/800195.805928
    .. [2]  Steven S. Skiena. 1997. The Algorithm Design Manual.
       Springer-Verlag New York, Inc., New York, NY, USA.
    )r   )ÚreversedÚlistr   )r   r   s     r   r   r   H   s)   € õj •DÕ/°¸YÐGÑGÔGÑHÔHÑIÔIÐIr   c              #   ó   K  — |€t          | ¦  «        }n || ¦  «        }|h}t          |g¦  «        }|r™|                     ¦   «         }|V — t          |                      t          | |         ¦  «        |z
  ¦  «        t          d¦  «        ¬¦  «        }d„ |D ¦   «         }|                     |¦  «         |                     |¦  «         |°—d S d S )Né   ©Úkeyc                 ó   — g | ]\  }}|‘ŒS © r   )Ú.0ÚnÚds      r   ú
<listcomp>z4connected_cuthill_mckee_ordering.<locals>.<listcomp>Œ   s   € Ð%Ð%Ð%™$˜!˜Q�AÐ%Ð%Ð%r   )	Úpseudo_peripheral_noder   ÚpopleftÚsortedÚdegreeÚsetr   ÚupdateÚextend)r   r   ÚstartÚvisitedÚqueueÚparentÚndÚchildrens           r   r   r   €   sâ   è è € àÐÝ& qÑ)Ô)ˆˆà�	˜!‘”ˆØˆg€GÝ�5�'‰NŒN€EØ
ð Ø—’‘”ˆØˆˆˆÝ�A—H’H�S  6¤™^œ^¨gÑ5Ñ6Ô6½JÀq¹M¼MÐJÑJÔJˆØ%Ð% "Ð%Ñ%Ô%ˆØ�Š�xÑ Ô Ð Ø�Š�XÑÔÐð ð ð ð ð ð r   c                 óX  ‡— t          | ¦  «        }d}|}	 t          j        | |¦  «        }t          |                     ¦   «         ¦  «        Š‰|k    rnW‰}ˆfd„|                     ¦   «         D ¦   «         }t          |                      |¦  «        t          d¦  «        ¬¦  «        \  }}Œ”|S )Nr   Tc              3   ó.   •K  — | ]\  }}|‰k    ¯|V — Œd S ©Nr   )r   r   ÚdistÚls      €r   ú	<genexpr>z)pseudo_peripheral_node.<locals>.<genexpr>�   s+   øè è € Ð>Ð>™'˜!˜T°D¸A²I°I�A°I°I°I°IÐ>Ð>r   r   r   )	r   r
   Úshortest_path_lengthÚmaxÚvaluesÚitemsÚminr#   r   )r   ÚuÚlpÚvÚsplÚfarthestÚdegr1   s          @r   r    r    ‘   s®   ø€ õ 	˜!ÑÔ€AØ	
€BØ	€Að<ÝÔ% a¨Ñ+Ô+ˆÝ�—
’
‘”ÑÔˆØ�Š7ˆ7ØØˆØ>Ð>Ð>Ð> S§Y¢Y¡[¤[Ð>Ñ>Ô>ˆÝ�Q—X’X˜hÑ'Ô'­Z¸©]¬]Ð;Ñ;Ô;‰ˆˆ3ð<ð €Hr   r/   )Ú__doc__Úcollectionsr   Úoperatorr   Únetworkxr
   Úutilsr   Ú__all__r   r   r   r    r   r   r   ú<module>rD      s½   ððð ð Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à Ð Ð Ð à %Ð %Ð %Ð %Ð %Ð %à#Ð%EÐ
F€ð6Nð 6Nð 6Nð 6Nðr5Jð 5Jð 5Jð 5Jðpð ð ð ð"ð ð ð ð r   