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

Bug fix/csr matrix set value binary search bug #1190

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

FreestyleBuild
Copy link

A bug in the binary search of GeneralCSRMatrix caused the collumn index array to be not sorted, if setValue operations are made in the wrong order. An example can be seen in #1104. The binary search function has been replaced at the appropriate places, now allowing setValue operations in any order, while keeping the values sorted. A test has been added to check this behavior for the example from #1104 and two other matrices.

@angriman
Copy link
Member

Thanks for the fix. In addition to the changes to the CSR matrix, could you also create a new test that fails with the previous implementation?

@FreestyleBuild
Copy link
Author

Thanks for the fix. In addition to the changes to the CSR matrix, could you also create a new test that fails with the previous implementation?

Two test cases for setValue have been added in networkit/cpp/algebraic/test/MatricesGTest.cpp. See test cases testCSRMatrixSetValueSorted and testCSRMatrixSetValueUnsorted. The fromer fails with the old implementation, while the later is added for competeness.


ASSERT_FALSE(A.sorted());

ASSERT_EQ(A(0, 0), 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, please use EXPECT_EQ.

Copy link
Member

@angriman angriman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I left a few more comments.

*/
CSRGeneralMatrix(count dimension, ValueType zero = 0)
CSRGeneralMatrix(count dimension, ValueType zero = 0, bool isSorted = true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in #1146 and #1158, I'd suggest not complicating the constructor with additional parameters. Instead, as done in #1158, we could add some getter and setter functions for the isSorted variable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would setting the sorted value not require unnecessary effort, if it is set from false to true? Or should the matrix be sorted by default and the setter only allows to create an unsorted version. In this case I would suggest a makeUnsorted() function or similar, instead of a setter-function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have the matrix sorted by default. If some user does not require that functionality, we could allow them to turn it off with some makeUnosrted() function as you suggest. What we should avoid is polluting the constructors with variables that control an internal behavior of the data structure.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest commit includes the makeUnsorted function and the old constructors

CSRGeneralMatrix(count nRows, count nCols, ValueType zero = 0)
: rowIdx(nRows + 1), columnIdx(0), nonZeros(0), nRows(nRows), nCols(nCols), isSorted(true),
zero(zero) {}
CSRGeneralMatrix(count nRows, count nCols, ValueType zero = 0, bool isSorted = true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

if (value == getZero()) { // remove the nonZero value
columnIdx.erase(columnIdx.begin() + colIdx);
nonZeros.erase(nonZeros.begin() + colIdx);
if (!sorted()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!sorted()) {
if (isSorted) {

colIdx = none;
} else if (!sorted()) {

if (!sorted()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!sorted()) {
if (isSorted) {


// update rowIdx
for (index k = i + 1; k < rowIdx.size(); ++k) {
--rowIdx[k];
rowIdx[k]++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rowIdx[k]++;
++rowIdx[k];

rowIdx[k]++;
// update rowIdx
for (index k = i + 1; k < rowIdx.size(); ++k) {
rowIdx[k]++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rowIdx[k]++;
++rowIdx[k];

@fabratu fabratu added the bug label Apr 3, 2024
@@ -69,6 +50,7 @@ class CSRGeneralMatrix {
* Constructs the CSRGeneralMatrix with size @a dimension x @a dimension.
* @param dimension Defines how many rows and columns this matrix has.
* @param zero The zero element (default = 0).
* @param isSorted If the matrix representation should uses sorted vectors.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed.

@@ -79,6 +61,7 @@ class CSRGeneralMatrix {
* @param nRows Number of rows.
* @param nCols Number of columns.
* @param zero The zero element (default = 0).
* @param isSorted If the matrix representation should uses sorted vectors.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@@ -79,6 +61,7 @@ class CSRGeneralMatrix {
* @param nRows Number of rows.
* @param nCols Number of columns.
* @param zero The zero element (default = 0).
* @param isSorted If the matrix representation should uses sorted vectors.
*/
CSRGeneralMatrix(count nRows, count nCols, ValueType zero = 0)
: rowIdx(nRows + 1), columnIdx(0), nonZeros(0), nRows(nRows), nCols(nCols), isSorted(true),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
: rowIdx(nRows + 1), columnIdx(0), nonZeros(0), nRows(nRows), nCols(nCols), isSorted(true),
: rowIdx(nRows + 1), columnIdx(0), nonZeros(0), nRows(nRows), nCols(nCols),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants