dmlc / dgl

Python package built to ease deep learning on graph, on top of existing DL frameworks.
http://dgl.ai
Apache License 2.0
13.58k stars 3.02k forks source link

Converting networkX graph to DGL graph object gives double the number of edge counts on the same Graph #6178

Open shakeel608 opened 1 year ago

shakeel608 commented 1 year ago

📚 Documentation

When I convert my networkX graph to DGL graph, why I get double the number of edges in DGL object.

How to fix it ?

Any help would be appreciated please ?

Rhett-Ying commented 1 year ago

could you share a minimum reproducible code?

shakeel608 commented 1 year ago

@Rhett-Ying The sample code is as follows. I am loading a graph from JSON file. ` def load_graph(self, path):

Load graph

    with open(path, 'r') as f:
        graph_load = json.loads(f.read())
        return nx.readwrite.json_graph.node_link_graph(graph_load)

graph_nx = load_graph(graphs_path) print("Number of Edges with Nx===>\n",graph_nx.number_of_edges()) dgl_graph = dgl.from_networkx(graph_nx, node_attrs=['features', 'label']) print("Number of Edges with DGL===>\n", dgl_graph.number_of_edges())

`

I think, DGL counts Non-directed edges twice here which is not the case with NetworkX Package

Say if adjacent_matrix[i,j] = 1, In Non directed Edges adjacent_matrix[j,i] is also 1.

Can you please let me know how to fix this. I tried to dig into the code, but I am yet to figure it out

Rhett-Ying commented 1 year ago

DGLGraph is always directed. You could use dgl.add_reverse_edges() or dgl.to_bidirected() to see if any new edges are added. If not, then the DGLGraph is already bi-directed. Or try with directed=True in node_link_graph of nx.

shakeel608 commented 1 year ago

Thank you so much But I have another questions I tried a sample code Suppose I have a graph with adj matrix as given below

graph.adjacency_matrix().to_dense()

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

When I just print the properties of variable 'graph', it gives me following

Graph(num_nodes=2, num_edges=4, ndata_schemes={'h': Scheme(shape=(), dtype=torch.float32)} edata_schemes={})

Why num_edges is 4. Shouldn't it be only two even if the DGL graphs are directed

Rhett-Ying commented 1 year ago

that's weird. could you share the minimum reproducible code for above case? Below is the one for illustration.

import dgl
import torch

g = dgl.graph(([0, 1], [1, 0]))
print(g.adjacency_matrix().to_dense())
print(g)
tensor([[0., 1.],
        [1., 0.]])
Graph(num_nodes=2, num_edges=2,
      ndata_schemes={}
      edata_schemes={})
github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale due to lack of activity. It will be closed if no further activity occurs. Thank you