# NetworkX: Examining and Removing Graph Elements

Examining graph elements

NetworkX provides four basic properties to facilitate examining graphs.

1. G.nodes - to view nodes

2. G.edges - to view edges

3. G.adj - to view neighbor nodes of each node

4. G.degree - to view degree of each node

These provide a continually updated read-only view into the graph structure and help us understand the underlying network and its properties.

Now, we will create a sample graph using the below code.

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

The graph we just created looks like the one below.

Now we will apply the four properties we discuss earlier to the above graph.

import networkx as nx graph_edges = [(0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 4), (3, 4)] G = nx.Graph() G.add_edges_from(graph_edges) print('Nodes:', end=' ') print(G.nodes) print('Edges:', end=' ') print(G.edges) print('Neighbors of 0:', end=' ') print(list(G.adj[0])) print('Degrees of nodes:', end=' ') print(G.degree)

The output is as below.

Nodes: [0, 2, 3, 4, 1]

Edges: [(0, 2), (0, 3), (0, 4), (2, 1), (2, 4), (3, 1), (3, 4), (4, 1)]

Neighbors of 0: [2, 3, 4]

Degrees of nodes: [(0, 3), (2, 3), (3, 3), (4, 4), (1, 3)]

Removing graph elements

Removing nodes and edges of a graph are done in the same way as adding.

We can use remove_node(), remove_nodes_from(), remove_edge() and remove_edges_from() methods to remove nodes and/or edges from a graph.

We can remove the edge (2,4) from the earlier graph using the below code.

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

The graph looks like this

Similarly, we can remove the node 4 using the below code.

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

The graph then looks like this

#### 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 507 2
pytest: Working 511 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 799 2
REST application using CherryPy 664 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 1633 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 1397 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 978 2
Vehicle Detection System 1081 2
NetworkX: Examining and Removing Graph Elements 608 2
Handling URLs with CherryPy 536 2
PEP 8 - Guide to Beautiful Python Code 757 2
NetworkX: Drawing Graphs 624 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 582 2
Contact Book using Python 2095 2
Introduction to CherryPy 547 2
nose: Introduction 505 2
Text-based Adventure Game using Python 3000 2
NetworkX: Adding Attributes 2279 2
NetworkX: Directed Graphs 1021 2
Dice Simulator using Python 560 2
Decorating CherryPy applications using CSS 833 2