| 1 | initial version |
You can also use method all_simple_paths, and there si no need for removing v from the graph
sage
def FindCycle(G, v, cycle_len):
for cycle in G.all_simple_paths(starting_vertices=[v], ending_vertices=[v], max_length=cycle_len):
if len(cycle) == cycle_len + 1:
return cycle
return None
For instance
sage
sage: FindCycle(graphs.CompleteGraph(4), 0, 3)
[0, 1, 2, 0]
It remains to deal with the special cases cycle_len == 1 which can happen if the graph has loops and the case cycle_len == 2 for which the proposed method returns a non simple cycle (e.g., [0, 1, 0]),
| 2 | No.2 Revision |
You can also use method all_simple_paths, and there si no need for removing v from the graph
graph
sage
def FindCycle(G, v, cycle_len):
for cycle in G.all_simple_paths(starting_vertices=[v], ending_vertices=[v], max_length=cycle_len):
if len(cycle) == cycle_len + 1:
return cycle
return None
For sage
sage: FindCycle(graphs.CompleteGraph(4), 0, 3)
[0, 1, 2, 0]
It remains to deal with the special cases cycle_len == 1 which can happen if the graph has loops and the case cycle_len == 2 for which the proposed method returns a non simple cycle (e.g., [0, 1, 0]),
Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.