Пример #1
0
def test_implementation_of_cut_edges_matches_naive_method(three_by_three_grid):
    graph = three_by_three_grid
    assignment = {0: 1, 1: 1, 2: 2, 3: 1, 4: 1, 5: 2, 6: 2, 7: 2, 8: 2}
    partition = Partition(graph, assignment)

    flip = {4: 2}
    new_partition = Partition(parent=partition, flips=flip)
    result = cut_edges(new_partition)

    naive_cut_edges = {
        edge
        for edge in graph.edges if new_partition.crosses_parts(edge)
    }

    assert edge_set_equal(result, naive_cut_edges)
Пример #2
0
def test_cut_edges_can_handle_multiple_flips(three_by_three_grid):
    graph = three_by_three_grid
    assignment = {0: 1, 1: 1, 2: 2, 3: 1, 4: 1, 5: 2, 6: 2, 7: 2, 8: 2}
    partition = Partition(graph, assignment)
    # 112    111
    # 112 -> 121
    # 222    222
    flip = {4: 2, 2: 1, 5: 1}

    new_partition = Partition(parent=partition, flips=flip)

    result = new_partition['cut_edges']

    naive_cut_edges = {
        tuple(sorted(edge))
        for edge in graph.edges if new_partition.crosses_parts(edge)
    }
    assert result == naive_cut_edges
Пример #3
0
def test_cut_edges_by_part_gives_same_total_edges_as_naive_method(
        three_by_three_grid):
    graph = three_by_three_grid
    assignment = {0: 1, 1: 1, 2: 2, 3: 1, 4: 1, 5: 2, 6: 2, 7: 2, 8: 2}
    updaters = {'cut_edges_by_part': cut_edges_by_part}
    partition = Partition(graph, assignment, updaters)
    # 112    111
    # 112 -> 121
    # 222    222
    flip = {4: 2, 2: 1, 5: 1}

    new_partition = Partition(parent=partition, flips=flip)

    result = new_partition['cut_edges_by_part']
    naive_cut_edges = {
        tuple(sorted(edge))
        for edge in graph.edges if new_partition.crosses_parts(edge)
    }

    assert naive_cut_edges == {
        tuple(sorted(edge))
        for part in result for edge in result[part]
    }
Пример #4
0
def compute_cross_edge(graph,partition):
    cross_list = []
    for n in graph.edges:
        if Partition.crosses_parts(partition,n):
            cross_list.append(n)
    return cross_list
Пример #5
0
def compute_cross_edges(partition):
    return set([
        edge for edge in partition.graph.edges
        if Partition.crosses_parts(partition, edge)
    ])