generic_graph_view#

generic_graph_view(G, create_using=None)[source]#

Returns a read-only view of G.

The graph G and its attributes are not copied but viewed through the new graph object of the same class as G (or of the class specified in create_using).

Parameters:
Ggraph

A directed/undirected graph/multigraph.

create_usingNetworkX graph constructor, optional (default=None)

Graph type to create. If graph instance, then cleared before populated. If None, then the appropriate Graph type is inferred from G.

Returns:
newGgraph

A view of the input graph G and its attributes as viewed through the create_using class.

Raises:
NetworkXError

If G is a multigraph (or multidigraph) but create_using is not, or vice versa.

Notes

The returned graph view is read-only (cannot modify the graph). Yet the view reflects any changes in G. The intent is to mimic dict views.

Examples

>>> G = nx.Graph()
>>> G.add_edge(1, 2, weight=0.3)
>>> G.add_edge(2, 3, weight=0.5)
>>> G.edges(data=True)
EdgeDataView([(1, 2, {'weight': 0.3}), (2, 3, {'weight': 0.5})])

The view exposes the attributes from the original graph.

>>> viewG = nx.graphviews.generic_graph_view(G)
>>> viewG.edges(data=True)
EdgeDataView([(1, 2, {'weight': 0.3}), (2, 3, {'weight': 0.5})])

Changes to G are reflected in viewG.

>>> G.remove_edge(2, 3)
>>> G.edges(data=True)
EdgeDataView([(1, 2, {'weight': 0.3})])
>>> viewG.edges(data=True)
EdgeDataView([(1, 2, {'weight': 0.3})])

We can change the graph type with the create_using parameter.

>>> type(G)
<class 'networkx.classes.graph.Graph'>
>>> viewDG = nx.graphviews.generic_graph_view(G, create_using=nx.DiGraph)
>>> type(viewDG)
<class 'networkx.classes.digraph.DiGraph'>