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

Add nodelist attribute to adjacency and incidence matrix #495

Open
aleable opened this issue Nov 30, 2023 · 3 comments
Open

Add nodelist attribute to adjacency and incidence matrix #495

aleable opened this issue Nov 30, 2023 · 3 comments
Labels
new feature New feature or request

Comments

@aleable
Copy link

aleable commented Nov 30, 2023

Hi all,

it would be nice to add a nodelist parameter for both the adjacency and incidence matrix of a hypergraph (similarly to what done for networks in networkx).

Currently, the order of rows and columns of the adjacency matrix (similarly the rows of the incidence matrix) is as per nodes attribute. However, this forces a workaround in some presumably typical scenarios when nodes' labels are their indexes in the incidence matrix.

For instance, if a nx.Graph and an xgi.HyperGraph are given in input their edges = [(2, 3), (0, 1), (1, 2)], the nodes are consistently ordered as 2, 3, 0, 1 in both libraries. However, I could not find a way to get the incidence matrix of the hyperpergraph with rows and columns sorted as 0, 1, 2, 3, other than by adding a fix similar to the one below.

import xgi
import networkx as nx

G = nx.Graph()
edges = [(2, 3), (0, 1), (1, 2)]
G.add_edges_from(edges)

H = xgi.Hypergraph()
edges = [(2, 3), (0, 1), (1, 2)]
H.add_edges_from(edges)

adj_G = nx.adjacency_matrix(G, nodelist=sorted(G.nodes)).todense()

node_values = list(H.nodes)
adj_H = xgi.adjacency_matrix(H).todense()
adj_H_sorted = adj_H[:, node_values][node_values, :]

The code snippet outputs:

adj_G = [[0 1 0 0]   adj_H = [[0 1 0 1]   adj_H_sorted = [[0 1 0 0]
         [1 0 1 0]            [1 0 0 0]                   [1 0 1 0]
         [0 1 0 1]            [0 0 0 1]                   [0 1 0 1]
         [0 0 1 0]]           [1 0 1 0]]                  [0 0 1 0]]

Thanks!

@maximelucas
Copy link
Collaborator

Hi @aleable, thanks for your suggestion!
Indeed this comes from the fact that nodes are added in the order that they appear in the edges, unless nodes are added first (like in networkx).
A probably simpler fix would be to add the sorted nodes to the hypergraph before the edges:

H = xgi.Hypergraph()
edges = [(2, 3), (0, 1), (1, 2)]
nodes = np.unique(edges) # returns sorted unique nodes

H.add_nodes_from(nodes) # add nodes before edges to control their order
H.add_edges_from(edges)

This way the nodes are ordered, in the hypergraph:

>>> H.nodes
NodeView((0, 1, 2, 3))

This will make the node order more natural for all matrices (incidence, adjcacency, etc) and other measures.

Would that solve your issue?
We'll consider adding a nodelist argument too.

@aleable
Copy link
Author

aleable commented Nov 30, 2023

Hi @maximelucas, thanks for the response!

Yes, surely this solves it, even more conveniently.

My issue was to be intended as a feature request rather than a bug. I think that adding nodelist would make operations on graph matrices, e.g. slicing, neater.

Thanks again for considering it.

@maximelucas maximelucas added the new feature New feature or request label Dec 1, 2023
@maximelucas
Copy link
Collaborator

Great. And sure, I understood it as a suggestion for a new feature :)
I labeled it as a potential new feature, and we'll also add the trick above to the recipes.
Thanks a don't hesitate to post more suggestions/bugs!

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

No branches or pull requests

2 participants