code: def random_kernel_graph(
      n, kernel_integral, kernel_root=None, seed=None
  ):
      r"""Returns an random graph based on the specified kernel.
  
      The algorithm chooses each of the $[n(n-1)]/2$ possible edges with
      probability specified by a kernel $\kappa(x,y)$ [1]_.  The kernel
      $\kappa(x,y)$ must be a symmetric (in $x,y$), non-negative,
      bounded function.
  
      Parameters
      ----------
      n : int
          The number of nodes
      kernel_integral : function
          Function that returns the definite integral of the kernel $\kappa(x,y)$,
          $F(y,a,b) := \int_a^b \kappa(x,y)dx$
      kernel_root: function (optional)
          Function that returns the root $b$ of the equation $F(y,a,b) = r$.
          If None, the root is found using :func:`scipy.optimize.brentq`
          (this requires SciPy).
      seed : integer, random_state, or None (default)
          Indicator of random number generation state.
          See :ref:`Randomness<randomness>`.
  
      Notes
      -----
      The kernel is specified through its definite integral which must be
      provided as one of the arguments. If the integral and root of the
      kernel integral can be found in $O(1)$ time then this algorithm runs in
      time $O(n+m)$ where m is the expected number of edges [2]_.
  
      The nodes are set to integers from $0$ to $n-1$.
  
      Examples
      --------
      Generate an Erdős–Rényi random graph $G(n,c/n)$, with kernel
      $\kappa(x,y)=c$ where $c$ is the mean expected degree.
  
      >>> def integral(u, w, z):
      ...     return c * (z - w)
      >>> def root(u, w, r):
      ...     return r / c + w
      >>> c = 1
      >>> graph = nx.random_kernel_graph(1000, integral, root)
  
      See Also
      --------
      gnp_random_graph
      expected_degree_graph
  
      References
      ----------
      .. [1] Bollobás, Béla,  Janson, S. and Riordan, O.
         "The phase transition in inhomogeneous random graphs",
         *Random Structures Algorithms*, 31, 3--122, 2007.
  
      .. [2] Hagberg A, Lemons N (2015),
         "Fast Generation of Sparse Random Kernel Graphs".
         PLoS ONE 10(9): e0135177, 2015. doi:10.1371/journal.pone.0135177
      """
      if kernel_root is None:
          import scipy as sp
  
          def kernel_root(y, a, r):
              def my_function(b):
                  return kernel_integral(y, a, b) - r
  
              return sp.optimize.brentq(my_function, a, 1)
  
      graph = nx.Graph()
      graph.add_nodes_from(range(n))
      (i, j) = (1, 1)
      while i < n:
          r = -math.log(
              1 - seed.random()
          )  # (1-seed.random()) in (0, 1]
          if kernel_integral(i / n, j / n, 1) <= r:
              i, j = i + 1, i + 1
          else:
              j = math.ceil(n * kernel_root(i / n, j / n, r))
              graph.add_edge(i - 1, j - 1)
      return graph
  
imports: import itertools
  import math
  from collections import defaultdict
  import networkx
  from networkx.utils import py_random_state
  from .classic import complete_graph, empty_graph, path_graph, star_graph
  from .degree_seq import degree_sequence_tree
label_desc: Use image node_id 18 for calling a global function with example usage: random_kernel_graph(n, kernel_integral, kernel_root, seed) and returns: graph, sp, unknown