subgraph_view(G, filter_node=<function no_filter at 0x0000000>, filter_edge=<function no_filter at 0x0000000>)
subgraph_view
provides a read-only view of the input graph that excludes nodes and edges based on the outcome of two filter functions :None:None:`filter_node`
and :None:None:`filter_edge`
.
The :None:None:`filter_node`
function takes one argument --- the node --- and returns :None:None:`True`
if the node should be included in the subgraph, and :None:None:`False`
if it should not be included.
The :None:None:`filter_edge`
function takes two (or three arguments if G
is a multi-graph) --- the nodes describing an edge, plus the edge-key if parallel edges are possible --- and returns :None:None:`True`
if the edge should be included in the subgraph, and :None:None:`False`
if it should not be included.
Both node and edge filter functions are called on graph elements as they are queried, meaning there is no up-front cost to creating the view.
A directed/undirected graph/multigraph
A function taking a node as input, which returns :None:None:`True`
if the node should appear in the view.
A function taking as input the two nodes describing an edge (plus the edge-key if G
is a multi-graph), which returns :None:None:`True`
if the edge should appear in the view.
A read-only graph view of the input graph.
View of G
applying a filter on nodes and edges.
>>> G = nx.path_graph(6)
Filter functions operate on the node, and return :None:None:`True`
if the node should appear in the view:
>>> def filter_node(n1):
... return n1 != 5 ...
>>> view = nx.subgraph_view(G, filter_node=filter_node)
... view.nodes() NodeView((0, 1, 2, 3, 4))
We can use a closure pattern to filter graph elements based on additional data --- for example, filtering on edge data attached to the graph:
>>> G[3][4]["cross_me"] = False
... def filter_edge(n1, n2):
... return G[n1][n2].get("cross_me", True) ...
>>> view = nx.subgraph_view(G, filter_edge=filter_edge)
... view.edges() EdgeView([(0, 1), (1, 2), (2, 3), (4, 5)])
>>> view = nx.subgraph_view(G, filter_node=filter_node, filter_edge=filter_edge,)
... view.nodes() NodeView((0, 1, 2, 3, 4))
>>> view.edges() EdgeView([(0, 1), (1, 2), (2, 3)])See :
The following pages refer to to this document either explicitly or contain code examples using this.
networkx.classes.graphviews.subgraph_view
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them