Open In App

Creating a Path Graph Using Networkx in Python

A path graph is a connected graph denoted by Pn if it contains n nodes. Nodes are connected in form of a straight line in a path graph. Here we will discuss how networkx module can be used to generate one using its inbuilt path_graph() function.

Properties of Path Graph:

Functions used

We will use the networkx module for realizing a Path graph. It comes with an inbuilt function networkx.path_graph() and can be illustrated using the networkx.draw() method. This method is straightforward method of creating a desired path graph using appropriate parameters. 



Syntax:  path_graph(n, create_using=None)

Parameter:



  • n: Number of nodes we want in path graph.
  • create_using: We can simply pass None or pass nx.DiGraph() as a value to this argument sending nx.Digraph() will lead to creation of a directed path graph.

Approach:

Program:




# import required module
import networkx as nx
 
# create object
G = nx.path_graph(5, create_using=nx.DiGraph())
 
# illustrate graph
nx.draw(G, node_color='green')

Output:

Article Tags :