Graph implementation with NetworkX in Python

in Programming & Dev11 days ago (edited)

Graph implementation with NetworkX in Python

The NetworkX library is one of the open source libraries in the Python programming language that is used to analyze and model graphs. A complete graph is a graph that has "n" vertices and the degree of each vertex - the number of edges connected to it - is equal to "n- 1" is In other words, in the complete graph, every vertex is connected to all other vertices.

For example, the figure shown below is a complete graph with 6 vertices.

comlete graph


What is a complete graph?

"Complete Graph" is a graph whose all vertices are connected to other graph vertices. I have provided all the specifications for the complete graph in the list below.

  • In the complete graph, the degree of each vertex is equal to "n-1".
  • The total number of vertices is equal to "n(n-1)/2".
  • All possible edges in the Simple Graph exist in the complete graph.
  • This graph is a cyclic graph.
  • In this graph, the maximum distance between each pair of nodes is equal to "1".
  • Since in this graph every node is connected to other nodes, "Chromatic Number" is equal to n.
  • The complement of this graph is the empty graph.

In this post, I will use the NetworkX module in Python to create a complete graph. For this purpose, I have to use the internal function networkx.complete_graph()  to create and the internal function networkx.draw()  to draw the graph. This library in Python is specifically used to depict and analyze different types of graphs.

Graph drawing syntax in Python

I use the NetworkX library to draw the graph. As mentioned in the previous section, I need to use two different functions, one for construction and another for drawing. I have shown the syntax of the function used to make the graph in the box below.

networkx.complete_graph(n)

In the above syntax, the only parameter used is "n". The parameter n determines the number of nodes in the graph. This graph returns a complete graph with n nodes in the object output. All the nodes of the graph are numbered from "0" to "n-1".

But to draw the graph for viewing by the user, the created object must be sent to the networkx.draw()  function. I have shown the raw syntax of this function in the box below.

networkx.draw(G, node_size, node_color)

It can be seen in the above box that this function accepts 3 separate parameters. I have explained all these parameters in the list below.

  • G: This parameter refers to the "complete graph object" created.
  • node_size: This parameter shows the size of the nodes.
  • node_color: This parameter indicates the color with which the nodes should be drawn.

The process of creating a graph with the help of the NetworkX library is very simple. But it is necessary to go through the following steps one by one and regularly.

  • First, we need to import the networkx module into the coding environment.
  • Then, using the networkx.complete_graph(n) method, we create a graph object with the desired number of vertices. In this method, n is equal to the number of nodes or vertices of the graph.
  • We use the networkx.draw(G, node_color = 'green', node_size=1500) method to depict the graph. In this method, node_color, color and node_size determine the size of the graph nodes.

Note: The values ​​node_size=1500 and node_color='green' are optional and any other allowed value can be assigned to these parameters.

In the example below, I have created a graph with the number of 6 vertices and drawn it with the specification node_size=1500 and node_color='green'.

# import required module 
import networkx 
# create object 
G = networkx.complete_graph(6) 
# illustrate graph 
networkx.draw(G, node_color = 'green',
node_size = 1500)

After executing the above code, the graph with the following shape and specifications will be displayed to the users. Note that the location of the nodes in the graph may change each time it is drawn.

Complete Graph

The image above is related to a graph with 6 vertices. Because I had sent the number 6 as a parameter in the complete_graph() graph builder function.

In the example below, I have created a graph with the number of 10 vertices and drawn it with the specification node_size=1500 and node_color = 'pink'.

# import required module 
import networkx 
# create object 
G = networkx.complete_graph(10) 
# illustrate graph 
networkx.draw(G, node_color = 'pink',
node_size = 1500)

After executing the above code, the graph with the following shape and specifications will be displayed to the users. Notice that I have changed the color and number of nodes in the graph, but the nature of the graph has not changed.

The graph above is still the complete graph and only the color and number of edges have changed.

On the one hand, graphs are a powerful tool for managing all kinds of networks in the computer world and even the real world, and on the other hand, they have become a tool to help analyze data. Diagrams are also from the graph family, which are responsible for transferring information. It is with the help of charts that data analysts monitor the performance status of any system or its future status and design strategies related to improving its quality and performance.

Sort:  

Congratulations!


You have obtained a vote from CHESS BROTHERS PROJECT

✅ Good job. Your post has been appreciated and has received support from CHESS BROTHERS ♔ 💪


♟ We invite you to use our hashtag #chessbrothers and learn more about us.

♟♟ You can also reach us on our Discord server and promote your posts there.

♟♟♟ Consider joining our curation trail so we work as a team and you get rewards automatically.

♞♟ Check out our @chessbrotherspro account to learn about the curation process carried out daily by our team.


🏅 If you want to earn profits with your HP delegation and support our project, we invite you to join the Master Investor plan. Here you can learn how to do it.


Kindly

The CHESS BROTHERS team

Hello,
this Comment has been upvoted with 9.3%, thanks to @bgmoha who burned 93 PLANET
With this burn @bgmoha is actively participating in the CLEAN PLANET reward protocol.
@bgmoha is helping @cleanplanet to grow with the curation.
Thanks for your help
@cleanplanet