Featured Article

Master Networkx Tutorial Python: Build Your First Graph Network

Kenneth Jul 13, 2026

Embarking on your Python programming journey and eager to explore graph algorithms? NetworkX, a popular Python library, is here to help. With powerful data structures and sophisticated functionalities, it's your key to unlocking the world of graphs and networks. Let's dive into a comprehensive tutorial that walks you through NetworkX's robust features.

Neural Networks Explained from Scratch using Python
Neural Networks Explained from Scratch using Python

Before we dive in, ensure you have NetworkX installed. If you haven't, install it using pip: `pip install networkx`. Now, let's create our first graph and explore the vast capabilities of NetworkX.

Python Projects | Notion
Python Projects | Notion

Creating and Manipulating Graphs

NetworkX offers multiple ways to create graphs, including undirected, directed, and weighted graphs. Let's start with creating a simple undirected graph.

an image of a computer screen with the text network graph using python
an image of a computer screen with the text network graph using python

```python import networkx as nx # Create an empty undirected graph G = nx.Graph() # Add nodes and edges G.add_node(1) G.add_node(2) G.add_edge(1,2) ```

Adding Nodes and Edges

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

Node addition is seamless, with methods like `add_node()` and `add_nodes_from()`. Edges can be added using `add_edge()`, which takes two nodes and an optional weight. You can also add multiple edges at once with `add_edges_from()`.

```python # Add multiple nodes and edges in one go G.add_nodes_from([3, 4]) G.add_edges_from([(3, 4), (1, 3)]) ```

Degree Centrality

Python for Graph and Network Analysis
Python for Graph and Network Analysis

NetworkX provides numerous graph metrics. Degrees, or the number of connections for a node, are straightforward to access. NetworkX also provides a degree centrality measure, ranking nodes based on their edge connections.

```python # Print nodes and their degrees print(nx.degree(G)) # Calculate and print degree centrality print(nx.degree_centrality(G)) ```

Graph Algorithms

Network Analysis with Python and NetworkX Cheat Sheet
Network Analysis with Python and NetworkX Cheat Sheet

NetworkX's prowess lies in its wide array of graph algorithms. Let's explore two popular ones: depth-first search (DFS) and shortest paths.

```python # Perform a DFS traversal starting from node 1 print(list(nx.dfs_tree(G, 1))) # Calculate shortest paths from node 1 print(nx.single_source_shortest_path_length(G, 1)) ```

NETWORK COMMANDS
NETWORK COMMANDS
Python syntax cheat sheet infographic
Python syntax cheat sheet infographic
Machine Learning with Python Tutorial
Machine Learning with Python Tutorial
5 Python Tricks You’ll Wish You Knew Sooner! 🐍✨
5 Python Tricks You’ll Wish You Knew Sooner! 🐍✨
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
Python Kivy Projects
Python Kivy Projects
the flyer for build real python projects
the flyer for build real python projects
the top 50 python project ideas
the top 50 python project ideas
a computer screen with the words python string method
a computer screen with the words python string method

Depth-First Search

DFS, a fundamental graph algorithm, explores as far as possible along each branch before backtracking. NetworkX's implementation, `nx.dfs_tree()`, returns the tree generated during the search.

```python # Perform a DFS traversal starting from an arbitrary node print(list(nx.dfs_tree(G, nodetype=0))) ```

Shortest Paths

NetworkX offers multiple algorithms for finding shortest paths, including Dijkstra's and Bellman-Ford. The `single_source_shortest_path_length()` function calculates shortest distances from a single source node.

```python # Calculate shortest paths from all nodes to a specific node print(nx.shortest_path_length(G, 1)) ```

NetworkX provides a wealth of graph algorithms and operations, just waiting to be discovered. From here, you can explore other algorithms, create more complex graphs, and even visualize your graphs with matplotlib. So, roll up your sleeves and get ready to dive deeper into the fascinating world of graphs and networks!