igraph

1.0.0 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

This quickstart demonstrates how to create a directed graph, add vertices with attributes, add weighted edges, retrieve basic graph properties, and find the shortest path between two vertices. Plotting functionality is also mentioned, although commented out as it requires additional dependencies.

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'])

view raw JSON →