2
$\begingroup$

This is regarding the complexity of an algorithm for counting triangles in an undirected graph which was suggested in a document I came across.

(Link - https://www.cs.cmu.edu/~15750/notes/lec1.pdf)

Suppose the graph has $m$ edges and $n$ vertices. The algorithm goes as follows: sort the vertices in increasing order of degree ($v_1 \cdots v_n$) and scan them beginning at the lowest degree vertex. When you are at vertex $v_i$, only scan pairs of edges (which could potentially be triangles) of the form $(\{v_i,v_j\},\{v_i, v_k\})$ where $j,k>i$. The document says that the time complexity of this algorithm is $O(m^{3/2})$. There’s no proof in the document and I’ve been trying to prove it but have not been able to. How do you show this?

$\endgroup$
2
  • 1
    $\begingroup$ Strictly speaking, sorting the vertices takes $O(n\log n)$ time, which can’t be bounded in terms of $m$. What you need to do to get the claimed time complexity is determine the degrees of the vertices by iterating over the edges, maintain a list of only the vertices with non-zero degree and sort only those; their number is $O(m)$. $\endgroup$
    – joriki
    Commented Mar 8 at 14:21
  • 1
    $\begingroup$ Also, this sentence in the text you linked to is misleading: “For the first improvement, observe that Algorithm $2$ above counts each triangle multiple times, once from each of its vertices.” That mischaracterizes the reason for the improvement. Counting each triangle once instead of three times can’t yield an improvement in the complextiy class; it can at most reduce the constant of proportionality by a factor $3$. $\endgroup$
    – joriki
    Commented Mar 8 at 14:23

1 Answer 1

5
$\begingroup$

Consider this simpler algorithm:

Pick an $\alpha \in \mathbb{R}_{>0}$ and partition the vertices of $G$ in two sets $A = \{u: d_u \leq a\}$ and $B=\{u: d_u > a\}$. Perform the following two subroutines:

  • Iterate through all triplets $u, v, w \in B$ and check if they form a triangle
  • For each $u\in A$ check for all pairs of neighbors $v, w$ if $\{u,v,w\}$ forms a triangle

The time complexity will be $$|B|^3 + \sum_{u\in A}\binom{d_u}{2} \leq |B|^3+\sum_{u\in A}d_u^2\leq |B|^3+\alpha\sum_{u\in V}d_u=|B|^3+2\alpha m$$

Furthermore, notice that $|B|\cdot \alpha \leq \sum_{u\in B}d_u\leq 2m$ and so $|B|\leq \frac{2m}{a}$. Choosing $\alpha = O(\sqrt{m})$ gives the desired complexity.

This logic is easily incorporated in the modified algorithm at hand. It is worth mentioning though that for this algorithm to work you require access both to the adjacency list of the graph as well as an oracle that tells you whether any edge $\{u,v\}$ exists in $O(1)$ time.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .