# Find closed loops in an undirected graph given an adjacency list

I am trying to find all the cycles in an undirected graph given the adjacency list of the vertices, with the an output of all the cycles in form of the vertices they are made up of.

For example -http://i.stack.imgur.com/Ku4fd.jpg

The example depicts the graph drawn by the adjacency list with desired cycles result

1 235
2 134
3 1245
4 235
5 134


The output from the algorithm would be

Cycles:
1 2 3
1 3 5
2 3 4
3 4 5


Note that I am not trying to find ALL possible cycles in the graph but rather all the loops. We assume for this problem that there any vertex is connected to at least 2 others

I am new to discrete maths, algorithms and graph theory, any help would be greatly appreciated.

• Does "loops" mean "sequences of vertices ​ v$_0$ v$_1$ v$_2$ ... v$_{L\hspace{.02 in}-1\hspace{-0.02 in}}$ v$_L$ ​ such that ​ [ v$_0$ = v$_L$ ​ and that sequence ​ ​ ​ ​ has no other repetitions and for all elements i of {0,1,2,3,...,L-2,L-1}, there is an edge from v$_i$ to v$_{i+1}$"$\hspace{.02 in}$? ​ ​ ​ ​ ​ ​ ​ ​​ – user12859 Apr 17 '16 at 19:22
• Yes, a loop is essentially a sequence of vertices that starts from vertex i and comes back to vertex i that does not enclose any other sequences starting and finishing at vi – Seb Hill Apr 17 '16 at 19:45
• (That's different from what I was guessing.) ​ Is "enclose" in the sense of a planar embedding, or just vi not also being an internal vertex? ​ ​ ​ ​ – user12859 Apr 17 '16 at 19:50
• It's not clear what you are asking for. In the first sentence, you say you want to find all cycles. Later you say you don't want to find all cycles; you want to find all loops. You never define what you mean by a loop. Please edit the question to provide a precise specification/definition of what you want and what you mean by a loop; as it stands, the question is not answerable, as it's not clear what you are looking for. – D.W. Apr 18 '16 at 5:10
• Also posted on SO. Please do not post the same question on multiple sites. Each community should have an honest shot at answering without anybody's time being wasted. – D.W. Apr 18 '16 at 5:14