Cycle Counting
Counting Simple Cycles
Example 1: Codeforces Beta Round 11 D. A Simple Task
Given a simple graph, find the number of simple cycles in the graph. A simple cycle is a cycle with no repeated vertices or edges.
Number of vertices \(1\leq n\leq 19\).
Solution Idea
Consider state compression dynamic programming. Let \(f(s,i)\) denote the number of paths satisfying that the current set of visited vertices is \(s\), the current vertex is \(i\), and the first vertex is the smallest numbered vertex in the set \(s\).
For state \(f(s,i)\), enumerate the next vertex \(u\). If \(u\) is in set \(s\) and is the smallest numbered vertex (i.e., the starting point), add \(f(s,i)\) to the answer \(A\). If \(u\) is not in \(s\), add \(f(s\cup\{u\},u)\) to \(f(s,i)\).
This will count 2-cycles (i.e., parallel edges) as well, and each non-2-cycle will be counted twice (because fixing the starting point allows traversal in two directions). Thus, the answer is \(\dfrac{A-m}2\), where \(m\) is the number of edges. Time complexity is \(O(2^nm)\).
Example Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
Counting Triangles
A triangle in a simple graph \(G\) refers to an unordered triple \((u,\ v,\ w)\) such that there exist edges connecting \((u,\ v)\), \((v,\ w)\), and \((w,\ u)\). The triangle counting problem asks to compute the number of all triangles in the graph.
First, orient all edges. We stipulate that edges go from vertices with smaller degree to vertices with larger degree; if degrees are equal, go from the vertex with smaller index to the vertex with larger index. Then the resulting graph is a directed acyclic graph (DAG).
Proof that this graph has no cycles
Proof by contradiction. Assume a cycle exists. Then the degrees of vertices in the cycle increase one by one. To form a cycle, all vertices must have the same degree, but their indices must differ, leading to a contradiction.
Therefore, after orientation, the graph certainly contains no cycles.
In fact, the above orientation rule can be used to construct a partial order, so the graph constructed by this rule (i.e., the Hasse diagram of this partial order) must be a DAG.
Enumerate \(u\) and the vertices \(v\) pointed to by \(u\), then among the vertices pointed to by \(v\), enumerate \(w\), and check whether \(u\) is connected to \(w\).
The time complexity of this algorithm is \(O(m\sqrt m)\).
Proof of Time Complexity
For the orientation part, all edges are traversed, with time complexity \(O(n+m)\).
For each pair \((v,\ w)\), the number of possible \(u\) does not exceed the in-degree \(d^-(v)\) of \(v\).
If \(d^-(v)\leq\sqrt m\), since the number of \(w\) is at most \(n\), the time complexity for this part is \(O(n\sqrt m)\).
If \(d^-(v) > \sqrt m\), since \(v\) points to \(w\), we have \(d(v) \leq d(w)\), implying \(d(w) > \sqrt m\). However, the total number of edges is only \(m\), so the number of such \(w\) is at most \(\sqrt m\). Hence, the time complexity is \(O(m\sqrt m)\).
The total time complexity is \(O(n+m+n\sqrt m+m\sqrt m)=O(m\sqrt m)\).
In fact, if we orient edges from vertices with larger degree to vertices with smaller degree, the complexity remains correct; we only need to swap the roles of \(u\) and \(w\), and the above proof still holds.
Example Code (Luogu P1989 Counting Triangles in Undirected Graph)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
Example 2
HDU 6184 Counting Stars
Given an undirected graph with \(n\) vertices and \(m\) edges, find the number of occurrences of the following shape.
\(2\leq n\leq 10^5\), \(1\leq m\leq\min\left\{2\times 10^5,\ \dfrac{n(n-1)}2\right\}\).
Solution Idea
This shape is formed by two triangles sharing a common edge. So we first run triangle counting to compute the number of triangles containing each edge. Then, for each shared edge, if there are \(x\) triangles containing it, the contribution to the answer is \(\dbinom x2\).
Time complexity is \(O(m\sqrt m)\).
Example Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
Counting 4-Cycles
Similarly, a 4-cycle refers to four vertices \(a,\ b,\ c,\ d\) such that edges exist connecting \((a,\ b)\), \((b,\ c)\), \((c,\ d)\), and \((d,\ a)\).
Consider first sorting the vertices. Vertices with smaller degree come first; vertices with larger degree come later.
Consider enumerating the vertex \(a\) with the largest rank. Now, for each vertex \(c\) with a rank smaller than \(a\), we need to find how many vertices \(b\) with rank smaller than \(a\) satisfy that edges exist connecting \((a,\ b)\) and \((b,\ c)\). Then, any two such \(b\) can form a 4-cycle. The number of such \(b\) can be found by iterating over \(b\) and \(c\).
Note that the complexity of our enumeration is essentially equivalent to enumerating triangles, so the time complexity is also \(O(m\sqrt m)\) (assuming \(n\) and \(m\) are of the same order).
It is worth noting that \((a,\ b,\ c,\ d)\) and \((a,\ c,\ b,\ d)\) can be two different 4-cycles.
Also, vertices with the same degree are assigned different ranks, and we must ensure \(a\neq c\).
Example Code (LibreOJ P191 Counting 4-Cycles in Undirected Graph)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
Example 3
Gym 102028L Connected Subgraphs
Given an undirected graph with \(n\) vertices and \(m\) edges, find the number of connected subgraphs induced by four edges.
\(4\leq n\leq 10^5\), \(4\leq m\leq 2\times 10^5\).
Solution Idea
The cases can be divided into five types: star graph, 4-cycle, a triangle with an extra edge attached to one vertex, a chain of four vertices with an extra edge attached to the middle vertex, and a chain of five vertices.
For star graphs, directly enumerate vertex degrees and use combinations. For 4-cycles, directly use the above algorithm. For triangles, enumerate each triangle \((u,\ v,\ w)\); the contribution to the answer is \([d(u)-2]+[d(v)-2]+[d(w)-2]\).
Now consider the fourth case. Enumerate the vertex \(x\) with degree 2, then enumerate one of its neighbors \(y\) as the vertex with degree 3. The contribution to the answer is \([d(x)-1]\cdot\dbinom{d(y)-1}2\). However, note that \(y\)'s neighbors may coincide with \(x\)'s neighbors; this case is equivalent to the third case. But each such overcounted third case is counted twice (because there are two vertices of degree 3), so we should subtract twice the number of third cases.
For the last case, first enumerate the middle vertex \(x\). It is easy to see that the contribution to the answer is
Again, there are overcounted parts. Let \(s\) be a neighbor of \(y\), and \(t\) be a neighbor of \(z\). After analysis, the overcounted cases are as follows:
- When \(y\) coincides with \(t\) but \(s\) does not coincide with \(z\), it is equivalent to the third case;
- When \(s\) coincides with \(z\) but \(y\) does not coincide with \(t\), it is also equivalent to the third case;
- When both \(y\) coincides with \(t\) and \(s\) coincides with \(z\), it is equivalent to a triangle;
- When \(s\) coincides with \(t\), it is equivalent to a 4-cycle (the second case).
Considering that in the third case, the two vertices of degree 2 acting as \(x\) correspond exactly to the overcounted cases 1 and 2 above, we need to subtract twice the number of third cases additionally. For a triangle, all three vertices can act as \(x\), so it is overcounted 3 times. Similarly, a 4-cycle is overcounted 4 times.
Thus, we obtain the algorithm for all cases, with time complexity \(O(n+m\sqrt m)\).
Example Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
Exercises
Luogu P3547 [POI2013] CEN-Price List
CodeForces 985G Team Players (Inclusion-Exclusion Principle)
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用