# How to Use Python NetworkX Library

Contents

In this article, you will learn how to use Python NetworkX library.

## Python NetworkX Library

Python NetworkX is a popular library for working with networks and graphs. It allows you to create, manipulate, and analyze graphs in Python. Here are the basic steps for using the NetworkX library:

##### Install NetworkX:

`pip install networkx`

##### Import NetworkX:

To use the library in your Python code, you need to import it first. You can do this using the following command:

`import networkx as nx`

##### Create a Graph:

Once you have imported NetworkX, you can create a graph object using the Graph() function. There are different types of graphs you can create such as directed, undirected, weighted, etc. Here’s an example of creating an undirected graph:

`G = nx.Graph()`

##### Add Nodes:

You can add nodes to the graph using the add_node() method. Nodes can be any hashable object such as strings, numbers, or objects. Here’s an example of adding nodes to the graph:

```
G.add_node(1)
G.add_node(2)
G.add_node(3)
```

##### Add Edges:

You can add edges to the graph using the add_edge() method. An edge is a connection between two nodes. Here’s an example of adding edges to the graph:

```
G.add_edge(1, 2)
G.add_edge(2, 3)
G.add_edge(3, 1)
```

##### Visualize the Graph:

You can use the Matplotlib library to visualize the graph. Here’s an example of drawing the graph:

```
import matplotlib.pyplot as plt
nx.draw(G, with_labels=True)
plt.show()
```

This is just a basic overview of how to use the NetworkX library. There are many more advanced features and methods available in the library that can help you analyze and manipulate graphs.