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

partition::partition(graph* g) : supergraph(g), outgoing_edges(0) {
    // Assigns all vertices to none
    for (unsigned int i = 0; i < g->num_vertices(); i++) {
        vertex_assignments[i] = none;
    }
    // Initializes vertex counting map
    vertices[sg_a] = 0;
    vertices[sg_b] = 0;
    vertices[none] = g->num_vertices();
}

unsigned int partition::num_vertices(subgraph sg) {
    return vertices[sg];
}

void partition::assign_vertex(vertex_id v, subgraph sg) {
    // Returns if v is already assigned to a subgraph
    if (vertex_assignments[v-1] != none) {
        return;
    }
    // Increments outgoing edges
    for (auto const& target : supergraph->get_adjacency(v)) {
        if (vertex_assignments[target-1] == -sg) {
            outgoing_edges++;    
        }
    }
    // Assigns vertex to subgraph
    vertex_assignments[v-1] = sg;
    vertices[sg]++;
    vertices[none]--;
}

void partition::unassign_vertex(vertex_id v) {
    subgraph sg = vertex_assignments[v-1];
    // Returns if v is not assigned to a subgraph
    if (sg == none) {
        return;
    }
    // Decrements outgoing edges
    for (auto const& target : supergraph->get_adjacency(v)) {
        if (vertex_assignments[target-1] == -sg) {
            outgoing_edges--;
        }
    }
    // Reverts assignment to subgraph 
    vertex_assignments[v-1] = none;
    vertices[sg]--;
    vertices[none]++;
}