Open In App

Visualizing the composition two of graph using networkx

In this article, we are going to see how to visualize the composition two of the graph using networkx. If we are given two graphs A and B then their composition can be found by performing union operation on node-set and edge-set of A and B.

Simply if an edge is contained between any two vertices in any of the initial graphs it will be contained in the composition of those graphs.



Example:

Initial Graph G:



G

Initial Graph H:

H

Result of Composition of G and H:

Composition(G,H)

Explanation: If we name nodes in both graphs G and H using integers then we will clearly observe that node and edge set of G will be superset of H and hence their union gives G in return.

Example 2:

Initial Graph G:

G

Initial Graph H:

H

Result of Composition of G and H:

R

Explanation: We can see that all the edges which are in graph G are present in graph H except the node between first and third node from right therefore doing composition of G and H adds all edges from G and H into the result.

Important points about Compose function:

Realizing Composition using Python :

We will use networkx.compose() function for our task to do composition of 2 graphs.

Syntax: compose(G, H)

  • G: It is a networkx Graph object which is passed as an argument to compose function.
  • H: It is also an networkx Graph object which is passed as an argument to compose function.

Result of compose function will be union of edge and vertices set of G and H.

Approach:

Code:




# importing networkx module 
import networkx as nx 
  
# creating sample graph object 
G = nx.path_graph(7
  
# creating sample graph object 
H = nx.path_graph(3)
  
# compose of G and H saving in R
R = nx.compose(G,H) 
  
# calling draw() to visualize the complement graph 
nx.draw(R) 

Output:

OUTPUT

Article Tags :