Skip to content
Snippets Groups Projects
partition.cpp 1.41 KiB
Newer Older
#include <gp-bnb/partition.hpp>

p-hamann's avatar
p-hamann committed
partition::partition(graph* g) : supergraph_(g) {
    // Assigns all vertices to none
    for (unsigned int i = 0; i < g->num_vertices(); i++) {
p-hamann's avatar
p-hamann committed
        vertex_assignments_[i] = none;
    }
    // Initializes vertex counting map
p-hamann's avatar
p-hamann committed
    vertices_[sg_a] = 0;
    vertices_[sg_b] = 0;
    vertices_[none] = g->num_vertices();
}

unsigned int partition::num_vertices(subgraph sg) {
p-hamann's avatar
p-hamann committed
    return vertices_[sg];
}

void partition::assign_vertex(vertex_id v, subgraph sg) {
    // Returns if v is already assigned to a subgraph
p-hamann's avatar
p-hamann committed
    if (vertex_assignments_[v-1] != none) {
p-hamann's avatar
p-hamann committed
    // Increments current objectives
    for (auto const& target : supergraph_->get_adjacency(v)) {
        if (vertex_assignments_[target-1] == -sg) {
            current_objective_++;    
        }
    }
    // Assigns vertex to subgraph
p-hamann's avatar
p-hamann committed
    vertex_assignments_[v-1] = sg;
    vertices_[sg]++;
    vertices_[none]--;
}

void partition::unassign_vertex(vertex_id v) {
p-hamann's avatar
p-hamann committed
    subgraph sg = vertex_assignments_[v-1];
    // Returns if v is not assigned to a subgraph
    if (sg == none) {
        return;
    }
p-hamann's avatar
p-hamann committed
    // Decrements current objectives
    for (auto const& target : supergraph_->get_adjacency(v)) {
        if (vertex_assignments_[target-1] == -sg) {
            current_objective_--;
        }
    }
    // Reverts assignment to subgraph 
p-hamann's avatar
p-hamann committed
    vertex_assignments_[v-1] = none;
    vertices_[sg]--;
    vertices_[none]++;