def _get_cycles(graph_dict, path, visited, result, vertice):
 if (vertice in path):
  cycle = [vertice]
  for node in path[::(-1)]:
   if (node == vertice):
    break
   cycle.insert(0, node)
  start_from = min(cycle)
  index = cycle.index(start_from)
  cycle = (cycle[index:] + cycle[0:index])
  if (not (cycle in result)):
   result.append(cycle)
  return
 path.append(vertice)
 try:
  for node in graph_dict[vertice]:
   if (node not in visited):
    _get_cycles(graph_dict, path, visited, result, node)
    visited.add(node)
 except KeyError:
  pass
 path.pop()

def _get_cycles ( graph_dict , path , visited , result , vertice ) : if ( vertice in path ) : cycle = [ vertice ] for node in path [ : : ( - 1 ) ] : if ( node == vertice ) : break cycle . insert ( 0 , node ) start_from = min ( cycle ) index = cycle . index ( start_from ) cycle = ( cycle [ index : ] + cycle [ 0 : index ] ) if ( not ( cycle in result ) ) : result . append ( cycle ) return path . append ( vertice ) try : for node in graph_dict [ vertice ] : if ( node not in visited ) : _get_cycles ( graph_dict , path , visited , result , node ) visited . add ( node ) except KeyError : pass path . pop ( )
recursive function doing the real work for get_cycles .