2
$\begingroup$

I'm looking for an algorithm which just counts the number of simple and distinct 4-cycles in an undirected graph labelled with integer keys. I don't need it to be optimal because I only have to use it as a term of comparison.

Thank you in advance.

EDIT: I realize I only have to count true 4-cycles, which can be seen as empty squares. In other words, the two opposite vertices of the cycle have not to be connected. I think this constraint would achieve a higher research interest.

$\endgroup$
1
  • 1
    $\begingroup$ You should be more specific on how non-optimal it can be. The stupidest algorithm is to list all quadruples of edges and count those that are cycles. $\endgroup$ Commented Jan 18, 2014 at 20:32

4 Answers 4

3
$\begingroup$

In an undirected graph with $m$ edges there can be as many as $\Theta(m^2)$ simple 4-cycles, so that's a reasonable time bound to aim for. And it's easy enough to achieve: set up a data structure that can test adjacency in constant time (e.g. a hash table or array indexed by pairs of vertices) and then, for each pair of oriented edges $uv$ and $xy$ perform the following two tests:

  • Are $uv$ and $xy$ two opposite edges in a 4-cycle? That is, are $vx$ and $yu$ also edges?

  • Is this a duplicate of another 4-cycle that we might list at another point in the algorithm? One way to prevent this is to only allow pairs of edges for which $u$ has the minimum index among all four vertices and $v$ has an index smaller than the index of $y$.

If it passes both tests, output it.

$\endgroup$
3
  • $\begingroup$ I think only the first condition is enough i.e; š¯‘¢ has the minimum index among all four vertices. It can be proved by contradiction method. $\endgroup$ Commented Sep 1, 2022 at 3:18
  • $\begingroup$ You need to also prevent the same cycle from being found from the pair (uy,xv). That's what the second check is for. $\endgroup$ Commented Oct 11, 2022 at 5:48
  • $\begingroup$ Got it, THanks! $\endgroup$ Commented Oct 11, 2022 at 16:34
3
$\begingroup$

Here is an $O(VE)$ algorithm. The number of 4-cycles is $$\frac{1}{2}\sum_{\lbrace v,w\rbrace} \binom{c(v,w)}{2},$$ where the sum is over the $\binom{V}{2}$ pairs of vertices and $c(v,w)$ is the number of common neighbours of $v$ and $w$. For particular $v,w$, $c(v,w)$ can be found by scanning the neighbour lists of the $v$ and $w$ once each. In total each neighbour list is scanned $O(V)$ times.

$\endgroup$
3
  • $\begingroup$ Thank you, your algorithm works fine and in a very reasonable time. The only problem is that I realize I only need to count true 4-cycles, which we can see them to be isomorphic to empty squares. In other words, I have to count 4-cycles where the two opposite vertices are not connected. Is there a way to modify your algorithm to respect this constraint? $\endgroup$
    – Dree
    Commented Jan 19, 2014 at 10:49
  • $\begingroup$ Btw, those would usually be called induced 4-cycles. $\endgroup$ Commented Jan 19, 2014 at 23:36
  • $\begingroup$ Also called holes of length 4. $\endgroup$ Commented Jan 19, 2014 at 23:47
2
$\begingroup$

This is answered in great generality by Alon/Yuster/Zwick.

$\endgroup$
3
  • $\begingroup$ As one would expect from the author list, it is a brilliant piece of theory. But I'm not sure if it would be practical. $\endgroup$ Commented Jan 19, 2014 at 0:09
  • 1
    $\begingroup$ @BrendanMcKay PRACTICAL you want! Geez... $\endgroup$
    – Igor Rivin
    Commented Jan 19, 2014 at 0:52
  • $\begingroup$ that's what the OP asked for, I think. $\endgroup$ Commented Jan 19, 2014 at 6:00
0
$\begingroup$

For a less sophisticated (but more practical) answer than my other answer: The trace of $A(G)^4$ counts the number of $4$-cycles. It counts every simple cycle eight times (once for each basepoint, and once for each direction). It counts every quadruply covered edge twice (once for each endpoint). It counts each four-cycle of the form $ABCBA$ four times. Now, since the number of edges is easy to compute, and the number of the last kind of four-cycles is just equal to $\sum d(v) (d(v) - 1),$ your number of four-cycles is not hard to back out (and the complexity is $O(V(G)^\omega),$ just like Alon, Yuster, Zwick!

$\endgroup$

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