From 38db41f6aedc73baafa721031373588e4f1445ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Apitzsch?= Date: Fri, 2 Aug 2024 09:57:03 +0200 Subject: [PATCH] CoinBronKerbosch: Replace magic number for pivoting strategy --- src/CoinBronKerbosch.cpp | 16 ++++++++-------- src/CoinBronKerbosch.hpp | 32 +++++++++++++++++++------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/CoinBronKerbosch.cpp b/src/CoinBronKerbosch.cpp index 886b77b9..a8b19655 100644 --- a/src/CoinBronKerbosch.cpp +++ b/src/CoinBronKerbosch.cpp @@ -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) @@ -351,14 +351,14 @@ 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); @@ -366,7 +366,7 @@ void CoinBronKerbosch::computeFitness(const double *weights) { 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]; @@ -374,7 +374,7 @@ void CoinBronKerbosch::computeFitness(const double *weights) { 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); @@ -382,7 +382,7 @@ void CoinBronKerbosch::computeFitness(const double *weights) { 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++) { @@ -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++) { diff --git a/src/CoinBronKerbosch.hpp b/src/CoinBronKerbosch.hpp index 39350810..ace75fbd 100644 --- a/src/CoinBronKerbosch.hpp +++ b/src/CoinBronKerbosch.hpp @@ -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 @@ -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