Skip to content

Commit

Permalink
make conflict code faster by using char
Browse files Browse the repository at this point in the history
  • Loading branch information
jjhforrest committed Dec 1, 2023
1 parent a4b1ef5 commit e4c432e
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/CoinBronKerbosch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ void CoinBronKerbosch::computeFitness(const double *weights) {
}
case 5: { //modified weight
size_t *neighs = (size_t*)xmalloc(sizeof(size_t) * cgraph_->size());
bool *iv = (bool*)xcalloc(cgraph_->size(), sizeof(bool));
char *iv = (char*)xcalloc(cgraph_->size(), sizeof(char));
for (size_t u = 0; u < nVertices_; u++) {
const size_t uIdx = vertices_[u].idx;
const std::pair<size_t, const size_t*> rescg = cgraph_->conflictingNodes(uIdx, neighs, iv);
Expand All @@ -397,7 +397,7 @@ void CoinBronKerbosch::computeFitness(const double *weights) {
}
case 6: { //modified degree + modified weight
size_t *neighs = (size_t*)xmalloc(sizeof(size_t) * cgraph_->size());
bool *iv = (bool*)xcalloc(cgraph_->size(), sizeof(bool));
char *iv = (char*)xcalloc(cgraph_->size(), sizeof(char));
for (size_t u = 0; u < nVertices_; u++) {
const size_t uIdx = vertices_[u].idx;
const std::pair<size_t, const size_t*> rescg = cgraph_->conflictingNodes(uIdx, neighs, iv);
Expand Down
8 changes: 4 additions & 4 deletions src/CoinCliqueExtender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ CoinCliqueExtender::CoinCliqueExtender(const CoinConflictGraph *cgraph, size_t e
newClique_ = (size_t*)xmalloc(sizeof(size_t) * cgSize);
costs_ = NULL;

iv_ = (bool*)xcalloc(cgSize, sizeof(bool));
iv2_ = (bool*)xcalloc(cgSize, sizeof(bool));
iv_ = (char*)xcalloc(cgSize, sizeof(char));
iv2_ = (char*)xcalloc(cgSize, sizeof(char));

extMethod_ = extMethod;
rc_ = rc;
Expand Down Expand Up @@ -298,7 +298,7 @@ void CoinCliqueExtender::fillCandidates(const size_t *clqIdxs, const size_t clqS
}

newClique_[nNewClique_++] = clqIdx;
iv_[clqIdx] = true;
iv_[clqIdx] = 1;
}

const std::pair<size_t, const size_t*> rescg = cgraph_->conflictingNodes(nodeSD, candidates_, iv2_);
Expand Down Expand Up @@ -327,7 +327,7 @@ void CoinCliqueExtender::fillCandidates(const size_t *clqIdxs, const size_t clqS
}

for (size_t i = 0; i < clqSize; i++) {
iv_[clqIdxs[i]] = false;
iv_[clqIdxs[i]] = 0;
}

#ifdef DEBUGCG
Expand Down
2 changes: 1 addition & 1 deletion src/CoinCliqueExtender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class COINUTILSLIB_EXPORT CoinCliqueExtender {
/**
* Auxiliary incidence vectors
**/
bool *iv_, *iv2_;
char *iv_, *iv2_;

/**
* Array containing the reduced cost associated
Expand Down
46 changes: 24 additions & 22 deletions src/CoinConflictGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ void CoinConflictGraph::recomputeDegree() {
minDegree_ = numeric_limits<size_t>::max();
maxDegree_ = numeric_limits<size_t>::min();

vector<bool> iv(size_, false);
char *iv = (char *) xcalloc(size_, sizeof(char));

for (size_t i = 0; (i < size_); ++i) {
const size_t ndc = nDirectConflicts(i);
const size_t *dc = directConflicts(i);

iv[i] = true;
iv[i] = 1;
for (size_t k = 0; k < ndc; k++) {
iv[dc[k]] = true;
iv[dc[k]] = 1;
}

size_t dg = ndc;
Expand All @@ -137,19 +137,19 @@ void CoinConflictGraph::recomputeDegree() {
for (size_t l = 0; (l < clqsize); ++l) {
const size_t clqEl = clqEls[l];
dg += 1 - ((int) iv[clqEl]);
iv[clqEl] = true;
iv[clqEl] = 1;
}
}

iv[i] = false;
iv[i] = 0;
for (size_t k = 0; (k < ndc); ++k)
iv[dc[k]] = false;
iv[dc[k]] = 0;
for (size_t k = 0; (k < nnc); ++k) {
const size_t idxc = nc[k];
const size_t clqsize = this->cliqueSize(idxc);
const size_t *clqEls = this->cliqueElements(idxc);
for (size_t l = 0; (l < clqsize); ++l) {
iv[clqEls[l]] = false;
iv[clqEls[l]] = 0;
}
}

Expand All @@ -160,27 +160,29 @@ void CoinConflictGraph::recomputeDegree() {
nConflicts_ += dg;
}

free(iv);
density_ = (double) nConflicts_ / maxConflicts_;
double secs = CoinCpuTime() - start;
// printf("recompute degree took %.3f seconds.\n", secs);
if (secs>1.0)
printf("recompute degree took %.3f seconds!\n", secs);
}

void CoinConflictGraph::computeModifiedDegree() {
if (!updateMDegree) {
return;
}

bool *iv = (bool *) xcalloc(size_, sizeof(bool));
char *iv = (char *) xcalloc(size_, sizeof(char));

for (size_t i = 0; i < size_; i++) {
const size_t ndc = nDirectConflicts(i);
const size_t *dc = directConflicts(i);
size_t mdegree = degree(i);

iv[i] = true;
iv[i] = 1;
for (size_t k = 0; k < ndc; k++) {
mdegree += degree(dc[k]);
iv[dc[k]] = true;
iv[dc[k]] = 1;
}

const size_t nnc = nNodeCliques(i);
Expand All @@ -190,22 +192,22 @@ void CoinConflictGraph::computeModifiedDegree() {
for (size_t l = 0; l < cliqueSize(nc[k]); l++) {
if (!iv[clqEls[l]]) {
mdegree += degree(clqEls[l]);
iv[clqEls[l]] = true;
iv[clqEls[l]] = 1;
}
}
}

setModifiedDegree(i, mdegree);

//clearing iv
iv[i] = false;
iv[i] = 0;
for (size_t k = 0; k < ndc; k++) {
iv[dc[k]] = false;
iv[dc[k]] = 0;
}
for (size_t k = 0; k < nnc; k++) {
const size_t *clqEls = cliqueElements(nc[k]);
for (size_t l = 0; l < cliqueSize(nc[k]); l++) {
iv[clqEls[l]] = false;
iv[clqEls[l]] = 0;
}
}
}
Expand All @@ -214,16 +216,16 @@ void CoinConflictGraph::computeModifiedDegree() {
free(iv);
}

std::pair<size_t, const size_t *> CoinConflictGraph::conflictingNodes(size_t node, size_t *temp, bool *iv) const {
std::pair<size_t, const size_t *> CoinConflictGraph::conflictingNodes(size_t node, size_t *temp, char *iv) const {
if (nNodeCliques(node)) {
const size_t ndc = nDirectConflicts(node);
const size_t *dc = directConflicts(node);

// adding direct conflicts and after conflicts from cliques
iv[node] = true;
iv[node] = 1;
for (size_t k = 0; k < ndc; k++) {
temp[k] = dc[k];
iv[dc[k]] = true;
iv[dc[k]] = 1;
}

size_t nConf = ndc;
Expand All @@ -236,7 +238,7 @@ std::pair<size_t, const size_t *> CoinConflictGraph::conflictingNodes(size_t nod
const size_t neigh = cliqueElements(idxClq)[j];
if (!iv[neigh]) {
temp[nConf++] = neigh;
iv[neigh] = true;
iv[neigh] = 1;
}
}
}
Expand All @@ -246,9 +248,9 @@ std::pair<size_t, const size_t *> CoinConflictGraph::conflictingNodes(size_t nod
#endif

// clearing iv
iv[node] = false;
iv[node] = 0;
for (size_t i = 0; (i < nConf); ++i)
iv[temp[i]] = false;
iv[temp[i]] = 0;

std::sort(temp, temp + nConf);
return std::pair<size_t, const size_t *>(nConf, temp);
Expand Down Expand Up @@ -334,7 +336,7 @@ void CoinConflictGraph::printSummary() const {

if (numEdges) {
size_t *neighs = (size_t *) xmalloc(sizeof(size_t) * size_);
bool *iv = (bool*)xcalloc(size_, sizeof(bool));
char *iv = (char *) xcalloc(size_, sizeof(char));

avgDegree = ((double) numEdges) / ((double) numVertices);
density = (2.0 * ((double) numEdges)) / (((double) numVertices) * (((double) numVertices) - 1.0));
Expand Down
2 changes: 1 addition & 1 deletion src/CoinConflictGraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class COINUTILSLIB_EXPORT CoinConflictGraph {
* the array may be a pointer to temp if the temporary storage
* area was used or a pointer to an array in the conflict graph itself.
**/
std::pair< size_t, const size_t* > conflictingNodes ( size_t node, size_t* temp, bool *iv ) const;
std::pair< size_t, const size_t* > conflictingNodes ( size_t node, size_t* temp, char *iv ) const;

/**
* Density of the conflict graph:
Expand Down
22 changes: 11 additions & 11 deletions src/CoinOddWheelSeparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ CoinOddWheelSeparator::CoinOddWheelSeparator(const CoinConflictGraph *cgraph, co
}
}

iv_ = (bool *) xcalloc(cgSize, sizeof(bool));
iv2_ = (bool *) xcalloc(cgSize, sizeof(bool));
iv_ = (char *) xcalloc(cgSize, sizeof(char));
iv2_ = (char *) xcalloc(cgSize, sizeof(char));

spf_ = NULL;

Expand Down Expand Up @@ -279,16 +279,16 @@ void CoinOddWheelSeparator::findOddHolesWithNode(size_t node) {

if (iv_[tmp_[i]]) { //repeated entry
for (size_t j = 0; j <= i; j++) {
iv_[tmp_[j]] = false;
iv_[tmp_[j]] = 0;
}
return;
}

iv_[tmp_[i]] = true;
iv_[tmp_[i]] = 1;
}
// clearing iv
for (size_t i = 0; i < oddSize; i++) {
iv_[tmp_[i]] = false;
iv_[tmp_[i]] = 0;
}

/* checking if it is violated */
Expand Down Expand Up @@ -332,7 +332,7 @@ bool CoinOddWheelSeparator::alreadyInserted(size_t nz, const size_t *idxs) {
bool repeated = false;

for (size_t i = 0; i < nz; i++) {
iv_[idxs[i]] = true;
iv_[idxs[i]] = 1;
}

for (size_t idxOH = 0; idxOH < numOH_; idxOH++) {
Expand All @@ -359,7 +359,7 @@ bool CoinOddWheelSeparator::alreadyInserted(size_t nz, const size_t *idxs) {

// clearing iv
for (size_t i = 0; i < nz; i++) {
iv_[idxs[i]] = false;
iv_[idxs[i]] = 0;
}

return repeated;
Expand All @@ -380,15 +380,15 @@ void CoinOddWheelSeparator::searchWheelCenter(size_t idxOH) {

/* picking node with the smallest degree */
size_t nodeSD = ohIdxs[0], minDegree = cgraph_->degree(ohIdxs[0]);
iv_[ohIdxs[0]] = true;
iv_[ohIdxs[0]] = 1;
for (size_t i = 1; i < ohSize; i++) {
const size_t dg = cgraph_->degree(ohIdxs[i]);
if (dg < minDegree) {
minDegree = dg;
nodeSD = ohIdxs[i];
}

iv_[ohIdxs[i]] = true;
iv_[ohIdxs[i]] = 1;
}

// generating candidates
Expand Down Expand Up @@ -471,7 +471,7 @@ void CoinOddWheelSeparator::searchWheelCenter(size_t idxOH) {

// clearing iv
for (size_t i = 0; i < ohSize; i++) {
iv_[ohIdxs[i]] = false;
iv_[ohIdxs[i]] = 0;
}
}

Expand Down Expand Up @@ -542,4 +542,4 @@ static void *xcalloc( const size_t elements, const size_t size ) {
}

return result;
}
}
2 changes: 1 addition & 1 deletion src/CoinOddWheelSeparator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class COINUTILSLIB_EXPORT CoinOddWheelSeparator {
/**
* Auxiliary incidence arrays
**/
bool *iv_, *iv2_;
char *iv_, *iv2_;

/**
* Class that contains the shortest path algorithm.
Expand Down

0 comments on commit e4c432e

Please sign in to comment.