Skip to content

Latest commit

 

History

History
586 lines (293 loc) · 13.4 KB

plot_isomorphic_graphs.rst

File metadata and controls

586 lines (293 loc) · 13.4 KB

html

Note

Click here <sphx_glr_download_auto_examples_pytorch_plot_isomorphic_graphs.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 means 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)

Generate two isomorphic graphs

Visualize the graphs

/auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_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 is empty because there is no node features in this example.

/auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_002.png

sphx-glr-script-out

<matplotlib.image.AxesImage object at 0x00000221B6EDC850>

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/pytorch/images/sphx_glr_plot_isomorphic_graphs_003.png

sphx-glr-script-out

<matplotlib.image.AxesImage object at 0x00000221B7671910>

Get the discrete matching matrix

Hungarian algorithm is then adopted to reach a discrete matching matrix

Visualization of the discrete matching matrix:

/auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_004.png

sphx-glr-script-out

<matplotlib.image.AxesImage object at 0x00000221B785ED60>

Align the original graphs

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

/auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_005.png

Align the nodes:

/auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_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/pytorch/images/sphx_glr_plot_isomorphic_graphs_007.png

sphx-glr-script-out

<matplotlib.image.AxesImage object at 0x00000221B6597160>

Classic SM solver

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

Visualization of SM matching result:

/auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_008.png

sphx-glr-script-out

<matplotlib.image.AxesImage object at 0x00000221B663ED90>

NGM neural network solver

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

Visualization of NGM matching result:

/auto_examples/pytorch/images/sphx_glr_plot_isomorphic_graphs_009.png

sphx-glr-script-out

<matplotlib.image.AxesImage object at 0x00000221B7E39C70>

sphx-glr-timing

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

html

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

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

html