def get_algorithm(algorithm, graph):
    if algorithm == 'generic':
        return Goldberg(graph)
    elif algorithm == 'height':
        return GoldbergHeight(graph)
    elif algorithm == 'wave':
        return GoldbergWave(graph)
Ejemplo n.º 2
0
def test_max_flow_scale_free_directed(n):
    seed_number = randint(1, 1000)
    generator = ScaleFree(n, directed=True, seed_number=seed_number)
    graph, source, target = generator.generate()
    solver = Goldberg(graph)
    max_flow = solver.get_max_flow(source, target)

    generator = ScaleFree(n, directed=True, seed_number=seed_number)
    graph, source, target = generator.generate()
    res = gt.push_relabel_max_flow(graph, source, target, graph.ep.cap)
    res.a = graph.ep.cap.a - res.a  # the actual flow
    gt_max_flow = sum(res[e] for e in target.in_edges())
    assert max_flow == gt_max_flow
def test_max_flow_triangulation_delaunay_directed(n):
    seed_number = randint(1, 1000)
    generator = Triangulation(n,
                              type="delaunay",
                              directed=True,
                              seed_number=seed_number)
    graph, source, target = generator.generate()
    solver = Goldberg(graph)
    max_flow = solver.get_max_flow(source, target)

    generator = Triangulation(n,
                              type="delaunay",
                              directed=True,
                              seed_number=seed_number)
    graph, source, target = generator.generate()
    res = gt.push_relabel_max_flow(graph, source, target, graph.ep.cap)
    res.a = graph.ep.cap.a - res.a  # the actual flow
    gt_max_flow = sum(res[e] for e in target.in_edges())
    assert max_flow == gt_max_flow
Ejemplo n.º 4
0
def test_max_flow_scale_random_undirected(size):
    seed_number = randint(1, 1000)
    generator = Random(size[0],
                       size[1],
                       directed=False,
                       seed_number=seed_number)
    graph, source, target = generator.generate()
    solver = Goldberg(graph)
    max_flow = solver.get_max_flow(source, target)

    generator = Random(size[0],
                       size[1],
                       directed=False,
                       seed_number=seed_number)
    graph, source, target = generator.generate()
    res = gt.push_relabel_max_flow(graph, source, target, graph.ep.cap)
    res.a = graph.ep.cap.a - res.a  # the actual flow
    gt_max_flow = sum(res[e] for e in target.in_edges())
    assert max_flow == gt_max_flow
file = open(
    "temporal_complexity_data_goldberg_4 edges for each vertex_10-90_nodes",
    "w")

for nodes in [10, 20, 30, 40, 50, 60, 70, 80, 90]:
    for i in range(0, 35):
        #Goldberg version - using as graph generator Random
        seed_number = randint(1, 1000)
        generator = Random(nodes,
                           nodes * 4,
                           directed=True,
                           seed_number=seed_number)
        g, source, target = generator.generate()

        title = '- Parte grafo versione Goldberg con ' + str(
            nodes) + ' nodi e ' + str(len(g.get_edges())) + ' archi - Random.'
        print(title)
        file.write(title)
        solver = Goldberg(graph=g)

        pr = cProfile.Profile()
        pr.enable()
        solution = solver.get_max_flow(source, target)
        pr.disable()
        s = StringIO()
        sortby = 'cumulative'
        ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
        ps.print_stats()

        file.write(s.getvalue())
file.close()
g, source, target = generator.generate()
print("Source: " + str(source) + " target " + str(target))

gt.graph_draw(g, edge_pen_width=gt.prop_to_size(g.ep.cap, mi=1, ma=5, power=1),
              output="cf/cf_capacities.pdf", vertex_text=g.vertex_index, edge_text=g.ep.cap)'''
#seed_number = randint(1, 1000)
seed_number = 14
generator = Triangulation(50, type="delaunay", directed=True, seed_number=seed_number)
g, source, target = generator.generate()

gt.graph_draw(g, edge_pen_width=gt.prop_to_size(g.ep.cap, mi=1, ma=5, power=1),
              output="cf/cf_capacities.pdf", vertex_text=g.vertex_index, edge_text=g.ep.cap)

# source = g.vertex(0)
# target = g.vertex(3)
solver = Goldberg(g)
solution = solver.get_max_flow(source, target)
print("Il max flow trovato da Golberg è:", solution)

labels = g.new_vertex_property("string")
for vertex in g.vertices():
    labels[vertex] = str(g.vertex_index[vertex]) + "(" + str(g.vp.height[vertex]) + "," + str(
        g.vp.excess[vertex]) + ")"

gt.graph_draw(g, edge_pen_width=gt.prop_to_size(g.ep.residual, mi=1, ma=5, power=1),
              output="cf/max-flow-our-solution.pdf", vertex_text=labels, edge_text=g.ep.residual)

generator = Triangulation(50, type="delaunay", directed=True, seed_number=seed_number)
g, source, target = generator.generate()
cap = g.ep.cap
res = gt.push_relabel_max_flow(g, source, target, cap)