def test_graph_5():
    from ktcut.isolation_branching import isolation_branching
    from ktcut.lp_algorithm import lp_algorithm
    test_graphs = SmallGraphs()
    test_graphs.set_test_graph(5)
    graph, terminals = test_graphs.get_graph(), test_graphs.get_terminals()
    _, cut_value, _ = isolation_branching(graph,
                                          terminals,
                                          persistence='strong')
    assert cut_value == 110
    cut_value = lp_algorithm(graph, terminals)
    assert cut_value == 110
def test_graph_4():
    from ktcut.isolation_branching import isolation_branching
    from ktcut.ip_algorithm import ip_algorithm
    from ktcut.lp_algorithm import lp_algorithm
    from ktcut.persistence import check_persistence
    test_graphs = SmallGraphs()
    test_graphs.set_test_graph(4)
    graph, terminals = test_graphs.get_graph(), test_graphs.get_terminals()
    _, cut_value, _ = isolation_branching(graph, terminals)
    assert cut_value == 27
    _, cut_value = ip_algorithm(graph, terminals)
    assert cut_value == 27
    cut_value = lp_algorithm(graph, terminals)
    assert cut_value == 26
    assert check_persistence(graph, terminals, 'weak')
    assert check_persistence(graph, terminals, 'strong')
Exemplo n.º 3
0
def isolation_branching(graph,
                        terminals,
                        persistence=None,
                        reporting=True,
                        time_limit=600):
    """Solves k-Terminal Cut for given graph and terminals.

    The k-terminal cut partitions the graph into k sets
        such that each partition contains exactly one terminal node
        and the total weight of edges between sets is minimized.

    Assumes that the graph has 'capacity' along each edge. Otherwise,
        assumes the capacity should be 1.0.

    Args:
        graph: the networkx graph in which to find the multi-terminal cut
        terminals: the terminals of the networkx graph
        persistence: if persistence is assumed [strong, weak, None]
        reporting: if the branching solver should print results as it goes
        time_limit: the time after which to terminate,
            even if the optimal solution has not yet been reached.

    Returns:
        source_sets: the partition of the nodes of the graph which defines the minimum cut
        cut_value: the weight of the optimal multi-terminal cut
        report: the final values in the Isolation Branching tree
    """
    for u, v in graph.edges:
        if "capacity" in graph[u][v]:
            continue
        else:
            graph[u][v]["capacity"] = 1.0

    if persistence in {"strong", "weak"}:
        terminals_by_vertex = lp_algorithm(graph,
                                           terminals,
                                           persistence=persistence)
    else:
        terminals_by_vertex = {node: terminals for node in graph.nodes()}

    branch_and_bound_tree = IsolationBranchingTree(
        graph, terminals=terminals, terminals_by_vertex=terminals_by_vertex)

    source_sets, cut_value = branch_and_bound_tree.solve(reporting=reporting,
                                                         time_limit=time_limit)

    return source_sets, cut_value, branch_and_bound_tree.report