# Flag algebra

Flag algebras are an important computational tool in the field of graph theory which have a wide range of applications in homomorphism density and related topics. Roughly, they formalize the notion of adding and multiplying homomorphism densities and set up a framework to solve graph homomorphism inequalities with computers by reducing them to semidefinite programming problems. Originally introduced by Alexander Razborov in a 2007 paper, the method has since come to solve numerous difficult, previously unresolved graph theoretic questions. These include the $D_{2,3}$ question regarding the region of feasible edge density, triangle density pairs and the maximum number of pentagons in triangle free graphs.

## Motivation

The motivation of the theory of flag algebras is credited to John Adrian Bondy and his work on the Caccetta-Haggkvist conjecture, where he illustrated his main ideas via a graph homomorphism flavored proof to Mantel's Theorem. This proof is an adaptation on the traditional proof of Mantel via double counting, except phrased in terms of graph homomorphism densities and shows how much information can be encoded with just density relationships.
   Theorem (Mantel): The edge density in a triangle-free graph $G$ is at most ${\frac {1}{2}}$. In other words, $\mathrm {ex} (K_{2},\{K_{3}\})\leq {\frac {1}{2}}.$
As the graph is triangle-free, among 3 vertices in $G$, they can either form an independent set, a single induced edge ${\tilde {P_{3}}}$, or a path of length 2 $P_{3}$. Denoting $d(H,G)$ as the induced density of a subgraph $H$ in $G$, double counting gives:
   ${\binom {n}{3}}\left(d({\tilde {P_{3}}},G)+2d(P_{3},G)\right)={\binom {n}{2}}(n-2)d(K_{2},G)\implies d({\tilde {P_{3}}},G)+2d(P_{3},G)=3d(K_{2},G).$
Intuitively, $d(P_{3},G)\approx 3d(K_{2},G)^{2}$ since a $P_{3}$ just consists of two $K_{2}$s connected together, and there are 3 ways to label the common vertex among a set of 3 points. In fact, this can be rigorously proven by double counting the number of induced $P_{3}$s. Letting $|G|$ denote the number of vertices of $G$, we have:
   $d(P_{3},G)={\binom {|G|}{3}}^{-1}\sum _{v\in V(G)}{\binom {d(v)}{2}}={\binom {|G|}{3}}^{-1}{\binom {|G|-1}{2}}\sum _{v\in V(G)}d(P_{3}^{b},G^{v})={\frac {3}{|G|}}\sum _{v\in V(G)}d(P_{3}^{b},G^{v}),$
where $P_{3}^{b}$ is the path of length 2 with its middle vertex labeled, and $d(P_{3}^{b},G^{v})$ represents the density of $P_{3}^{b}$s subject to the constraint that the labeled vertex is used, and that $P_{3}^{b}$ is counted as a proper induced subgraph only when its labeled vertex coincides with $v$. Now, note that $d(P_{3}^{b},G^{v})\approx d(K_{2}^{b},G^{v})^{2}$ since the probability of choosing two $K_{2}^{b}$s where the unlabeled vertices coincide is small (to be rigorous, a limit as $|G|\to \infty$ should be taken, so $d(H,G)$ acts as a limit function on a sequence of larger and larger graphs $G$. This idea will be important for the actual definition of flag algebras.) To finish, apply the Cauchy–Schwarz inequality to get
   $\sum _{v\in V(G)}d(P_{3}^{b},G^{v})\approx \sum _{v\in V(G)}d(K_{2}^{b},G^{v})^{2}\geq {\frac {1}{|G|}}\left(\sum _{v\in V(G)}d(K_{2}^{b},G^{v})\right)^{2}={\frac {1}{|G|}}\left({\frac {1}{|G|-1}}{\binom {|G|}{2}}2d(K_{2},G)\right)^{2}=|G|d(K_{2},G)^{2}.$
Plugging this back into our original relation proves what was hypothesized intuitively. Finally, note that $d({\tilde {P_{3}}},G)\geq 0$ so
   $6d(K_{2},G)^{2}\leq 2d(P_{3},G)\leq 3d(K_{2},G)\implies d(K_{2},G)\leq {\frac {1}{2}}.$
The important ideas from this proof which will be generalized in the theory of flag algebras are substitutions such as $d(P_{3},G)\to 3d(K_{2},G)^{2}$, the use of labeled graph densities, considering only the "limit case" of the densities, and applying Cauchy at the end to get a meaningful result.

## Definition

Fix a collection of forbidden subgraphs ${\mathcal {H}}$ and consider the set of graphs ${\mathcal {G}}$ of ${\mathcal {H}}$-free graphs. Now, define a type of size $k$ to be a graph $\sigma \in {\mathcal {G}}$ with labeled vertices $V(\sigma )=[k]$. The type of size 0 is typically denoted as $\varnothing$.
First, we define a $\sigma$-flag, a partially labeled graph which will be crucial for the theory of flag algebras:
   Definition: A $\sigma$-flag is a pair $(F,\theta )$ where $F\in {\mathcal {G}}$ is an underlying, unlabeled, ${\mathcal {H}}$-free graph, while $\theta :[k]\to V(F)$ defines a labeled graph embedding of $\sigma$ onto the vertices $F$.
Denote the set of $\sigma$-flags to be ${\mathcal {F}}^{\sigma }$ and the set of $\sigma$-flags of size $n$ to be ${\mathcal {F}}_{n}^{\sigma }$. As an example, $P_{3}^{b}$ from the proof of Mantel's Theorem above is a $\sigma$-flag where $\sigma$ is a type of size 1 corresponding to a single vertex.
For $\sigma$-flags $F_{1},F_{2},\ldots ,F_{t},(G,\theta )$ satisfying $|G|-|\sigma |\geq \sum _{i=1}^{t}|F_{i}|-|\sigma |$, we can define the density of the $\sigma$-flags onto the underlying graph $G$ in the following way:
   Definition: The density $p(F_{1},F_{2},\ldots ,F_{t};G)$ of the $\sigma$-flags $F_{1},\ldots ,F_{t}$ in $G$ is defined to be the probability of successfully randomly embedding $F_{1},\ldots ,F_{t}$ into $V(G)$ such that they are nonintersecting on $V(G)\setminus \mathrm {im} (\theta )$ and are all labeled in the exact same way as $G$ on $\mathrm {im} (\theta )$.
   More precisely, choose pairwise disjoint $U_{1},U_{2},\ldots ,U_{t}\subseteq V(G)\setminus \mathrm {im} (\theta )$ at random and define $p(F_{1},\ldots ,F_{t};G)$ to be the probability that the $\sigma$-flag $(G[U_{i}\cup \mathrm {im} (\theta )],\theta )$ is isomorphic to $F_{i}$ for all $i\in [t]$.
Note that, when embedding $F$ into $G$, where $F,G$ are $\sigma$-flags, it can be done by first embedding $F$ into a $\sigma$-flag $F'$ of size $n\in [|F|,|G|]$ and then embedding $F'$ into $G$, which gives the formula: $p(F;G)=\sum _{F'\in {\mathcal {F}}_{n}^{\sigma }}p(F;F')p(F';G)$. Extending this to sets of $\sigma$-flags gives the Chain Rule:
   Theorem (Chain Rule): If $F_{1},F_{2},\ldots ,F_{t},G$ are $\sigma$-flags, $s,n$ are naturals such that $F_{1},\ldots ,F_{t}$ fit in $G$, $F_{1},\ldots ,F_{s}$ fit in a $\sigma$-flag of size $n$, and a $\sigma$-flag of size $n$ combined with $F_{s+1},\ldots ,F_{t}$ fit in $G$, then
      $p(F_{1},F_{2},\ldots ,F_{t};G)=\sum _{F\in {\mathcal {F}}_{n}^{\sigma }}p(F_{1},\ldots ,F_{s};F)p(F,F_{s+1},\ldots ,F_{t};G)$
Recall that the previous proof for Mantel's involved linear combinations of terms of the form $d(H,G)$. The relevant ideas were slightly imprecise with letting $|G|$ tend to infinity, but explicitly there is a sequence $G_{1},G_{2},\ldots$ such that $d(H,G_{i})$ converges to some $\phi (H)$ for all $H$, where $\phi$ is called a limit functional. Thus, all references to $d(H,G)$ really refer to the limit functional. Now, graph homomorphism inequalities can be written as linear combinations of $\phi$ with different $H$s, but it would be convenient to express them as a single term. This motivates defining $\mathbb {R} {\mathcal {F}}^{\sigma }$, the set of formal linear combinations of $\sigma$-flags over $\mathbb {R}$, and now $\phi$ can be extended to a linear function over $\mathbb {R} {\mathcal {F}}^{\sigma }$.
However, using the full space $\mathbb {R} {\mathcal {F}}^{\sigma }$ is wasteful when investigating just limit functionals, since there exist nontrivial relations between densities of certain $\sigma$-flags. In particular, the Chain Rule shows that
   $\phi (F)=\phi \left(\sum _{F'\in {\mathcal {F}}_{n}^{\sigma }}p(F;F')F'\right)\implies F-\sum _{F'\in {\mathcal {F}}_{n}^{\sigma }}p(F;F')F'\in \ker \phi$
is always true. Rather than dealing with all of these elements of the kernel, let the set of expressions of the above form (i.e. those obtained from Chain Rule with a single $\sigma$-flag) as ${\mathcal {K}}^{\sigma }$ and quotient them out in our final analysis. These ideas combine to form the definition for a flag algebra:
   Definition (Flag Algebras): A flag algebra is defined on the space of linear combinations of $\sigma$-flags ${\mathcal {A}}^{\sigma }=\mathbb {R} {\mathcal {F}}^{\sigma }/{\mathcal {K}}^{\sigma }$ equipped with bilinear operator
      $F\cdot G=\sum _{H\in {\mathcal {F}}_{n}^{\sigma }}p(F,G;H)H$
   for $F,G\in {\mathcal {F}}^{\sigma }$ and any natural $n$ such that $F,G$ fit in a $\sigma$-flag of size $n$, extending the operator linearly to $\mathbb {R} {\mathcal {F}}^{\sigma }$.
It remains to check that the choice of $n$ does not matter for a pair $F,G$ provided it is large enough (this can be proven with Chain Rule) as well as that if $f\in {\mathcal {K}}^{\sigma }$ then $f\cdot g\in {\mathcal {K}}^{\sigma }$, meaning that the operator respects the quotient and thus forms a well-defined algebra on the desired space.
One important result of this definition for the operator is that multiplication is respected on limit functionals. In particular, for a limit functional $\phi$, the identity $\phi (f\cdot g)=\phi (f)\cdot \phi (g)$ holds true. For example, it was shown that $\phi (P_{3}^{b})=\phi (K_{2}^{b})^{2}$ in our proof for Mantel's, and this result is just a corollary of this statement. More generally, the fact that $\phi$ is multiplicative means that all limit functionals are algebra homomorphisms between ${\mathcal {A}}^{\sigma }$ and $\mathbb {R}$.

## The downward operator

The definition above provides a framework for dealing with $\sigma$-flags, which are partially labeled graphs. However, most of the time, unlabeled graphs, or $\varnothing$-flags, are of greatest interest. To get from the former to the latter, define the downward operator.
The downward operator is defined in the most natural way: given a $\sigma$-flag $F$, let $\downarrow \!\!F$ to be the $\varnothing$-flag resulting from forgetting the labels assigned to $\sigma$. Now, to define a natural mapping between $\sigma$-flags and unlabeled graphs, let $q_{\sigma }(F)$ be the probability that an injective map $\theta :[k]\to V(F)$ taken at random has image isomorphic to $\sigma$, and define $[\![F]\!]_{\sigma }=q_{\sigma }(F)\downarrow \!\!F$. Extending $[\![\cdot ]\!]$ linearly to $\mathbb {R} {\mathcal {F}}^{\sigma }$ gives a valid linear map which sends combinations of $\sigma$-flags to combinations of unlabeled ones.
The most important result regarding $[\![\cdot ]\!]$ is its averaging properties. In particular, fix a $\sigma$-flag $F$ and unlabeled graph $G$ with $|G|\geq |F|$, then choosing an embedding $\theta$ of $\sigma$ on $G$ at random defines random variable $p(F;(G,\theta ))$. It can be shown that
   $\mathbb {E} [p(F;(G,\theta )]={\frac {q_{\sigma }(F)p(\downarrow \!\!F;G)}{q_{\sigma }(\sigma )p(\downarrow \!\!\sigma ;G)}}.$

## Optimization with flag algebras

All linear functionals, $\phi$ are algebra homomorphisms $\phi :{\mathcal {A}}^{\sigma }\to \mathbb {R}$. Furthermore, by definition, $\phi (F)\geq 0$ for any $\sigma$-flag $F$ since $\phi$ represents a density limit. Thus, say that a homomorphism $\psi \in \operatorname {Hom} ({\mathcal {A}}^{\sigma },\mathbb {R} )$ is positive if and only if $\psi (F)\geq 0\forall F\in {\mathcal {F}}^{\sigma }$, and let $\operatorname {Hom} ^{+}(A^{\sigma },\mathbb {R} )$ be the set of positive homomorphisms. One can show that the set of limit functionals $\Phi$ is exactly the set of positive homomorphisms $\operatorname {Hom} ^{+}(A^{\sigma },\mathbb {R} )$, so it suffices to understand the latter definition of the set.
In order for a linear combination $f\in {\mathcal {A}}^{\sigma }$ to yield a valid graph homomorphism inequality, it needs to be nonnegative over all possible linear functionals, which will then imply that it is true for all graphs. With this in mind, define the semantic cone of type $\sigma$, a set ${\mathcal {S}}^{\sigma }\subset {\mathcal {A}}^{\sigma }$ such that
   ${\mathcal {S}}^{\sigma }=\{f\in {\mathcal {A}}^{\sigma }\mid \phi (f)\geq 0\,\,\,\forall \phi \in \operatorname {Hom} ^{+}({\mathcal {A}}^{\sigma },\mathbb {R} )\}.$
Once again, $\sigma =\varnothing$ is the case of most interest, which corresponds to the case of unlabeled graphs. However, the downward operator has the property of mapping ${\mathcal {S}}^{\sigma }$ to ${\mathcal {S}}^{\varnothing }$, and it can be shown that the image of ${\mathcal {S}}^{\sigma }$ under $[\![\cdot ]\!]_{\sigma }$ is a subset of ${\mathcal {S}}^{\varnothing }$, meaning that any results on the type $\sigma$ semantic cone readily generalize to unlabeled graphs as well.
Just by naively manipulating elements of ${\mathcal {A}}^{\sigma }$, numerous elements of the semantic cone ${\mathcal {S}}^{\sigma }$ can be generated. For example, since elements of $\operatorname {Hom} ^{+}({\mathcal {A}}^{\sigma },\mathbb {R} )$ are nonnegative for $\sigma$-flags, any conical combination of elements of ${\mathcal {F}}^{\sigma }$ will yield an element of ${\mathcal {S}}^{\sigma }$. Perhaps more non-trivially, any conical combination of squares of elements of $A^{\sigma }$ will also yield an element of the semantic cone.
Though one can find squares of flags which sum to nontrivial results manually, it is often simpler to automate the process. In particular, it is possible to adapt the ideas in sum-of-squares optimization for polynomials to flag algebras. Define the degree of a vector $f\in \mathbb {R} {\mathcal {F}}^{\sigma }$ to be the largest flag with nonzero coefficient in the expansion of $f$, and let the degree of $f^{*}\in {\mathcal {A}}^{\sigma }$ to be the minimum degree of a vector over all choices in $f^{*}+{\mathcal {K}}^{\sigma }$. Also, define $v_{\sigma ,n}={\mathcal {F}}_{n}^{\sigma }\to {\mathcal {A}}^{\sigma }$ as the canonical embedding sending $F$ to itself for all $F\in {\mathcal {F}}_{n}^{\sigma }$. These definitions give rise to the following flag-algebra analogue:
   Theorem: Given $f\in {\mathcal {A}}^{\sigma }$, $n\geq |\sigma |$, then there exist $g_{1},\ldots ,g_{t}\in {\mathcal {A}}^{\sigma }$ for some $t\geq 1$ if and only if there is a positive semidefinite matrix $Q:{\mathcal {F}}_{n}^{\sigma }\times {\mathcal {F}}_{n}^{\sigma }\to \mathbb {R}$ such that $f=v_{\sigma ,n}^{\intercal }Qv_{\sigma ,n}$.
With this theorem, graph homomorphism problems can now be relaxed into semidefinite programming ones which can be solved via computer. For example, Mantel's Theorem can be rephrased as finding the smallest $\lambda \in \mathbb {R}$ such that $\lambda \varnothing -K_{2}\in S^{\varnothing }$. As $S^{\varnothing }$ is poorly understood, it is difficult to make progress on the question in this form, but note that conic combinations of $\varnothing$-flags and squares of vectors lie in $S^{\varnothing }$, so instead take a semidefinite relaxation. In particular, minimize $\lambda$ under the constraint that $\lambda \varnothing -K_{2}=r+[\![v_{1,2}^{\intercal }Qv_{1,2}]\!]$ where $r$ is a conic combination of $\varnothing$-flags and $Q$ is positive semi-definite. This new optimization problem can be transformed into a semidefinite-programming problem which is then solvable with standard algorithms.

## Generalizations

The method of flag algebras readily generalizes to numerous graph-like constructs. As Razborov wrote in his original paper, flags can be described with finite model theory instead. Instead of graphs, models of some nondegenerate universal first-order theory $T$ with equality in a finite relational signature $L$ with only predicate symbols can be used. A model $M$, which replaces our previous notion of a graph, has ground set $V(M)$, whose elements are called vertices.
Now, defining sub-models and model embeddings in an analogous way to subgraphs and graph embeddings, all of the definitions and theorems above can be nearly directly translated into the language of model theory. The fact that the theory of flag algebras generalizes well means that it can be used not only to solve problems in simple graphs, but also similar constructs such as, but not limited to, directed graphs and hypergraphs.