ó
    §ñ:i«  ã                   óš   • S r SSKrSSKJr  SSKJr  SSKJrJ	r	J
r
  \
" SS	/\	" \SSS
S9/\	" \SSS
S9S/S.SS9SS.S j5       r  SS jrg)zGraph utilities and algorithms.é    N)Úsparseé   )Úpairwise_distancesé   )ÚIntegralÚIntervalÚvalidate_paramsz
array-likezsparse matrixÚleft)Úclosed)ÚgraphÚsourceÚcutoffT)Úprefer_skip_nested_validation)r   c                óT  • [         R                  " U 5      (       a  U R                  5       n O[         R                  " U 5      n 0 nSnU/nU(       aW  Un[	        5       nU H,  nXs;  d  M
  XCU'   UR                  U R                  U   5        M.     Ub  X$::  a   U$ US-  nU(       a  MW  U$ )aÔ  Return the length of the shortest path from source to all reachable nodes.

Parameters
----------
graph : {array-like, sparse matrix} of shape (n_nodes, n_nodes)
    Adjacency matrix of the graph. Sparse matrix of format LIL is
    preferred.

source : int
   Start node for path.

cutoff : int, default=None
    Depth to stop the search - only paths of length <= cutoff are returned.

Returns
-------
paths : dict
    Reachable end nodes mapped to length of path from source,
    i.e. `{end: path_length}`.

Examples
--------
>>> from sklearn.utils.graph import single_source_shortest_path_length
>>> import numpy as np
>>> graph = np.array([[ 0, 1, 0, 0],
...                   [ 1, 0, 1, 0],
...                   [ 0, 1, 0, 0],
...                   [ 0, 0, 0, 0]])
>>> single_source_shortest_path_length(graph, 0)
{0: 0, 1: 1, 2: 2}
>>> graph = np.ones((6, 6))
>>> sorted(single_source_shortest_path_length(graph, 2).items())
[(0, 1), (1, 1), (2, 0), (3, 1), (4, 1), (5, 1)]
r   r   )r   ÚissparseÚtolilÚ
lil_matrixÚsetÚupdateÚrows)r   r   r   ÚseenÚlevelÚ
next_levelÚ
this_levelÚvs           ÚV/srv/projetos/modelo_ml_acdoc/venv/lib/python3.13/site-packages/sklearn/utils/graph.pyÚ"single_source_shortest_path_lengthr      s¨   € ôV ‡‚�u×ÑØ—‘“‰ä×!Ò! %Ó(ˆØ€DØ€EØ�€JÞ
Øˆ
Ü“Uˆ
ÛˆAØ�}Ø�Q‘Ø×!Ñ! %§*¡*¨Q¡-Ö0ñ ð Ñ &£/Øà€Kð 	�‰
ˆ÷ ˆ*ð €Kó    c                 ór  • US:X  a&  [         R                  " U 5      (       a  [        S5      e[        U5       Hû  n[        R
                  " X7:H  5      nX   n	[        U5       HÍ  n
[        R
                  " X::H  5      nX   nUS:X  a  U [        R                  " X‹5         nO[        Xœ4SU0UD6n[        R                  " UR                  SS9UR                  5      u  pïUS:X  a  SXU   X¿   4'   SXU   XŽ   4'   M›  US:X  a   XÞU4   XU   X¿   4'   XÞU4   XU   XŽ   4'   MÁ  [        S	U-  5      e   Mý     U$ )
a   Add connections to sparse graph to connect unconnected components.

For each pair of unconnected components, compute all pairwise distances
from one component to the other, and add a connection on the closest pair
of samples. This is a hacky way to get a graph with a single connected
component, which is necessary for example to compute a shortest path
between all pairs of samples in the graph.

Parameters
----------
X : array of shape (n_samples, n_features) or (n_samples, n_samples)
    Features to compute the pairwise distances. If `metric =
    "precomputed"`, X is the matrix of pairwise distances.

graph : sparse matrix of shape (n_samples, n_samples)
    Graph of connection between samples.

n_connected_components : int
    Number of connected components, as computed by
    `scipy.sparse.csgraph.connected_components`.

component_labels : array of shape (n_samples)
    Labels of connected components, as computed by
    `scipy.sparse.csgraph.connected_components`.

mode : {'connectivity', 'distance'}, default='distance'
    Type of graph matrix: 'connectivity' corresponds to the connectivity
    matrix with ones and zeros, and 'distance' corresponds to the distances
    between neighbors according to the given metric.

metric : str
    Metric used in `sklearn.metrics.pairwise.pairwise_distances`.

kwargs : kwargs
    Keyword arguments passed to
    `sklearn.metrics.pairwise.pairwise_distances`.

Returns
-------
graph : sparse matrix of shape (n_samples, n_samples)
    Graph of connection between samples, with a single connected component.
ÚprecomputedzŒ_fix_connected_components with metric='precomputed' requires the full distance matrix in X, and does not work with a sparse neighbors graph.ÚmetricN)ÚaxisÚconnectivityr   Údistancez?Unknown mode=%r, should be one of ['connectivity', 'distance'].)r   r   ÚRuntimeErrorÚrangeÚnpÚflatnonzeroÚix_r   Úunravel_indexÚargminÚshapeÚ
ValueError)ÚXr   Ún_connected_componentsÚcomponent_labelsÚmoder!   ÚkwargsÚiÚidx_iÚXiÚjÚidx_jÚXjÚDÚiiÚjjs                   r   Ú_fix_connected_componentsr<   Q   sX  € ðf �Ó¤6§?¢?°1×#5Ñ#5Üðó
ð 	
ô Ð)Ö*ˆÜ—’Ð/Ñ4Ó5ˆØ‰XˆÜ�q–ˆAÜ—N’NÐ#3Ñ#8Ó9ˆEØ‘ˆBà˜Ó&Ø”b—f’f˜UÓ*Ñ+‘ä& rÑG°fÐGÀÑG�ä×%Ò% a§h¡h°D hÐ&9¸1¿7¹7ÓC‰FˆBØ�~Ó%Ø./�˜B‘i ¡Ð*Ñ+Ø./�˜B‘i ¡Ð*Ó+Ø˜Ó#Ø./°B°©i�˜B‘i ¡Ð*Ñ+Ø./°B°©i�˜B‘i ¡Ð*Ó+ä ØUØñóð ó# ñ +ð2 €Lr   )r$   Ú	euclidean)Ú__doc__Únumpyr'   Úscipyr   Úmetrics.pairwiser   Ú_param_validationr   r   r	   r   r<   © r   r   Ú<module>rD      s~   ðÙ %ó Ý å 1ß BÑ Bñ à Ð0Ù˜H a¨°fÑ=Ð>Ù˜H a¨°fÑ=¸tÐDñð
 #'ñð AEô 4óð4ðx 
ØõSr   