code: def tsp_linearize(data, niter=1000, metric='euclidean', **kwargs):
      
  
      # Compute pairwise distances between all datapoints
      N = data.shape[0]
      D = scipy.spatial.distance.pdist(data, metric=metric, **kwargs)
  
      # To solve the travelling salesperson problem with no return to the
      # original node we add a dummy node that has distance zero connections
      # to all other nodes. The dummy node is then removed after we've converged
      # to a solution.
      dist = np.zeros((N+1, N+1))
      dist[:N, :N] = scipy.spatial.distance.squareform(D)
  
      # solve TSP
      perm, cost_hist = _solve_tsp(dist)
  
      # remove dummy node at position i
      i = np.argwhere(perm == N).ravel()[0]
      perm = np.hstack((perm[(i+1):], perm[:i]))
  
      return perm
docs: Sorts a matrix dataset to (approximately) solve the traveling
      salesperson problem. The matrix can be re-sorted so that sequential rows
      represent datapoints that are close to each other based on some
      user-defined distance metric. Uses 2-opt local search algorithm.
  
      Args
      ----
      data : ndarray, N x R matrix of data
          Datapoints are held in rows, features are held in columns
  
      Returns
      -------
      perm : ndarray, vector of N integers
          Permutation / ordering of the rows of data that approximately
          solves the travelling salesperson problem.