Pradeep T — April 12, 2022
Graphs & Networks Intermediate Libraries Python

This article was published as a part of the Data Science Blogathon.

Introduction

In this article, we will discover Graph Network Tools and Packages in python that are currently dominating in the data science industry.  The world is all about relations. Every entity we see around us is related to each other somehow. Modelling these real-world entities into programming objects is always a challenging task for programmers. While programming, when we are trying to model these real-world objects, we need a specific kind of data structure. Graphs are the best choice for us to solve this challenging problem. There are several tools and packages in python language for integrating graph data structure into our codebase. 

Introduction | Graph Network Tools
Source: medium.com

 The Graph Network Tools which we are going to explore in this blog is given below:

  • Networkit
  • Igraph
  • Graph-tool
  • Networkx

Popular Graph Network Tools

1) Networkit

 NetworKit is an open-source toolkit for large-scale network analysis growing in popularity. Its goal is to develop tools for analyzing huge networks with hundreds of thousands to billions of edges. It accomplishes this by implementing efficient graph algorithms, many of which are parallelized to use multicore processors. These are used to calculate network analysis standard measurements such as degree sequences, clustering coefficients, and centrality measures. In this regard, NetworKit is comparable to NetworkX; however, focusing on parallelism and scalability. NetworKit also serves as a testbed for algorithm development, with new algorithms from recently published research.

NetworKit is a Python library. The Cython toolchain exposes performance-aware algorithms written in C++ (typically using OpenMP for shared-memory parallelism) to Python. Python allows us to work interactively and with a diverse set of data analysis tools. Additionally, the core of NetworKit can be developed and utilized as a native library.

Features

  • Interactive Workflow Pipeline
  • High Performance
  • Easy Integration and scalability

Python integration

For installation

#To install networkit
pip3 install networkit

Sample python code for generating the graph with vertices and edges using networkit

import networkit as nk
import matplotlib.pyplot as plt
#Create directed graph object and to add nodes
G = nk.Graph(5, directed=True,weighted=True)
#Add edges to the graph
G.addEdge(1, 3)
G.addEdge(2, 4)
G.addEdge(1, 2)
G.addEdge(3, 4)
G.addEdge(2, 3)
G.addEdge(4, 0)
#Set weights to edges
G.setWeight(1, 3, 2)
G.setWeight(2, 4, 3)
G.setWeight(3, 4, 4)
G.setWeight(4, 0, 5)
#To see the graph structural overview
print(nk.overview(G))
#For visualization
nk.viztasks.drawGraph(G)
plt.show()

Output

Output

 

Graph visualization | Graph Network Tools

 

Official documentation

Many of the netoworkit functionalities are in the development stage. For further details check the official documentation of networkit here 

2) Igraph

Igraph is a set of graph-based network analysis tools focused on performance, portability, and simplicity of use. Igraph is a free and open-source tool. It is written in C and C++ and can be easily integrated with different programming languages such as R, Python, Mathematica, and C/C++. 

Features

  •  Igraph uses the Cairo library to implement and render a variety of layout techniques.
  • It was well documented and got over a thousand stars on GitHub across its various modules.

Python integration

For installation

#to install igraph
pip3 install igraph
#to install cairocffi for visualization
pip3 install cairocffi

Sample python code for generating the graph with vertices and edges using igraph

from igraph import *
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
g = Graph(directed=True)
#Adding the vertices
g.add_vertices(7)
#Adding the vertex properties
g.vs["name"] = ["Alice", "Bob", "Claire", "Dennis", "Esther", "Frank", "George"]
g.vs["age"] = [25, 31, 18, 47, 22, 23, 50]
g.vs["gender"] = ["f", "m", "f", "m", "f", "m", "m"]
#Set the edges
g.add_edges([(0,1), (0,2), (2,3), (3,4), (4,2), (2,5), (5,0), (6,3), (5,6)])
#Set the edge properties
g.es["is_formal"] = [False, False, True, True, True, False, True, False, False]
#Set different colors based on the gender
g.vs["label"] = g.vs["name"]
color_dict = {"m": "blue", "f": "pink"}
g.vs["color"] = [color_dict[gender] for gender in g.vs["gender"]]
#To display the Igraph
layout = g.layout("kk")
plot(g, layout=layout, bbox=(400, 400), margin=20)

Output

 

python code using igraph | Graph Network Tools

Official documentation

For further details check the official documentation of igraph here.

3) Graph-tool

Graph-tool is a Python module that allows you to manipulate and analyze graphs statistically (a.k.a. networks). Unlike most other Python modules with similar capabilities, the basic data structures and algorithms are developed in C++, with a heavy reliance on the Boost Graph Library and substantial use of template metaprogramming. This gives it performance comparable to a pure C/C++ library (both in terms of memory usage and processing time). The graph tool module includes a Graph class and a number of graph-related techniques. The Boost Graph Library is used to write the internals of this class, as well as most algorithms, in C++ for performance.

Features

  • Since it is written in C++, it is very fast
  • Extensive features such as arbitrary vertex, edge or graph characteristics are supported, efficient “on-the-fly” vertices and edge filtering
  • Supports Powerful Visualization
  • OpenMP Support
  • Fully documentation available

Python integration

For installation

Python packages are usually very easy to install via pip. However, the case is different for graph-tool. This is because graph-tool is really a C++ library wrapped in Python, and it has a lot of C++ dependencies like BoostCGAL, and ex-pat, which aren’t available through Python-only package management systems like pip.

Using Docker is the most hands-off and OS-agnostic way to install graph-tool. If you have Docker installed, all you have to do is run

docker pull tiagopeixoto/graph-tool

This will download a Docker image based on Arch GNU/Linux that includes graph-tool and may be used on any recent GNU/Linux distribution, as well as MacOS X and Windows. It also includes matplotlib, Pandas, IPython, and Jupyter, among other essential Python tools.

You can start an interactive python shell in the container after the image has been fetched by running:

docker run -it -u user -w /home/user tiagopeixoto/graph-tool ipython

which will install graph-tool and give you a Python 3 environment.

Sample python code for generating the graph with vertices and edges using graph-tool

#For getting all sub modules of graph-tool
from graph_tool.all import *
#Initializing a graph object (By default, newly created graphs are always directed)
g = Graph()
#Initializing an undirected graph object
ug = Graph(directed=False)
#For making a undirected graph to directed one on-the-fly
ug.set_directed(True)
#Adding the verices
v1 = g.add_vertex()
v2 = g.add_vertex()
#Adding the edges between vertices
e = g.add_edge(v1, v2)
#For visualizing the generated directed graph
graph_draw(g, vertex_text=g.vertex_index, output="sampleGraph.pdf")

Output

 

graph-tool | Graph Network Tools
Source: Graph-tool

Official documentation

For further details check the official documentation of the graph tool here.

4) NetworkX

NetworkX is a Python tool for creating, manipulating, and studying complex networks’ structure, dynamics, and functions. Python 3.8, 3.9, or 3.10 is required for Networkx, and it is written itself in python. It’s used to investigate massive, complicated networks that are represented as graphs with nodes and edges. We can load and store complex networks with networkx. We may create a variety of random and traditional networks, study their structure, establish network models, create new network algorithms, and draw them. Since it is written in python, Networkx is significantly slower than the other libraries.

Features

  • Graph, digraph, and multigraph data structures
  • It supports numerous standard graph algorithms.
  • Generators for classic graphs, random graphs, and synthetic networks
  • With over 90% code coverage, this product has been thoroughly tested.
  • Prototyping is quick, and it’s simple to teach. It’s also cross-platform.
  • Well documented with 4k GitHub stars

Python integration

For installation

pip3 install networkx
pip3 install pyvis  # For visualization

Sample python code for generating the directed graph with vertices and edges (with weights) using networkx

import networkx as nx
from pyvis.network import Network
net                = Network()
#Create directed graph object
Graph = nx.DiGraph()
#Create nodes
Graph.add_node("C++")
Graph.add_node("Python")
Graph.add_node("Java")
Graph.add_node("C#")
Graph.add_node("Go")
Graph.add_node("Julia")
#For adding the edges between nodes with weight and value (Edges with higher value will appear as bold)
Graph.add_edge("C++", "C#", weight=.75, value=20)
Graph.add_edge("Python", "Julia", weight=.75, value=20)
Graph.add_edge("Java", "Go", weight=.5)
Graph.add_edge("Python", "C#", weight=.5)
Graph.add_edge("Go", "C++", weight=.5)
Graph.add_edge("Java", "Julia", weight=.5)
Graph.add_edge("Java", "C#", weight=.75, value=20)
#For visualizing the graph
net.from_nx(Graph)
net.show("graph.html")

Output

 

networkx |

 

Official documentation

For further details check the official documentation of networkx here.

Other Graph Network Tools

  1. Snap – For more details check here
  2. Easy graph  – For more details check here
  3. Another Python Graph Library(APGL) –  For more details check here
  4. Python-graph – For more details check here
  5. Easy-Graph –   For more details check here

Conclusion

Graphs have long been an important component of NLP applications, such as syntax-based Machine Translation, knowledge graph-based question answering, and abstract meaning representation for common sense reasoning tasks. NetworkX, igraph, networkit and graph-tool are just a few of the graph data packages available to Python programmers. Aside from the pros and cons, their interfaces for managing and processing Python graph data structures are extremely comparable. In this article, we have come across the usage details of some of the popular graph network libraries in Python, which are commonly used in the data science field today. They are

  • Networkit
  • Igraph
  • Graph-tool
  • Networkx

There are many more libraries are there in Python for building and managing the graph networks, but the above-mentioned libraries are used widely today. The other three packages are written in C/C++ but offer Python APIs. Networkx is purely written in Python. A graph has an R and Mathematica binding. All the above libraries place a greater emphasis on performance and provide multi-processing capabilities. If you run into any problems while building the graph, or if you have any ideas/suggestions/comments, please share them in the space below. 

Happy coding!! 😊

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

About the Author

Our Top Authors

Download Analytics Vidhya App for the Latest blog/Article

One thought on "All About Popular Graph Network Tools in Python"

Harish Nagpal
Harish Nagpal says: April 12, 2022 at 7:06 pm
Informative article. Thanks for sharing. Reply

Leave a Reply Your email address will not be published. Required fields are marked *