Skip to content

What aggregation does Weighted_Adjacency assume for COO matrices with repeated coordinates? #903

Backlog

Description

@SUPERZJ827

Does Graph.Weighted_Adjacency assume that occupied coordinates in a SciPy COO matrix are unique?

NetworkX sums parallel edge weights in the numerical adjacency matrix, and its COO export can represent that sum with repeated coordinates. With python-igraph 1.0.0, SciPy 1.18.1 and NetworkX 3.6.1, upper-triangle mode gives different edge weights for the COO matrix and its dense representation. The vertex strengths agree.

import igraph as ig
import networkx as nx
from scipy.sparse import coo_matrix

g = nx.MultiGraph()
g.add_edges_from([(0, 1), (0, 1), (1, 2)])
a = nx.to_scipy_sparse_array(g, nodelist=[0, 1, 2], format="coo", weight=None)
m = coo_matrix(a)
assert all((getattr(a, k) == getattr(m, k)).all() for k in ("row", "col", "data"))

fixed = m.copy()
fixed.sum_duplicates()
for x in [m, fixed, m.toarray()]:
    h = ig.Graph.Weighted_Adjacency(x, mode="upper")
    print(
        list(zip(h.get_edgelist(), map(int, h.es["weight"]))),
        h.strength(weights="weight"),
    )

Observed output

[((0, 1), 1), ((0, 1), 1), ((1, 2), 1)] [2.0, 3.0, 1.0]
[((0, 1), 2), ((1, 2), 1)] [2.0, 3.0, 1.0]
[((0, 1), 2), ((1, 2), 1)] [2.0, 3.0, 1.0]

The COO input gives two unit-weight edges on (0, 1). The merged and dense inputs each give one edge of weight 2 on that pair. Filtering for edges with weight ≥ 2 therefore retains the pair only in the latter two graphs. The assertion checks that wrapping the NetworkX array as a coo_matrix preserves every row index, column index and value. In a separate check, passing the coo_array directly raised TypeError in both upper and directed modes.

If unique coordinates are an expected precondition, would it make sense to note this in the Weighted_Adjacency docstring? Otherwise, should the sparse matrix and dense array inputs produce the same weighted edges?

References

Activity

  1. szhorvat commented on Nov 29, 2026

    @szhorvat
    Member

    It seems that there is a bug. Thanks for the report. Graph.Weighted_Adjacency() should never produce a multigraph. The result should always be simple.

    How the input matrix is represented—in dense format, or various sparse formats—should not matter.

  2. added this to the 1.1 milestone on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions