graph_edit_distance#
- graph_tool.structure.graph_edit_distance(g1, g2, vertex_label=None, edge_label=None, vertex_cost=1.0, edge_cost=1.0, vertex_subst_cost=1.0, edge_subst_cost=1.0, exact=False, upper_bound=None, roots=None, timeout=None, return_map=False)[git][source]#
Return the edit distance between two graphs.
- Parameters:
- g1
Graph First graph.
- g2
Graph Second graph, with the same directedness.
- vertex_labelpair of
VertexPropertyMap(optional, default:None) Vertex labels of both graphs. If given, the substitution of a vertex by another with a different label has cost
vertex_subst_cost.- edge_labelpair of
EdgePropertyMap(optional, default:None) Edge labels of both graphs. If given, the substitution of an edge by another with a different label has cost
edge_subst_cost.- vertex_cost
floator pair ofVertexPropertyMap(optional, default:1.) Cost of the insertion or deletion of a vertex, or the costs of the deletion of every vertex of
g1and of the insertion of every vertex ofg2.- edge_cost
floator pair ofEdgePropertyMap(optional, default:1.) Cost of the insertion or deletion of an edge, or the costs of the deletion of every edge of
g1and of the insertion of every edge ofg2.- vertex_subst_cost
float(optional, default:1.) Cost of the substitution of a vertex by another with a different label.
- edge_subst_cost
float(optional, default:1.) Cost of the substitution of an edge by another with a different label.
- exact
bool(optional, default:False) If
True, the exact distance is computed, in time exponential in the number of vertices. Otherwise, an approximation is returned, which is an upper bound.- upper_bound
float(optional, default:None) If given,
Noneis returned if the distance exceeds this value. Withexact == False, this happens only if a lower bound of the distance exceeds it.- rootspair of
Vertex(optional, default:None) If given, the first vertex, of
g1, is mapped to the second, ofg2, and the distance is the minimum over the edit paths with this correspondence.- timeout
float(optional, default:None) If given, the exact search stops after this number of seconds, and the best distance found so far, which is an upper bound, is returned, with a
RuntimeWarning. Ignored ifexact == False.- return_map
bool(optional, default:False) If
True, the vertex map that yields the distance is also returned.
- g1
- Returns:
- d
float The edit distance (or its upper bound, if
exact == Falseor the search timed out), orNoneif it exceedsupper_bound.- vmap
VertexPropertyMap Map from the vertices of
g1to their counterparts ing2(-1for the deleted vertices). Only returned ifreturn_map == True.
- d
See also
maximum_common_subgraphsMaximum common induced subgraphs
isomorphismIsomorphism test between two graphs
similarityAdjacency similarity between two graphs
Notes
The edit distance [sanfeliu-distance-1983] is the minimum total cost of the vertex and edge insertions, deletions, and substitutions that transform \(G_1\) into a graph isomorphic to \(G_2\). Every edit path corresponds to a map \(f\) from the vertices of \(G_1\) to those of \(G_2\) and to deletions, in which the vertices of \(G_2\) outside the image are inserted, and the edges between every pair of vertices \((u, v)\) are transformed into those between \((f(u), f(v))\) with the minimum number of operations (which accounts for parallel edges and self-loops). The distance is symmetric, and is a metric if the costs are positive. If the insertion and deletion costs differ between the edges, the edges between a pair of vertices are transformed by an optimal assignment, which is trivial in the absence of parallel edges.
With
exact == False, the map is obtained from an optimal assignment between the vertices of both graphs and the deletions and insertions, where the cost of each assignment includes half the cost of matching the incident edges [riesen-approximate-2009] [blumenthal-improved-2018]. The assignment is solved with the Hungarian algorithm [kuhn-hungarian-1955], in time \(O((V_1+V_2)^3)\) and memory \(O((V_1+V_2)^2)\). Since it accounts for the edges only through the degrees (and labels), the map is then improved by exchanging the images of pairs of vertices while the cost decreases [riesen-improving-2015], in time \(O((V_1+V_2)^2\left<k\right>)\) per pass, and by iterated linearization: the assignment problem is solved again with the cost of every assignment computed with the other vertices at their current images, while this lowers the exact cost (the integer step of the IPFP method [bougleux-graph-2017], at most 20 times); the exact cost of the final map is returned.With
exact == True, the vertices of the smaller graph are assigned one at a time by depth-first branch and bound, where the remaining cost is bounded from below by the same assignment problem restricted to the remaining vertices [blumenthal-improved-2018], and the assignments it returns provide upper bounds. The time is exponential in the number of vertices in the worst case, which makes the computation feasible only for small graphs (a few tens of vertices). Anupper_boundclose to the distance prunes the search further, so that deciding whether the distance is at most a given value can be much faster than computing it. If the edge costs differ, the bound uses the smallest edge cost of every vertex for the edges to the remaining vertices, which makes it less tight.References
[sanfeliu-distance-1983]Alberto Sanfeliu and King-Sun Fu, “A distance measure between attributed relational graphs for pattern recognition”, IEEE Transactions on Systems, Man, and Cybernetics 13, 353-362 (1983), DOI: 10.1109/TSMC.1983.6313167 [sci-hub, @tor]
[riesen-approximate-2009]Kaspar Riesen and Horst Bunke, “Approximate graph edit distance computation by means of bipartite graph matching”, Image and Vision Computing 27, 950-959 (2009), DOI: 10.1016/j.imavis.2008.04.004 [sci-hub, @tor]
[blumenthal-improved-2018] (1,2)David B. Blumenthal and Johann Gamper, “Improved lower bounds for graph edit distance”, IEEE Transactions on Knowledge and Data Engineering 30, 503-516 (2018), DOI: 10.1109/TKDE.2017.2772243 [sci-hub, @tor]
[kuhn-hungarian-1955]Harold W. Kuhn, “The Hungarian method for the assignment problem”, Naval Research Logistics Quarterly 2, 83-97 (1955), DOI: 10.1002/nav.3800020109 [sci-hub, @tor]
[riesen-improving-2015]Kaspar Riesen and Horst Bunke, “Improving bipartite graph edit distance approximation using various search strategies”, Pattern Recognition 48, 1349-1363 (2015), DOI: 10.1016/j.patcog.2014.11.002 [sci-hub, @tor]
[bougleux-graph-2017]Sébastien Bougleux, Luc Brun, Vincenzo Carletti, Pasquale Foggia, Benoit Gaüzère and Mario Vento, “Graph edit distance as a quadratic assignment problem”, Pattern Recognition Letters 87, 38-46 (2017), DOI: 10.1016/j.patrec.2016.10.001 [sci-hub, @tor]
Examples
>>> g1 = gt.complete_graph(4) >>> g2 = gt.circular_graph(5) >>> d, m = gt.graph_edit_distance(g1, g2, exact=True, return_map=True) >>> print(d, m.a) 6.0 [...]
The approximation can exceed the exact distance for unlabeled graphs that differ only slightly, as for this network with three edges removed:
>>> g1 = gt.collection.data["karate"] >>> g2 = gt.Graph(g1.get_edges()[3:], directed=False) >>> print(gt.graph_edit_distance(g1, g2), ... gt.graph_edit_distance(g1, g2, exact=True)) 9.0 3.0
With an upper bound, the computation decides whether the distance is at most this value, and returns
Noneotherwise:>>> print(gt.graph_edit_distance(g1, g2, exact=True, upper_bound=2)) None