Barbell Graph Using Python networkx
There are many kinds of definitions of the barbell graphs. The most commonly used one is an n-barbell graph which is a simple graph obtained by connecting two copies of a complete graph with n nodes. In this article, we are going to see how to use a barbell graph using python.
Examples of n-barbell graph:
If N=3 Nodes then the graph will be shown as figures:
If N=4 Nodes then the graph will be shown as figures:
1.Total number of nodes(In n-barbell graph):
The Total number of Nodes = 2*N
2.Total number of edges(In n-barbell graph):
Total number of edges = 2*number of edgesin complete graph + 1 =2*(n*(n-1)/2)+1 = n*(n-1) + 1
- The barbell graph contains cycles in it.
- The barbell graph is connected every two nodes have a path between them.
- It has a bridge between 2 complete graphs.
- Bridge may and may not have nodes in it.
Barbell Graphs using Python:
It is realized in python using the barbell_graph(n, m) function of the networkx library and matplotlib library.
- networkx library Library in python used for realizing and analyzing different kinds of graphs(data structure) in python. For installation use this command:
pip install networkx
- matplotlib library: Library in python used for realizing and analyzing different kinds of functions in python. For installation use this command:
pip install matplotlib
barbell_graph(n, m): It returns a Barbell Graph with two complete graphs of n nodes which are connected via m node bridge in between.
- Import networkx and matplotlib libraries.
- Create a networkx graph object G using nx.barbell_graph(n, m) function as mentioned above.
- Use nx.draw_networkx(G) function to print the graph.
As we passed (4,2) as parameter’s to nx.barbell_graph() function is assigned a graph with 4 node clusters joined by a bridge of 2 nodes. And finally, we got the output as a view of graph G using the draw_networkx(G) function.