You are on page 1of 3

Graph Network

Introduction
➔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.
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.
#to install cairocffi for visualization
pip3 install cairocffi
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)

You might also like