Throckmorton
Throckmorton

Reputation: 581

NetworkX trivially nested views

From the networkx.DiGraph.reverse documentation.

DiGraph.reverse(copy=True)[source] Returns the reverse of the graph. The reverse is a graph with the same nodes and edges but with the directions of the edges reversed.

copy (bool optional (default=True)) – If True, return a new DiGraph holding the reversed edges. If False, the reverse graph is created using a view of the original graph.

From the networkx graph views documentation:

Note: Since graphviews look like graphs, one can end up with view-of-view-of-view chains. Be careful with chains because they become very slow with about 15 nested views.

Question

If I am repeatedly calling G.reverse(copy=False) on my graph to alternate between the original and reverse views, will this cause a so called "nested chain of views", even though the views are trivially cyclic?

Upvotes: 0

Views: 61

Answers (1)

Joel
Joel

Reputation: 23887

It appears that way:

import networkx as nx
G = nx.directed_configuration_model([1,5]*30, [2,3,4]*20)  #a simple directed graph
H = G.reverse(copy = False)
H
>  <networkx.classes.graphviews.MultiReverseView at 0x10bf61c88>
I = H.reverse(copy = False)
I
>  <networkx.classes.graphviews.MultiReverseView at 0x10bf69a90>

If you look at the source code, in the case copy = False, it calls reverse_view, whose code is here. It doesn't do anything to check whether it's already a reverse view.

It would probably introduce problems if it weren't like this. You might expect a view to behave differently from a graph, and if it actually gave back the graph, code might misbehave.

Upvotes: 1

Related Questions