networkx.classes.function.restricted_view¶
-
restricted_view
(G, nodes, edges)[source]¶ Returns a view of
G
with hidden nodes and edges.The resulting subgraph filters out node
nodes
and edgesedges
. Filtered out nodes also filter out any of their edges.Parameters: - G (NetworkX Graph)
- nodes (iterable) – An iterable of nodes. Nodes not present in
G
are ignored. - edges (iterable) – An iterable of edges. Edges not present in
G
are ignored.
Returns: subgraph – A read-only restricted view of
G
filtering out nodes and edges. Changes toG
are reflected in the view.Return type: SubGraph View
Notes
To create a mutable subgraph with its own copies of nodes edges and attributes use
subgraph.copy()
orGraph(subgraph)
If you create a subgraph of a subgraph recursively you may end up with a chain of subgraph views. Such chains can get quite slow for lengths near 15. To avoid long chains, try to make your subgraph based on the original graph. We do not rule out chains programmatically so that odd cases like an
edge_subgraph
of arestricted_view
can be created.Examples
>>> import networkx as nx >>> G = nx.path_graph(5) >>> H = nx.restricted_view(G, [0], [(1, 2), (3, 4)]) >>> list(H.nodes) [1, 2, 3, 4] >>> list(H.edges) [(2, 3)]