code: def _hdbscan_sparse_distance_matrix(
      X,
      min_samples=5,
      alpha=1.0,
      metric="minkowski",
      p=2,
      leaf_size=40,
      gen_min_span_tree=False,
      **kwargs,
  ):
      assert issparse(X)
      # Check for connected component on X
      if (
          csgraph.connected_components(
              X, directed=False, return_labels=False
          )
          > 1
      ):
          raise ValueError(
              "Sparse distance matrix has multiple connected "
              "components!\nThat is, there exist groups of points "
              "that are completely disjoint -- there are no distance "
              "relations connecting them\n"
              "Run hdbscan on each component."
          )
  
      lil_matrix = X.tolil()
  
      # Compute sparse mutual reachability graph
      # if max_dist > 0, max distance to use when the reachability is infinite
      max_dist = kwargs.get("max_dist", 0.0)
      mutual_reachability_ = sparse_mutual_reachability(
          lil_matrix,
          min_points=min_samples,
          max_dist=max_dist,
          alpha=alpha,
      )
      # Check connected component on mutual reachability
      # If more than one component, it means that even if the distance matrix X
      # has one component, there exists with less than `min_samples` neighbors
      if (
          csgraph.connected_components(
              mutual_reachability_, directed=False, return_labels=False
          )
          > 1
      ):
          raise ValueError(
              (
                  "There exists points with less than %s neighbors. "
                  "Ensure your distance matrix has non zeros values for "
                  "at least `min_sample`=%s neighbors for each points (i.e. K-nn graph), "
                  "or specify a `max_dist` to use when distances are missing."
              )
              % (min_samples, min_samples)
          )
  
      # Compute the minimum spanning tree for the sparse graph
      sparse_min_spanning_tree = csgraph.minimum_spanning_tree(
          mutual_reachability_
      )
  
      # Convert the graph to scipy cluster array format
      nonzeros = sparse_min_spanning_tree.nonzero()
      nonzero_vals = sparse_min_spanning_tree[nonzeros]
      min_spanning_tree = np.vstack(nonzeros + (nonzero_vals,)).T
  
      # Sort edges of the min_spanning_tree by weight
      min_spanning_tree = min_spanning_tree[
          np.argsort(min_spanning_tree.T[2]), :
      ][0]
  
      # Convert edge list into standard hierarchical clustering format
      single_linkage_tree = label(min_spanning_tree)
  
      if gen_min_span_tree:
          return single_linkage_tree, min_spanning_tree
      else:
          return single_linkage_tree, None
  
imports: import numpy
  from sklearn.base import BaseEstimator, ClusterMixin
  from sklearn.metrics import pairwise_distances
  from scipy.sparse import issparse
  from sklearn.neighbors import KDTree, BallTree
  from joblib import Memory
  from warnings import warn
  from sklearn.utils import check_array
  from joblib.parallel import cpu_count
  from scipy.sparse import csgraph
  from ._hdbscan_linkage import single_linkage, mst_linkage_core, mst_linkage_core_vector, label
  from ._hdbscan_tree import condense_tree, compute_stability, get_clusters, outlier_scores
  from ._hdbscan_reachability import mutual_reachability, sparse_mutual_reachability
  from ._hdbscan_boruvka import KDTreeBoruvkaAlgorithm, BallTreeBoruvkaAlgorithm
  from .dist_metrics import DistanceMetric
  from .plots import CondensedTree, SingleLinkageTree, MinimumSpanningTree
  from .prediction import PredictionData
  from numpy import isclose
label_desc: Use image node_id 3 for calling a global function with example usage: _hdbscan_sparse_distance_matrix(X, min_samples, alpha, metric, p, leaf_size, gen_min_span_tree) and returns: single_linkage_tree,  min_spanning_tree, single_linkage_tree,  None