Let G=⟨V,E⟩ be an undirected graph with ∣V∣=n vertices and ∣E∣=m edges.
The number of triangle in G is denoted as Δ.
For each v∈V, let N(v)≐{u:(v,u)∈E} be the set of neighbors of v.
The degree of v is dv≐∣N(v)∣.
Define λ(v) be the number of triangles whose vertices contains v.
Similarly, for each e=(u,v)∈E, define λ(e) the number of triangles whose edges contains e.
Exact Algorithm
Theorem. The number of triangles in G can be computed in O(m1.5) time.
Proof.
WLOG, we assume that the vertices in V are labelled from 1 to n in increasing order according to their degrees, such that
d1≤d2≤...≤dn.
Each triangle in G can be represented by a unique triple (u,v,w)∈[n]3 such that du≤dv≤dw.
To count the number of such triples, we can execute the following algorithm.
For each u∈[n]do Forv∈N(u) of u s.t. dv≥dudo Forw∈N(u) of u s.t. dw≥dvdo Check whether (u,v,w) is a triangle.
It is left to prove that the algorithm has complexity O(m1.5).
Lemma. For each (u,v)∈E, such that du≤dv, the number of w∈N(u) such that dw≥dv is bounded by O(m).
The claim is trivial if du≤m.
Otherwise, suppose that du≥m.
But the number of w∈V such that dw≥du≥m is bounded by O(m/m)=O(m).
□
Sampling Algorithms
Subgraph Sampling
Sample each edge independently with some probability p.
Count the number of triangles, Δ~, in the sampled subgraph.
X←(1/p3)⋅Δ~.
Properties
Theorem (Unbiased Estimator).X is an unbiased estimator of Δ.
Proof.
For each triangle in G, define the indicator random variable Z for its inclusion in teh sampled subgraph.
Then E[Z]=p3.
Let S be the collection of such random variable.
By linearity of expectation.
E[Z∈S∑Z]=Z∈S∑E[Z]=p3⋅Δ.
It follows that (1/p3)∑Z∈SZ is an unbiased estimator of Δ.
□
Let ΔN be the number of triangles that share an edge.
Theorem (Variance).X has variance
(1/p6)⋅(Δ⋅(p3−p6)+2⋅ΔN⋅(p5−p6)).
Proof.
Var[p31Z∈S∑Z]=p61⋅Z∈S∑Var[Z]+Z,Z′∈S∑Cov[Z,Z′]=p61⋅Z∈S∑Var[Z]+Z,Z′∈S∧Z shares an edge with Z′∑Cov[Z,Z′]=p61⋅Z∈S∑(p3−p6)+Z,Z′∈S∧Z shares an edge with Z′∑(p5−p6)=p61⋅(Δ⋅(p3−p6)+2⋅ΔN⋅(p5−p6)).
□
Vertex Sampling
Sample a vertex v uniformly at random from V.
Compute λ(v).
X←(n/3)⋅λ(v).
Properties
Theorem (Unbiased Estimator).X is an unbiased estimator of Δ.
Proof.
E[X]=3n⋅v∈V∑n1⋅λ(v)=31⋅v∈V∑λ(v)=Δ.
□
Theorem (Variance).X has variance ((n/9)⋅∑v∈Vλ(v)2)−Δ2.
Proof.
Var[X]=E[X2]−E[X]2=(9n⋅v∈V∑λ(v)2)−Δ2.
□
Edge Sampling
Sample an edge e uniformly at random from E.
Compute λ(e).
X←(m/3)⋅λ(e).
Properties
Theorem (Unbiased Estimator).X is an unbiased estimator of Δ.
Proof.
E[X]=3m⋅e∈E∑m1⋅λ(e)=31⋅e∈E∑λ(e)=Δ.
□
Theorem (Variance).X has variance ((m/9)⋅∑e∈Eλ(v)2)−Δ2.
Proof.
Var[X]=E[X2]−E[X]2=(9m⋅e∈E∑λ(e)2)−Δ2.
□
Triangle Sampling
Sample an edge e=(u,v) uniformly at random from E.
Sample a vertex uniformly at random from {N(u),N(v),w.p.w.p.du/(du+dv),dv/(du+dv).
I←{1,0,if(u,v,w) constitutes a triangle,otherwise.
X←(m/6)⋅(du+dv)⋅I.
Properties
Theorem (Unbiased Estimator).X is an unbiased estimator of Δ.
A wedge is a triple (u,v,w)∈V3 such that (u,v),(v,w)∈E.
If a wedge is closed, i.e., (v,w)∈E, then it constitutes a triangle.
Note that each triangle contains 3 wedges.
Denote W the number of wedges in G.
Sample a wedge (u,v,w) uniformly at random.
I←{1,0,if(u,v,w) constitutes a triangle,otherwise.
X←(W/3)⋅I.
Properties
Theorem (Unbiased Estimator).X is an unbiased estimator of Δ.
Proof.
E[I]=W3⋅Δ.
Therefore, X=(W/3)⋅I is an unbiased estimator of Δ.