Visualizing the complement of a graph using networkx Last Updated : 13 Jan, 2021 Comments Improve Suggest changes Like Article Like Report Given a graph G complement of the graph is obtained by removing the edges contained in G from the complete graph having the same number of nodes as G. Example: Initial Graph G: G The complement of G: G ComplementRealizing Complement using Python : We will use networkx.complement() function for our task Syntax: networkx.complement() Returns the complement of the graph object passed.The value returned is of the same type of the value passed i.e networkx graph object.G is the initial object passed.Although the complement is being created but no self-loops and parallel edges are created.Working of complement(G) function: An edge (n,n2) where n is iterator used to iterate over G is added to complement graph if following conditions are met: n and n2 are not neighbours i.e n2 are in the adjacency list of n and vice versa.And n != n2.n and n2 both are part of G. Approach: We will import networkx with an alias nx.Create a sample graph object G using path_graph() function.We will then call complement function passing G as an argument.We will capture the object returned by complement function in another object G_C.We will then call draw() function passing G_C as an argument which will display of the complement graph. Python3 # importing networkx module import networkx as nx # creating sample graph object G = nx.path_graph(3) # complement of G and saving in G_C G_C = nx.complement(G) # calling draw() to visualize the original graph nx.draw(G, node_color='Green', with_labels=True) # calling draw() to visualize the complement graph nx.draw(G_C, node_color='Green', with_labels=True) Output: Original GraphComplement Graph By using the complement() method all the edges in G were removed and all other possibilities were added to the complement of G and printed. Comment P parthbanathia Follow 0 Improve P parthbanathia Follow 0 Improve Article Tags : Data Science Explore Introduction to Machine LearningWhat is Data Science?8 min readTop 25 Python Libraries for Data Science in 202510 min readDifference between Structured, Semi-structured and Unstructured data2 min readTypes of Machine Learning7 min readWhat's Data Science Pipeline?3 min readApplications of Data Science6 min readPython for Machine LearningData Science with Python Tutorial2 min readPandas Tutorial4 min readNumPy Tutorial - Python Library3 min readData Preprocessing in Python4 min readEDA - Exploratory Data Analysis in Python6 min readIntroduction to StatisticsStatistics For Data Science11 min readDescriptive Statistic5 min readWhat is Inferential Statistics?7 min readBayes' Theorem13 min readProbability Data Distributions in Data Science8 min readParametric Methods in Statistics6 min readHypothesis Testing9 min readANOVA for Data Science and Data Analytics9 min readBayesian Statistics & Probability6 min readFeature EngineeringWhat is Feature Engineering?5 min readIntroduction to Dimensionality Reduction4 min readFeature Selection Techniques in Machine Learning6 min readFeature Engineering: Scaling, Normalization and Standardization5 min readPrincipal Component Analysis(PCA)7 min readModel Evaluation and TuningEvaluation Metrics in Machine Learning9 min readRegularization in Machine Learning5 min readCross Validation in Machine Learning5 min readHyperparameter Tuning7 min readML | Underfitting and Overfitting5 min readBias and Variance in Machine Learning6 min readData Science PracticeData Science Interview Questions and Answers15+ min readData Science Coding Interview Questions15 min readTop 65+ Data Science Projects with Source Code 6 min read Like