pyg-team / pytorch_geometric

Graph Neural Network Library for PyTorch
https://pyg.org
MIT License
21.18k stars 3.64k forks source link

Convert adjacency graph to incidence graph #2943

Open WOOSHIK-M opened 3 years ago

WOOSHIK-M commented 3 years ago

Hello, I want to ask about converting adjacency matrix to incidence matrix to use torch.nn.HypergraphConv. I am aware that directional attribute is considered when constructing the adjacency matrix for some undirected network graph, such as KarateClub, Cora etc. Should I consider this when it is converted to incidence one? I hope to know which one is correct to use your hypergraphConv module.

When we convert a simple graph ( three vertices are connected as 0 -- 1 -- 2 ) to incidence matrix (we consider each normal edge as a hyperedge only containing two vertices), how many edges are generated? 2 or 4? which one is correct?

Thanks!

rusty1s commented 3 years ago

If we consider the undirected graph 0 -- 1 -- 2, we have:

edge_index = torch.tensor([[0, 1, 1, 2], [1, 0, 2, 1]])

which we can convert to an incidence matrix via:

row, col = edge_index
arange = torch.arange(row.size(0))

hyperedge_index = torch.stack([torch.cat([row, col]), torch.cat([arange, arange])], dim=0)

which results in 4 edges. If you only want to create two edges, I suggest that you pre-filter your edge_index first to only encode a single direction, e.g., via:

mask = edge_index[0] < edge_index[1]
edge_index = edge_index[:, mask]

Both ways are correct and work with HyperGraphConv, although the first one is more computationally expensive and does not provide any benefits in undirected graph scenarios.