`L i@ dZddlZddlmZddlmZddlmZm Z m Z e dd ge eddd ge eddd dgd d dddZ ddZ y)zGraph utilities and algorithms.N)sparse)pairwise_distances)IntegralIntervalvalidate_paramsz array-likez sparse matrixleft)closed)graphsourcecutoffT)prefer_skip_nested_validation)rc(tj|r|j}ntj|}i}d}|g}|rM|}t }|D]*}||vs|||<|j |j |,|||kr |S|dz }|rM|S)aDReturn 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)] rr)rissparsetolil lil_matrixsetupdaterows)r r rseenlevel next_level this_levelvs Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/utils/graph.py"single_source_shortest_path_lengthrsVu !!%( D EJ  U  1A}Q!!%**Q-0 1  &E/  K    Kc x|dk(r tj|r tdt|D]}t j ||k(}||} t|D]} t j || k(} || } |dk(r|t j || } nt| | fd|i|} t j| jd| j\}}|dk(rd|||| |f<d|| |||f<|dk(r%| ||f|||| |f<| ||f|| |||f<td |z|S) aAdd 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 connectivityrdistancez?Unknown mode=%r, should be one of ['connectivity', 'distance'].) rr RuntimeErrorrangenp flatnonzeroix_r unravel_indexargminshape ValueError)Xr n_connected_componentscomponent_labelsmoder!kwargsiidx_iXijidx_jXjDiijjs r_fix_connected_componentsr<Osf6??1#5   ) */145 uXq ANN#3q#89E5B&bffUE*+&r2GfGG%%ahhDh&9177CFB~%./eBir*+./eBir*+#./BieBir*+./BieBir*+ U# 2 Lr)r$ euclidean) __doc__numpyr'scipyrmetrics.pairwiser_param_validationrrr rr<rrrDs~% 1BB 0Haf=>Haf=tD #' AE44x   Sr