Open In App

Star Graph using Networkx Python

In this article, we are going to see Star Graph using Networkx Python. A Star graph is a special type of graph in which n-1 vertices have degree 1 and a single vertex have degree n – 1. This looks like that n – 1 vertex is connected to a single central vertex. A star graph with total n – vertex is termed as Sn.

Properties of Star Graph:



Example of S10 :

S10

Example of S6 :



S6

Approach:

Example 1:




# import required module
import networkx as nx
   
# create object
G = nx.star_graph(6)
   
# illustrate graph
nx.draw(G, node_color = 'green',
        node_size = 100)

Output:

Explanation:

As we passed 6 as an argument to the star_graph() function hence we got a star graph with 6 edges as output. We changed the color and size of nodes by passing extra arguments node_size and node_color to the nx.draw() function.

Example 2:




# import required module
import networkx as nx
   
# create object
G = nx.star_graph(10)
   
# illustrate graph
nx.draw(G, node_color = 'green',
        node_size = 100)

Output:

Explanation:

As we passed 10 as an argument to the star_graph() function hence we got a star graph with 10 edges as output. We changed the color and size of nodes by passing extra arguments node_size and node_color to the nx.draw() function.


Article Tags :