Skip to content

fix: ignore stored zeros in sparse adjacency - #3012

Open
j-stroey wants to merge 1 commit into
igraph:mainfrom
j-stroey:fix/sparse-adjacency-zeros
Open

j-stroey wants to merge 1 commit into
igraph:mainfrom
j-stroey:fix/sparse-adjacency-zeros

Conversation

@j-stroey

@j-stroey j-stroey commented Nov 9, 2026

Copy link
Copy Markdown

This PR fixes #3010.

My colleagues and I ran into this issue when working on an algorithm that sums several sparse adjacency matrices entry-wise, counts overlap and sets entries to zero if they are below a certain threshold. These explicitly stored zeros were interpreted by graph_from_adjacency_matrix() as edges, when materializing the graph.

We traced the issue to a shortcut in the unweighted sparse-matrix conversion. The code checks whether the largest stored value is 1 and, if so, creates one edge for every stored entry. That check also passes when the stored values are c(1, 0), so the zero produces an edge too. I changed the shortcut to check whether all stored values are 1. When a stored zero is present, the code now falls back to the existing logic that uses each value as an edge count.

I added a small test based on the example in the issue. It returned two edges before the change and one afterward, as expected. The adjacency tests ran with zero failures and one skip because pcalg was unavailable.

LLM disclosure: Both Claude and ChatGPT were used to help identify the issue and produce the fix.

  • By submitting this pull request, I assign the copyright of my contribution to The igraph development team.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

graph_from_adjacency_matrix() adds an edge for an explicitly stored zero

2 participants