Skip to content

Latest commit

 

History

History
610 lines (307 loc) · 14.4 KB

plot_isomorphic_graphs_paddle.rst

File metadata and controls

610 lines (307 loc) · 14.4 KB

html

Note

Click here <sphx_glr_download_auto_examples_paddle_plot_isomorphic_graphs_paddle.py> to download the full example code

sphx-glr-example-title

Introduction: Matching Isomorphic Graphs

This example is an introduction to pygmtools which shows how to match isomorphic graphs. Isomorphic graphs mean graphs whose structures are identical, but the node correspondence is unknown.

Note

The following solvers support QAP formulation, and are included in this example:

  • ~pygmtools.classic_solvers.rrwm (classic solver)
  • ~pygmtools.classic_solvers.ipfp (classic solver)
  • ~pygmtools.classic_solvers.sm (classic solver)
  • ~pygmtools.neural_solvers.ngm (neural network solver)

sphx-glr-script-out

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/setuptools/depends.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp

Generate two isomorphic graphs

Visualize the graphs

/auto_examples/paddle/images/sphx_glr_plot_isomorphic_graphs_paddle_001.png

These two graphs look dissimilar because they are not aligned. We then align these two graphs by graph matching.

Build affinity matrix

To match isomorphic graphs by graph matching, we follow the formulation of Quadratic Assignment Problem (QAP):

$$\begin{aligned} &\max_{\mathbf{X}} \ \texttt{vec}(\mathbf{X})^\top \mathbf{K} \texttt{vec}(\mathbf{X})\\\ s.t. \quad &\mathbf{X} \in \{0, 1\}^{n_1\times n_2}, \ \mathbf{X}\mathbf{1} = \mathbf{1}, \ \mathbf{X}^\top\mathbf{1} \leq \mathbf{1} \end{aligned}$$

where the first step is to build the affinity matrix (K)

Visualization of the affinity matrix. For graph matching problem with N nodes, the affinity matrix has N2 × N2 elements because there are N2 edges in each graph.

Note

The diagonal elements of the affinity matrix are empty because there is no node features in this example.

/auto_examples/paddle/images/sphx_glr_plot_isomorphic_graphs_paddle_002.png

sphx-glr-script-out

<matplotlib.image.AxesImage object at 0x7fa57dda32b0>

Solve graph matching problem by RRWM solver

See ~pygmtools.classic_solvers.rrwm for the API reference.

The output of RRWM is a soft matching matrix. Visualization:

/auto_examples/paddle/images/sphx_glr_plot_isomorphic_graphs_paddle_003.png

sphx-glr-script-out

<matplotlib.image.AxesImage object at 0x7fa57e0d6d30>

Get the discrete matching matrix

Hungarian algorithm is then adopted to reach a discrete matching matrix

Visualization of the discrete matching matrix:

/auto_examples/paddle/images/sphx_glr_plot_isomorphic_graphs_paddle_004.png

sphx-glr-script-out

<matplotlib.image.AxesImage object at 0x7fa57dd7ef10>

Align the original graphs

Draw the matching (green lines for correct matching, red lines for wrong matching):

/auto_examples/paddle/images/sphx_glr_plot_isomorphic_graphs_paddle_005.png

Align the nodes:

/auto_examples/paddle/images/sphx_glr_plot_isomorphic_graphs_paddle_006.png

Other solvers are also available

Classic IPFP solver

See ~pygmtools.classic_solvers.ipfp for the API reference.

Visualization of IPFP matching result:

/auto_examples/paddle/images/sphx_glr_plot_isomorphic_graphs_paddle_007.png

sphx-glr-script-out

<matplotlib.image.AxesImage object at 0x7fa57e6b6910>

Classic SM solver

See ~pygmtools.classic_solvers.sm for the API reference.

Visualization of SM matching result:

/auto_examples/paddle/images/sphx_glr_plot_isomorphic_graphs_paddle_008.png

sphx-glr-script-out

<matplotlib.image.AxesImage object at 0x7fa54e882820>

NGM neural network solver

See ~pygmtools.neural_solvers.ngm for the API reference.

sphx-glr-script-out

Downloading to /Users/guoziao/Library/Caches/pygmtools/ngm_voc_paddle.pdparams...

Downloading to /Users/guoziao/Library/Caches/pygmtools/ngm_voc_paddle.pdparams...
  0%|          | 0/14583 [00:00<?, ?it/s]    100%|##########| 14.2k/14.2k [00:00<00:00, 744kB/s]

Visualization of NGM matching result:

/auto_examples/paddle/images/sphx_glr_plot_isomorphic_graphs_paddle_009.png

sphx-glr-script-out

<matplotlib.image.AxesImage object at 0x7fa54e755d90>

sphx-glr-timing

Total running time of the script: ( 0 minutes 15.571 seconds)

html

Download Python source code: plot_isomorphic_graphs_paddle.py <plot_isomorphic_graphs_paddle.py>

Download Jupyter notebook: plot_isomorphic_graphs_paddle.ipynb <plot_isomorphic_graphs_paddle.ipynb>

html