def add_vertex_and_update_sets(G, v_num, v_helpfulness, helpful_set,
                               set_helpfulness, big_set, partitions):
    helpful_set.append({'v_num': v_num, 'helpfulness': v_helpfulness})
    set_helpfulness += v_helpfulness
    update_helpfulness_of_neighbours(G, v_num, big_set, partitions)
    sort_vertices_helpfulness(G, big_set)
    return set_helpfulness
Exemple #2
0
def find_vertices_to_balance_normal(G, size_to_balance, vertices_helpfulness,
                                    partitions, adj_partition, cur_partition):
    vertices_helpfulness_to_consider, adj_set = find_vertices_adj_to_another_partition(
        G, partitions, vertices_helpfulness, adj_partition, cur_partition)

    vertices_to_balance = set()

    set_size = 0
    end = False

    while not end and len(vertices_helpfulness_to_consider
                          ) > 0 and set_size < size_to_balance:
        vertex_num, vertex_helpfulness = pop_vertex(
            vertices_helpfulness_to_consider)
        if set_size + 1 > size_to_balance:
            end = True
        else:
            set_size += 1
            vertices_to_balance.add(vertex_num)
            update_adjacent_vertices(G=G,
                                     v_num=vertex_num,
                                     partitions=partitions,
                                     vertices_helpfulness=vertices_helpfulness,
                                     vertices_helpfulness_to_consider=
                                     vertices_helpfulness_to_consider,
                                     adj_set=adj_set,
                                     helpful_set=vertices_to_balance,
                                     cur_partition=cur_partition)
            update_helpfulness_of_neighbours(G, vertex_num,
                                             vertices_helpfulness_to_consider,
                                             partitions)
            sort_vertices_helpfulness(G, vertices_helpfulness_to_consider)

    return vertices_to_balance, set_size
def search_for_helpful_set(G, vertices_helpfulness, limit, partitions):
    helpful_set = set()
    set_helpfulness = 0
    end = False
    while not end and set_helpfulness < limit and len(vertices_helpfulness) > 0:
        vertex_num, vertex_helpfulness = pop_vertex(vertices_helpfulness)
        if vertex_helpfulness < 0:
            end = True
        else:
            set_helpfulness += vertex_helpfulness
            helpful_set.add(vertex_num)
            update_helpfulness_of_neighbours(G, vertex_num, vertices_helpfulness, partitions)
            sort_vertices_helpfulness(G, vertices_helpfulness)

    return helpful_set, set_helpfulness
def phase_1(G, vertices_helpfulness, S_size, S_helpfulness, partitions):
    set_helpfulness = 0
    helpful_set = set()
    end = False
    while (not end and len(helpful_set) < S_size
           and set_helpfulness < S_helpfulness - 1
           and len(vertices_helpfulness) > 0):
        vertex_data = vertices_helpfulness.pop()
        vertex_helpfulness = vertex_data['helpfulness']
        vertex_num = vertex_data['v_num']
        if vertex_helpfulness < 0:
            end = True
        else:
            set_helpfulness += vertex_helpfulness
            helpful_set.add(vertex_num)
            update_helpfulness_of_neighbours(G, vertex_num,
                                             vertices_helpfulness, partitions)
            sort_vertices_helpfulness(G, vertices_helpfulness)

    return helpful_set, set_helpfulness
def phase_1_improved(G, vertices_helpfulness, vertices_helpfulness_to_consider, adj_set, S_helpfulness, max_,
                     cur_partition, partitions):
    set_helpfulness = 0
    set_weight = 0
    helpful_set = set()
    end = False

    while (not end
           and len(vertices_helpfulness_to_consider) > 0):
        vertex_data = vertices_helpfulness_to_consider.pop()
        vertices_helpfulness.remove(vertex_data)
        vertex_helpfulness = vertex_data['helpfulness']
        vertex_num = vertex_data['v_num']
        vertex_weight = G.nodes[vertex_num]['data']['weight']
        if (vertex_helpfulness < 0
                or vertex_weight + set_weight > max_
                or set_helpfulness + vertex_helpfulness >= S_helpfulness - 1):
            end = True
            vertices_helpfulness_to_consider.insert(0, vertex_data)
        else:
            set_helpfulness += vertex_helpfulness
            set_weight += vertex_weight
            helpful_set.add(vertex_num)
            update_adjacent_vertices(G=G,
                                     v_num=vertex_num,
                                     partitions=partitions,
                                     vertices_helpfulness=vertices_helpfulness,
                                     vertices_helpfulness_to_consider=vertices_helpfulness_to_consider,
                                     adj_set=adj_set,
                                     helpful_set=helpful_set,
                                     cur_partition=cur_partition)
            update_helpfulness_of_neighbours_improved(G=G,
                                                      v_num=vertex_num,
                                                      vertices_helpfulness=vertices_helpfulness,
                                                      vertices_helpfulness_to_consider=vertices_helpfulness_to_consider,
                                                      partitions=partitions)
        sort_vertices_helpfulness(G, vertices_helpfulness_to_consider)

    return helpful_set, set_helpfulness, set_weight
def search_for_helpful_set_improved(G, vertices_helpfulness, limit, s_max, partitions, adj_partition, cur_partition, draw_):
    helpful_set = set()
    set_weight = 0
    set_helpfulness = 0
    end = False
    vertices_helpfulness_to_consider, adj_set = find_vertices_adj_to_another_partition(G,
                                                                                       partitions,
                                                                                       vertices_helpfulness,
                                                                                       adj_partition,
                                                                                       cur_partition)

    while not end and set_helpfulness < limit and len(vertices_helpfulness_to_consider) > 0 and len(
            helpful_set) < s_max:
        # draw_helpfulness(G, vertices_helpfulness, '')
        # draw_helpfulness(G, vertices_helpfulness_to_consider, '')
        vertex_num, vertex_helpfulness = pop_vertex_improved(vertices_helpfulness_to_consider, vertices_helpfulness)
        if vertex_helpfulness < 0:
            end = True
        else:
            # draw_vertices(G, {vertex_num}, 1, '')
            set_helpfulness += vertex_helpfulness
            set_weight += G.nodes[vertex_num]['data']['weight']
            helpful_set.add(vertex_num)
            update_adjacent_vertices(G=G,
                                     v_num=vertex_num,
                                     partitions=partitions,
                                     vertices_helpfulness=vertices_helpfulness,
                                     vertices_helpfulness_to_consider=vertices_helpfulness_to_consider,
                                     adj_set=adj_set,
                                     helpful_set=helpful_set,
                                     cur_partition=cur_partition)
            update_helpfulness_of_neighbours_improved(G, vertex_num, vertices_helpfulness,
                                                      vertices_helpfulness_to_consider, partitions)

            sort_vertices_helpfulness(G, vertices_helpfulness_to_consider)
    if draw_:
        draw_helpful_set(G, helpful_set=helpful_set, color=3, title='helpfulset')
    return helpful_set, set_helpfulness, set_weight
def add_vert_with_helpfulness_gte_zero(G, _2_set, _2_set_helpfulness, _2_set_weight, S_dash_weight,
                                       big_set, max_, partitions):
    end = False
    while (_2_set_helpfulness < 0
           and not end
           and contains_diff_values_greater_than_0(big_set)):
        v_num, v_helpfulness, v_weight = pop_vertex_improved(G, big_set)
        if S_dash_weight + _2_set_weight + v_weight <= max_:
            _2_set_helpfulness, _2_set_weight = add_vertex_and_update_sets(G=G,
                                                                           v_num=v_num,
                                                                           v_helpfulness=v_helpfulness,
                                                                           v_weight=v_weight,
                                                                           helpful_set=_2_set,
                                                                           set_helpfulness=_2_set_helpfulness,
                                                                           set_weight=_2_set_weight,
                                                                           big_set=big_set,
                                                                           partitions=partitions)
        else:
            end = True
            big_set.insert(0, {'v_num': v_num, 'helpfulness': v_helpfulness})
            sort_vertices_helpfulness(G, big_set)

    return _2_set_helpfulness, _2_set_weight
Exemple #8
0
def find_vertices_to_balance_weighted(G, weight_to_balance,
                                      vertices_helpfulness, partitions,
                                      adj_partition, cur_partition):
    vertices_helpfulness_to_consider, adj_set = find_vertices_adj_to_another_partition(
        G, partitions, vertices_helpfulness, adj_partition, cur_partition)

    vertices_to_balance = set()

    set_weight = 0
    end = False

    while not end and len(vertices_helpfulness_to_consider
                          ) > 0 and set_weight < weight_to_balance:
        vertex_num, vertex_helpfulness = pop_vertex(
            vertices_helpfulness_to_consider)
        vertex_weight = G.nodes[vertex_num]['data']['weight']
        if set_weight + vertex_weight > weight_to_balance:
            end = True
        else:
            set_weight += G.nodes[vertex_num]['data']['weight']
            vertices_to_balance.add(vertex_num)
            update_adjacent_vertices(G=G,
                                     v_num=vertex_num,
                                     partitions=partitions,
                                     vertices_helpfulness=vertices_helpfulness,
                                     vertices_helpfulness_to_consider=
                                     vertices_helpfulness_to_consider,
                                     adj_set=adj_set,
                                     helpful_set=vertices_to_balance,
                                     cur_partition=cur_partition)
            update_helpfulness_of_neighbours(G, vertex_num,
                                             vertices_helpfulness_to_consider,
                                             partitions)
            sort_vertices_helpfulness(G, vertices_helpfulness_to_consider)

    return vertices_to_balance, set_weight