import networkx as nx
def find_all_cycles(graph):
return list(nx.simple_cycles(graph))
def find_cycle_with_minimum_latency(graph, latencies):
all_cycles = find_all_cycles(graph)
if not all_cycles:
return None
min_latency = float('inf')
selected_cycle = None
for cycle in all_cycles:
cycle_latency = sum(latencies[edge] for edge in zip(cycle, cycle[1:] + [cycle[0]]))
if cycle_latency < min_latency:
min_latency = cycle_latency
selected_cycle = cycle
return selected_cycle
# Ejemplo de uso
graph = nx.DiGraph()
graph.add_edges_from([(1, 2), (2, 3), (3, 4), (4, 1), (4, 5), (5, 2)])
latencies = {(1, 2): 3, (2, 3): 5, (3, 4): 2, (4, 1): 4, (4, 5): 1, (5, 2): 6}
selected_cycle = find_cycle_with_minimum_latency(graph, latencies)
print("Ciclo con latencia mínima:", selected_cycle)