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

Optimize greedy packing and incremental bfs

parent 256a20d3
Branches
1 merge request!9Perf optimization
......@@ -7,13 +7,13 @@ bool do_swap = true;
greedy_packing::greedy_packing(graph& g, incremental_bfs& ibfs, bool with_flow)
: graph_(g), i_bfs(ibfs), with_flow_(with_flow){
max_a = (graph_.num_nodes())/2;
flow_edges_ = &ibfs.get_flow_edges();
};
void greedy_packing::reset(std::vector<node_id>& a, std::vector<node_id>& b) {
a_ = &a;
b_ = &b;
x_.clear();
flow_edges.clear();
partitioning.clear();
a_count = 0;
......@@ -27,13 +27,13 @@ void greedy_packing::reset(std::vector<node_id>& a, std::vector<node_id>& b) {
//breadth-first-search to determine neighbors of B
void greedy_packing::bfs(){
for (unsigned int i = 0; i < b_->size(); ++i) {
for (node_id i = 0; i < b_->size(); ++i) {
node_id node = b_->operator[](i);
q.push(node);
visited[node] = true;
}
for (unsigned int i = 0; i < a_->size(); ++i) {
for (node_id i = 0; i < a_->size(); ++i) {
visited[a_->operator[](i)] = true;
}
......@@ -41,12 +41,14 @@ void greedy_packing::bfs(){
node_id node = q.front();
q.pop();
const auto& neighbors = graph_.get_adjacency(node);
const auto& adjacency = graph_.get_adjacency(node);
const auto& edge_ids = graph_.get_edge_ids(node);
for(const auto& v : neighbors){
for(node_id i = 0; i < adjacency.size(); ++i){
node_id v = adjacency[i];
if(visited[v] == false){
//falls nicht(mit_flow angegeben und knoten unter den flow_edges ist)dann push in zu x
if(!(with_flow_ && (std::find(flow_edges.begin(), flow_edges.end(), graph_.get_edge_id(node, v)) != flow_edges.end()))){
if(!with_flow_ && !flow_edges_->operator[](edge_ids[i]-1) ){
x_.push_back(v);
visited[v] = true;
}
......@@ -66,7 +68,6 @@ void greedy_packing::run(){
i_bfs.reset(*a_, *b_);
i_bfs.run();
flow_ = i_bfs.get_max_flow();
flow_edges = i_bfs.get_flow_edges();
}
a_count = a_->size();
......@@ -91,9 +92,14 @@ void greedy_packing::run(){
found_new_element = false;
for (auto& partition : partitioning) {
node_id v = partition.back();
for (const auto& node : graph_.get_adjacency(v)) {
const auto& adjacency = graph_.get_adjacency(v);
const auto& edge_ids = graph_.get_edge_ids(v);
for(node_id i = 0; i < adjacency.size(); ++i){
node_id node = adjacency[i];
if (!visited[node]) {
if(!(with_flow_ && (std::find(flow_edges.begin(), flow_edges.end(), graph_.get_edge_id(node, v)) != flow_edges.end()))){
if(!with_flow_ && !flow_edges_->operator[](edge_ids[i]-1) ){
partition.push_back(node);
visited[node] = true;
found_new_element = true;
......
......@@ -127,11 +127,4 @@ bool incremental_bfs::grow(ibfs_subtree& st) {
return true;
}
std::vector<unsigned int> incremental_bfs::get_flow_edges() const {
std::vector<unsigned int> result;
for (unsigned int i = 0; i < flow_edges_.size(); ++i) {
if (flow_edges_[i]) result.push_back(i+1);
}
return result;
}
......@@ -40,7 +40,7 @@ private:
std::queue<node_id> q;
std::vector<edge_id> flow_edges;
std::vector<bool>* flow_edges_;
std::vector<bool> visited;
......
......@@ -31,7 +31,9 @@ public:
/** @brief Returns edges with flow > 0
*/
std::vector<unsigned int> get_flow_edges() const;
std::vector<bool>& get_flow_edges() {
return flow_edges_;
};
private:
enum subtree { s = -1, t = 1, none, s_root, t_root };
......
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