Ejemplo n.º 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, {"cut_edges": cut_edges})

    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)
def compute_cross_edge(graph, partition):
    """
    The function finds the edges that cross from one partition to another
    partition
    Parameters:
        graph (Graph): The given graph represent state information
        partition (Partition): the partition of the given graph
    Returns:
        list: the list of edges that cross two partitions
    """
    cross_list = []
    for n in graph.edges:
        if Partition.crosses_parts(partition, n):
            cross_list.append(n)
    return cross_list  # cut edges of partition
Ejemplo n.º 3
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, {"cut_edges": cut_edges})
    # 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
Ejemplo n.º 4
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]
    }
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  # cut edges of partiitons