Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Algorithms/ColoringAlgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
std::tuple<int, std::vector<int>> ColoringAlgorithms::TupleNumOfColorAndColors() {
std::vector<int> colors;
std::set<int> unique_colors;

ForEachVertex(GraphInstance, [&](int v) {
colors.push_back(get(vertex_color, GraphInstance, v));
int the_color = get(vertex_color, GraphInstance, v) - 1;
Expand Down
12 changes: 7 additions & 5 deletions Algorithms/ColoringAlgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ColoringAlgorithms {
*/
std::tuple<int, std::vector<int>> TupleNumOfColorAndColors();
public:
virtual ~ColoringAlgorithms() = default;
explicit ColoringAlgorithms(Graph &G_b) : GraphInstance(G_b) { SetAllColorsTo(0);};

ColoringAlgorithms(Graph &G_b, vector<unsigned int> &V, bool restricted = false, map<string, any>&& pars = {})
Expand Down Expand Up @@ -89,11 +90,11 @@ class ColoringAlgorithms {
*
* @return Number of colors
*/
int NumOfColors() {
[[nodiscard]] int NumOfColors() const
{
int max_color = 0;
ForEachVertex(GraphInstance, [&](Ver v) {
int color = get(vertex_color, GraphInstance, v);
if (max_color < color)
ForEachVertex(GraphInstance, [&](const Ver& v) {
if (const int color = get(vertex_color, GraphInstance, v); max_color < color)
max_color = color;
});
return max_color;
Expand All @@ -104,7 +105,8 @@ class ColoringAlgorithms {
*
* @param value the given value
*/
void SetAllColorsTo(int value) {
void SetAllColorsTo(const int value) const
{
ForEachVertexConst(GraphInstance, [&](Ver v) {
boost::put(vertex_color, GraphInstance, v, value);
});
Expand Down
4 changes: 2 additions & 2 deletions Algorithms/GreedyColoringSimpleGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
* Output:
* - G a simple graph with colors as weights vertex_color
*/
class GreedyColoringSimpleGraph : public ColoringAlgorithms {
class GreedyColoringSimpleGraph final : public ColoringAlgorithms {
public:
using ColoringAlgorithms::ColoringAlgorithms;

int color();
int color() override;
};


Expand Down
13 changes: 9 additions & 4 deletions Algorithms/GreedyColoringSimpleGraphBoost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
int GreedyColoringSimpleGraphBoost::color() {
Graph &g = GraphInstance;
typedef graph_traits<Graph>::vertices_size_type vertices_size_type;
typedef property_map<Graph, boost::vertex_index_t>::const_type vertex_index_map;
std::vector<vertices_size_type> color_vec(boost::num_vertices(g));
boost::iterator_property_map<vertices_size_type*, vertex_index_map> color(&color_vec.front(), get(boost::vertex_index, g));
auto num_colors = boost::sequential_vertex_coloring(g, color);
const boost::iterator_property_map color(&color_vec.front(), get(boost::vertex_index, g));
auto ordering = boost::copy_range<std::vector<Ver>>(boost::vertices(g));
std::ranges::sort(ordering, std::greater<>{},
[&g](Ver v) { return degree(v, g); });
const auto index_map = get(boost::vertex_index, g);
const auto order_map = boost::make_safe_iterator_property_map(
ordering.begin(), ordering.size(), index_map);
const auto num_colors = boost::sequential_vertex_coloring(g, order_map, color);
ForEachVertexConst(g, [&](Ver v) {
boost::put(vertex_color, g, v, color[v]);
});
return num_colors;
return static_cast<int>(num_colors);
}
4 changes: 2 additions & 2 deletions Algorithms/OneSidedD2ColorNonReqDiag.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class OneSidedD2ColorNonReqDiag : public ColoringAlgorithms {

//Find the first color which can be assigned to v
auto result = find_if(forbiddenColors.begin(), forbiddenColors.end(),
bind1st(not_equal_to<int>(), v));
bind(not_equal_to<int>(), v, std::placeholders::_1));

int col1 = distance(forbiddenColors.begin(), result);
///////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -66,7 +66,7 @@ class OneSidedD2ColorNonReqDiag : public ColoringAlgorithms {

//Find first color which can be assigned to v
result = find_if(forbiddenColors.begin(), forbiddenColors.end(),
bind1st(not_equal_to<int>(), v));
bind(not_equal_to<int>(), v, std::placeholders::_1));


int col2 = distance(forbiddenColors.begin(), result);
Expand Down
61 changes: 28 additions & 33 deletions Algorithms/OneSidedD2Coloring.h
Original file line number Diff line number Diff line change
@@ -1,63 +1,58 @@
//
// Created by rostam on 20.07.16.
// Created by rostam on 20.07.16
//

#ifndef PRECOL_ONESIDEDD2COLORING_H
#define PRECOL_ONESIDEDD2COLORING_H

#include "ColoringAlgorithms.h"

/**
* \brief Compute one-sided distance-2 coloring of a bipartite graph GraphInstance
* \brief One-sided distance-2 coloring for bipartite graphs.
*
* It can be also IsRestrictedColoring to some required edges if the
* IsRestrictedColoring value is equal to true
* Performs a one-sided distance-2 coloring on the vertex set V of a bipartite graph.
* Optionally supports restricted coloring based on required edges.
*
* Input:
* - GraphInstance bipartite graph
* - V contained vertices are colored in the given ordering v_1, ..., v_n
* - GraphInstance: The bipartite graph to color.
* - V_c: The vertex subset to color, provided in a specific order.
*
* Output:
* - GraphInstance bipartite graph with colors as weights vertex_color
* - Colors assigned to vertices in GraphInstance, stored via vertex_color property.
*/
class OneSidedD2Coloring : public ColoringAlgorithms {
class OneSidedD2Coloring final : public ColoringAlgorithms {
public:
using ColoringAlgorithms::ColoringAlgorithms;

int color() {
vector<unsigned int> V = V_c;
property_map<Graph, vertex_color_t>::type color = get(vertex_color, GraphInstance);
vector<unsigned int> forbiddenColors(NumOfVertices(GraphInstance), -1);
//All edges in E_S have edge_weight=1; otherwise edge_weight=0
int color() override {
const std::vector<unsigned int>& V = V_c;
std::vector forbiddenColors(NumOfVertices(GraphInstance), static_cast<unsigned int>(-1));

//Iterate over all vertices which should be colored
for_each(V.begin(), V.end(), [&](unsigned int v) {
for (unsigned int v : V) {
// Reset forbidden color tracking for current vertex
forbiddenColors[0] = v;

if (neighbors::IncidentToReqEdge(GraphInstance, v)) {
//Get the distance-2 neighbors
vector<unsigned int> N_2 = neighbors::Distance2NeighborsRestricted(GraphInstance, v);

//Iterate over distance-2 neighbors
for_each(N_2.begin(), N_2.end(), [&](unsigned int n_2) {
//Mark colors which are used by distance-2 neighbors in forbiddenColors
if (get(vertex_color, GraphInstance, n_2) > 0) {
forbiddenColors[get(vertex_color, GraphInstance, n_2)] = v;
for (std::vector<unsigned int> N2 = neighbors::Distance2NeighborsRestricted(GraphInstance, v); unsigned int n2 : N2) {
if (const unsigned int n2_color = get(vertex_color, GraphInstance, n2); n2_color > 0) {
forbiddenColors[n2_color] = v;
}
});
}

//Find first color which can be assigned to v
auto result = find_if(forbiddenColors.begin(), forbiddenColors.end(), std::bind1st(std::not_equal_to<int>(), v));
// Find the first color not forbidden for this vertex
const auto available_color = std::ranges::find_if(forbiddenColors,
[v](const unsigned int c) { return c != v; });

//Color v
SetVertexColor(GraphInstance, v,distance(forbiddenColors.begin(), result));
// put(color, v, distance(forbiddenColors.begin(), result));
const unsigned int assigned_color = std::distance(forbiddenColors.begin(), available_color);
SetVertexColor(GraphInstance, v, assigned_color);
} else {
// Assign color 0 if no restriction
SetVertexColor(GraphInstance, v, 0);
// put(color, v, 0);
}
});
}

return NumOfColors();
}
};


#endif //PRECOL_ONESIDEDD2COLORING_H
#endif // PRECOL_ONESIDEDD2COLORING_H
2 changes: 1 addition & 1 deletion Algorithms/OneSidedD2ColoringNonReq.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class OneSidedD2ColoringNonReq : public ColoringAlgorithms {

//Find first color which can be assigned to v
vector<unsigned int>::iterator result = find_if(forbiddenColors.begin(), forbiddenColors.end(),
std::bind1st(std::not_equal_to<int>(), v));
std::bind(std::not_equal_to<int>(), v, std::placeholders::_1));

//Color v
SetVertexColor(GraphInstance, v, distance(forbiddenColors.begin(), result));
Expand Down
7 changes: 4 additions & 3 deletions Algorithms/OneSidedD2ColoringNonReqBalanced.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ class OneSidedD2ColoringNonReqBalanced : public ColoringAlgorithms {
}
});

//Find first color which can be assigned to v
vector<unsigned int>::iterator result = find_if(forbiddenColors.begin(), forbiddenColors.end(),
std::bind1st(std::not_equal_to<int>(), v));
//Find the first color which can be assigned to v
vector<unsigned int>::iterator result = find_if(forbiddenColors.begin(),
forbiddenColors.end(),
std::bind(std::not_equal_to<int>(), v, std::placeholders::_1));

//Color v
SetVertexColor(GraphInstance, v, distance(forbiddenColors.begin(), result));
Expand Down
6 changes: 3 additions & 3 deletions Algorithms/StarBicoloringVertexCover.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ class StarBicoloringVertexCover : public ColoringAlgorithms {
});
}
});
//Find first color which can be assigned to v_c
//Find the first color which can be assigned to v_c
vector<int>::iterator color_v_it = find_if(forbiddenColors.begin(),
forbiddenColors.end(),
bind1st(not_equal_to<int>(), *v)
bind(not_equal_to<int>(), *v, std::placeholders::_1)
);
SetVertexColor(GraphInstance, *v, distance(forbiddenColors.begin(), color_v_it));
// put(color, *v, distance(forbiddenColors.begin(), color_v_it));
Expand Down Expand Up @@ -203,7 +203,7 @@ class StarBicoloringVertexCover : public ColoringAlgorithms {
}
//Find first color which can be assigned to v_c
auto color_v_it = find_if(forbiddenColors.begin(), forbiddenColors.end(),
bind1st(not_equal_to<int>(), *v));
bind(not_equal_to<int>(), *v, std::placeholders::_1));
SetVertexColor(GraphInstance, *v, distance(forbiddenColors.begin(), color_v_it));
// put(color, *v, distance(forbiddenColors.begin(), color_v_it));
}
Expand Down
12 changes: 6 additions & 6 deletions Algorithms/StarBicoloringVertexCoverNonReq.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ class StarBicoloringVertexCoverNonReq : public ColoringAlgorithms{
}
}
}
//Find first color which can be assigned to v_c
vector<int>::iterator color_v_it = find_if(forbiddenColors.begin(),
//Find the first color which can be assigned to v_c
std::vector<int>::iterator color_v_it = find_if(forbiddenColors.begin(),
forbiddenColors.end(),
bind1st(not_equal_to<int>(), *v)
bind(not_equal_to<int>(), *v, std::placeholders::_1)
);
SetVertexColor(GraphInstance, *v, distance(forbiddenColors.begin(), color_v_it));
// put(color, *v, distance(forbiddenColors.begin(), color_v_it));
Expand Down Expand Up @@ -233,7 +233,7 @@ class StarBicoloringVertexCoverNonReq : public ColoringAlgorithms{
//Find first color which can be assigned to v_c
vector<int>::iterator color_v_it = find_if(forbiddenColors.begin(),
forbiddenColors.end(),
bind1st(not_equal_to<int>(), *v)
bind(not_equal_to<int>(), *v, std::placeholders::_1)
);
SetVertexColor(GraphInstance, *v, distance(forbiddenColors.begin(),color_v_it));
// put(color,*v,distance(forbiddenColors.begin(),color_v_it));
Expand Down Expand Up @@ -503,7 +503,7 @@ class StarBicoloringVertexCoverNonReq : public ColoringAlgorithms{
//Find first color which can be assigned to v_c
vector<int>::iterator color_v_it = find_if(forbiddenColors.begin(),
forbiddenColors.end(),
bind1st(not_equal_to<int>(), *v)
bind(not_equal_to<int>(), *v, std::placeholders::_1)
);
SetVertexColor(GraphInstance, *v, distance(forbiddenColors.begin(), color_v_it));
// put(color, *v, distance(forbiddenColors.begin(), color_v_it));
Expand Down Expand Up @@ -591,7 +591,7 @@ class StarBicoloringVertexCoverNonReq : public ColoringAlgorithms{
//Find first color which can be assigned to v_c
vector<int>::iterator color_v_it = find_if(forbiddenColors.begin(),
forbiddenColors.end(),
bind1st(not_equal_to<int>(), *v)
bind(not_equal_to<int>(), *v, std::placeholders::_1)
);
SetVertexColor(GraphInstance, *v, distance(forbiddenColors.begin(), color_v_it));
// put(color, *v, distance(forbiddenColors.begin(), color_v_it));
Expand Down
23 changes: 12 additions & 11 deletions Algorithms/algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,22 @@ static vector<string> algs = {"D2Columns", "D2Rows", "GreedyColoring","GreedyCol
* @param G_b the input bipartite graph
* @param V_r the row vertices
* @param V_c the column vertices
* @param order the ordering of vertices for coloring
* @param alpha
* @param MaxColor
* @return
*/
static shared_ptr<ColoringAlgorithms> getAlg(int Mode2, const string &alg,
int Mode, Graph &G_b, vector<unsigned int> &V_r,
vector<unsigned int> &V_c,
unique_ptr<Ordering>& order, int alpha, int MaxColor = 0) {
static shared_ptr<ColoringAlgorithms> getAlg(int Mode2, const string& alg,
int Mode, Graph& G_b, vector<unsigned int>& V_r,
vector<unsigned int>& V_c,
int alpha, int MaxColor = 0) {
if (alg == "D2Columns") {
return shared_ptr<ColoringAlgorithms>(new OneSidedD2Coloring(G_b, V_c, false));
return std::make_shared<OneSidedD2Coloring>(G_b, V_c, false);
} else if (alg == "D2Rows") {
return shared_ptr<ColoringAlgorithms>(new OneSidedD2Coloring(G_b, V_r, false));
return std::make_shared<OneSidedD2Coloring>(G_b, V_r, false);
} else if (alg == "D2RestrictedColumns") {
return shared_ptr<ColoringAlgorithms>(new OneSidedD2Coloring(G_b, V_c, true));
return std::make_shared<OneSidedD2Coloring>(G_b, V_c, true);
} else if (alg == "D2RestrictedRows") {
return shared_ptr<ColoringAlgorithms>(new OneSidedD2Coloring(G_b, V_r, true));
return std::make_shared<OneSidedD2Coloring>(G_b, V_r, true);
} else if (alg == "D2RestrictedColumnsNonReq") {
return shared_ptr<ColoringAlgorithms>(new OneSidedD2ColoringNonReq(G_b, V_c, true, {{"alpha", alpha}}));
} else if (alg == "D2RestrictedColumnsNonReqBalanced") {
Expand All @@ -67,9 +68,9 @@ static shared_ptr<ColoringAlgorithms> getAlg(int Mode2, const string &alg,
return shared_ptr<ColoringAlgorithms>(new StarBicoloringVertexCoverNonReq(G_b, V_r, V_c, true,
{{"Mode", Mode},{"Mode2",Mode2},{"alpha",alpha}}));
} else if (alg == "GreedyColoringBoost") {
return shared_ptr<ColoringAlgorithms>(new GreedyColoringSimpleGraphBoost(G_b));
return std::make_shared<GreedyColoringSimpleGraphBoost>(G_b);
} else if (alg == "GreedyColoring") {
return shared_ptr<ColoringAlgorithms>(new GreedyColoringSimpleGraph(G_b, V_c));
return std::make_shared<GreedyColoringSimpleGraph>(G_b, V_c);
} else if (alg == "GreedyColoringLimitedMaxColor") {
return shared_ptr<ColoringAlgorithms>(new GreedyColoringLimitedMaxColor(G_b, V_c, false, {{"MaxColor", MaxColor}}));
} else {
Expand Down
2 changes: 1 addition & 1 deletion Algorithms/d2_color_omp.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class D2ColorOMP : public ColAlg {

//Find first color which can be assigned to v
auto result = find_if(forbiddenColors.begin(), forbiddenColors.end(),
bind1st(std::not_equal_to<int>(), v));
bind(std::not_equal_to<int>(), v, std::placeholders::_1));

//Color v
put(color, v, distance(forbiddenColors.begin(), result));
Expand Down
2 changes: 1 addition & 1 deletion Application/Preconditioning/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int main(int argc, char *argv[]) {
ColoringOrder->OrderGivenVertexSubset(G_b, V_r, ColoringAlgorithm.find("Restricted") != string::npos);
// ApplyColoringOrder(ColoringAlgorithm, ColoringOrder, GraphInstance, V_r, V_c);
//Coloring of the vertices
int cols = getAlg(Mode2, ColoringAlgorithm, Mode, G_b, V_r, V_c, ColoringOrder, AlphaForBalancedColoring)->color();
int cols = getAlg(Mode2, ColoringAlgorithm, Mode, G_b, V_r, V_c, AlphaForBalancedColoring)->color();
end = clock();
//all edges A - \Rinit
vector<Edge> edge_ordering;
Expand Down
2 changes: 1 addition & 1 deletion BoostTest/ApplicationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <iomanip>

#define BOOST_TEST_DYN_LINK
//#define BOOST_TEST_MAIN - dont need to repeat this define in more than one cpp file
//#define BOOST_TEST_MAIN - don't need to repeat this definition in more than one cpp file
BOOST_AUTO_TEST_SUITE(ApplicationTestSuite)
BOOST_AUTO_TEST_CASE(SILUTest) {

Expand Down
Loading