Algorithm Search⌘K

Proofs: VF2 algorithm

These are the human proofs of VF2 algorithm, at full length.

VF2 outputs exactly the mappings of the requested kind, each once full

This proof shows: correctness, termination.

Setting. G1=(V1,E1)G_1 = (V_1, E_1) and G2=(V2,E2)G_2 = (V_2, E_2) are simple undirected graphs with vertex labels ℓ1,ℓ2\ell_1, \ell_2, and n1≤n2n_1 \le n_2. A target is an injective map m:V1→V2m : V_1 \to V_2 with ℓ2(m(u))=ℓ1(u)\ell_2(m(u)) = \ell_1(u) for all uu and, for all u≠wu \ne w in V1V_1:

  • mode mono: {u,w}∈E1  ⟹  {m(u),m(w)}∈E2\{u, w\} \in E_1 \implies \{m(u), m(w)\} \in E_2;
  • modes iso and induced: {u,w}∈E1  ⟺  {m(u),m(w)}∈E2\{u, w\} \in E_1 \iff \{m(u), m(w)\} \in E_2, and in iso mode also n1=n2n_1 = n_2.

A state is a partial injective map MM. For a state, TiT_i is the set of unmapped vertices of GiG_i with at least one mapped neighbor and N~i\tilde N_i the set of unmapped vertices with none. A state MM extends to a target mm when m(u)=M(u)m(u) = M(u) for every uu mapped by MM.

Invariant (I). For every state on the search path, any two mapped pattern vertices u,wu, w satisfy the edge condition of the mode for M(u),M(w)M(u), M(w), labels agree on mapped vertices, and MM is injective.

Proof of (I). It holds for M=∅M = \emptyset. Let (u,v)(u, v) be added in L6. Both are unmapped, since L3 draws uu from unmapped pattern vertices and vv from unmapped host vertices, so M∪{(u,v)}M \cup \{(u, v)\} is injective. L9 checks the label. For a mapped ww: if {u,w}∈E1\{u, w\} \in E_1, L10 checks {v,M(w)}∈E2\{v, M(w)\} \in E_2. In iso and induced mode, if {v,M(w)}∈E2\{v, M(w)\} \in E_2, then M(w)M(w) is a mapped neighbor of vv and L10 checks {u,w}∈E1\{u, w\} \in E_1. Pairs not involving uu are unchanged. □\square

Lemma 1 (soundness). Every output of L2 is a target.

Proof. At L2, ∣M∣=n1|M| = n_1, so MM is a total injective map V1→V2V_1 \to V_2, and by (I) it satisfies the labels and the edge condition for every pair. In iso mode the algorithm starts only when n1=n2n_1 = n_2. □\square

Lemma 2 (the rules keep every extendable state). Let MM extend to a target mm, let uu be unmapped, and let v=m(u)v = m(u). Then (u,v)(u, v) passes L9 to L12.

Proof. L9: mm keeps labels. In iso mode mm is an isomorphism, so deg⁡u=deg⁡v\deg u = \deg v. Otherwise mm maps the neighbors of uu injectively to neighbors of vv, so deg⁡u≤deg⁡v\deg u \le \deg v. L10 holds because mm satisfies the edge condition for uu and each mapped vertex.

For L11 and L12 let ww be an unmapped neighbor of uu. Then m(w)m(w) is unmapped by MM, since mm is injective and agrees with MM on the mapped vertices, and m(w)m(w) is a neighbor of vv.

  • If w∈T1w \in T_1, ww has a mapped neighbor w′w', so m(w)m(w) is adjacent to M(w′)M(w') and m(w)∈T2m(w) \in T_2. Hence mm maps N1(u)∩T1N_1(u) \cap T_1 injectively into N2(v)∩T2N_2(v) \cap T_2, and ∣N1(u)∩T1∣≤∣N2(v)∩T2∣|N_1(u) \cap T_1| \le |N_2(v) \cap T_2| in every mode.
  • In iso and induced mode, if w∈N~1w \in \tilde N_1, then m(w)m(w) has no mapped neighbor: a mapped neighbor M(w′)M(w') of m(w)m(w) would make w′w' a mapped neighbor of ww. So m(w)∈N~2m(w) \in \tilde N_2, and ∣N1(u)∩N~1∣≤∣N2(v)∩N~2∣|N_1(u) \cap \tilde N_1| \le |N_2(v) \cap \tilde N_2|. This proves the induced rule.
  • In mono mode, mm maps N1(u)∖MN_1(u) \setminus M injectively into N2(v)∖MN_2(v) \setminus M, which proves the second mono rule.
  • In iso mode, mm is a bijection with m(T1)=T2m(T_1) = T_2 and m(N~1)=N~2m(\tilde N_1) = \tilde N_2: the argument above applied to m−1m^{-1}, which is an isomorphism extending M−1M^{-1}, gives the reverse inclusions. Since mm maps N1(u)N_1(u) onto N2(v)N_2(v), the counts are equal. □\square

Lemma 3 (the right candidate is present). Let MM extend to a target mm with ∣M∣<n1|M| < n_1, and let uu be the pattern vertex chosen in L3. Then (u,m(u))(u, m(u)) is in the candidate list.

Proof. If T1≠∅T_1 \ne \emptyset, then u∈T1u \in T_1, and by the first point of the proof of Lemma 2, m(u)∈T2m(u) \in T_2, so T2≠∅T_2 \ne \emptyset and (u,m(u))(u, m(u)) is listed. If T1=∅T_1 = \emptyset, uu is the smallest unmapped vertex. In mono and induced mode, every unmapped host vertex is listed, including m(u)m(u). In iso mode, mm is a bijection that maps T1T_1 onto T2T_2, so T2=∅T_2 = \emptyset as well, and all unmapped host vertices are listed. □\square

Theorem. VF2 terminates, and its outputs are exactly the targets, each output once.

Proof. Termination. Each step L6 increases ∣M∣|M|, which is at most n1n_1, and each state has a finite candidate list that is scanned once from left to right; so the search tree is finite, and the explicit stack visits each node once.

Soundness is Lemma 1.

Completeness. Let mm be a target. Start at M0=∅M_0 = \emptyset, which extends to mm. If MkM_k extends to mm and ∣Mk∣<n1|M_k| < n_1, Lemma 3 puts (uk,m(uk))(u_k, m(u_k)) in the list of MkM_k, and Lemma 2 shows that it passes FEASIBLE; so the search enters Mk+1=Mk∪{(uk,m(uk))}M_{k+1} = M_k \cup \{(u_k, m(u_k))\}, which also extends to mm. After n1n_1 steps L2 outputs mm.

Uniqueness. The choice of uu in L3 depends only on the state, and distinct candidates at one state have distinct host vertices vv, so distinct children of a node hold different values M(u)M(u). Two output leaves with the same map would agree on every pair along the path from the root and so be the same leaf. Hence each target is output once. ■\blacksquare

Back to VF2 algorithm