Here is some code for computing k-cycles. The efficiency is no better than O(c*k*n) where c = #cycles and n = #vertices. In general it is probably worse, since it tries to build cycles from shorter paths and there might be many of those that eventually fail to become full cycles.
extendCycle[cyc_List, edges_List] :=
Map[If[# > First[cyc] && ! MemberQ[cyc, #], Append[cyc, #],
Null /. Null :> Sequence[]] &, edges[[Last[cyc]]]]
cycles[mat_, k_] :=
Module[{n = Length[mat], m2, cyc, cyclist},
m2 = MapIndexed[#1*#2[[2]] &, mat, {2}] /. 0 :> Sequence[];
cyclist = Flatten[Drop[MapIndexed[{#2[[1]], #1} &, m2, {2}], -k+1], 1];
Do[cyclist =
Flatten[Map[extendCycle[#, m2] &, cyclist], 1], {k - 2}];
Map[If[MemberQ[m2[[Last[#]]], First[#]], Append[#, First[#]],
Null /. Null :> Sequence[]] &, cyclist]
]
Here is an example. For a reasonably sparse graph it seems to work well. In this construction, "mat2" is the adjacency matrix we will use.
len = 20;
mat = RandomChoice[{.6, .4} -> {0, 1}, {len, len}];
Do[mat[[j, j]] = 0, {j, len}];
mat2 = Floor[(mat + Transpose[mat])/2];
First I show what this looks like, though in a sparse format. The first entry, {18,19}, means that vertex 1 is connected (only) to vertices 18 and 19.
mat3 = MapIndexed[#1*#2[[2]] &, mat2, {2}] /. 0 :> Sequence[]
Out[114]= {{18, 19}, {6, 7}, {9, 14, 15, 20}, {13, 20}, {6, 10, 11,
12, 14, 15, 20}, {2, 5, 8, 20}, {2, 14, 19}, {6}, {3, 12, 17},
{5, 14}, {5, 13, 14, 15}, {5, 9}, {4, 11}, {3, 5, 7, 10, 11, 18},
{3, 5, 11, 18}, {}, {9, 18, 19}, {1, 14, 15, 17}, {1, 7, 17}, {3, 4, 5, 6}}
Here is the result for finding all 6-cycles.
In[116]:= Timing[cycles[mat2, 6]]
Out[116]= {0.02, {{2, 6, 5, 10, 14, 7, 2}, {2, 6, 5, 11, 14, 7,
2}, {2, 6, 20, 3, 14, 7, 2}, {2, 6, 20, 5, 14, 7, 2}, {2, 7, 14, 3,
20, 6, 2}, {2, 7, 14, 5, 20, 6, 2}, {2, 7, 14, 10, 5, 6, 2}, {2,
7, 14, 11, 5, 6, 2}, {3, 9, 12, 5, 6, 20, 3}, {3, 9, 12, 5, 10, 14,
3}, {3, 9, 12, 5, 11, 14, 3}, {3, 9, 12, 5, 11, 15, 3}, {3, 9, 17,
19, 7, 14, 3}, {3, 14, 7, 19, 17, 9, 3}, {3, 14, 10, 5, 6, 20,
3}, {3, 14, 10, 5, 11, 15, 3}, {3, 14, 10, 5, 12, 9, 3}, {3, 14,
11, 5, 6, 20, 3}, {3, 14, 11, 5, 12, 9, 3}, {3, 14, 11, 13, 4, 20,
3}, {3, 14, 11, 15, 5, 20, 3}, {3, 14, 18, 15, 5, 20, 3}, {3, 15,
11, 5, 6, 20, 3}, {3, 15, 11, 5, 10, 14, 3}, {3, 15, 11, 5, 12, 9,
3}, {3, 15, 11, 13, 4, 20, 3}, {3, 15, 11, 14, 5, 20, 3}, {3, 15,
18, 14, 5, 20, 3}, {3, 20, 4, 13, 11, 14, 3}, {3, 20, 4, 13, 11,
15, 3}, {3, 20, 5, 14, 11, 15, 3}, {3, 20, 5, 14, 18, 15, 3}, {3,
20, 5, 15, 11, 14, 3}, {3, 20, 5, 15, 18, 14, 3}, {3, 20, 6, 5, 10,
14, 3}, {3, 20, 6, 5, 11, 14, 3}, {3, 20, 6, 5, 11, 15, 3}, {3,
20, 6, 5, 12, 9, 3}, {4, 13, 11, 5, 6, 20, 4}, {4, 13, 11, 14, 5,
20, 4}, {4, 13, 11, 15, 5, 20, 4}, {4, 20, 5, 14, 11, 13, 4}, {4,
20, 5, 15, 11, 13, 4}, {4, 20, 6, 5, 11, 13, 4}, {5, 10, 14, 18,
15, 11, 5}, {5, 11, 15, 18, 14, 10, 5}, {5, 12, 9, 17, 18, 14,
5}, {5, 12, 9, 17, 18, 15, 5}, {5, 14, 18, 17, 9, 12, 5}, {5, 15,
18, 17, 9, 12, 5}, {10, 5, 11, 15, 18, 14, 10}}}
Here is a denser example. In this instance, the sparse form of the adjacency matrhx is as below.
{{5, 6, 7, 10, 12, 13, 17, 19}, {3, 6, 9, 13, 14, 16, 17, 20}, {2, 4,
5, 6, 7, 8, 11}, {3, 9, 12, 13, 14, 15, 17, 19}, {1, 3, 7, 11, 12,
13, 15, 20}, {1, 2, 3, 8, 14, 16, 17, 18, 19, 20}, {1, 3, 5, 8, 9,
12, 16, 18}, {3, 6, 7, 9, 12, 13, 14, 16, 17, 18, 19}, {2, 4, 7, 8,
17}, {1, 11, 13}, {3, 5, 10, 14, 18}, {1, 4, 5, 7, 8}, {1, 2, 4, 5,
8, 10, 14, 15, 18, 20}, {2, 4, 6, 8, 11, 13, 16, 19}, {4, 5, 13, 17,
18}, {2, 6, 7, 8, 14, 17, 18, 19}, {1, 2, 4, 6, 8, 9, 15, 16,
19}, {6, 7, 8, 11, 13, 15, 16, 20}, {1, 4, 6, 8, 14, 16, 17,
20}, {2, 5, 6, 13, 18, 19}}
In[124]:= Timing[Length[cycles[mat2, 6]]]
Out[124]= {0.55, 17105}