Skip to content

Kronecker graphs

The Kronecker graph of two graphs.

  • Class: KroneckerGraphSystem
  • Import: from skcomplex.systems.mathematics.networks import KroneckerGraphSystem
  • Field: mathematics/networks
  • Topics: network, kronecker product, random graph, degree distribution
  • References:
    • Leskovec et al. 2010
    • Gros 2015

Use this system

The block imports the class and simulates the system with its default parameters and its tuned run settings.

from skcomplex.systems.mathematics.networks import KroneckerGraphSystem

trajectory = KroneckerGraphSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
first None
second None
random_state 0
solver 'auto'
backend 'auto'

Tags

The system with its default parameters has these capability tags, which solvers and analyses check.

Tag Values
Time any
Space network
Laws any
Randomness deterministic
Domains any
Structure fixed

Class

The source of the class shows how it builds its components from its model parameters.

class KroneckerGraphSystem(BaseSystem):
    """The Kronecker graph of two graphs.

    Its adjacency matrix is the Kronecker product of theirs.

    The node `(i, j)` joins `(k, l)` when `i` joins `k` in the first graph and `j` joins `l` in the second (Gros 2015,
    exercise of chapter 1). The degree of `(i, j)` is the product of the degrees of `i` and `j`, so the mean degree is
    the product of the mean degrees, and the degree distribution is the distribution of the products. The system has the
    group `nodes` and their states `degree` and `clustering`.

    Args:
        first:
            The first graph, or `None` for a Barabási-Albert graph of 30 nodes with two edges per new node.

        second:
            The second graph, or `None` for a Barabási-Albert graph of 20 nodes with one edge per new node.

        random_state:
            The seed or generator of the default graphs.

        label:
            The label of the system, which names it when it is nested in another one.

        solver:
            The solver of the problem, or `'auto'` for the one the system chooses.

        backend:
            The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.

    Examples:
        >>> import networkx as nx
        >>> from skcomplex.systems.mathematics.networks import KroneckerGraphSystem
        >>> KroneckerGraphSystem(nx.path_graph(3), nx.path_graph(2)).prepare().system_.elements[0].size
        6
    """

    field = 'mathematics/networks'
    topics = ('network', 'kronecker product', 'random graph', 'degree distribution')
    references = ('Leskovec et al. 2010', 'Gros 2015')
    until = 1
    dt = 1

    @staticmethod
    def _get_static_system(label: str, graph: nx.Graph) -> System:
        """Return the system of a static network whose nodes hold their degree and clustering."""
        clustering = nx.clustering(graph)
        nodes = NetworkNodeElement(
            'nodes',
            degree=np.asarray([graph.degree(node) for node in graph], dtype=float),
            clustering=np.asarray([clustering[node] for node in graph], dtype=float),
            size=graph.number_of_nodes(),
        )
        return System(label, elements=[nodes], space=NetworkSpace(graph))

    def __init__(
        self,
        first: nx.Graph | None = None,
        second: nx.Graph | None = None,
        *,
        random_state: int | np.random.Generator | None = 0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.first = first
        self.second = second
        self.random_state = random_state

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of the network and its nodes."""
        super()._prepare(context)
        rng = np.random.default_rng(self.random_state)
        first = nx.barabasi_albert_graph(30, 2, seed=int(rng.integers(2**31))) if self.first is None else self.first
        second = nx.barabasi_albert_graph(20, 1, seed=int(rng.integers(2**31))) if self.second is None else self.second
        graph = nx.convert_node_labels_to_integers(nx.tensor_product(first, second), label_attribute='pair')
        self.system_ = KroneckerGraphSystem._get_static_system(self.label, graph)

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

class NetworkNodeElement(Element):
    """Nodes of a network, each with its degree, clustering, betweenness, and community.

    Args:
        label:
            The label of the nodes.

        degree:
            The degree of every node, one per node or shared.

        clustering:
            The clustering coefficient of every node, one per node or shared.

        betweenness:
            The normalized betweenness centrality of every node, one per node or shared.

        community:
            The index of the community of every node, one per node or shared.

        size:
            The number of nodes, or `None` for one.

        space:
            The network with one node per site, or `None`.

    Examples:
        >>> from skcomplex.systems.mathematics.networks import NetworkNodeElement
        >>> NetworkNodeElement('nodes', degree=[1.0, 2.0, 1.0], size=3).degree
        [1.0, 2.0, 1.0]
    """

    visual = VisualSpec('agents', {'color_by': 'community'})

    def __init__(
        self,
        label: str,
        *,
        degree: Annotated[object, NN] = 0.0,
        clustering: Annotated[object, RealSet.closed_open(0, oo)] = 0.0,
        betweenness: Annotated[object, RealSet.closed_open(0, oo)] = 0.0,
        community: Annotated[object, NN] = 0.0,
        size: int | None = None,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, size=size, space=space)
        self.degree = degree
        self.clustering = clustering
        self.betweenness = betweenness
        self.community = community