How can we detect a simple cycle in a graph ?
Simple Cycle : "Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1. "
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 170 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
5 | -is-this-fft- | 158 |
7 | adamant | 154 |
7 | Dominater069 | 154 |
9 | awoo | 153 |
10 | luogu_official | 152 |
How can we detect a simple cycle in a graph ?
Simple Cycle : "Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1. "
Name |
---|
How to "detect" a simple cycle is actually quite easier than how to "find" one.
Consider any connected component of a graph. Let the size of the connected component be N and the number of edges in it be M. The inequality $$$N-1 \le M$$$ must hold, because a graph with N-1 edges is a tree, and removing any edge from a tree causes it to be disconnected.
I claim that any connected component that satisfies $$$M \ge N$$$ has a loop. Proof: Consider the spanning tree of said connected component. This spanning tree only uses up $$$N-1$$$ of the M edges. Since adding any edge to a tree creates a loop, there is at least one loop in this connected component.
Okk . Got it . Thanks a lot .
To implement that, it is usually easiest to use a disjoint set and just check if the two positions were already connected. If there were, you have a cycle.