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