Skip to content

ENH: Add format parameter to adjacency_matrix - #8545

Merged
dschult merged 4 commits into
networkx:mainfrom
toroleapinc:feat/issue-7980-adjacency-matrix-format
Mar 15, 2026
Merged

dschult merged 4 commits into
networkx:mainfrom
toroleapinc:feat/issue-7980-adjacency-matrix-format

Conversation

@toroleapinc

Copy link
Copy Markdown
Contributor

Summary

Add a format parameter to adjacency_matrix() to allow specifying the sparse format of the returned matrix (or 'dense' for a numpy ndarray), consistent with to_scipy_sparse_array().

Partial fix for #7980.

Changes

adjacency_matrix() is a thin wrapper around to_scipy_sparse_array() but previously didn't expose the format parameter. This change simply passes it through.

Example

>>> G = nx.path_graph(4)
>>> nx.adjacency_matrix(G, format='dense')
array([[0, 1, 0, 0],
       [1, 0, 1, 0],
       [0, 1, 0, 1],
       [0, 0, 1, 0]])
>>> nx.adjacency_matrix(G, format='coo')
<COOrdinate sparse array of dtype 'int64'...>

Add a 'format' parameter to adjacency_matrix() to allow specifying
the sparse format of the returned matrix, consistent with
to_scipy_sparse_array(). This includes support for 'dense' format
which returns a numpy ndarray.

The adjacency_matrix function is a thin wrapper around
to_scipy_sparse_array but previously didn't expose the format
parameter. This change simply passes it through.

Closes #7980 (partial: adjacency_matrix only)

Signed-off-by: edvatar <[email protected]>

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The kwarg should be made keyword-only and a test is needed.

rossbar

This comment was marked as duplicate.

rossbar

This comment was marked as duplicate.

@toroleapinc

Copy link
Copy Markdown
Contributor Author

@rossbar Thanks for the feedback! You're absolutely right about both points:

  1. Keyword-only argument: I'll update the function signature to make the parameter keyword-only
  2. Tests needed: I'll add comprehensive tests covering both strict and non-strict modes

I'll implement these changes and push an update. Thanks for the clear guidance!

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I took the liberty of pushing up the last few bits. This should be all set now, thanks @toroleapinc

@toroleapinc

Copy link
Copy Markdown
Contributor Author

Thanks @rossbar — really appreciate you pushing the final polish and the thorough review. Learned a lot from the feedback on this one!

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good @toroleapinc Thanks very much!
And thanks @rossbar for the review --

I notice that we are still using .todense() instead of .toarray(). But that is throughout this whole module and maybe beyond -- so definitely not part of this PR. The results of todense() and toarray() are identical for sparse arrays so there shouldn't be any issues here. In it goes!

@dschult
dschult merged commit 4792afb into networkx:main Mar 15, 2026
38 checks passed
@dschult dschult added this to the 3.7 milestone Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants