NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. It provides data structures and methods for storing graphs.
Graphs are basically of four types:
- Graph: This a undirected graph, with possible self-loops but no parallel edges.
G = nx.Graph()
- DiGraph: This a directed graph, with possible self-loops but no parallel edges.
G = nx.DiGraph()
- MultiGraph: This a undirected graph, with possible self-loops and also parallel edges.
G = nx.MultiGraph()
- MultiDiGraph: This a directed graph, with possible self-loops and also parallel edges.
G = nx.MultiDiGraph()
Graph Views
For some algorithms it is convenient to temporarily morph a graph to exclude some nodes or edges. NetworkX provides a easier way to accomplish this using Graph Views. This is a view of a graph as SubGraph, Reverse, Directed or Undirected. The graph created using view is a read only graph object.
view = nx.reverse_view(G)
This creates a view of the original graph but edges reversed.
def filter_node(node):
return node != 5
view = nx.subgraph_view(G, filter_node=filter_node)
view.nodes()
This creates a view of the original graph but it will not contain nodes which do satisfy the condition in filter_node.
Top comments (0)