예제 #1
0
def test_correctness():
    # note: in the future can restrict all graphs to have the same number of nodes, for analysis
    graphs = [lambda: basic_graph(), lambda: complete_graph(10), lambda: balanced_tree(3, 5), lambda: barbell_graph(6, 7),
              lambda: binomial_tree(10), lambda: cycle_graph(50),
              lambda: path_graph(200), lambda: star_graph(200)]
    names = ["basic_graph", "complete_graph", "balanced_tree", "barbell_graph", "binomial_tree", "cycle_graph",
             "path_graph", "star_graph"]
    for graph, name in zip(graphs, names):
        print(f"Testing graph {name}...")
        
        # Initialize both graphs
        G_dinitz = graph()
        G_gr = graph()

        # Set random capacities of graph edges
        for u, v in G_dinitz.edges:
            cap = randint(1, 20)
            G_dinitz.edges[u, v]["capacity"] = cap
            G_gr.edges[u, v]["capacity"] = cap

        # Pick random start and end node
        start_node = randint(0, len(G_dinitz.nodes)-1)
        end_node = randint(0, len(G_dinitz.nodes)-1)
        while start_node == end_node: end_node = randint(0, len(G_dinitz.nodes)-1)

        # Run max-flow
        R_dinitz = dinitz(G_dinitz, start_node, end_node)
        R_gr = goldberg_rao(G_gr, start_node, end_node)

        # Check correctness
        d_mf = R_dinitz.graph["flow_value"]
        gr_mf = R_gr.graph["flow_value"]
        assert d_mf == gr_mf, f"Computed max flow in {name} graph is {d_mf}, but goldberg_rao function computed {gr_mf}"
def run_analysis_n_nodes(n, unit_cap, n_runs=1):
    print(f"Running analysis for {n} nodes...")
    graphs = [lambda: complete_graph(n)]

    results_gr = {}
    results_dinitz = {}
    for graph, name in zip(graphs, names):
        # Initialize both graphs
        G_dinitz = graph()
        G_gr = G_dinitz.copy()

        total_time_gr = 0
        total_time_dinitz = 0
        for _ in range(n_runs):
            # Set random capacities of graph edges
            for u, v in G_dinitz.edges:
                cap = randint(1, 100) if not unit_cap else 1
                G_dinitz.edges[u, v]["capacity"] = cap
                G_gr.edges[u, v]["capacity"] = cap

            # Pick random start and end node
            start_node = randint(0, len(G_dinitz.nodes) - 1)
            end_node = randint(0, len(G_dinitz.nodes) - 1)
            while start_node == end_node:
                end_node = randint(0, len(G_dinitz.nodes) - 1)

            # Run max-flow
            init_time = time.time()
            R_dinitz = dinitz(G_dinitz, start_node, end_node)
            total_time_dinitz += time.time() - init_time

            init_time = time.time()
            R_gr = goldberg_rao(G_gr, start_node, end_node)
            total_time_gr += time.time() - init_time

            # Check correctness
            d_mf = R_dinitz.graph["flow_value"]
            gr_mf = R_gr.graph["flow_value"]
            if d_mf != gr_mf:
                vprint(
                    f"\t\t\tComputed max flow in {name} graph is {d_mf}, but goldberg_rao function computed {gr_mf}"
                    .upper())

        vprint(
            f"{name} with {n} nodes took {total_time_gr / n_runs} seconds with goldberg_rao"
        )
        vprint(
            f"{name} with {n} nodes took {total_time_dinitz / n_runs} seconds with dinitz"
        )
        results_gr[name] = total_time_gr / n_runs
        results_dinitz[name] = total_time_dinitz / n_runs

    return results_gr, results_dinitz
def run_analysis_n_nodes(n, unit_cap, n_runs=3):
    print(f"Running analysis for {n} nodes...")
    graphs = [
        lambda: balanced_tree(2, int(round(np.log2(n) - 1))),
        lambda: binomial_tree(int(round(np.log2(n)))), lambda: cycle_graph(n),
        lambda: path_graph(n), lambda: star_graph(n - 1),
        lambda: random_regular_graph(3, n), lambda: random_regular_graph(5, n)
    ]

    results_dinitz = {}
    for graph, name in zip(graphs, names):
        # Initialize both graphs
        G_dinitz = graph()

        total_time_dinitz = 0
        for _ in range(n_runs):
            # Set random capacities of graph edges
            for u, v in G_dinitz.edges:
                cap = randint(1, 100) if not unit_cap else 1
                G_dinitz.edges[u, v]["capacity"] = cap

            # Pick random start and end node
            start_node = randint(0, len(G_dinitz.nodes) - 1)
            end_node = randint(0, len(G_dinitz.nodes) - 1)
            while start_node == end_node:
                end_node = randint(0, len(G_dinitz.nodes) - 1)

            # Run max-flow
            init_time = time.time()
            R_dinitz = dinitz(G_dinitz, start_node, end_node)
            total_time_dinitz += time.time() - init_time

        vprint(
            f"{name} with {len(G_dinitz.nodes)} nodes took {total_time_dinitz / n_runs} seconds with dinitz"
        )
        results_dinitz[name] = total_time_dinitz / n_runs

    return results_dinitz
예제 #4
0
def Din(G, a, b):
    start_time = time()
    R = dinitz(G, a, b)
    time_elapsed = time() - start_time
    return time_elapsed