0
# Python igraph
1
2
A comprehensive Python library for creating, manipulating, and analyzing graphs and networks. igraph provides powerful tools for graph theory, network analysis, community detection, and visualization with over 264 public API symbols covering everything from basic graph operations to advanced algorithms.
3
4
## Package Information
5
6
- **Package Name**: igraph
7
- **Language**: Python
8
- **Installation**: `pip install igraph`
9
- **Dependencies**: Optional Cairo (pycairo/cairocffi), matplotlib, plotly for visualization
10
11
## Core Imports
12
13
```python
14
import igraph
15
```
16
17
Common patterns for specific functionality:
18
19
```python
20
from igraph import Graph
21
import igraph as ig
22
```
23
24
For visualization:
25
26
```python
27
from igraph import plot
28
from igraph.drawing import Palette
29
```
30
31
## Basic Usage
32
33
```python
34
import igraph as ig
35
36
# Create a simple graph
37
g = ig.Graph(edges=[(0, 1), (1, 2), (2, 0), (2, 3)], directed=False)
38
39
# Add vertex and edge attributes
40
g.vs["name"] = ["Alice", "Bob", "Carol", "Dave"]
41
g.es["weight"] = [1, 2, 3, 1]
42
43
# Basic graph properties
44
print(f"Vertices: {g.vcount()}") # 4
45
print(f"Edges: {g.ecount()}") # 4
46
print(f"Directed: {g.is_directed()}") # False
47
48
# Graph analysis
49
betweenness = g.betweenness()
50
clustering = g.community_leiden()
51
layout = g.layout_fruchterman_reingold()
52
53
# Visualization (requires Cairo or matplotlib)
54
ig.plot(g, vertex_label=g.vs["name"],
55
vertex_color=clustering.membership,
56
layout=layout)
57
```
58
59
## Architecture
60
61
The igraph library follows a layered architecture designed for flexibility and performance:
62
63
- **Core Graph Classes**: Graph, Vertex, Edge form the foundation for all graph operations
64
- **Sequence Classes**: VertexSeq, EdgeSeq provide efficient access to collections of vertices/edges
65
- **Analysis Layer**: Comprehensive algorithms for centrality, community detection, pathfinding
66
- **Data Structures**: Matrix, Layout, clustering objects for specialized operations
67
- **Visualization Layer**: Multiple backends (Cairo, matplotlib, plotly) for graph drawing
68
- **I/O Layer**: Support for numerous graph file formats and data interchange
69
70
This design enables igraph to serve as both a high-level tool for network analysis and a foundation for specialized graph applications, with seamless integration into the Python scientific ecosystem.
71
72
## Capabilities
73
74
### Graph Creation and Generation
75
76
Methods for creating graphs from various sources including edge lists, adjacency matrices, classic graph families, and random graph models.
77
78
```python { .api }
79
class Graph:
80
def __init__(n=0, edges=None, directed=False, **attrs): ...
81
@classmethod
82
def Full(cls, n, directed=False, loops=False): ...
83
@classmethod
84
def Ring(cls, n, directed=False, mutual=False, circular=True): ...
85
@classmethod
86
def Tree(cls, n, children=2, mode=TREE_UNDIRECTED): ...
87
@classmethod
88
def Erdos_Renyi(cls, n, m=None, p=None, directed=False, loops=False): ...
89
@classmethod
90
def Barabasi(cls, n, m=1, directed=False): ...
91
```
92
93
[Graph Creation](./graph-creation.md)
94
95
### Graph Structure Manipulation
96
97
Core operations for adding, removing, and modifying vertices and edges, plus basic graph properties and transformations.
98
99
```python { .api }
100
class Graph:
101
def add_vertex(self, name=None, **attrs): ...
102
def add_vertices(self, n, attrs=None): ...
103
def add_edge(self, source, target, **attrs): ...
104
def add_edges(self, edges, attrs=None): ...
105
def delete_vertices(self, vertices): ...
106
def delete_edges(self, edges): ...
107
def vcount(self): ...
108
def ecount(self): ...
109
def is_directed(self): ...
110
```
111
112
[Graph Structure](./graph-structure.md)
113
114
### Graph Analysis
115
116
Comprehensive analysis algorithms including centrality measures, shortest paths, connectivity analysis, and structural properties.
117
118
```python { .api }
119
class Graph:
120
def betweenness(self, vertices=None, directed=True, weights=None): ...
121
def closeness(self, vertices=None, mode=ALL, weights=None): ...
122
def pagerank(self, vertices=None, directed=True, weights=None): ...
123
def shortest_paths(self, source=None, target=None, weights=None): ...
124
def diameter(self, directed=True, weights=None): ...
125
def is_connected(self, mode=WEAK): ...
126
```
127
128
[Analysis](./analysis.md)
129
130
### Community Detection
131
132
Advanced community detection algorithms for identifying groups and clusters within networks.
133
134
```python { .api }
135
class Graph:
136
def community_leiden(self, weights=None, resolution=1.0, **kwargs): ...
137
def community_multilevel(self, weights=None, resolution=1.0): ...
138
def community_infomap(self, edge_weights=None, vertex_weights=None): ...
139
def community_walktrap(self, weights=None, steps=4): ...
140
def community_edge_betweenness(self, weights=None, directed=True): ...
141
def community_fastgreedy(self, weights=None): ...
142
def community_label_propagation(self, weights=None, initial=None, fixed=None): ...
143
def community_leading_eigenvector(self, clusters=None, weights=None): ...
144
def community_spinglass(self, weights=None, spins=25, **kwargs): ...
145
def community_optimal_modularity(self): ...
146
```
147
148
[Community Detection](./community.md)
149
150
### Layout Algorithms
151
152
Graph layout and positioning algorithms for visualization and spatial analysis.
153
154
```python { .api }
155
class Graph:
156
def layout_fruchterman_reingold(self, weights=None, dim=2, **kwargs): ...
157
def layout_kamada_kawai(self, weights=None, dim=2, **kwargs): ...
158
def layout_drl(self, weights=None, dim=2, **kwargs): ...
159
def layout_circle(self, order=None): ...
160
def layout_grid(self, width=0, dim=2): ...
161
def layout_sugiyama(self, layers=None, weights=None, **kwargs): ...
162
def layout_fruchterman_reingold_3d(self, **kwargs): ...
163
def layout_kamada_kawai_3d(self, **kwargs): ...
164
def layout_random_3d(self, **kwargs): ...
165
def layout_grid_3d(self, **kwargs): ...
166
def layout_sphere(self): ...
167
```
168
169
[Layout](./layout.md)
170
171
### Visualization and Drawing
172
173
Multi-backend visualization system supporting Cairo, matplotlib, and plotly for creating publication-quality graph visualizations.
174
175
```python { .api }
176
def plot(obj, target=None, bbox=(0,0,600,600), **kwds): ...
177
178
class Plot:
179
def __init__(self, bbox=(0,0,600,600), palette=None): ...
180
def save(self, fname=None): ...
181
182
class Palette:
183
def get(self, index): ...
184
```
185
186
[Visualization](./visualization.md)
187
188
### File I/O and Formats
189
190
Comprehensive support for reading and writing graphs in various formats including GraphML, GML, Pajek, DIMACS, and more.
191
192
```python { .api }
193
class Graph:
194
@classmethod
195
def Read(cls, filename, format=None, **kwargs): ...
196
def write(self, filename, format=None, **kwargs): ...
197
def to_networkx(self): ...
198
@classmethod
199
def from_networkx(cls, graph): ...
200
```
201
202
[I/O and Formats](./io-formats.md)
203
204
### Clustering Objects
205
206
Specialized classes for representing and manipulating clustering results from community detection algorithms.
207
208
```python { .api }
209
class Clustering:
210
def size(self, idx): ...
211
def sizes(self): ...
212
def compare_to(self, other, method="vi"): ...
213
214
class VertexClustering(Clustering):
215
@classmethod
216
def FromAttribute(cls, graph, attribute): ...
217
def modularity(self): ...
218
def crossing(self): ...
219
```
220
221
[Clustering](./clustering.md)
222
223
### Vertex and Edge Sequences
224
225
Efficient sequence classes for accessing and manipulating collections of vertices and edges with filtering and attribute operations.
226
227
```python { .api }
228
class VertexSeq:
229
def select(self, *args, **kwargs): ...
230
def find(self, *args, **kwargs): ...
231
def attributes(self): ...
232
233
class EdgeSeq:
234
def select(self, *args, **kwargs): ...
235
def find(self, *args, **kwargs): ...
236
def attributes(self): ...
237
```
238
239
[Sequences](./sequences.md)
240
241
### Core Data Types
242
243
Essential data structures including matrices, layouts, and utility classes that support graph operations and analysis.
244
245
```python { .api }
246
class Matrix:
247
@classmethod
248
def Fill(cls, value, shape): ...
249
@classmethod
250
def Zero(cls, shape): ...
251
@classmethod
252
def Identity(cls, shape): ...
253
254
class Layout:
255
def mirror(self, dim): ...
256
def rotate(self, angle, dim1=0, dim2=1): ...
257
def scale(self, *args): ...
258
```
259
260
[Data Types](./data-types.md)