Complete Graph using Networkx in Python


One of the most important ideas in graph theory is the idea of a full graph. It is made up of "points" called "nodes" that are all connected by "edges." In other words, it has more links anything else. Complete graphs are very important in many areas, such as computer networks, social networks, and solving optimization problems.

Networkx is a powerful Python tool that makes it easy for programmers and data scientists to work with and look at complex graphs. Users can easily make, edit, visualize, and move through graphs with Networkx's easy-to-use interface and many features. Because of this, it is an important tool for learning about the rich world of graph theory and how it can be used in data structures and other areas.

Installing Networkx

Networkx can be added to Python with either of the two most well-known package managers. The packages used to build them are called pip and conda. Anyway, here's all you have to do to get Networkx up and running −

Using pip (for Windows

  • Open your command-line interface (e.g., Command Prompt on Windows, Terminal on mac OS/Linux).

  • Ensure that you have Python installed on your system. You can check by running python --version in the command line.

  • Update pip to the latest version by executing pip install --upgrade pip.

  • To install Networkx, simply type pip install networkx and press Enter.

pip will now download and install the latest version of Networkx and its dependencies automatically

Using conda (for Mac)

  • If you have Anaconda or Miniconda installed, open your terminal or Anaconda Prompt.

  • If you don't have Anaconda or Miniconda, download and install the appropriate version from the official website

  • Once you have the Anaconda Prompt open, execute the following command: conda install -c conda-forge networkx.

  • conda will now resolve the dependencies and install Networkx on your system.

After completing either of the preceding steps, Networkx should be installed successfully on your machine, and you can begin using it to generate and analyze graphs in Python.

To verify the installation, you can integrate Networkx into a Python interactive shell or script by typing import networkx. If there are no errors, the installation was successful, and you are set to explore different graph-related tasks and algorithms using Networkx

Creating a Complete Graph

Networkx makes it easy to construct whole graphs. A full graph is one in which there is a unique edge connecting every pair of unique nodes. The complete_graph() method in Networkx can be used to construct a whole graph. The number of nodes in the graph is the illustrates how to generate a whole graph consisting of five nodes

import networkx as nx

# Create a complete graph with 5 nodes
complete_graph = nx.complete_graph(5)

Adding Nodes and Edges

Networkx makes it easy to add nodes and links to a graph, whether it is already full or not. We use the add_node() method with the name of the node as an input to add a single node.As an example −

Python code


The add_nodes_from() method takes a list of node labels as a parameter, which lets us add several nodes at once. The method for including edges is the same. Using the add_edge() method, a link is made between nodes 1 and 2 −

Python code

complete_graph.add_edge(1, 2)

A list of tuples, where each tuple represents an edge between two nodes, can be passed to the add_edges_from() method to add numerous edges at once.

Python code

edges_to_add = [(3, 4), (4, 0), (2, 3)]

Networkx additionally lets you customize edges with additional attributes like weight and label, which might be useful in many graph-based use cases.

Visualizing the Complete Graph

Understanding the structure and properties of a graph requires visualizing its entirety. Networkx, when coupled with the Matplotlib toolkit, makes graph visualization a breeze. Networkx's draw() method allows us to create a comprehensive graph −

Python code

import matplotlib.pyplot as plt

# Draw the complete graph
nx.draw(complete_graph, with_labels=True, node_color='skyblue', node_size=800,

# Show the plot

Accessing Graph Information

Networkx gives you different ways to get to important details about the whole graph. For example, we can use the nodes() method to get a list of nodes −

Python code

all_nodes = complete_graph.nodes()

We can use the edges() method to get the list of edges

Python code

all_edges = complete_graph.edges()

We can also get the degree distribution of nodes by using the degree() method, which gives a dictionary with nodes as keys and their degrees as values

Python code

degree_distribution =

By looking at this information, we can learn a lot about the structure and properties of the whole graph, which is important for many methods and applications that use graphs.


Complete graphs have many uses in the real world because they show how everything is linked. They are also a good way to understand complicated relationships

  • Social Networks −Complete graphs shows how people in small social groups gets linked to each other. This is basically used for small towns or groups of friends

  • Transportation Systems − Complete transport networks have clear links between all locations are shown by complete graphs. They help figure out the best ways and planfor infrastructure

  • Computer Networks − All the gadgets in a network can talk to each other directly in a complete graph. They help people understand how data is sent and how to deal with mistakes.


In conclusion, using Networkx in Python to work with complete graphs opens up a whole new world of data structures and graph theory options. Networkx gives you an easy-to-use and effective way to make, see, and examine complete graphs. Complete graphs are important for understanding the complexities of all-connected networks, which makes them useful in many real-world situations. Complete graphs show how connections and speed work in everything from social networks to transportation systems and computer networks. By using Networkx's power, developers and researchers can learn more, improve system designs, and solve hard problems in many different areas. Anyone interested in graph-based data analysis will find it rewarding to use Python and Networkx to look at full graphs.

Someswar Pal
Someswar Pal

Studying Mtech/ AI- ML

Updated on: 28-Jul-2023


Kickstart Your Career

Get certified by completing the course

Get Started