Open In App

Lollipop Graph in Python using Networkx module

The lollipop graph consists of 2 components a complete graph called clique and a path graph. More precisely L (m ,n)  is a graph with an m-node complete graph and n-node path graph.

L(4,2) Lollipop Graph:



L(4,2) Graph

Here let’s see the properties of this graph:

So.no

Properties of Lollipop Graph:

1

It is a connected graph with two components clique and path graph.

2

The number of vertices is m + n.

3

The number of edges=Number of edges in complete graph+ Number of edges in path graph.

4

The number of edges=m(m-1)/2 + n.

5

Diameter of lollipop graph = n+1.

6

It is a cyclic graph as cycles are contained in a clique of the lollipop graph.

7

It is like a barbell graph without one of the barbells.

8

For the complete graph’s nodes are labeled from 0 to m-1.

9

And m to m+n-1 for the path graph.

10

Complete graph and path graph are joined via an edge (m – 1, m).

We will use the networkx module for realizing a Lollipop graph. It comes with an inbuilt function networkx.lollipop_graph() and can be illustrated using the networkx.draw() method. This module in Python is used for visualizing and analyzing different kinds of graphs.



Syntax: networkx.lollipop_graph(m,n)

Parameters:

  • M: Number of nodes in complete graph(clique)
  • N: Number of nodes in path graph.

networkx.draw(G, node_size, node_color)

  • Used to realize the graph by passing graph object.
  • G: It refers to the Tutte graph object
  • node_size: It refers to the size of nodes.
  • node_color: It refers to color of the nodes.

Example 1: Create a simple lollipop graph

Approach:

Code:




# import required module
import networkx
 
# create object
G = networkx.lollipop_graph(4, 2)
 
# illustrate graph
networkx.draw(G, node_color='green')

Output:

Example 2: Changing the size of the nodes.

Approach:

Code:




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

Output:

Example 3: Creating Lollipop graph without using lollipop_graph() function.

Approach:

Code:




# importing networkx
import networkx as nx
 
 
def lollipop_graph(m, n):
 
    # the complete graph part.
    G = nx.complete_graph(m)
 
    # adding path graph edges.
    G.add_nodes_from([v for v in range(m, m+n)])
    if n > 1:
 
        # joining adjacent path graph edges.
        G.add_edges_from([(v, v+1) for v in range(m, m + n - 1)])
 
    # connect complete graph to path graph
    if m > 0:
        G.add_edge(m - 1, m)
    return G
 
 
# invoking the function defined.
G = lollipop_graph(5, 2)
nx.draw(G)

Output:


Article Tags :