Create a Cycle Graph using Networkx in Python
A cycle graph is a graph which contains a single cycle in which all nodes are structurally equivalent therefore starting and ending nodes cannot be identified.
- Number of nodes in a Cycle Graph(Cn) are equal to N.
- Number of edges in a Cycle Graph(Cn) are equal to N.
- Every node is connected to 2 edges hence degree of each node is 2.
- It is a Hamiltonian Graph.
- It is a Connected Graph.
- Even after removal of one edge it remains connected as it is cyclic.
- We can simply obtain cycle graph by joining the initial an final node of a path graph.
We will use the networkx module for realizing a Cycle graph. This module in python is used for visualizing and analyzing different kind of graphs for most of which the generators are predefined in this library.
It comes with an inbuilt function networkx.cycle_graph() and can be illustrated using the networkx.draw() method.
- draw()-This function is used to draw the required graph using networkx module.
networkx.draw(G, node_size, node_color)
- G: It refers to the Cycle graph object it is passed so that draw method knows exactly which graph is to be printed.
- node_size: It refers to the size of nodes.
- node_color: It refers to color of the nodes.
- cycle_graph()- This function is used to create a cycle graph, it gives all the required information for creating one.
n: This parameter is used to specify the number of nodes in the cycle graph.
This method returns Cn (Cycle graph with n nodes). Therefore it is used to initialize the graph object G.
- Import module
- Create cycle graph object using cycle_graph() constructor
- Use nx.draw() function
- Display plot
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.