Open In App

Ladder Graph Using Networkx Module in Python

In this article, we are going to see the ladder graph using Python. It is a graph that looks like ladders used commonly with every node attached to two other nodes in a specific manner. We can obtain a ladder graph by joining two-path graphs of n nodes each by each node connected with a corresponding node in another path graph.

Representation:



Below attached is an image of the L4 (n) Ladder Graph that Returns the Ladder graph of length 4(n).

Ladder Graph

Properties of the ladder graph:



We will use the networkx module for realizing a Ladder graph. It comes with an inbuilt function networkx.ladder_graph() and can be illustrated using the networkx.draw() method. 

Syntax: networkx.draw(G, node_size, node_color)

Parameters:

  • G: It refers to the ladder graph object
  • node_size: It refers to the size of nodes.
  • node_color: It refers to color of the nodes.

Below are some examples to depict how to illustrate a Ladder graph in Python:

Approach:

Example 1:




# import required module
import networkx
 
# number of nodes
n = 5
 
# create object
G = networkx.ladder_graph(n)
 
# illustrate graph
networkx.draw(G)

 

 

Output:

 

 

Example 2: Making color of nodes green and increasing size by passing extra arguments to nx.draw() function as discussed above. 

 

Approach:

 

 




# import required module
import networkx
 
# create object
G = networkx.ladder_graph(5)
 
# illustrate graph
networkx.draw(G, node_size = 500,
            node_color = 'green')

Output:


Article Tags :