DEV Community

Abhi-Kmr2046
Abhi-Kmr2046

Posted on

Graph Types in NetworkX

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:

  1. Graph: This a undirected graph, with possible self-loops but no parallel edges.
G = nx.Graph()

Enter fullscreen mode Exit fullscreen mode
  1. DiGraph: This a directed graph, with possible self-loops but no parallel edges.
G = nx.DiGraph()

Enter fullscreen mode Exit fullscreen mode
  1. MultiGraph: This a undirected graph, with possible self-loops and also parallel edges.
G = nx.MultiGraph()

Enter fullscreen mode Exit fullscreen mode
  1. MultiDiGraph: This a directed graph, with possible self-loops and also parallel edges.
G = nx.MultiDiGraph()

Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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()

Enter fullscreen mode Exit fullscreen mode

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)