Skip to content
Snippets Groups Projects
Commit 15a8eab6 authored by Florian Heinrichs's avatar Florian Heinrichs
Browse files

Merge branch '7-greedy-packing' into 'master'

Resolve "Greedy Packing"

Closes #7

See merge request !8
parents 10cf2713 d2186b0b
Branches
1 merge request!8Resolve "Greedy Packing"
......@@ -59,7 +59,12 @@ unsigned int solver::get_lower(){
return pr.get_max_flow();
}
else if(lb_algorithm_ == lb::gp){
bool with_flow = true;
auto g_p = greedy_packing(graph_, sources, sinks, i_bfs_, with_flow);
g_p.run();
if(with_flow)
return g_p.get_max_flow();
return g_p.get_max_flow() + partition_.current_objective();
}
return partition_.current_objective();
}
......
#include <algorithm>
#include <gp-bnb/greedy_packing.hpp>
#include <iostream>
bool do_swap = true;
greedy_packing::greedy_packing(graph& g, std::vector<node_id> A, std::vector<node_id> B, incremental_bfs& ibfs, bool with_flow)
: graph_(g), a(A), b(B), i_bfs(ibfs), with_flow_(with_flow){
if(do_swap && A.size() > B.size()){
std::swap(a, b);
}
visited.resize(graph_.num_nodes() +1, false);
max_a = (graph_.num_nodes()+1)/2;
};
//breadth-first-search to determine neighbors of B
void greedy_packing::bfs(){
for(node_id node : b){
q.push(node);
visited[node] = true;
}
for(node_id node : a){
visited[node] = true;
}
while(!q.empty()){
node_id node = q.front();
q.pop();
std::vector<node_id> neighbors = graph_.get_adjacency(node);
for(node_id v : neighbors){
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())))
x.push_back(v);
}
visited[v] = true;
}
}
return;
};
void greedy_packing::run(){
flow_ = 0;
if(with_flow_){
//get all flow edges and the flow
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();
if(a_count >= max_a)
return;
//x per bfs konstruieren
bfs();
//P konstruieren
partitioning.resize(x.size());
//dafür für jeden nachbarn von B einen eigenen Block
for(unsigned int i = 0; i <x.size(); i++){
partitioning[i].push_back(x[i]);
}
//dann jeweils kleinsten block um einen nachbarn erweitern
//baue mit B zusammenhängende Partitionen (noch ohne lokale Suche) und sortiere nach Größe absteigend
bool found_new_element = true;
while (found_new_element) {
found_new_element = false;
for (std::vector<node_id> partition : partitioning) {
for (node_id node : graph_.get_adjacency(partition.back())) {
if (!visited[node]) {
partition.push_back(node);
visited[node] = true;
found_new_element = true;
break;
}
}
}
}
//danach lokale suche um balance zu verbessern ?
std::sort(partitioning.begin(), partitioning.end(), [](std::vector<node_id> a, std::vector<node_id> b) {
return a.size() > b.size();
});
// behandle von B aus unnerreichbare Knoten
for (node_id i = 1; i <= graph_.num_nodes(); i++) {
if (!visited[i]) {
a_count++;
if (a_count >= max_a){
return;
}
}
}
// packe greedy (erst hier wird flow erhöht)
for (std::vector<node_id> partition : partitioning) {
flow_++;
a_count += partition.size();
if (a_count >= max_a){
return;
}
}
return;
};
......@@ -9,6 +9,7 @@
#include <gp-bnb/edmonds_karp.hpp>
#include <gp-bnb/incremental_bfs.hpp>
#include <gp-bnb/push_relabel.hpp>
#include <gp-bnb/greedy_packing.hpp>
namespace gp_bnb {
......
#ifndef GREEDYPACKING_H_
#define GREEDYPACKING_H_
#include <queue>
#include <vector>
#include <gp-bnb/graph.hpp>
#include <gp-bnb/incremental_bfs.hpp>
class greedy_packing{
public:
greedy_packing(graph& g, std::vector<node_id> a, std::vector<node_id> b, incremental_bfs& ibfs, bool with_flow);
void run();
int get_max_flow() const{
return flow_;
};
private:
void bfs();
graph& graph_;
//partial partition into blocks a and b
//x is block with the neighbors of B
std::vector<node_id> a, b, x;
incremental_bfs& i_bfs;
bool with_flow_;
std::vector<std::vector<node_id>> partitioning;
std::queue<node_id> q;
std::vector<edge_id> flow_edges;
std::vector<bool> visited;
unsigned int a_count, max_a, flow_;
};
#endif /* GREEDY_PACKING_H_ */
......@@ -17,6 +17,7 @@ executable('gp-bnb',
'bnb/ibfs_subtree.cpp',
'bnb/push_relabel.cpp',
'bnb/bnb.cpp',
'bnb/greedy_packing.cpp',
include_directories: inc)
......
......@@ -13,6 +13,7 @@ testexe = executable(
'../bnb/ibfs_subtree.cpp',
'../bnb/push_relabel.cpp',
'../bnb/bnb.cpp',
'../bnb/greedy_packing.cpp',
include_directories : inc) # declared include directories in root :code:`meson.build`
# test execution
......
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