Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmLollipop Graph in Python using Networkx module

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:

  • We will import the required module network
  • Then we will create a graph object using networkx.lollipop_graph(m,n).
  • For realizing graph, we will use networkx.draw(G, node_color=’green’)
  • With three arguments first one being graph object and the other two used to set the color and size of node.

Code:

Python3




# 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:

  • We will import the required module network.
  • Then we will create a graph object using networkx.lollipop_graph(m,n).
  • For realizing graph, we will use networkx.draw(G, node_color=’green’,node_size=1500)
  • Note that here we have passed an extra argument in draw function namely node_size.
  • This extra argument determines the relative size of the node.

Code:

Python3




# 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:

  • We will import the required module networkx as nx.
  • We will then manually define lollipop_graph function.
  • We will first create the complete graph using complete_graph function.
  • Then we will n nodes numbered from m to m+n-1 which will be from straight part of lollipop
  • Then we will join the adjacent nodes to complete the path graph part of lollipop.
  • At last we will join the two disjoint parts to obtain the final result.
  • Note that two if conditions are used to check whether the number of nodes in either of the part is needed to be added or that part is to be kept empty.

Code:

Python3




# 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:

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments