Dgl .to_networkx returning a multigraph

Hi, I understand that the DGL graph I am transforming to Networkx using .to_networkx is not a multigraph, although you can see on the print value that it is a multigraph. Is this behavior expected? Here is my code snippet:

            drawGraph( self.graph, self.graph.name )
            nx_graph = self.graph.to_networkx()
            print( "nx_graph:\n", nx_graph )
            print( ".nodes:\n", nx_graph.nodes(data=True))
            print( ".edges:\n", nx_graph.edges(data=True))
                   
            eigen = nx.eigenvector_centrality( nx_graph )

Here is the output I am getting:

nx_graph:
 MultiDiGraph with 14 nodes and 15 edges
.nodes:
 [(0, {}), (1, {}), (2, {}), (3, {}), (4, {}), (5, {}), (6, {}), (7, {}), (8, {}), (9, {}), (10, {}), (11, {}), (12, {}), (13, {})]
.edges:
 [(2, 11, {'id': 0}), (3, 13, {'id': 1}), (4, 8, {'id': 2}), (4, 13, {'id': 3}), (5, 9, {'id': 4}), (5, 11, {'id': 5}), (6, 8, {'id': 6}), (7, 1, {'id': 7}), (8, 7, {'id': 8}), (9, 7, {'id': 9}), (10, 0, {'id': 10}), (11, 10, {'id': 11}), (12, 10, {'id': 12}), (13, 9, {'id': 13}), (13, 12, {'id': 14})]

Here is an image of the graph in self.graph from drawGraph():

Furthermore I am getting an error when trying to calculate the eigenvector centrality because it is a multigraph:

Traceback (most recent call last):
  File "/home/gudeh/Desktop/congestionPrediction/regression.py", line 1056, in <module>
    train_dataset = DataSetFromYosys( currentDir, split, ablationIter, mode='train' )
  File "/home/gudeh/Desktop/congestionPrediction/regression.py", line 267, in __init__
    super().__init__( name='mydata_from_yosys_'+mode )
  File "/home/gudeh/.local/lib/python3.10/site-packages/dgl/data/dgl_dataset.py", line 112, in __init__
    self._load()
  File "/home/gudeh/.local/lib/python3.10/site-packages/dgl/data/dgl_dataset.py", line 203, in _load
    self.process()
  File "/home/gudeh/Desktop/congestionPrediction/regression.py", line 271, in process
    graph = self._process_single( path )
  File "/home/gudeh/Desktop/congestionPrediction/regression.py", line 347, in _process_single
    eigen = nx.eigenvector_centrality( nx_graph )
  File "/home/gudeh/.local/lib/python3.10/site-packages/networkx/classes/backends.py", line 148, in wrapper
    return func(*args, **kwds)
  File "/home/gudeh/.local/lib/python3.10/site-packages/networkx/utils/decorators.py", line 766, in func
    return argmap._lazy_compile(__wrapper)(*args, **kwargs)
  File "<class 'networkx.utils.decorators.argmap'> compilation 4", line 3, in argmap_eigenvector_centrality_1
  File "/home/gudeh/.local/lib/python3.10/site-packages/networkx/utils/decorators.py", line 86, in _not_implemented_for
    raise nx.NetworkXNotImplemented(errmsg)
networkx.exception.NetworkXNotImplemented: not implemented for multigraph type

Yes. to_networkx will always return a multigraph even if the actual graph is simple. You will need to convert it into a simple graph by youself:

nx_graph = nx.Graph(nx_graph)

That is what I did and I was able to retrieve the values for eigenvector centrality after using nx.Graph(). Good to know I am correctly getting the values.

One other question. I also tried use nx.DiGraph, since my original graph is directed:

nx_graph = nx.DiGraph(nx_graph)

But I got the following error:

Traceback (most recent call last):
  File "/home/gudeh/Desktop/congestionPrediction/regression.py", line 1057, in <module>
    train_dataset = DataSetFromYosys( currentDir, split, ablationIter, mode='train' )
  File "/home/gudeh/Desktop/congestionPrediction/regression.py", line 267, in __init__
    super().__init__( name='mydata_from_yosys_'+mode )
  File "/home/gudeh/.local/lib/python3.10/site-packages/dgl/data/dgl_dataset.py", line 112, in __init__
    self._load()
  File "/home/gudeh/.local/lib/python3.10/site-packages/dgl/data/dgl_dataset.py", line 203, in _load
    self.process()
  File "/home/gudeh/Desktop/congestionPrediction/regression.py", line 271, in process
    graph = self._process_single( path )
  File "/home/gudeh/Desktop/congestionPrediction/regression.py", line 348, in _process_single
    eigen = nx.eigenvector_centrality( nx_graph )
  File "/home/gudeh/.local/lib/python3.10/site-packages/networkx/classes/backends.py", line 148, in wrapper
    return func(*args, **kwds)
  File "/home/gudeh/.local/lib/python3.10/site-packages/networkx/utils/decorators.py", line 766, in func
    return argmap._lazy_compile(__wrapper)(*args, **kwargs)
  File "<class 'networkx.utils.decorators.argmap'> compilation 8", line 4, in argmap_eigenvector_centrality_5
  File "/home/gudeh/.local/lib/python3.10/site-packages/networkx/algorithms/centrality/eigenvector.py", line 138, in eigenvector_centrality
    raise nx.PowerIterationFailedConvergence(max_iter)
networkx.exception.PowerIterationFailedConvergence: (PowerIterationFailedConvergence(...), 'power iteration failed to converge within 100 iterations')

I wonder if the eigenvector values will be incorrect, since they are being calculated on a undirected graph, when using nx.Graph() instead of nx.DiGraph()

Eigenvalues of an asymmetric real matrix could be complex, so Iā€™m not sure if the eigenvalues on a directed graph is necessarily meaningful.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.