Пример #1
0
def g_of_v_algo(_: np.ndarray, orig: Graph):

    graph: Graph = orig.copy()
    set_name(graph)

    cover_group = []
    cover_group_degree = []
    cover_group_g_of_v = []
    removed_counter = 0
    iteration = 0
    while not is_empty_graph(graph):
        iteration += 1

        if is_empty_graph(graph):
            break

        selected_vertex, g_of_v_value, degree = select_vertex(graph, iteration)
        cover_group_degree.append(degree)
        cover_group_g_of_v.append(g_of_v_value)

        zero_vertices(graph, [selected_vertex])
        cover_group.append(selected_vertex)

    return cover_group, removed_counter, (cover_group_degree,
                                          cover_group_g_of_v)
Пример #2
0
def xyz_v3_algo(_, orig: Graph):
    graph: Graph = orig.copy()
    set_name(graph)

    cover_group = []
    removed_counter = 0

    # add_to_cover, removed_in_reduce = reduce_graph(graph, do_reduce_1=True, do_reduce_2=True, do_reduce_3=True)
    # cover_group.extend(add_to_cover)
    # removed_counter += removed_in_reduce

    while True:

        add_to_cover, removed_in_reduce = reduce_graph(graph,
                                                       do_reduce_1=False,
                                                       do_reduce_2=False,
                                                       do_reduce_3=False)
        cover_group.extend(add_to_cover)
        removed_counter += removed_in_reduce

        # xyz
        selected_vertex_index = xyz_select_vertex(graph)
        if selected_vertex_index is None:
            break
        cover_group.append(graph.vs[selected_vertex_index]['name'])
        zero_vertices_by_index(graph, selected_vertex_index)
    return list(cover_group), removed_counter
Пример #3
0
def degree(_: np.ndarray, orig: Graph):

    graph: Graph = orig.copy()
    set_name(graph)

    cover_group = []
    cover_group_degree = []
    cover_group_g_of_v = []
    removed_counter = 0

    while not is_empty_graph(graph):

        add_to_cover, removed_in_reduce = reduce_graph(graph,
                                                       do_reduce_1=False,
                                                       do_reduce_2=False,
                                                       do_reduce_3=False)
        cover_group.extend(add_to_cover)
        removed_counter += removed_in_reduce

        if is_empty_graph(graph):
            break

        selected_vertex, selected_max_degree, selected_g_of_v = select_vertex(
            graph)
        cover_group_degree.append(selected_max_degree)
        cover_group_g_of_v.append(selected_g_of_v)

        zero_vertices(graph, [selected_vertex])
        cover_group.append(selected_vertex)

    return cover_group, removed_counter, (cover_group_degree,
                                          cover_group_g_of_v)
    def test_remove_vertex_if_contained_neighbors(self):
        graph = Graph(n=8,
                      edges=[(1, 3), (3, 4), (4, 1), (1, 2), (3, 2), (2, 5),
                             (5, 4)])
        set_name(graph)

        add_to_cover = remove_vertex_if_contained_neighbors(graph)
        self.assertEqual(add_to_cover, ['v1'])
Пример #5
0
def run_reduce_graph_all(graph=None):
    graph: Graph = graph or Graph.Erdos_Renyi(100, 3 / 100)
    set_name(graph)

    print_graph_info(graph, leaves=True, zero_degree=True, connected_components=True, label='start')
    added_to_cover, counter_of_removed = reduce_graph(graph, do_reduce_1=True, do_reduce_2=True, do_reduce_3=True)
    print(f'size of cover: {len(added_to_cover) + counter_of_removed}')
    print_graph_info(graph, leaves=True, zero_degree=True, connected_components=True, label='end')
Пример #6
0
def xyz_weak_algo(_, orig: Graph):
    log = False
    graph: Graph = orig.copy()
    set_name(graph)

    cover_group = []
    removed_counter = 0
    iteration = 0

    if log:
        print_graph_info(graph,
                         leaves=True,
                         zero_degree=True,
                         connected_components=True,
                         label='xyz_weak: start')
        print_vertex_number(graph)

    while True:

        add_to_cover, removed_in_reduce = reduce_graph(graph,
                                                       do_reduce_1=True,
                                                       do_reduce_2=True,
                                                       do_reduce_3=True)
        cover_group.extend(add_to_cover)
        removed_counter += removed_in_reduce

        # xyz
        result = xyz_weak_select_vertex(graph)
        if result is None:
            break

        add_to_cover_index, to_remove_index = result

        if log:
            print(
                f'will be added to cover: {[graph.vs[i]["name"] for i in add_to_cover_index]}'
            )
            print(
                f'will be removed (not in cover): {[graph.vs[i]["name"] for i in to_remove_index]}'
            )

        cover_group = cover_group + [
            graph.vs[i]['name'] for i in add_to_cover_index
        ]
        zero_vertices_by_index(graph, add_to_cover_index + to_remove_index)

        iteration += 1
        if log:
            print_graph_info(graph,
                             leaves=True,
                             zero_degree=True,
                             connected_components=True,
                             label=f'xyz_weak: iteration ${iteration}')
            print_vertex_number(graph)

    return list(cover_group), removed_counter
Пример #7
0
def most_neighbors_with_minimal_degree_algo(_, orig: Graph):
    cover_group = []
    graph: Graph = orig.copy()
    set_name(graph)

    while not is_empty_graph(graph):
        selected_vertex = select_vertices(graph)
        zero_vertices(graph, [selected_vertex])
        cover_group = cover_group + [selected_vertex]
    return cover_group
Пример #8
0
def run_reduce_1_by_iteration(graph=None):
    n = 1000
    c = 3
    graph: Graph = graph or Graph.Erdos_Renyi(n, c / n)
    set_name(graph)

    print_theoretical_number_of_leaves(n, c)
    print_graph_info(graph, leaves=True, zero_degree=True, connected_components=True, label='initial graph')
    added_to_cover = remove_parents_of_leaves(graph, log=True, one_time=False)
    print(f'how many added to cover: {len(added_to_cover)}')
def first_vertex_with_degree_algo(_, orig: Graph):
    cover_group = []
    graph = orig.copy()
    set_name(graph)

    while not is_empty_graph(graph):
        selected_vertices = select_vertices(graph)
        zero_vertices(graph, selected_vertices)
        cover_group = cover_group + selected_vertices
    return cover_group
Пример #10
0
def run_reduce_graph_compare(graph=None):
    graph: Graph = graph or Graph.Erdos_Renyi(10000, 2.8 / 10000)
    copy1 = graph.copy()
    copy2 = graph.copy()
    set_name(copy1)
    set_name(copy2)

    print_graph_info(graph, leaves=True, zero_degree=True, connected_components=True, label='initial graph')

    reduce_graph(copy1, do_reduce_1=True, do_reduce_2=False, do_reduce_3=False)
    print_graph_info(copy1, leaves=True, zero_degree=True, connected_components=True, label='copy1: after only reduce 1')

    reduce_graph(copy2, do_reduce_1=True, do_reduce_2=True, do_reduce_3=False)
    print_graph_info(copy2, leaves=True, zero_degree=True, connected_components=True, label='copy2: after reduce 1 + 2')
Пример #11
0
def neighbors_algo(_, orig: Graph):
    cover_group = []
    graph: Graph = orig.copy()
    set_name(graph)

    while not is_empty_graph(graph):
        parents = find_parents_of_leaves(graph)
        zero_vertices(graph, parents)
        cover_group = cover_group + parents

        if is_empty_graph(graph):
            break

        selected_vertices = select_vertices(graph)
        zero_vertices(graph, [selected_vertices[0]] + selected_vertices[1])
        cover_group = cover_group + selected_vertices[1]
    return cover_group
Пример #12
0
def xyz_neighbors_combined_algo(_, orig: Graph):
    cover_group = []
    graph: Graph = orig.copy()
    set_name(graph)

    while not is_empty_graph(graph):
        parents = find_parents_of_leaves(graph)
        zero_vertices(graph, parents)
        cover_group = cover_group + parents

        if is_empty_graph(graph):
            break

        selected_vertices, vertices_to_zero = select_vertices(graph)
        zero_vertices(graph, selected_vertices + vertices_to_zero)
        cover_group = cover_group + selected_vertices
    return cover_group
Пример #13
0
def novac1_algo(_, orig: Graph):
    graph: Graph = orig.copy()
    set_name(graph)

    cover_group = []
    removed_counter = 0

    while not is_empty_graph(graph):

        add_to_cover, removed_in_reduce = reduce_graph(graph, do_reduce_1=False, do_reduce_2=False, do_reduce_3=False)
        cover_group.extend(add_to_cover)
        removed_counter += removed_in_reduce

        if is_empty_graph(graph):
            break
        selected_vertices = select_vertices(graph)
        zero_vertices(graph, selected_vertices)
        cover_group = cover_group + selected_vertices

    return cover_group, removed_counter