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 | 3856 |
2 | jiangly | 3747 |
3 | orzdevinwang | 3706 |
4 | jqdai0815 | 3682 |
5 | ksun48 | 3591 |
6 | gamegame | 3477 |
7 | Benq | 3468 |
8 | Radewoosh | 3462 |
9 | ecnerwala | 3451 |
10 | heuristica | 3431 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | -is-this-fft- | 162 |
3 | Dominater069 | 160 |
4 | Um_nik | 158 |
5 | atcoder_official | 157 |
6 | Qingyu | 156 |
7 | adamant | 151 |
7 | djm03178 | 151 |
7 | luogu_official | 151 |
10 | awoo | 146 |
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.