Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CoinBronKerbosch: Replace magic number for pivoting strategy #237

Merged
merged 1 commit into from
Aug 13, 2024
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
16 changes: 8 additions & 8 deletions src/CoinBronKerbosch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bool compareNodes(const BKVertex &u, const BKVertex &v) {
return u.fitness >= v.fitness + BK_EPS;
}

CoinBronKerbosch::CoinBronKerbosch(const CoinConflictGraph *cgraph, const double *weights, size_t pivotingStrategy)
CoinBronKerbosch::CoinBronKerbosch(const CoinConflictGraph *cgraph, const double *weights, PivotingStrategy pivotingStrategy)
: nVertices_(0)
, minWeight_(0.0)
, calls_(0)
Expand Down Expand Up @@ -351,38 +351,38 @@ size_t CoinBronKerbosch::numCalls() const {

void CoinBronKerbosch::computeFitness(const double *weights) {
switch (pivotingStrategy_) {
case 0:
case PivotingStrategy::Off:
//do nothing
break;
case 1: { //random
case PivotingStrategy::Random: {
shuffle_vertices(vertices_, nVertices_);
break;
}
case 2: { //degree
case PivotingStrategy::Degree: {
for (size_t u = 0; u < nVertices_; u++) {
const size_t uIdx = vertices_[u].idx;
vertices_[u].fitness = cgraph_->degree(uIdx);
}
std::sort(vertices_, vertices_ + nVertices_, compareNodes);
break;
}
case 3: { //weight
case PivotingStrategy::Weight: {
for (size_t u = 0; u < nVertices_; u++) {
const size_t uIdx = vertices_[u].idx;
vertices_[u].fitness = weights[uIdx];
}
std::sort(vertices_, vertices_ + nVertices_, compareNodes);
break;
}
case 4: { //modified degree
case PivotingStrategy::ModifiedDegree: {
for (size_t u = 0; u < nVertices_; u++) {
const size_t uIdx = vertices_[u].idx;
vertices_[u].fitness = cgraph_->modifiedDegree(uIdx);
}
std::sort(vertices_, vertices_ + nVertices_, compareNodes);
break;
}
case 5: { //modified weight
case PivotingStrategy::ModifiedWeight: {
size_t *neighs = (size_t*)xmalloc(sizeof(size_t) * cgraph_->size());
char *iv = (char*)xcalloc(cgraph_->size(), sizeof(char));
for (size_t u = 0; u < nVertices_; u++) {
Expand All @@ -399,7 +399,7 @@ void CoinBronKerbosch::computeFitness(const double *weights) {
std::sort(vertices_, vertices_ + nVertices_, compareNodes);
break;
}
case 6: { //modified degree + modified weight
case PivotingStrategy::ModifiedDegreeWeight: {
size_t *neighs = (size_t*)xmalloc(sizeof(size_t) * cgraph_->size());
char *iv = (char*)xcalloc(cgraph_->size(), sizeof(char));
for (size_t u = 0; u < nVertices_; u++) {
Expand Down
32 changes: 19 additions & 13 deletions src/CoinBronKerbosch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,28 @@ struct BKVertex {
**/
class COINUTILSLIB_EXPORT CoinBronKerbosch {
public:
/**
* Pivoting strategies used in BK algorithm.
**/
enum PivotingStrategy {
Off = 0,
Random = 1,
Degree = 2,
Weight = 3,
ModifiedDegree = 4,
ModifiedWeight = 5,
ModifiedDegreeWeight = 6,
};

/**
* Default constructor.
*
* @param cgraph conflict graph
* @param weights array containing the weights for each vertex
* @param pivotingStrategy pivoting strategy used in BK algorithm. Values:
* 0 = off; 1 = random; 2 = degree; 3 = weight; 4 = modified degree;
* 5 = modified weight; 6 = modified degree + modified weight. Default: 3
* @param pivotingStrategy pivoting strategy used in BK algorithm.
**/
CoinBronKerbosch(const CoinConflictGraph *cgraph, const double *weights, size_t pivotingStrategy = 3);
CoinBronKerbosch(const CoinConflictGraph *cgraph, const double *weights,
PivotingStrategy pivotingStrategy = PivotingStrategy::Weight);

/**
* Destructor
Expand Down Expand Up @@ -232,16 +244,10 @@ class COINUTILSLIB_EXPORT CoinBronKerbosch {
**/
size_t maxCalls_;

/** Pivoting strategy used in BK algorithm. Options:
* 0 - off
* 1 - random
* 2 - degree
* 3 - weight
* 4 - modified degree
* 5 - modified weight
* 6 - modified degree + modified weight
/**
* Pivoting strategy used in BK algorithm.
**/
size_t pivotingStrategy_;
PivotingStrategy pivotingStrategy_;

/**
* If BK algorithm ran completely, without stopping
Expand Down