.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_basics/plot_02_maximum_matching.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_01_basics_plot_02_maximum_matching.py: Maximum matching and the minimum number of driver nodes ======================================================= A matching is a set of edges without common start or end nodes; a node is matched when a matching edge ends in it. Liu, Slotine and Barabási proved that the unmatched nodes of a *maximum* matching form a minimum set of driver nodes (the minimum input theorem). The four-node network of the NOCAD manual, a three-cycle with a tail, has several maximum matchings: the number of driver nodes is fixed, their positions are not. On random networks the share of driver nodes falls with the mean degree, the classical result of network controllability. .. GENERATED FROM PYTHON SOURCE LINES 15-17 One matching, two pictures -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 17-41 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import nocad from nocad.datasets import load_four_node_example, make_random_network from nocad.plotting import draw_network from nocad.viz import plot_bipartite_matching # [snippet:matching] adj = load_four_node_example().adj m = nocad.maximum_matching(adj) print("size of the matching:", m.size) print("matched edges (start, end):", (m.edges + 1).tolist()) print("unmatched (driver) nodes:", m.unmatched + 1) # [/snippet:matching] pos = np.array([[-0.55, 0.65], [0.55, 0.65], [0.0, -0.25], [0.0, -1.1]]) fig, axes = plt.subplots(1, 2, figsize=(7.2, 3.4), gridspec_kw={"width_ratios": [1.1, 1]}) hl = {(int(a), int(b)): {"color": nocad.plotting.ROLE_COLORS["matched"], "lw": 2.6} for a, b in m.edges} draw_network(adj, pos, ax=axes[0], highlight=hl, drivers=m.unmatched, node_size=420, font_size=9) axes[0].set_title("directed network: matched edges green", fontsize=9) plot_bipartite_matching(adj, m, ax=axes[1], title="bipartite representation") fig.tight_layout() .. image-sg:: /auto_examples/01_basics/images/sphx_glr_plot_02_maximum_matching_001.png :alt: directed network: matched edges green, bipartite representation :srcset: /auto_examples/01_basics/images/sphx_glr_plot_02_maximum_matching_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none size of the matching: 3 matched edges (start, end): [[3, 1], [1, 2], [2, 3]] unmatched (driver) nodes: [4] .. GENERATED FROM PYTHON SOURCE LINES 42-50 All maximum matchings --------------------- The small network has two maximum matchings. Each leaves exactly one node unmatched, but a different one: :math:`x_4` when the cycle is matched by itself and the tail is left over, :math:`x_1` when the stem runs through the cycle into the tail. Nodes :math:`x_1` and :math:`x_4` are therefore *intermittent* driver nodes, :math:`x_2` and :math:`x_3` are never driver nodes (*redundant*). [snippet:enumerate] .. GENERATED FROM PYTHON SOURCE LINES 50-62 .. code-block:: Python all_m = nocad.all_maximum_matchings(adj) for k, mm in enumerate(all_m, start=1): print(f"matching {k}: edges {(mm.edges + 1).tolist()} drivers {mm.unmatched + 1}") print("node roles:", nocad.node_roles(adj)) # [/snippet:enumerate] fig, axes = plt.subplots(1, len(all_m), figsize=(3.0 * len(all_m), 2.9)) for k, (ax, mm) in enumerate(zip(np.atleast_1d(axes), all_m, strict=True), start=1): hl = {(int(a), int(b)): {"color": nocad.plotting.ROLE_COLORS["matched"], "lw": 2.6} for a, b in mm.edges} draw_network(adj, pos, ax=ax, highlight=hl, drivers=mm.unmatched, node_size=380, font_size=9) ax.set_title(f"maximum matching {k}: driver $x_{{{mm.unmatched[0] + 1}}}$", fontsize=9) fig.tight_layout() .. image-sg:: /auto_examples/01_basics/images/sphx_glr_plot_02_maximum_matching_002.png :alt: maximum matching 1: driver $x_{4}$, maximum matching 2: driver $x_{1}$ :srcset: /auto_examples/01_basics/images/sphx_glr_plot_02_maximum_matching_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none matching 1: edges [[3, 1], [1, 2], [2, 3]] drivers [4] matching 2: edges [[1, 2], [2, 3], [3, 4]] drivers [1] node roles: ['intermittent' 'redundant' 'redundant' 'intermittent'] .. GENERATED FROM PYTHON SOURCE LINES 63-69 Parameter study: mean degree ---------------------------- The share of driver nodes :math:`n_D=N_D/N` of Erdős--Rényi and scale-free networks with :math:`N=300` nodes. Sparse networks need many inputs because of their many sources, sinks and dilations; the heterogeneous scale-free networks need more than random ones of the same mean degree. .. GENERATED FROM PYTHON SOURCE LINES 69-86 .. code-block:: Python degrees = np.linspace(0.5, 8, 14) fig, ax = plt.subplots(figsize=(5.2, 3.4)) for k, kind in enumerate(["erdos_renyi", "scale_free"]): share = [] for kd in degrees: vals = [nocad.n_driver_nodes(make_random_network(300, kd, kind=kind, random_state=s).adj, "matching") / 300 for s in range(3)] share.append(np.mean(vals)) ax.plot(degrees, share, marker="os"[k], ms=4, lw=1.5, color=nocad.plotting.PALETTE[k], label=kind.replace("_", "-")) ax.set_xlabel(r"mean degree $\langle k\rangle$") ax.set_ylabel("share of driver nodes $n_D$") ax.set_title("driver nodes of random networks (N = 300)", fontsize=10) ax.grid(True, color="0.9") ax.legend(frameon=False, fontsize=8) fig.tight_layout() .. image-sg:: /auto_examples/01_basics/images/sphx_glr_plot_02_maximum_matching_003.png :alt: driver nodes of random networks (N = 300) :srcset: /auto_examples/01_basics/images/sphx_glr_plot_02_maximum_matching_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 87-90 **Take-away.** Maximum matching fixes how many driver nodes are needed, not which ones; denser and more homogeneous networks are controlled by a smaller share of their nodes. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.829 seconds) .. _sphx_glr_download_auto_examples_01_basics_plot_02_maximum_matching.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/nocad/main?urlpath=lab/tree/notebooks/auto_examples/01_basics/plot_02_maximum_matching.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_maximum_matching.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_maximum_matching.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_maximum_matching.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_