{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction to Graph Matching"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The graph matching problem (GMP), is meant to find an allignment of nodes between two graphs that minimizes the number of edge disagreements between those two graphs. Therefore, the GMP can be formally written as an optimization problem: \n",
"\n",
"\\begin{equation}\n",
"\\begin{aligned}\n",
"\\min & {\\;-trace(APB^T P^T)}\\\\\n",
"\\text{s.t. } & {\\;P \\: \\epsilon \\: \\mathcal{P}} \\\\\n",
"\\end{aligned}\n",
"\\end{equation}\n",
"\n",
"Where $\\mathcal{P}$ is the set of possible permutation matrices.\n",
"\n",
"The Quadratic Assignment problem is a combinatorial opimization problem, modeling following the real-life problem: \n",
"\n",
"\"Consider the problem of allocating a set of facilities to a set of locations, with the\n",
"cost being a function of the distance and flow between the facilities, plus costs associated\n",
"with a facility being placed at a certain location. The objective is to assign each facility\n",
"to a location such that the total cost is minimized.\" [1]\n",
"\n",
"When written as an optimization problem, the QAP is represented as:\n",
"\n",
"\\begin{equation}\n",
"\\begin{aligned}\n",
"\\min & {\\; trace(APB^T P^T)}\\\\\n",
"\\text{s.t. } & {\\;P \\: \\epsilon \\: \\mathcal{P}} \\\\\n",
"\\end{aligned}\n",
"\\end{equation}\n",
"\n",
"Since the GMP objective function is the negation of the QAP objective function, any algorithm that solves one can solve the other. \n",
"\n",
"\n",
"This class is an implementation of the Fast Approximate Quadratic Assignment Problem (FAQ), an algorithm designed to efficiently and accurately solve the QAP, as well as GMP. \n",
"\n",
"[1] Optimierung, Diskrete & Er, Rainer & Ela, A & Burkard, Rainer & Dragoti-Cela, Eranda & Pardalos, Panos & Pitsoulis, Leonidas. (1998). The Quadratic Assignment Problem. Handbook of Combinatorial Optimization. 10.1007/978-1-4613-0303-9_27. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from graspy.match import GraphMatch as GMP\n",
"from graspy.simulations import er_np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For the sake of tutorial, we will use FAQ to solve the GMP for two graphs where we know a solution exists. \n",
"Below, we sample a binary graph (undirected and no self-loops) $G_1 \\sim ER_{NP}(50, 0.3)$.\n",
"Then, we randomly shuffle the nodes of $G_1$ to initiate $G_2$.\n",
"The number of edge disagreements as a result of the node shuffle is printed below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n = 50\n",
"p = 0.3\n",
"\n",
"np.random.seed(1)\n",
"G1 = er_np(n=n, p=p)\n",
"node_shuffle_input = np.random.permutation(n)\n",
"G2 = G1[np.ix_(node_shuffle_input, node_shuffle_input)]\n",
"print(\"Number of edge disagreements: \", sum(sum(abs(G1-G2))))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualize the graphs using heat mapping"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from graspy.plot import heatmap\n",
"heatmap(G1, cbar=False, title = 'G1 [ER-NP(50, 0.3) Simulation]')\n",
"heatmap(G2, cbar=False, title = 'G2 [G1 Randomly Shuffled]')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Below, we create a model to solve GMP. The model is then fitted for the two graphs $G_1$ and $G_2$. One of the option for the algorithm is the starting position of $P$. In this case, the class default of barycenter intialization is used, or the flat doubly stochastic matrix. The number of edge disagreements is printed below. With zero edge disagreements, we see that FAQ is successful in unshuffling the graph."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gmp = GMP()\n",
"gmp = gmp.fit(G1,G2)\n",
"G2 = G2[np.ix_(gmp.perm_inds_, gmp.perm_inds_)]\n",
"print(\"Number of edge disagreements: \", sum(sum(abs(G1-G2))))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"heatmap(G1, cbar=False, title = 'G1[ER-NP(50, 0.3) Simulation]')\n",
"heatmap(G2, cbar=False, title = 'G2[ER-NP(50, 0.3) Randomly Shuffled] unshuffled')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}