def test_improve_flows(): mi_grafo = Grafo([1, 2, 3, 4, 5], [(1, 2), (3, 2), (4, 3), (4, 5), (2, 4)], directed=True) cap = {(1, 2): 1, (3, 2): 1, (4, 3): 1, (4, 5): 1, (2, 4): 1} f = {(1, 2): 0, (3, 2): 0, (4, 3): 0, (4, 5): 0, (2, 4): 0} P = [1, 2, 4, 5] assert is_flow(f, mi_grafo, {1, 5}, cap=cap) ff, epsilon = improve_flow(f, cap, mi_grafo, P) assert is_flow(ff, mi_grafo, {1, 5}, cap=cap) assert epsilon == 1 assert ff[(1, 2)] == 1 assert ff[(2, 4)] == 1 assert ff[(4, 5)] == 1 mi_grafo = Grafo([1, 2, 3, 4, 5], [(1, 2), (3, 2), (4, 3), (4, 5), (2, 4)], directed=True) cap = {(1, 2): 1, (3, 2): 1, (4, 3): 1, (4, 5): 1, (2, 4): 1} f = {(1, 2): 0.5, (3, 2): 0, (4, 3): 0, (4, 5): 0.5, (2, 4): 0.5} assert is_flow(f, mi_grafo, {1, 5}, cap=cap) # (1, 2), (2, 1), (3, 2), (4, 3), (4, 5), (5, 4), (2, 4), (4, 2) P = [1, 2, 4, 5] ff, epsilon = improve_flow(f, cap, mi_grafo, P) assert is_flow(ff, mi_grafo, {1, 5}, cap=cap) assert epsilon == 0.5 assert ff[(1, 2)] == 1 assert ff[(2, 4)] == 1 assert ff[(4, 5)] == 1
def test_multiple_augmented_paths(): mi_grafo = Grafo([1, 2, 3, 4, 5, 6, 7, 8], [(5, 8), (6, 8), (2, 8), (1, 2), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5), (3, 6), (5, 6), (4, 6), (4, 7), (6, 7)], directed=True) mi_grafo2 = Grafo([1, 2, 3, 4, 5, 6, 7], [(1, 2), (2, 3), (3, 4), (1, 5), (5, 6), (6, 7), (7, 4), (6, 3)], directed=True) D_final, B = multiple_augmented_paths(mi_grafo2, 1, 4) print('estos son los arcos de D_final', D_final.aristas) print('estas son las aristas de D que están reversadas en D_final', B)
def test_invertir_camino(): mi_grafo = Grafo([1, 2, 3, 4, 5, 6], [(1, 2), (1, 3), (2, 4), (3, 5), (5, 4), (4, 6)], directed=True) path = [1, 2, 4, 6] grafo_path_inv = Grafo([1, 2, 3, 4, 5, 6], [(2, 1), (1, 3), (4, 2), (3, 5), (5, 4), (6, 4)], directed=True) assert invertir_camino(mi_grafo, path) == grafo_path_inv
def test_menger_path_cut_speedup(): mi_grafo = Grafo([1, 2, 3, 4, 5, 6, 7, 8], [(5, 8), (6, 8), (2, 8), (1, 2), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5), (3, 6), (5, 6), (4, 6), (4, 7), (6, 7)], directed=True) mi_grafo2 = Grafo([1, 2, 3, 4, 5, 6, 7], [(1, 2), (2, 3), (3, 4), (1, 5), (5, 6), (6, 7), (7, 4), (6, 3)], directed=True) packing, cut = menger_path_cut_speedup(mi_grafo2, 1, 4) print('este es el packing', packing) print('este es el corte', cut)
def test_augmenting_paths(): mi_grafo = Grafo([1, 2, 3, 4, 5, 6, 7], [(1, 2), (2, 3), (3, 4), (1, 5), (5, 6), (6, 7), (7, 4), (6, 3)], directed=True) grafo_final = Grafo([1, 2, 3, 4, 5, 6, 7], [(2, 1), (3, 2), (4, 3), (5, 1), (6, 5), (7, 6), (4, 7), (6, 3)], directed=True) B_final = {(1, 2), (2, 3), (3, 4), (1, 5), (5, 6), (6, 7), (7, 4)} assert augmenting_paths(mi_grafo, 1, 4) == grafo_final, B_final
def test_maximum_flows(): mi_grafo = Grafo([1, 2, 3, 4, 5], [(1, 2), (3, 2), (4, 3), (4, 5), (2, 4)], directed=True) cap = {(1, 2): 1, (3, 2): 1, (4, 3): 1, (4, 5): 1, (2, 4): 1} f = {(1, 2): 1, (3, 2): 0, (4, 3): 0, (4, 5): 1, (2, 4): 1} P = [1, 2, 4, 5] assert is_flow(f, mi_grafo, {1, 5}, cap=cap) ff = maximum_flow(mi_grafo, cap, 1, 5) assert is_flow(ff, mi_grafo, {1, 5}, cap=cap) assert ff == f mi_grafo = Grafo([1, 2, 3, 4, 5], [(1, 2), (2, 3), (2, 4), (4, 5), (3, 5)], directed=True) cap = {(1, 2): 4, (2, 3): 2, (2, 4): 1, (4, 5): 2, (3, 5): 1} f = {(1, 2): 2, (2, 3): 1, (2, 4): 1, (4, 5): 1, (3, 5): 1} assert is_flow(f, mi_grafo, {1, 5}, cap=cap) ff = maximum_flow(mi_grafo, cap, 1, 5) assert is_flow(ff, mi_grafo, {1, 5}, cap=cap) assert ff == f N = 10000 vertices = ['s', 't'] + ['p{}'.format(k) for k in range(1, N + 1) ] + ['q{}'.format(k) for k in range(1, N + 1)] mi_grafo = Grafo(vertices, [('s', 'p1'), ('s', 'q1')] \ + [('p{}'.format(k), 'p{}'.format(k + 1)) for k in range(1, N)] \ + [('q{}'.format(k), 'q{}'.format(k + 1)) for k in range(1, N)] \ + [('p{}'.format(N), 't'), ('q{}'.format(N), 't')], directed=True) cap = {e: 1 for e in mi_grafo.aristas} f = {e: 1 for e in mi_grafo.aristas} assert is_flow(f, mi_grafo, {'s', 't'}, cap=cap) ff = maximum_flow(mi_grafo, cap, 's', 't') assert is_flow(ff, mi_grafo, {'s', 't'}, cap=cap) assert ff == f N = 10000 start = time() vertices = ['s', 't'] + ['p{}'.format(k) for k in range(1, N + 1) ] + ['q{}'.format(k) for k in range(1, N + 1)] mi_grafo = Grafo(vertices, [('s', 'p1'), ('s', 'q1')] \ + [('p{}'.format(k), 'p{}'.format(k + 1)) for k in range(1, N)] \ + [('q{}'.format(k), 'q{}'.format(k + 1)) for k in range(1, N)] \ + [('p{}'.format(N), 't'), ('q{}'.format(N), 't')], directed=True) cap = {e: random() for e in mi_grafo.aristas} ff = maximum_flow(mi_grafo, cap, 's', 't') assert is_flow(ff, mi_grafo, {'s', 't'}, cap=cap) delta = (time() - start) assert delta < 1.0
def test_reverse_blocking_collection(): mi_grafo = Grafo([1, 2, 3, 4, 5, 6, 7, 8], [(5, 8), (6, 8), (2, 8), (1, 2), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5), (3, 6), (5, 6), (4, 6), (4, 7), (6, 7)], directed=True) Dprima2, alpha_grafo = reverse_blocking_collection(mi_grafo, 1, 8) print('estos son los arcos de Dprima2', Dprima2.aristas)
def test_menger_path(): mi_grafo = Grafo([1, 2, 3, 4, 5, 6, 7], [(1, 4), (1, 2), (2, 3), (2, 4), (3, 7), (3, 4), (1, 5), (5, 6), (6, 7), (7, 4)], directed=True) packing, cut = menger_path_cut(mi_grafo, 1, 4) print('este es el packing', packing) print('este es el cut', cut)
def test_describe_path_packing(): mi_grafo = Grafo([1, 2, 3, 4, 5, 6, 7], [(1, 2), (2, 3), (3, 4), (1, 5), (5, 6), (6, 7), (7, 4)], directed=True) packing = {[1, 2, 3, 4], [1, 5, 6, 7, 4], [10000]} # debería mostrarme error assert describe_path_packing(mi_grafo, 1, 4) == packing
def test_caminos_simples_st_prioritize_minlenFIFO(): mi_grafo = Grafo([1, 2, 3, 4, 5, 6, 7], [(1, 2), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5), (3, 6), (5, 6), (4, 6), (4, 7), (6, 7)], directed=True) mi_grafo = Grafo([1, 2, 3, 4, 5, 6, 7], [(1, 2), (1, 5), (2, 3), (2, 6), (2, 5), (3, 4), (3, 5), (3, 6), (5, 6), (4, 6), (4, 7), (6, 7)], directed=True) caminos, shortest_caminos, alpha_grafo = caminos_simples_st_prioritize_minlenFIFO( mi_grafo, 1, 6) for camino in caminos: print(camino.lista_camino()) for camino in shortest_caminos: print(camino.lista_camino()) print('este es alpha_grafo', alpha_grafo)
def test_blocking_collection(): #mi_grafo = Grafo([1, 2, 3, 4, 5, 6, 7], [(1, 2), (1, 3), (3, 5), (3, 4), (5, 6), (4, 6), (4, 7), (1, 2), #(2, 4)], directed=True) mi_grafo = Grafo([1, 2, 3, 4, 5, 6, 7], [(1, 2), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5), (3, 6), (5, 6), (4, 6), (4, 7), (6, 7)], directed=True) blocking = blocking_collection(mi_grafo, 1, 7) print('este es blocking', blocking)
def test_dfs_iterative(): mi_grafo = Grafo([1, 2, 3, 4, 5, 6, 7], [(1, 2), (1, 3), (3, 5), (3, 4), (5, 6), (4, 6), (4, 7), (1, 2), (2, 4)], directed=True) dfs = DFS() aristas, padres, P = dfs.do_dfs(tree=mi_grafo, root=1, destino=4) print('estas son las aristas', aristas) print('estos son los padres', padres) print('este es el camino origen destino', P)
def test_residual_graph(): mi_grafo = Grafo([1, 2, 3, 4, 5], [(1, 2), (3, 2), (4, 3), (4, 5), (2, 4)], directed=True) f = {(1, 2): 0, (3, 2): 0, (4, 3): 0, (4, 5): 0, (2, 4): 0} cap = {(1, 2): 1, (3, 2): 1, (4, 3): 1, (4, 5): 1, (2, 4): 1} residual = residual_graph(f, cap, mi_grafo) assert residual.aristas == {(1, 2), (3, 2), (4, 3), (4, 5), (2, 4)} mi_grafo = Grafo([1, 2, 3, 4, 5], [(1, 2), (3, 2), (4, 3), (4, 5), (2, 4)], directed=True) f = {(1, 2): 1, (3, 2): 0.5, (4, 3): 0, (4, 5): 0, (2, 4): 0} cap = {(1, 2): 1, (3, 2): 1, (4, 3): 1, (4, 5): 1, (2, 4): 1} residual = residual_graph(f, cap, mi_grafo) assert residual.aristas == {(2, 1), (3, 2), (2, 3), (4, 3), (4, 5), (2, 4)} mi_grafo = Grafo([1, 2, 3, 4, 5], [(1, 2), (3, 2), (4, 3), (4, 5), (2, 4)], directed=True) f = {(1, 2): 0.1, (3, 2): 0.5, (4, 3): 0.5, (4, 5): 0.5, (2, 4): 0.5} cap = {(1, 2): 1, (3, 2): 1, (4, 3): 1, (4, 5): 1, (2, 4): 1} residual = residual_graph(f, cap, mi_grafo) assert residual.aristas == {(1, 2), (2, 1), (3, 2), (2, 3), (4, 3), (3, 4), (4, 5), (5, 4), (2, 4), (4, 2)}
def test_blocking_flow(): mi_grafo = Grafo([1, 2, 3, 4, 5, 6], [(1, 2), (1, 3), (2, 4), (3, 5), (4, 6), (5, 6), (2, 3), (4, 5)], directed=True) cap = { (1, 2): 3, (1, 3): 3, (2, 4): 3, (3, 5): 2, (4, 6): 2, (5, 6): 3, (2, 3): 2, (4, 5): 4 } blocking_f = blocking_flow(mi_grafo, 1, 6, cap) print('este es blocking_f', blocking_f)
def test_spptw_desrochers1988_imp3(): mi_grafo = Grafo([0, 1, 2, 3, 4], [(0, 1), (0, 2), (0, 3), (1, 4), (2, 4), (3, 4)], directed=True) tiempo = { (0, 1): 8, (0, 2): 5, (0, 3): 12, (1, 4): 4, (2, 4): 2, (3, 4): 4 } costo = {(0, 1): 3, (0, 2): 5, (0, 3): 2, (1, 4): 7, (2, 4): 6, (3, 4): 3} ventana = {0: [0, 0], 1: [6, 14], 2: [9, 12], 3: [8, 12], 4: [9, 15]} P = spptw_desrochers1988_imp3(mi_grafo, 0, tiempo, costo, ventana) print(P) return P
def test_Bidirectional_Dijkstra(): mi_grafo = Grafo([1, 2, 3, 4, 5, 6], [(1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (3, 5), (4, 6), (5, 4), (5, 6)], directed=True) l = { (1, 2): 6, (1, 3): 4, (2, 3): 2, (2, 4): 2, (3, 4): 1, (3, 5): 2, (4, 6): 7, (5, 6): 3, (5, 4): 1 } d = Bidirectional_Dijkstra(mi_grafo, l, 1, 6)
def test_is_flow(): mi_grafo = Grafo([1, 2, 3, 4], [(1, 2), (1, 3), (2, 3), (3, 4)], directed=True) flow = {(1, 2): 3, (1, 3): 2, (2, 3): 1, (3, 4): 5} S = set() assert not is_flow(flow, mi_grafo, S) flow = {(1, 2): 0, (1, 3): 0, (2, 3): 0, (3, 4): 0} S = set() assert is_flow(flow, mi_grafo, S) flow = {(1, 2): 1, (1, 3): 1, (2, 3): 1, (3, 4): 2} S = {1, 4} assert is_flow(flow, mi_grafo, S) flow = {(1, 2): -1, (1, 3): 1, (2, 3): 1, (3, 4): 2} S = {1, 4} assert not is_flow(flow, mi_grafo, S)
def test_augmenting_along_flows(): mi_grafo = Grafo([1, 2, 3, 4, 5, 6], [(1, 2), (1, 3), (2, 4), (3, 5), (4, 6), (5, 6), (2, 3), (4, 5)], directed=True) cap = { (1, 2): 3, (1, 3): 3, (2, 4): 3, (3, 5): 2, (4, 6): 2, (5, 6): 3, (2, 3): 2, (4, 5): 4 } mi_grafo2 = Grafo([1, 2, 3, 4, 5, 6], [(1, 2), (1, 3), (2, 4), (3, 5), (4, 6), (5, 6), (2, 3), (5, 4)], directed=True) mi_grafo3 = Grafo([0, 1, 2, 3, 4, 5], [(0, 1), (0, 2), (1, 2), (1, 3), (1, 4), (2, 4), (4, 3), (4, 5), (3, 5)], directed=True) cap2 = { (1, 2): 10, (1, 3): 8, (2, 4): 5, (3, 5): 10, (4, 6): 7, (5, 6): 10, (2, 3): 2, (5, 4): 8 } cap3 = { (0, 1): 10, (0, 2): 10, (1, 2): 2, (1, 3): 4, (1, 4): 8, (2, 4): 9, (4, 3): 6, (4, 5): 10, (3, 5): 10 } flow1 = { (1, 2): 0, (1, 3): 0, (2, 4): 0, (3, 5): 0, (4, 6): 0, (5, 6): 0, (2, 3): 0, (4, 5): 0 } flow2 = { (1, 2): 0, (1, 3): 0, (2, 4): 0, (3, 5): 0, (4, 6): 0, (5, 6): 0, (2, 3): 0, (5, 4): 0 } flow3 = { (0, 1): 0, (0, 2): 0, (1, 2): 0, (1, 3): 0, (1, 4): 0, (2, 4): 0, (4, 3): 0, (4, 5): 0, (3, 5): 0 } f = augmenting_along_flows(mi_grafo3, flow3, cap3, 0, 5)