maximum_common_subgraphs

maximum_common_subgraphs#

graph_tool.structure.maximum_common_subgraphs(g1, g2, vertex_label=None, edge_label=None, connected=True, max_n=0)[git][source]#

Return the maximum common induced subgraphs of two graphs.

Parameters:
g1Graph

First graph.

g2Graph

Second graph, with the same directedness.

vertex_labelpair of VertexPropertyMap (optional, default: None)

If given, only vertices with equal labels are matched.

edge_labelpair of EdgePropertyMap (optional, default: None)

If given, only edges with equal labels are matched.

connectedbool (optional, default: True)

If True, only connected common subgraphs are considered.

max_nint (optional, default: 0)

If positive, at most this number of subgraphs is returned.

Returns:
mapslist of VertexPropertyMap

For every maximum common subgraph, a map from the vertices of g1 to their counterparts in g2 (-1 for the vertices outside the subgraph).

See also

subgraph_isomorphism

Embeddings of a graph as a subgraph of another

graph_edit_distance

Edit distance between two graphs

isomorphism

Isomorphism test between two graphs

Notes

The common subgraphs are induced subgraphs of both graphs that are isomorphic under the given map (with equal labels, if given), and the maximum ones are those with the largest number of vertices. They are found with McGregor’s backtracking algorithm [mcgregor-backtrack-1982], which explores the whole search space before returning the largest ones, so that the time is exponential in the sizes of the graphs; it is feasible only for small graphs (a few tens of vertices). Parallel edges and self-loops are ignored.

References

[mcgregor-backtrack-1982]

James J. McGregor, “Backtrack search algorithms and the maximal common subgraph problem”, Software: Practice and Experience 12, 23-34 (1982), DOI: 10.1002/spe.4380120103 [sci-hub, @tor]

Examples

>>> g1 = gt.Graph([(0, 1), (1, 2), (2, 0), (2, 3)], directed=False)
>>> g2 = gt.Graph([(0, 1), (1, 2), (2, 3), (3, 0)], directed=False)
>>> ms = gt.maximum_common_subgraphs(g1, g2)
>>> print(len(ms), (ms[0].a >= 0).sum())
16 3