docstr: Helper method to compute the adjacency matrix of the topology
  
  This method computes the adjacency matrix of the topology using
  the randomized algorithm proposed in [TSWJ2013]. The resulting
  topology is a connected graph. This is achieved by creating three
  matrices:
  
      * adj_matrix :  The adjacency matrix of the generated graph.
                      It's initialized as an identity matrix to
                      make sure that every particle has itself as
                      a neighbour. This matrix is the return
                      value of the method.
      * neighbor_matrix : The matrix of randomly generated neighbors.
                          This matrix is a matrix of shape
                          :code:`(swarm.n_particles, k)`:
                          with randomly generated elements. It is used
                          to create connections in the :code:`adj_matrix`.
      * dist_matrix : The distance matrix computed with Dijkstra's
                      algorithm. It is used to determine where the
                      graph needs edges to change it to a connected
                      graph.
  
  Parameters
  ----------
  swarm : pyswarms.backend.swarms.Swarm
      a Swarm instance
  k : int
      number of neighbors to be considered. Must be a
      positive integer less than :code:`n_particles-1`
  
  Returns
  -------
  numpy.ndarray
      Adjacency matrix of the topology
docstr_tok: ["Helper","method","to","compute","the","adjacency","matrix","of","the","topology","This","method","computes","the","adjacency","matrix","of","the","topology","using","the","randomized","algorithm","proposed","in","[","TSWJ2013","]",".","The","resulting","topology","is","a","connected","graph",".","This","is","achieved","by","creating","three","matrices",":","*","adj_matrix",":","The","adjacency","matrix","of","the","generated","graph",".","It","'s","initialized","as","an","identity","matrix","to","make","sure","that","every","particle","has","itself","as","a","neighbour",".","This","matrix","is","the","return","value","of","the","method",".","*","neighbor_matrix",":","The","matrix","of","randomly","generated","neighbors",".","This","matrix","is","a","matrix","of","shape",":","code",":","`","(","swarm.n_particles",",","k",")","`",":","with","randomly","generated","elements",".","It","is","used","to","create","connections","in","the",":","code",":","`","adj_matrix","`",".","*","dist_matrix",":","The","distance","matrix","computed","with","Dijkstra's","algorithm",".","It","is","used","to","determine","where","the","graph","needs","edges","to","change","it","to","a","connected","graph",".","Parameters","--","--","--","--","--","swarm",":","pyswarms.backend.swarms.Swarm","a","Swarm","instance","k",":","int","number","of","neighbors","to","be","considered",".","Must","be","a","positive","integer","less","than",":","code",":","`","n_particles-1","`","Returns","--","--","--","-","numpy.ndarray","Adjacency","matrix","of","the","topology"]
code: def __compute_neighbors(self, swarm, k):
      """Helper method to compute the adjacency matrix of the topology
  
      This method computes the adjacency matrix of the topology using
      the randomized algorithm proposed in [TSWJ2013]. The resulting
      topology is a connected graph. This is achieved by creating three
      matrices:
  
          * adj_matrix :  The adjacency matrix of the generated graph.
                          It's initialized as an identity matrix to
                          make sure that every particle has itself as
                          a neighbour. This matrix is the return
                          value of the method.
          * neighbor_matrix : The matrix of randomly generated neighbors.
                              This matrix is a matrix of shape
                              :code:`(swarm.n_particles, k)`:
                              with randomly generated elements. It is used
                              to create connections in the :code:`adj_matrix`.
          * dist_matrix : The distance matrix computed with Dijkstra's
                          algorithm. It is used to determine where the
                          graph needs edges to change it to a connected
                          graph.
  
      Parameters
      ----------
      swarm : pyswarms.backend.swarms.Swarm
          a Swarm instance
      k : int
          number of neighbors to be considered. Must be a
          positive integer less than :code:`n_particles-1`
  
      Returns
      -------
      numpy.ndarray
          Adjacency matrix of the topology
      """
  
      adj_matrix = np.identity(swarm.n_particles, dtype=int)
  
      neighbor_matrix = np.array(
          [
              np.random.choice(
                  # Exclude i from the array
                  np.setdiff1d(
                      np.arange(swarm.n_particles), np.array([i])
                  ),
                  k,
                  replace=False,
              )
              for i in range(swarm.n_particles)
          ]
      )
  
      # Set random elements to one using the neighbor matrix
      adj_matrix[
          np.arange(swarm.n_particles).reshape(swarm.n_particles, 1),
          neighbor_matrix,
      ] = 1
      adj_matrix[
          neighbor_matrix,
          np.arange(swarm.n_particles).reshape(swarm.n_particles, 1),
      ] = 1
  
      dist_matrix = dijkstra(
          adj_matrix,
          directed=False,
          return_predecessors=False,
          unweighted=True,
      )
  
      # Generate connected graph.
      while (
          connected_components(
              adj_matrix, directed=False, return_labels=False
          )
          != 1
      ):
          for i, j in itertools.product(
              range(swarm.n_particles), repeat=2
          ):
              if dist_matrix[i][j] == np.inf:
                  adj_matrix[i][j] = 1
  
      return adj_matrix
  
file_path: pyswarms/pyswarms/backend/topology/random.py
imports: import itertools
  import logging
  import numpy
  from scipy.sparse.csgraph import connected_components, dijkstra
  from ..None import operators
  from ..handlers import BoundaryHandler, VelocityHandler
  from ...utils.reporter import Reporter
  from .base import Topology
label_desc: Use image node_id 5 for calling the Random obj's underlying member method code with example usage: obj.__compute_neighbors(swarm, k) and returns: adj_matrix