pyg-team / pytorch_geometric

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

Number of edges for an undirected graph is double counted #2092

Open amirziai opened 3 years ago

amirziai commented 3 years ago

🐛 Bug

Number of edges for an undirected graph is double counted.

To Reproduce

Steps to reproduce the behavior:

this line returns the number of items in the adjacency list, which is going to double count

Expected behavior

i think the number of edges should be divided by 2 when the graph is undirected. here's an example:

from torch_geometric.datasets import TUDataset
d = TUDataset('./enzymes', 'ENZYMES')[0]
assert d.is_undirected()
assert d.num_edges == 168

edges = set()
for u, v in zip(d.edge_index[0], d.edge_index[1]):
    u, v = u.item(), v.item()
    edges.add(tuple(sorted([u, v])))  # u=> v and v => u the same

assert len(edges) == (d.num_edges // 2)
rusty1s commented 3 years ago

This is actually intended, as we do not distinguish between directed and undirected graphs. If you want your graph to be undirected, you will need to add edges for both directions.

amirziai commented 3 years ago

👍

open to adding a comment clarifying this in the docstring? i can create a PR if you think it'd be useful. it can be a bit confusing.

rusty1s commented 3 years ago

Sure, feel free to create a PR.