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. "
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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. "
Название |
---|
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
Unable to parse markup [type=CF_MATHJAX]
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
Unable to parse markup [type=CF_MATHJAX]
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.