igraph
igraph is a high-performance Python library that provides data structures and algorithms for creating, manipulating, and analyzing graphs (networks). It is a Python interface to the powerful C library `igraph`. The library focuses on efficiency for large graphs and offers extensive functionality, including graph generation, layout algorithms, community detection, and various graph measures. As of version 1.0.0, the project aims for a stable API after a series of breaking changes in earlier 0.x releases, and it maintains a consistent release cadence with frequent bugfix updates.
Warnings
- breaking The `python-igraph` PyPI package was renamed to `igraph` in version 0.9.8. The `python-igraph` package became a stub that depended on `igraph` and stopped receiving updates after September 1, 2022. Projects should update their dependencies to `igraph` directly.
- breaking Versions 0.10.x of `python-igraph` (and its underlying C core) introduced numerous API-breaking changes and function renamings in preparation for the 1.0 stable release. Code written for 0.9.x will likely not work directly with 0.10.x without modifications.
- breaking The 1.0.0 release of `igraph` continues with API refinements, building on the breaking changes introduced in 0.10.x to achieve a more consistent and predictable API. This includes updates to the C core to version 1.0.0, which may necessitate further code adjustments for applications migrating from 0.10.x.
- gotcha Using `from igraph import *` is discouraged. While functional, it pollutes the global namespace and can lead to name collisions with other modules or variables, reducing code clarity and maintainability.
Install
-
pip install igraph
Imports
- Graph
import igraph as ig g = ig.Graph()
Quickstart
import igraph as ig
# Create a directed graph with 5 vertices
g = ig.Graph(n=5, directed=True)
# Add names to vertices
g.vs['name'] = ['Alice', 'Bob', 'Charlie', 'David', 'Eve']
# Add edges with attributes
g.add_edges([(0, 1), (0, 2), (1, 3), (2, 3), (3, 4)])
g.es['weight'] = [1.0, 0.5, 2.0, 1.0, 1.5]
# Get basic graph information
print(f"Number of vertices: {g.vcount()}")
print(f"Number of edges: {g.ecount()}")
print(f"Is directed: {g.is_directed()}")
# Find shortest path from 'Alice' to 'Eve'
path = g.get_shortest_paths('Alice', to='Eve', weights='weight', output='vpath')
print(f"Shortest path from Alice to Eve (vertex IDs): {path}")
# Example of plotting (requires Cairo or Matplotlib/Plotly backends)
# from igraph import plot
# plot(g, layout=g.layout_auto(), target='graph.png', vertex_label=g.vs['name'])