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:
- g1
Graph First graph.
- g2
Graph 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.
- connected
bool(optional, default:True) If
True, only connected common subgraphs are considered.- max_n
int(optional, default:0) If positive, at most this number of subgraphs is returned.
- g1
- Returns:
- maps
listofVertexPropertyMap For every maximum common subgraph, a map from the vertices of
g1to their counterparts ing2(-1for the vertices outside the subgraph).
- maps
See also
subgraph_isomorphismEmbeddings of a graph as a subgraph of another
graph_edit_distanceEdit distance between two graphs
isomorphismIsomorphism 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