Skip to content
Snippets Groups Projects
Commit e2d8d9b3 authored by p-hamann's avatar p-hamann
Browse files

Refactor graph identifiers

parent 114b3ed0
No related merge requests found
......@@ -11,7 +11,7 @@ namespace gp_bnb {
// trail_state implementation.
// --------------------------------------------------------------------------------------
void trail_state::push_node(vertex_id node, subgraph alt) {
void trail_state::push_node(node_id node, subgraph alt) {
stack_.push_back({node, alt});
}
......@@ -23,12 +23,12 @@ void trail_state::pop() {
// solver implementation.
// --------------------------------------------------------------------------------------
solver::solver(graph& g) : nodes_(g), partition_(partition{g}) {
solver::solver(graph& g) : graph_(g), partition_(partition{g}) {
}
void solver::solve() {
//anfangs upper bound?
best_objective_ = nodes_.num_vertices();
best_objective_ = graph_.num_nodes();
//partition vector initializieren
......@@ -56,11 +56,11 @@ void solver::solve() {
}
//wende dann die entsprechende alternative an
expand(node, alt);
}else if(trail_.length() == nodes_.num_vertices()) {
}else if(trail_.length() == graph_.num_nodes()) {
//falls wir an einem blatt im suchbaum sind
// Memorize the new solution.
best_solution_.resize(nodes_.num_vertices());
for(vertex_id node = 0; node < nodes_.num_vertices(); node++){
best_solution_.resize(graph_.num_nodes());
for(node_id node = 0; node < graph_.num_nodes(); node++){
best_solution_[node] = partition_.assigned_subgraph_of(node);
}
best_objective_ = partition_.current_objective();
......@@ -73,7 +73,7 @@ void solver::solve() {
}
}
void solver::expand(vertex_id node, subgraph sg) {
void solver::expand(node_id node, subgraph sg) {
// Search for an alternative BEFORE assigning the node.
// Because the node is not assigned, this calculation is easy.
auto alt = next_possible_subgraph(node, static_cast<subgraph>(-sg));
......@@ -82,9 +82,9 @@ void solver::expand(vertex_id node, subgraph sg) {
std::cerr << " Alternative " << alt << std::endl;
}
std::vector<unsigned int> neighbors = nodes_.get_adjacency(node);
std::vector<unsigned int> neighbors = graph_.get_adjacency(node);
partition_.assign_vertex(node, sg);
partition_.assign_node(node, sg);
trail_.push_node(node, alt);
}
......@@ -96,11 +96,11 @@ void solver::backtrack() {
std::cout << "Unassign node " << node << std::endl;
trail_.pop();
partition_.unassign_vertex(node);
partition_.unassign_node(node);
}
// Finds the next partition in which the given node can be placed.
subgraph solver::next_possible_subgraph(vertex_id node, subgraph m) {
subgraph solver::next_possible_subgraph(node_id node, subgraph m) {
// This function is only correct if the node is not assigned yet.
assert(partition_.assigned_subgraph_of(node) == subgraph::none);
......@@ -108,7 +108,7 @@ subgraph solver::next_possible_subgraph(vertex_id node, subgraph m) {
subgraph sg = m;
//falls in einer partition schon die hälfte der knoten: nicht als alt wählen
if((partition_.num_vertices_of(sg) * 2) < nodes_.num_vertices()){
if((partition_.num_nodes_of(sg) * 2) < graph_.num_nodes()){
return sg;
}
else{
......
#include <gp-bnb/graph.hpp>
graph::graph(std::vector<std::vector<unsigned int>> a) : vertices_(a.size()), adjacency_list_(a) {
graph::graph(std::vector<std::vector<unsigned int>> a) : nodes_(a.size()), adjacency_list_(a) {
}
unsigned int graph::num_vertices() const {
return vertices_;
unsigned int graph::num_nodes() const {
return nodes_;
}
std::vector<unsigned int> graph::get_adjacency(vertex_id v) const {
std::vector<unsigned int> graph::get_adjacency(node_id v) const {
return adjacency_list_[v-1];
}
......@@ -12,6 +12,6 @@ int main() {
std::string graphFile = "../test/inputs/delaunay_n10.graph";
auto g = metis_reader().read(graphFile);
std::cout << "Number of nodes: " << g.num_vertices() << std::endl;
std::cout << "Number of nodes: " << g.num_nodes() << std::endl;
}
......@@ -2,60 +2,60 @@
#include <gp-bnb/partition.hpp>
partition::partition(graph& g) : supergraph_(g) {
// Assigns all vertices to none
for (unsigned int i = 0; i < g.num_vertices(); i++) {
vertex_assignments_[i] = none;
partition::partition(graph& g) : graph_(g) {
// Assigns all nodes to none
for (unsigned int i = 0; i < g.num_nodes(); i++) {
node_assignments_[i] = none;
}
// Initializes vertex counting map
vertices_[sg_a] = 0;
vertices_[sg_b] = 0;
vertices_[none] = g.num_vertices();
// Initializes node counting map
nodes_[sg_a] = 0;
nodes_[sg_b] = 0;
nodes_[none] = g.num_nodes();
}
unsigned int partition::num_vertices_of(subgraph sg) const {
return vertices_.at(sg);
unsigned int partition::num_nodes_of(subgraph sg) const {
return nodes_.at(sg);
}
unsigned int partition::current_objective() const {
return current_objective_;
}
partition::subgraph partition::assigned_subgraph_of(vertex_id v) const {
return vertex_assignments_[v-1];
partition::subgraph partition::assigned_subgraph_of(node_id v) const {
return node_assignments_[v-1];
}
void partition::assign_vertex(vertex_id v, subgraph sg) {
assert(vertex_assignments_[v-1] != none);
void partition::assign_node(node_id v, subgraph sg) {
assert(node_assignments_[v-1] != none);
// Increments current objectives
for (auto const& target : supergraph_.get_adjacency(v)) {
if (vertex_assignments_[target-1] == -sg) {
for (auto const& target : graph_.get_adjacency(v)) {
if (node_assignments_[target-1] == -sg) {
current_objective_++;
}
}
// Assigns vertex to subgraph
vertex_assignments_[v-1] = sg;
vertices_[sg]++;
vertices_[none]--;
// Assigns node to subgraph
node_assignments_[v-1] = sg;
nodes_[sg]++;
nodes_[none]--;
}
void partition::unassign_vertex(vertex_id v) {
subgraph sg = vertex_assignments_[v-1];
void partition::unassign_node(node_id v) {
subgraph sg = node_assignments_[v-1];
assert(sg == none);
// Decrements current objectives
for (auto const& target : supergraph_.get_adjacency(v)) {
if (vertex_assignments_[target-1] == -sg) {
for (auto const& target : graph_.get_adjacency(v)) {
if (node_assignments_[target-1] == -sg) {
current_objective_--;
}
}
// Reverts assignment to subgraph
vertex_assignments_[v-1] = none;
vertices_[sg]--;
vertices_[none]++;
node_assignments_[v-1] = none;
nodes_[sg]--;
nodes_[none]++;
}
......@@ -16,7 +16,7 @@ struct trail_state {
private:
struct decision {
// Index of node that is assigned at a certain level of the search tree.
vertex_id node;
node_id node;
// Next alternative (or none if no alternative exists).
subgraph alt;
......@@ -24,7 +24,7 @@ private:
public:
// Extends the current path by adding another node.
void push_node(vertex_id node, subgraph alt);
void push_node(node_id node, subgraph alt);
// Reduces the current path by removing the last node.
void pop();
......@@ -33,7 +33,7 @@ public:
return stack_.size();
}
vertex_id node_at(size_t n) const {
node_id node_at(size_t n) const {
return stack_[n].node;
}
......@@ -54,7 +54,7 @@ struct solver {
// Read-only access to the nodes.
const graph &nodes() const {
return nodes_;
return graph_;
}
// Read-only access to the optimal solution.
......@@ -63,7 +63,7 @@ struct solver {
}
// Expand a search tree node, i.e., assigns an node to a partition and update all data structures.
void expand(vertex_id node, subgraph sg);
void expand(node_id node, subgraph sg);
// Performs backtracking after the solver ran into a dead end,
// i.e., the current branch has been pruned.
......@@ -73,9 +73,9 @@ struct solver {
void solve();
private:
subgraph next_possible_subgraph(vertex_id node, subgraph sg);
subgraph next_possible_subgraph(node_id node, subgraph sg);
graph nodes_;
graph graph_;
partition partition_;
trail_state trail_;
......
......@@ -3,7 +3,7 @@
#include <vector>
using vertex_id = unsigned int;
using node_id = unsigned int;
// Represents the input graph to be partitioned
class graph {
......@@ -14,19 +14,19 @@ public:
*/
graph(std::vector<std::vector<unsigned int>> a);
/** @brief Returns the number of vertices of the graph
* @return Number of vertices
/** @brief Returns the number of nodes of the graph
* @return Number of nodes
*/
unsigned int num_vertices() const;
unsigned int num_nodes() const;
/** @brief Provides access to the adjacency of a vertex
* @param Vertex Id
* @return Adjacency of the vertex
/** @brief Provides access to the adjacency of a node
* @param Node Id
* @return Adjacency of the node
*/
std::vector<unsigned int> get_adjacency(vertex_id v) const;
std::vector<unsigned int> get_adjacency(node_id v) const;
private:
unsigned int vertices_;
unsigned int nodes_;
std::vector<std::vector<unsigned int>> adjacency_list_;
};
......
......@@ -5,7 +5,7 @@
#include <vector>
#include <gp-bnb/graph.hpp>
using vertex_id = unsigned int;
using node_id = unsigned int;
// Represents potential solutions
class partition {
......@@ -17,38 +17,38 @@ public:
*/
partition(graph& g);
/** @brief Gets the number of vertices of an element of the partition
/** @brief Gets the number of nodes of an element of the partition
* @param Subgraph A, B or none
* @return Number of vertices of sg
* @return Number of nodes of sg
*/
unsigned int num_vertices_of(subgraph sg) const;
unsigned int num_nodes_of(subgraph sg) const;
/** @brief Gets the number of edges between subgraph A and B
* @return Objective to be minimized
*/
unsigned int current_objective() const;
/** @brief Gets the assignment of a vertex
* @param Vertex Id
/** @brief Gets the assignment of a node
* @param Node Id
* @return Subgraph A, B or none
*/
subgraph assigned_subgraph_of(vertex_id v) const;
subgraph assigned_subgraph_of(node_id v) const;
/** @brief Assigns a vertex to a subgraph
* @param Vertex Id
/** @brief Assigns a node to a subgraph
* @param Node Id
* @param Subgraph A or B
*/
void assign_vertex(vertex_id v, subgraph sg);
void assign_node(node_id v, subgraph sg);
/** @brief Reverts the assignment of a vertex to a subgraph
* @param Vertex Id
/** @brief Reverts the assignment of a node to a subgraph
* @param Node Id
*/
void unassign_vertex(vertex_id v);
void unassign_node(node_id v);
private:
graph supergraph_;
std::vector<subgraph> vertex_assignments_;
std::map<subgraph,unsigned int> vertices_;
graph graph_;
std::vector<subgraph> node_assignments_;
std::map<subgraph,unsigned int> nodes_;
unsigned int current_objective_ = 0;
};
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment