NetworkX: Creating a Graph














































NetworkX: Creating a Graph



Python: NetworkX

NetworkX: Creating a Graph

A graph or a network is a diagram representing a system of connections or interrelations among two or more things. It can also be defined as a collection of nodes or vertices along with identified pairs of nodes called edges.

In NetworkX, nodes can be any hashable object like a text string, an image, etc.

We will start by creating an empty graph i.e., a graph with no nodes or edges.

The below code imports the networkx module and creates a Graph() object which is by definition an empty graph.

import networkx as nx G = nx.Graph()

Note that Python's None object should not be used as a node as it is used to determine whether optional function arguments have been assigned in many functions and will thus result in unexpected behavior.

There are several ways to grow a graph. Some of the most common ones are to add edges and/or nodes.

Growing graph by adding nodes

We can add nodes in several different ways.

  1. Adding one node at a time: We can use the add_node() method to add a single node to our graph. The below code shows how to create a graph and add nodes to it one by one.

import networkx as nx G = nx.Graph() G.add_node(1) G.add_node(2) G.add_node(3)

The graph from the above code looks like this


Tip: Save the code in a cell of a Jupyter notebook and run the cell after adding the line 'nx.draw(G)' at the end to view the graph.

  1. Adding nodes through Python lists: We can add several nodes simultaneously using lists or any other iterable container like strings, tuples using the add_nodes_from() method. The below code shows how to create a graph and add nodes using a list.

import networkx as nx G = nx.Graph() G.add_nodes_from([1, 2, 3])

The graph from the above code looks like this


  1. Adding node attributes with nodes: We can add node attributes while adding nodes if our nodes given to the add_nodes_from() method are tuples of the form '(node, node_attribute_dictionary)'. The below code shows how to create a graph and add nodes along with their attributes.

import networkx as nx G = nx.Graph() G.add_nodes_from([(1, {'label': 1}), (2, {'label': 2}), (3, {'label': 3})])

The graph from the above code looks like this


  1. Adding nodes from one graph to another: We can also use the add_nodes_from() method to add nodes from one graph to another. The below code shows how to add nodes of one graph to another.

import networkx as nx G = nx.Graph() G.add_nodes_from([1, 2, 3]) H = nx.Graph() H.add_nodes_from(G)

As expected the graph looks like this


Growing graph by adding edges

We can add edges similar to the way we added nodes.

  1. Adding one edge at a time: We can use the add_edge() method to add a single node to our graph. The below code shows how to create a graph and add edges to it one by one.

import networkx as nx G = nx.Graph() G.add_edge(1, 2) G.add_edge(3, 4) G.add_edge(2, 4)

The graph from the above code looks like this


  1. Adding edges through Python lists: We can add several nodes simultaneously using lists using the add_edges_from() method. The below code shows how to create a graph and add edges using a list.

import networkx as nx G = nx.Graph() G.add_edges_from([(1, 2), (3, 4), (2, 3)])

The graph from the above code looks like this


As in the case of nodes we can add edge attributes also by keeping our edge tuples of the form '(node1, node2, edge_attribute_dictionary)'.


More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 990 2
Number Guessing Game using Python 683 2
Pyperclip: Not Implemented Error 1026 2
Hangman Game using Python 16785 2
Using Databases with CherryPy application 1672 2
nose: Working 506 2
pytest: Working 510 2
Open Source and Hacktoberfest 867 2
Managing Logs of CherryPy applications 1001 2
Top 20 Data Science Tools 684 2
Ajax application using CherryPy 798 2
REST application using CherryPy 663 2
On Screen Keyboard using Python 5508 2
Elastic Net Regression 815 2
US Presidential Election 2020 Prediction using Python 794 2
Sound Source Separation 1164 2
URLs with Parameters in CherryPy 1632 2
Testing CherryPy application 635 2
Handling HTML Forms with CherryPy 1448 2
Applications of Natural Language Processing in Businesses 508 2
NetworkX: Multigraphs 648 2
Tracking User Activity with CherryPy 1396 2
CherryPy: Handling Cookies 820 2
Introduction to NetworkX 633 2
TorchServe - Serving PyTorch Models 1301 2
Fake News Detection Model using Python 734 2
Keeping Home Routers secure while working remotely 483 2
Email Slicer using Python 2996 2
NetworkX: Creating a Graph 1108 2
Best Mathematics Courses for Machine Learning 551 2
Hello World in CherryPy 680 2
Building dependencies as Meson subprojects 977 2
Vehicle Detection System 1081 2
NetworkX: Examining and Removing Graph Elements 607 2
Handling URLs with CherryPy 536 2
PEP 8 - Guide to Beautiful Python Code 756 2
NetworkX: Drawing Graphs 623 2
Mad Libs Game using Python 643 2
Hosting Cherry applications 612 2
Top 5 Free Online IDEs of 2020 866 2
pytest: Introduction 534 2
Preventing Pwned and Reused Passwords 581 2
Contact Book using Python 2095 2
Introduction to CherryPy 546 2
nose: Introduction 505 2
Text-based Adventure Game using Python 3000 2
NetworkX: Adding Attributes 2278 2
NetworkX: Directed Graphs 1021 2
Dice Simulator using Python 560 2
Decorating CherryPy applications using CSS 833 2

Comments