### An automaton for multi-pattern matching

←Return to chapter

## 4.6. An automaton for multi-pattern matching

We have shown in the previous sections that graph pattern-matching can be reduced to a problem of tree inclusions, with trees of fixed width w w w. To complete the pattern-matching algorithm, we must provide a fast way to evaluate the subtree relation for many trees representing the set of all patterns we wish to match.

More precisely, for patterns P 1,…,P ℓ P_1, \dots, P_\ell P 1​,…,P ℓ​ with width w w w, fix a root operation r i r_i r i​ in P i P_i P i​ for each 1⩽i⩽ℓ 1 \leqslant i \leqslant \ell1⩽i⩽ℓ and consider the rooted tree duals of the canonical PSGs τ r i(P i π i)\tau_{r_i}(P_i^{\pi_i})τ r i​​(P i π i​​), with π i=π r i(P i)\pi_i = \pi_{r_i}(P_i)π i​=π r i​​(P i​) the canonical anchors. Then given a subject graph G GG, we wish to compute the set

{1⩽i⩽ℓ∣τ r i(P i π i)⊆τ r(G π)},\{1 \leqslant i \leqslant \ell \mid \tau_{r_i}(P_i^{\pi_i}) \subseteq \tau_r(G^\pi)\},{1⩽i⩽ℓ∣τ r i​​(P i π i​​)⊆τ r​(G π)},

for all anchor sets π∈Π r w(G)\pi \in \Pi_r^w(G)π∈Π r w​(G) and root operation r r r in G GG. This corresponds to the `IsSubTree` predicate introduced in the sketch of the algorith in section 4.4.

Instead of considering the trees of PSGs, it will prove easier to consider the contracted PSGs (cPSGs)

τ r i(c(P i π i))and τ r(c(G π)).\tau_{r_i}(c(P_i^{\pi_i}))\quad\textrm{and}\quad \tau_r(c(G^\pi)).τ r i​​(c(P i π i​​))and τ r​(c(G π)).

Such tree inclusions are equivalent to finding embeddings in the subject graph itself, provided that we keep track of the s p l i t splits pl it and c o n t r a c t contractcont r act weight maps (see section 4.3).

It will be useful to remind ourselves the following properties of contracted PSGs. Every operation of a cPSG (and thus every node in its rooted dual tree) is an anchor operation of the PSG. Per Proposition 4.4, the rooted dual tree of a cPSG is a ternary tree and has exactly w i d t h(G)−1 width(G) - 1 w idth(G)−1 nodes. Finally, recall the concept of an open value of a graph, i.e. a value that is missing either a use or define operation (see section 4.2).

#### Reduction of tree inclusion to string prefix matching #

Now consider two contracted spanning tree reductions c(G 1 π 1)c(G_1^{\pi_1})c(G 1 π 1​​) and c(G 2 π 2)c(G_2^{\pi_2})c(G 2 π 2​​) with values V 1 V_1 V 1​ and V 2 V_2 V 2​. To simplify notation, define

τ 1=τ r 1(c(G 1 π 1))and τ 2=τ r 2(c(G 2 π 2))\tau_1 = \tau_{r_1}(c(G_1^{\pi_1})) \quad\textrm{and}\quad \tau_2 = \tau_{r_2}(c(G_2^{\pi_2}))τ 1​=τ r 1​​(c(G 1 π 1​​))and τ 2​=τ r 2​​(c(G 2 π 2​​))

for some choice of root operations r 1 r_1 r 1​ and r 2 r_2 r 2​ in G 1 G_1G 1​ and G 2 G_2G 2​, respectively. We lift the ⊆\subseteq⊆ relation on rooted dual trees of PSGs introduced in section 4.5 to rooted dual trees of cPSGs in Such a way that there is an inclusion relation between two rooted dual trees of PSGs if and only if the same relation holds on the rooted duals of cPSGs.

We say that τ 1⊆τ 2\tau_1 \subseteq \tau_2 τ 1​⊆τ 2​ if and only if

- the trees share the same root operation,
- τ 1\tau_1 τ 1​ is a subtree of τ 2\tau_2 τ 2​,
- the s p i l t spiltspilt map coincides on the common subtree, and
- the c o n t r a c t contractcont r act map satisfies for all v∈V 1 v \in V_1 v∈V 1​:
  {c o n t r a c t(v)⊆c o n t r a c t(f(v))if v is an open value, c o n t r a c t(v)=c o n t r a c t(f(v))otherwise,\begin{cases}contract(v) \subseteq contract(f(v))\quad&\textrm{if }v\textrm{ is an open value},\\contract(v) = contract(f(v))\quad&\textrm{otherwise},\\\end{cases}{cont r act(v)⊆cont r act(f(v))cont r act(v)=cont r act(f(v))​if v is an open value, otherwise,​

where f: V 1↪V 2 f: V_1 \hookrightarrow V_2 f: V 1​↪V 2​ designates the embedding of V 1 V_1 V 1​ into V 2 V_2 V 2​ given by the tree embedding.

The first three conditions are taken as-is from the ⊆\subseteq⊆ relation on non-contracted trees, whilst the fourth condition on the c o n t r a c t contractcont r act map is specific to contracted trees.

Using Proposition 4.2, there are at most 2 open values for each linear path in the graph, and thus at most 2⋅w 2 \cdot w2⋅w open values in a rooted dual tree of a cPSG of width w w w. For each such contracted rooted dual, we can thus define a contracted string tuple S=(s 1,…,s 2 w)∈(O∗)2 w S = (s_1, \dots, s_{2w}) \in (O^\ast)^{2w}S=(s 1​,…,s 2 w​)∈(O∗)2 w given by the values of the c o n t r a c t contractcont r act map evaluated in the (up to) 2 w 2w2 w open values1.

If c o n t r a c t∣C contract|_Ccont r act∣C​ is the restriction of c o n t r a c t contractcont r act to the domain of definition of non-open values of a cPSG, the fourth condition for the inclusion relation ⊆\subseteq⊆ on rooted dual cPSGs, given above becomes an equality condition when restricted to non-open values. A special case of this property of particular interest to us is stated as the following result. The ⊆\subseteq⊆ relation on strings refers to prefix inclusion, i.e. s⊆t s \subseteq ts⊆t if and only if s ss is a prefix of t tt.

Proposition 4.14Inclusion of equal-width trees

Let S=(s 1,…,s 2 w)S = (s_1, \dots, s_{2w})S=(s 1​,…,s 2 w​) and T=(t 1,…,t 2 w)∈(O∗)2 w T = (t_1, \dots, t_{2w}) \in (O^\ast)^{2w}T=(t 1​,…,t 2 w​)∈(O∗)2 w be the contracted string tuples of τ 1\tau_1 τ 1​ and τ 2\tau_2 τ 2​ respectively. Then τ 1⊆τ 2\tau_1 \subseteq \tau_2 τ 1​⊆τ 2​ if and only if the trees share the same root, are isomorphic, have the same s p l i t splits pl it and c o n t r a c t∣C contract|_Ccont r act∣C​ maps and for all i∈{1,…,2 w}i \in \{1, \dots, 2w\}i∈{1,…,2 w}: s i⊆t i s_i \subseteq t_is i​⊆t i​.

The proof of this follows directly from observing that rooted duals of cPSGs have the same set of nodes and that the restriction to non-open values c o n t r a c t∣C contract|_Ccont r act∣C​ must satisfy equality.

Why restricting ourselves to trees of the same width w w w? It is sufficient for our purposes! All patterns are of width w w w by assumption and so are the rooted dual trees of the form τ r(G π)\tau_r(G^\pi)τ r​(G π), given that π∈Π r w(G)\pi \in \Pi_r^w(G)π∈Π r w​(G).

The string prefix matching problem is a simple computational task that can be generalised to check for multiple string patterns at the same time using a prefix tree. An overview of this problem can be found in appendix A. We can thus obtain a solution for the pattern matching problem for ℓ\ellℓ patterns:

Proposition 4.15Fixed anchor pattern matching

As above, let

- G GG be a graph, with π∈Π r w(G)\pi \in \Pi_r^w(G)π∈Π r w​(G) a set of w−1 w - 1 w−1 operations and r∈π r \in \pi r∈π a choice of root operation,
- P 1,…,P ℓ P_1, \dots, P_\ell P 1​,…,P ℓ​ be patterns of width w w w and depth d dd, with choices of root operations r 1,…,r ℓ r_1, \dots, r_\ell r 1​,…,r ℓ​ and canonical anchors π i=π r i(P i).\pi_i = \pi_{r_i}(P_i).π i​=π r i​​(P i​).

The set of all pattern embeddings mapping the canonical anchor set π i\pi_i π i​ to π\pi π and root r i r_i r i​ to r r r for 1≤i≤ℓ 1 \leq i \leq \ell1≤i≤ℓ can be computed in time O(w⋅d)O(w\cdot d)O(w⋅d) using at most ℓ\ellℓ pre-computed prefix tree of size at most (ℓ⋅d)w+1(\ell \cdot d)^w + 1(ℓ⋅d)w+1, each constructed in time complexity O((ℓ⋅d)w)O((\ell \cdot d)^w)O((ℓ⋅d)w).

For each pattern, we consider its canonical spanning tree reduction and construct a multi-dimensional prefix tree (see Appendix ) for each group of patterns that share the same spanning tree reduction.

Given a graph G GG, we can compute the cPSG of G GG for anchors π\pi π and map its rooted dual tree to the corresponding prefix tree. This can be done in O(∣T G∣)O(|T_G|)O(∣T G​∣) time by using a search tree. We can restrict G π G^\piG π to a graph of size O(w⋅d)O(w \cdot d)O(w⋅d) by truncating the linear paths to at most 2 d 2d2d length, as in the proof of Proposition 4.12. Thus we can assume ∣G π∣∈O(w⋅d)|G^\pi| \in O(w \cdot d)∣G π∣∈O(w⋅d).

The rest of the proof and the runtime follow from the multi-dimensional prefix tree construction detailed in Appendix ).

#### Combining everything #

Finally, putting Proposition . and Proposition 4.12 together, we obtain our main result.

Proposition 4.13Pattern matching

Let P 1,…,P ℓ P_1, \dots, P_\ell P 1​,…,P ℓ​ be patterns with width w w w and depth d dd. The pre-computation runs in time and space complexity

O((d⋅ℓ)w⋅ℓ+ℓ⋅w⋅d).O \left( (d\cdot \ell)^w \cdot \ell + \ell \cdot w \cdot d \right).O((d⋅ℓ)w⋅ℓ+ℓ⋅w⋅d).

For any subject graph G GG, the pre-computed prefix tree can be used to find all pattern embeddings P i→G P_i \to G P i​→G in time

O(∣G∣⋅c w w 1/2⋅d)O \left( |G| \cdot \frac{c^w}{w^{1/2}} \cdot d \right)O(∣G∣⋅w 1/2 c w​⋅d)

where c=6.75 c = 6.75c=6.75 is a constant.

The pre-computation consists of running the `CanonicalAnchors` procedure on each of the ℓ\ellℓ patterns and then transforming them into a map of prefix trees using Proposition .. By Proposition 4.7, `CanonicalAnchors` runs in O(w⋅d)O(w\cdot d)O(w⋅d) for each pattern, where we used that ∣P i∣⩽w⋅d|P_i| \leqslant w \cdot d∣P i​∣⩽w⋅d for all patterns. The total runtime of prefix construction is thus

O((d⋅ℓ)w⋅ℓ+ℓ⋅w⋅d).O \left( (d\cdot \ell)^w \cdot \ell + \ell \cdot w \cdot d \right).O((d⋅ℓ)w⋅ℓ+ℓ⋅w⋅d).

The complexity of pattern matching itself on the other hand is composed of two parts: the computation of all possible anchor sets Π r w(G)\Pi_r^w(G)Π r w​(G), and the execution of the prefix string matcher for each of the trees resulting from these sets π∈Π r w(G)\pi \in \Pi_r^w(G)π∈Π r w​(G). As `AllAnchors` must be run for every choice of root vertex r r r in G GG, the runtime is thus obtained by multiplying i) ∣G∣|G|∣G∣ with ii) the runtime of the prefix tree matching (Proposition .), and with iii) ∣Π r w(G)∣|\Pi_r^w(G)|∣Π r w​(G)∣, i.e. the number of anchor lists returned by `AllAnchors` (Proposition 4.10):

O(∣G∣⋅w⋅d⋅C w),O(|G| \cdot w \cdot d \cdot C_w ),O(∣G∣⋅w⋅d⋅C w​),

where C w C_w C w​ is the bound for the number of anchor lists returned by `AllAnchors`. The result follows.

1. The values can be ordered as usual by using the total lexicographic order on port labels of the tree. ↩︎

←Return to chapter