NetworkX is a Python package that allows us to create, manipulate, and study structure, functions and dynamics of complex networks.

Table of Contents

## NetworkX

- NetworkX is suitable for real-world graph problems and is good at handling big data as well.
- As the library is purely made in python, this fact makes it highly scalable, portable and reasonably efficient at the same time.
- It is open source and released under 3-clause BSD License.

## Why NetworkX?

NetworkX gives you a lot of reasons to go with it. Following are some features of NetworkX that makes it a package to go with:

- It has numerous standard graph algorithms
- It supports data structures for graphs, digraphs, and multigraphs
- It provides various network structure and measures for analysis
- Making classic/random graphs and synthetic networks is much easier using generators provided in the package
- Nodes in your network or graph can be absolutely anything, be it images, XML data or anything else
- Edges also can hold arbitrary data like timestamp and weight
- It has been well tested with about 90% code coverage

Apart from above, it has an additional benefit because it is based on pure Python and so, it has a fast-prototyping syntax and very easy to learn. Let’s get started!

## Getting Started with NetworkX

NetworkX requires Python >= 2.7 installed on the machine. Let’s complete the installation as a first step.

### Install NetworkX

We can install NetworkX using Python Package Index (pip):

```
pip install networkx
```

In case you face any issues while installing the package using pip, install it from GitHub using the git command:

```
pip install git://github.com/networkx/networkx.git
```

### Using NetworkX

Now that we have NetworkX installed on our machine, we can use it in any of our scripts using the following import statement:

```
import networkx
```

### Creating Graphs

As NetworkX library is used to manage relationships using the Graph structure, we can get started by creating a graph with no nodes and edges:

```
import networkx
graph = networkx.Graph()
```

Since there are no nodes or edges we can’t see the graph so let’s use idle to check if a graph is created or not:

### Adding Nodes to a Graph

Adding and checking nodes is quite simple and can be done as:

```
graph.add_node(1)
```

Or using list as:

```
graph.add_nodes_from([2,3])
```

And to see the nodes in existing graph:

```
graph.nodes()
```

When we run these set of commands, we will see the following output:

As of now, a graph does exist in the system but the nodes of the graphs aren’t connected. This can be done using the edges in a graph which makes a path between two Graph nodes.

### Adding Edges between nodes

Adding and checking edges is quite simple as well and can be done as:

```
graph.add_edge(1,2)
```

Or using list as:

```
graph.add_edges_from([(1,2),(2,3)])
```

And to see the nodes in existing graph, we can again print the edges of the graph object:

```
graph.edges()
```

When we run these set of commands, we will see the following output:

## Attributes

Graphs are data structures which are used to connect related data and show the relationship between them by using a weight. This weight can be called an attribute of the relation of the two nodes in the Graph. Also, to exhibit properties for a node or an edge or for the graph itself, we can use attributes as well.

### Graph Attributes

We can assign meta-data to a Graph by adding graph attributes to a Graph object. Let’s see a code snippet on how this can be done:

```
graph.graph["day"]="Monday"
graph.graph
```

### Node Attributes

Here, we will add attributes to the nodes of the Graph object:

```
graph.add_node(1, time='5pm')
graph.add_nodes_from([3], time='2pm')
graph.node[1]
graph.node[1]['room'] = 714
graph.nodes(data=True)
```

### Edge Attributes

Finally, we will assign some attributes to the edges of the Graph object. To assign edge attributes:

```
graph.add_edge(1, 2, weight=4.7 )
graph.add_edges_from([(3,4),(4,5)], color='red')
graph.add_edges_from([(1,2,{'color':'blue'}), (2,3,{'weight':8})])
graph[1][2]['weight'] = 4.7
graph.edge[1][2]['weight'] = 4
```

Once we have added the attributes to the Graph, the nodes and the edges, we can finally print all the data:

## Directed Graph

In the last section, we saw we could assign attributes to edges of a Graph. We can create a directed graph and add weighted edges as shown below.

```
DG=networkx.DiGraph()
DG.add_weighted_edges_from([(1,2,0.5), (3,1,0.75)])
DG.out_degree(1,weight='weight')
DG.degree(1,weight='weight')
DG.successors(1)
DG.neighbors(1)
```

Once we run these commands, we will be able to see neighbors and successors of the Graph we just made:

## Drawing Graph

So far we have been performing various operations on graphs but not able to visualize any of the operations. Now let’s try to visualize them. For this, we’ll need the help of matplotlib library:

```
import matplotlib.pyplot as plt
networkx.draw(graph)
networkx.draw_random(graph)
networkx.draw_circular(graph)
networkx.draw_spectral(graph)
plt.show()
```

And the result of above-created graphs can be seen as:

## Conclusion

In this post, we have seen that NetworkX make it very easy to create and work with graphs. We have seen several examples of creating graphs and assigning attributes, weights and direction to the edges of the Graphs as well.

NetworkX makes it easy to create graphs without much of hassle and with just a few lines of code. It also has generators for graphs and various networks and also it’s easy to analyse the graphs.

Reference: API Doc