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

fixed correctness - to-do: packing-flow bound

parent 2da68e72
Branches
1 merge request!8Resolve "Greedy Packing"
......@@ -2,27 +2,30 @@
#include <gp-bnb/greedy_packing.hpp>
#include <iostream>
bool do_swap = true;
greedy_packing::greedy_packing(const graph& g, std::vector<node_id> a, std::vector<node_id> b)
: graph_(g), a(a), b(b) {
max_a = graph_.num_nodes()/2;
greedy_packing::greedy_packing(const graph& g, std::vector<node_id> A, std::vector<node_id> B)
: graph_(g), a(A), b(B){
if(do_swap && A.size() > B.size()){
std::swap(a, b);
}
max_a = (graph_.num_nodes()+1)/2;
};
//breadth-first-search to determine neighbors of B
void greedy_packing::bfs(){
x.resize(graph_.num_nodes() - (a.size() + b.size()));
visited.resize(graph_.num_nodes() +1, false);
for(node_id node : b){
q.push(node);
visited[node] = true;
}
for(node_id node: a){
visited[node] = true;
}
unsigned int last_node = b.size();
unsigned int count = 0;
while(!q.empty()){
node_id node = q.front();
......@@ -31,47 +34,37 @@ void greedy_packing::bfs(){
std::vector<node_id> neighbors = graph_.get_adjacency(node);
for(node_id v : neighbors){
if(v == last_node)
end_of_neighbors = count;
if(visited[v] == false){
q.push(v);
x[count++] = v;
x.push_back(v);
}
visited[v] = true;
}
}
return;
};
void greedy_packing::run(){
flow_ = 0;
//x konstruieren: enthält alle unpartitionierten knoten
//zuerst die nachbarn von b bis zum index end_of_neighbors
//ab end_of_neighbors +1 kommen alle anderen unpartitionierten knoten
//x per bfs konstruieren
bfs();
/*
std::cerr << x.size() << std::endl;
for(int i = 0; i < x.size(); i++){
std::cerr << x[i] << " ";
}
std::cerr << " end_of_neighbors: " << end_of_neighbors << std::endl;
*/
a_count = a.size();
if(a_count >= max_a)
return;
//P konstruieren
partitioning.resize(x.size());
//dafür für jeden nachbarn von B einen eigenen Block
//dann jeweils kleinsten block um einen nachbarn erweitern
//danach lokale suche um balance zu verbessern
//knoten aus x die in keinem block von P sind sind unerreichbar von B
partitioning.resize(end_of_neighbors+1);
for(unsigned int i = 0; i <= end_of_neighbors; i++){
for(unsigned int i = 0; i <x.size(); i++){
partitioning[i].push_back(x[i]);
}
// baue mit B zusammenhängende Partitionen (noch ohne lokale Suche) und sortiere nach Größe absteigend
//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;
......@@ -86,6 +79,10 @@ void greedy_packing::run(){
}
}
}
//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();
});
......@@ -94,26 +91,31 @@ void greedy_packing::run(){
//füge von B unerreichbar knoten zu a hinzu (sind die knoten die
//bei konstruktion von P "übrig bleiben")
//danach füge Knoten aus größtem Block hinzu bis A* < n/2
// behandle von B aus unnerreichbare Knoten
for (node_id i = 1; i <= graph_.num_nodes(); ++i) {
for (node_id i = 1; i <= graph_.num_nodes(); i++) {
if (!visited[i]) {
a.push_back(i);
a_count++;
if (a_count == max_a) return;
if (a_count >= max_a){
return;
}
}
}
// packe greedy (erst hier wird flow erhöht)
for (std::vector<node_id> partition : partitioning) {
flow_++;
for (node_id node : partition) {
a.push_back(node);
a_count++;
if (a_count == max_a) return;
if (a_count >= max_a){
return;
}
}
}
return;
};
......
......@@ -23,13 +23,10 @@ private:
const graph& graph_;
//end of neighbors determines index of x where the nodes
//aren't neighbors of B anymore
unsigned int a_count, end_of_neighbors;
unsigned int a_count;
//partial partition into blocks a and b
//x is block which starts with the neighbors of B
//following the other unpartitioned nodes
//x is block with the neighbors of B
std::vector<node_id> a, b, x;
std::vector<std::vector<node_id>> partitioning;
......
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