subgraph_isomorphism#
- graph_tool.structure.subgraph_isomorphism(sub, g, max_n=0, vertex_label=None, edge_label=None, induced=False, subgraph=True, generator=False, vertex_domains=None, edge_time=None, delta=0)[git][source]#
Obtain all subgraph isomorphisms of sub in g (or at most max_n subgraphs, if max_n > 0).
- Parameters:
- sub
Graph Subgraph for which to be searched.
- g
Graph Graph in which the search is performed.
- max_nint (optional, default:
0) Maximum number of matches to find. If max_n == 0, all matches are found.
- vertex_labelpair of
VertexPropertyMap(optional, default:None) If provided, this should be a pair of
VertexPropertyMapobjects, belonging tosubandg(in this order), which specify vertex labels which should match, in addition to the topological isomorphism.- edge_labelpair of
EdgePropertyMap(optional, default:None) If provided, this should be a pair of
EdgePropertyMapobjects, belonging tosubandg(in this order), which specify edge labels which should match, in addition to the topological isomorphism.- inducedbool (optional, default:
False) If
True, only node-induced subgraphs are found.- subgraphbool (optional, default:
True) If
False, all non-subgraph isomorphisms between sub and g are found.- generatorbool (optional, default:
False) If
True, a generator will be returned, instead of a list. This is useful if the number of isomorphisms is too large to store in memory. Ifgenerator == True, the optionmax_nis ignored.- vertex_domainslist of iterables of
int(optional, default:None) If given, for every vertex of
sub(in index order), the vertices ofgto which it may be mapped.- edge_time
EdgePropertyMap(optional, default:None) If given, the times of the edges of
g, and only the time-respecting matches are returned (see below).- delta
float(optional, default:0) Largest time difference of the edges incident to a vertex in a time-respecting match.
- sub
- Returns:
- vertex_mapslist (or generator) of
VertexPropertyMapobjects List (or generator) containing vertex property map objects which indicate different isomorphism mappings. The property maps vertices in sub to the corresponding vertex index in g.
- vertex_mapslist (or generator) of
See also
mark_subgraphMark a subgraph in a graph
isomorphismIsomorphism test between two graphs
maximum_common_subgraphsMaximum common induced subgraphs of two graphs
graph_tool.clustering.motifsCount of the motifs of a graph
Notes
The implementation is based on the VF2 algorithm, introduced by Cordella et al. [cordella-improved-2001] [cordella-subgraph-2004]. The spatial complexity is of order \(O(V)\), where \(V\) is the (maximum) number of vertices of the two graphs. Time complexity is \(O(V^2)\) in the best case and \(O(V!\times V)\) in the worst case [boost-subgraph-iso].
If
generator == False, the search is split by the image of the first vertex ofsubmatched by VF2, and the parts are searched in parallel; the matches are returned in the same order as in a single search, and withmax_n > 0they are the firstmax_nof this order. The generator performs a single serial search.With
vertex_domains, every vertex ofsubcan only be mapped to the vertices ofgin its domain.With
edge_time, a match is time-respecting if, for every matched vertex ofg, the times of the edges ofgbetween it and the other matched vertices differ by at mostdelta, and, for directed graphs, none of these out-edges is earlier than any of these in-edges, as in the temporal subgraph isomorphism of [redmond-temporal-2013]. The time condition is checked for every match found by the VF2 search, so that the time of the search does not decrease withdelta.References
[cordella-improved-2001]L. P. Cordella, P. Foggia, C. Sansone, and M. Vento, “An improved algorithm for matching large graphs.”, 3rd IAPR-TC15 Workshop on Graph-based Representations in Pattern Recognition, pp. 149-159, Cuen, 2001. http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.101.5342
[cordella-subgraph-2004]L. P. Cordella, P. Foggia, C. Sansone, and M. Vento, “A (Sub)Graph Isomorphism Algorithm for Matching Large Graphs.”, IEEE Trans. Pattern Anal. Mach. Intell., vol. 26, no. 10, pp. 1367-1372, 2004. DOI: 10.1109/TPAMI.2004.75 [sci-hub, @tor]
[subgraph-isomorphism-wikipedia][redmond-temporal-2013]Ursula Redmond and Pádraig Cunningham, “Temporal subgraph isomorphism”, Proceedings of the 2013 IEEE/ACM International Conference on Advances in Social Networks Analysis and Mining, 1451-1452 (2013), DOI: 10.1145/2492517.2492586 [sci-hub, @tor]
Examples
We search for the wheel graph with five spokes among the induced subgraphs of the Delaunay triangulation of random points in the plane:
>>> points = np.random.random((50, 2)) * 4 >>> g, pos = gt.triangulation(points, type="delaunay") >>> sub = gt.Graph([(0, i) for i in range(1, 6)] + ... [(i, i % 5 + 1) for i in range(1, 6)], directed=False) >>> vm = gt.subgraph_isomorphism(sub, g, induced=True) >>> print(len(vm)) 140
The 140 mappings correspond to 14 distinct wheels, since each of them is found once for every one of the 10 automorphisms of the wheel. We highlight the first one with
mark_subgraph():>>> vmask, emask = gt.mark_subgraph(g, sub, vm[0]) >>> vcolor = g.new_vp("string", vals=np.where(vmask.a, "#f28c8e", ... "#cccccc")) >>> ecolor = g.new_ep("string", vals=np.where(emask.a, "#e41a1c", ... "#999999")) >>> ewidth = g.new_ep("double", vals=np.where(emask.a, 2.5, 1)) >>> gt.graph_draw(g, pos, vertex_fill_color=vcolor, edge_color=ecolor, ... edge_pen_width=ewidth, output="subgraph-iso-embed.pdf") <...> >>> a = 2 * np.pi * np.arange(5) / 5 >>> spos = sub.new_vp("vector<double>", ... vals=[(0, 0)] + list(zip(np.sin(a), -np.cos(a)))) >>> gt.graph_draw(sub, spos, vertex_fill_color="#f28c8e", ... edge_color="#e41a1c", output="subgraph-iso.pdf") <...>
Left: wheel graph searched. Right: one of its occurrences as an induced subgraph of the Delaunay triangulation of 50 random points, with its vertices and edges in red.