code: def pseudo_peripheral_node(A):
      
      from pyamg.graph import breadth_first_search
      n = A.shape[0]
  
      valence = np.diff(A.indptr)
  
      # select an initial node x, set delta = 0
      x = int(np.random.rand() * n)
      delta = 0
  
      while True:
          # do a level-set traversal from x
          order, level = breadth_first_search(A, x)
  
          # select a node y in the last level with min degree
          maxlevel = level.max()
          lastnodes = np.where(level == maxlevel)[0]
          lastnodesvalence = valence[lastnodes]
          minlastnodesvalence = lastnodesvalence.min()
          y = np.where(lastnodesvalence == minlastnodesvalence)[0][0]
          y = lastnodes[y]
  
          # if d(x,y)>delta, set, and go to bfs above
          if level[y] > delta:
              x = y
              delta = level[y]
          else:
              return x, order, level
docs: Find a pseudo peripheral node.
  
      Parameters
      ----------
      A : sparse matrix
          Sparse matrix
  
      Returns
      -------
      x : int
          Locaiton of the node
      order : array
          BFS ordering
      level : array
          BFS levels
  
      Notes
      -----
      Algorithm in Saad