All About Popular Graph Network Tools in Python

Pradeep T 16 Feb, 2024 • 8 min read

Introduction

In this tutorial, 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. Modeling these real-world entities into programming objects is always a challenging task for programmers. When programming, as we endeavor 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 the Python language for integrating graph data structures into our codebase. One common format for handling graph data in Python is through CSV files, providing a convenient way to store and manipulate graph-related data.

Learning Objectives

  • Understand the importance of graph data structures in modeling real-world relationships and entities within the context of programming.
  • Explore prominent graph network tools and packages in Python, including Networkit, Igraph, Graph-tool, and NetworkX.
  • Gain insights into the features and capabilities of each graph network tool, such as performance, scalability, and ease of integration with Python.
  • Learn how to install and set up graph network tools in Python, along with practical examples of generating graphs and performing basic operations.
  • Discover the diverse applications of graph network analysis in data science, natural language processing (NLP), and other fields, along with real-world use cases.

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

Introduction | Graph Network Tools

We will explore the following tools in this article:

  • Networkit
  • Igraph
  • Graph-tool
  • Networkx

Networkit

NetworKit, an open-source toolkit gaining traction for large-scale network analysis, aims to tackle the challenges posed by massive networks, often comprising hundreds of thousands to billions of edges. Its primary goal is to provide efficient graph algorithms tailored for such scales, many of which optimize for parallel execution on multicore processors.

These algorithms play a pivotal role in computing standard network analysis metrics, including degree sequences, clustering coefficients, and centrality measures. Notably, NetworKit stands out from alternatives like NetworkX by placing a strong emphasis on parallelism and scalability. Moreover, it serves as a valuable platform for algorithmic experimentation, facilitating the integration of newly developed algorithms stemming from cutting-edge research. Its utility extends beyond mere analysis; it also functions as a repository for datasets, fostering a comprehensive ecosystem for network analysis endeavors.

NetworKit, a Python library, offers a versatile toolkit for various computational tasks. Leveraging the Cython toolchain, it seamlessly integrates performance-optimized algorithms crafted in C++. These algorithms, often utilizing OpenMP for shared-memory parallelism, enhance efficiency significantly. Python’s interactive nature facilitates seamless interaction with diverse data analysis tools, amplifying the library’s utility. Furthermore, NetworKit’s core can be seamlessly incorporated as a native library, empowering users to delve into network graphs and social network analysis effortlessly.

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.

Igraph

Igraph is a set of graph-based network analysis tools focused on performance, portability, and simplicity of use. It’s a free and open-source tool, written in C and C++, and can be easily integrated with different programming languages such as R, Python, Mathematica, and C/C++. Additionally, its versatility extends to dash and visualizations, making it a comprehensive solution for various graph-related tasks.

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.

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.

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. With the NetworkX library, we can load and store complex networks, create a variety of random and traditional networks, study their structure, establish network models, develop new network algorithms, and visualize them for better understanding and analysis. Since it is written in Python, NetworkX is significantly slower than some other libraries, but its ease of use and powerful capabilities for data visualization make it a valuable tool for network analysis tasks.

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!! 😊

Key Takeaways

  • Graph network tools like Networkit, Igraph, Graph-tool, and NetworkX offer versatile solutions for analyzing and manipulating graph data structures in Python.
  • Networkit stands out for its emphasis on scalability and parallelism, making it suitable for large-scale network analysis tasks.
  • Igraph provides a comprehensive set of graph-based analysis tools with support for various programming languages and visualization techniques.
  • Graph-tool offers high performance and extensive features for statistical graph analysis, leveraging C++ for efficient memory usage and processing time.
  • NetworkX, while slower compared to other libraries, excels in ease of use and data visualization capabilities, making it a valuable tool for network analysis tasks in Python.
  • The usage of graph network tools extends beyond data science into fields like NLP, knowledge graph-based question answering, and common sense reasoning tasks.

Frequently Asked Questions

Q1.How do you graph data in Python?

In Python, you can graph data using libraries like Matplotlib, Seaborn, or Plotly. First, load your data into a DataFrame using Pandas. Then, use the plotting functions from these libraries to create various types of graphs such as line plots, bar plots, and scatter plots. These libraries offer customization options for creating visually appealing graphs.

Q2. What is the alternative to NetworkX?

A. The alternative to NetworkX is NetworKit, which emphasizes parallelism and scalability, making it suitable for large-scale network analysis tasks.

Q3. What is the best way to visualize a network?

A. The best way to visualize a network is by using graph visualization software or libraries like Matplotlib, Seaborn, or Plotly, depending on the complexity and customization requirements.

Q4. Which is better Gephi or Cytoscape?

A. The choice between Gephi and Cytoscape depends on the specific requirements and preferences. Gephi is known for its user-friendly interface and visualization capabilities, while Cytoscape offers advanced network analysis tools and integration with various data sources.

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

Pradeep T 16 Feb 2024

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

Clear

Harish Nagpal
Harish Nagpal 12 Apr, 2022

Informative article. Thanks for sharing.

Mebele O
Mebele O 12 Sep, 2022

Hi, Great article. Can any of these libraries build 3D network graphs, with nodes and edges, that rotates in 3D?