Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: higher_order(lower_order=True) not working in edge cases #738

@martinfleis

Description

@martinfleis

I encountered super strange behaviour of higher_order, both in old W and new Graph implementation.

g = graph.Graph.from_arrays([0, 1, 2, 3, 3, 4, 4], [0, 3, 4, 1, 4, 2, 3], [0, 1, 1, 1, 1, 1, 1])
g.adjacency

focal  neighbor
0      0           0
1      3           1
2      4           1
3      1           1
       4           1
4      2           1
       3           1
Name: weight, dtype: int64

g.higher_order(k=2, lower_order=True).adjacency

focal  neighbor
0      0           0
1      4           1
2      3           1
3      2           1
4      1           1
Name: weight, dtype: int64

As you can see, this includes only neighbors of second order, not the first one as it should. The reason is that the underlying matrix power computation does not include them.

g.sparse.todense()

array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.],
       [0., 1., 0., 0., 1.],
       [0., 0., 1., 1., 0.]])

sparse.linalg.matrix_power(int_g.sparse, 2).todense()

array([[0., 0., 0., 0., 0.],
       [0., 1., 0., 0., 1.],
       [0., 0., 1., 1., 0.],
       [0., 0., 1., 2., 0.],
       [0., 1., 0., 0., 2.]])

The solution is to sum the original sparse with the new one. It seems to be an edge case resulting from extremely sparse matrix but could be real nevertheless.

I think that the solution is to sum the result of matrix power(s) with the original sparse to ensure a value is present. Here

if lower_order:
wk = sum(sparse.linalg.matrix_power(sp, x) for x in range(2, k + 1))

Metadata

Metadata

Assignees

Labels

bugfunctionality that: returns invalid, erroneous, or meaningless results; or doesn't work at all.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions