Run in Google Colab
|
View on GitHub
|
igraph basics (python library)¶
iGraph is a library for creating and manipulating graphs and networks. It is widely used in network analysis, social network analysis, and graph theory.
Installation¶
To install the igraph library in Python, you can use pip:
pip install python-igraph
You can also check the igraph tutorial at https://igraph.org/python/doc/tutorial/tutorial.html
Let's start with some basic examples of how to use igraph in Python. But first let's import it.
import igraph as ig
Let's generate a random graph using the Erdos-Renyi model, which is a simple model for generating random graphs. In this model, we specify the number of vertices and the probability of an edge between any two vertices.
# Generating a random network
g_random = ig.Graph.Erdos_Renyi(n=15, p=0.2, directed=False, loops=False)
Cool, now we have a random graph with 15 vertices/nodes and a probability of connection of 0.3 between any two vertices. Let's see what igraph prints.
print(g_random)
IGRAPH U--- 15 20 -- + edges: 0 -- 2 4 11 13 14 5 -- 7 10 -- 1 -- 2 6 7 6 -- 1 2 8 11 11 -- 0 6 2 -- 0 1 6 9 7 -- 1 5 12 14 12 -- 4 7 13 3 -- 8 -- 4 6 13 -- 0 9 12 4 -- 0 8 9 12 9 -- 2 4 13 14 -- 0 7
Ok it is difficult to understand. But internally igraph uses a adjacency list to represent the graph. Let's print the adjacency list of the graph. The first two numbers are the number of vertices and edges, followed by the adjacency list.
For instance an entry 0 -- 2 4 11 13 14 means that vertex 0 is connected to vertices 2, 4, 11, 13, and 14.
We can access the nodes and edges of the graph using the vs and es attributes, respectively. A node may have attributes associated with it. One of the attributes that is built-in is index, which is the index of the node in the graph. We can access it like this:
# Nodes
[node.index for node in g_random.vs]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
We can access the edges of the graph using the es attribute. Each edge has an source and target vertex, which can be accessed using the source and target attributes.
# Edges list
[(edge.source, edge.target) for edge in g_random.es]
[(0, 2), (1, 2), (0, 4), (1, 6), (2, 6), (1, 7), (5, 7), (4, 8), (6, 8), (2, 9), (4, 9), (0, 11), (6, 11), (4, 12), (7, 12), (0, 13), (9, 13), (12, 13), (0, 14), (7, 14)]
or simply:
g_random.get_edgelist()
[(0, 2), (1, 2), (0, 4), (1, 6), (2, 6), (1, 7), (5, 7), (4, 8), (6, 8), (2, 9), (4, 9), (0, 11), (6, 11), (4, 12), (7, 12), (0, 13), (9, 13), (12, 13), (0, 14), (7, 14)]
There are several methods to calculate properties of the graph, such as the degree of each vertex. The degree of a vertex is the number of edges connected to it. We can calculate the degree of each vertex using the degree method:
# Calculate degree of nodes
g_random.degree()
[5, 3, 4, 0, 4, 1, 4, 4, 2, 3, 0, 2, 3, 3, 2]
You can also set a node attribute using the vs attribute. For example, let's store the double of the degree of each vertex as an attribute called degree doubled:
# Setting node attributes
g_random.vs["degree doubled"] = [2*d for d in g_random.degree()]
print(g_random)
IGRAPH U--- 15 20 -- + attr: degree doubled (v) + edges: 0 -- 2 4 11 13 14 5 -- 7 10 -- 1 -- 2 6 7 6 -- 1 2 8 11 11 -- 0 6 2 -- 0 1 6 9 7 -- 1 5 12 14 12 -- 4 7 13 3 -- 8 -- 4 6 13 -- 0 9 12 4 -- 0 8 9 12 9 -- 2 4 13 14 -- 0 7
We can get the neighbors of a vertex using the neighbors method. For example, let's get the neighbors of vertex 0:
# Getting neighbors of a node
g_random.neighbors(0)
[2, 4, 11, 13, 14]
igraph also works well with numpy arrays.
import numpy as np
g_random.vs["random number"] = np.random.random(g_random.vcount())
Attributes can also be set for edges. For example, let's set the weight of each edge to a random value between 0 and 10:
# Setting an random weight attribute to edges
g_random.es["weight"] = 10*np.random.random(g_random.ecount())
print(g_random)
IGRAPH U-W- 15 20 -- + attr: degree doubled (v), random number (v), weight (e) + edges: 0 -- 2 4 11 13 14 5 -- 7 10 -- 1 -- 2 6 7 6 -- 1 2 8 11 11 -- 0 6 2 -- 0 1 6 9 7 -- 1 5 12 14 12 -- 4 7 13 3 -- 8 -- 4 6 13 -- 0 9 12 4 -- 0 8 9 12 9 -- 2 4 13 14 -- 0 7
One of the most used file formats for graphs is gml (Graph Modelling Language). We can save the graph to a gml file using the write_gml method:
# Saving as gml
g_random.write_gml("random_network.gml")
And you can load it back using the Graph.Read_GML method:
# Loading as gml
g_random_loaded = ig.Graph.Read_GML("random_network.gml")
print(g_random_loaded)
If you plan to use Helios-Web, you can use the xnetwork package to convert igraph graphs to xnetwork file format. Then you can go to heliosweb.io, select a network configuration and drag and drop the .xnet file there to visualize the graph.
# Saving xnet (useful for the Helios-Web application)
import xnetwork as xn
xn.save(g_random, "random_network.xnet")
You can load the xnetwork file using xn.load:
g_random_from_xnet = xn.load("random_network.xnet")
print(g_random_from_xnet)
Exercises¶
Create a larger random network and plot the degree distribution.
Hint: ig.Graph.Erdos_Renyi
import matplotlib.pyplot as plt
Create a Barabasi-Albert network of similar size and degree, and plot the degree distrib. check https://igraph.readthedocs.io/ .
Hint: ig.Graph.Barabasi
Generate a plot of avg. clustering coefficient and shortest path length along p in [0,1] for a 1D Watts-Strogatz network. Use at least nei=2.
hint: ig.Graph.Watts_Strogatz
Choose a network or model and create a plot of degree (first level) and second level degree (number of connections from first neighborhood to the second) (no need to optimize)
Run in Google Colab
View on GitHub