0
# Graph Creation
1
2
Methods for creating graphs from various sources including edge lists, adjacency matrices, classic graph families, and random graph models. igraph provides extensive capabilities for generating both synthetic graphs for testing and real-world graph structures.
3
4
## Capabilities
5
6
### Basic Graph Construction
7
8
Create graphs from fundamental data structures like edge lists, adjacency matrices, and vertex/edge specifications.
9
10
```python { .api }
11
class Graph:
12
def __init__(self, n=0, edges=None, directed=False, graph_attrs=None, vertex_attrs=None, edge_attrs=None):
13
"""
14
Create a graph from basic components.
15
16
Parameters:
17
- n: int, number of vertices (default 0)
18
- edges: list of tuples, edge list [(source, target), ...]
19
- directed: bool, whether graph is directed (default False)
20
- graph_attrs: dict, graph-level attributes
21
- vertex_attrs: dict, vertex attribute dictionaries
22
- edge_attrs: dict, edge attribute dictionaries
23
24
Returns:
25
Graph object
26
"""
27
28
@classmethod
29
def Adjacency(cls, matrix, mode=ADJ_DIRECTED, weighted=None, loops=True):
30
"""
31
Create graph from adjacency matrix.
32
33
Parameters:
34
- matrix: 2D array-like, adjacency matrix
35
- mode: adjacency mode (ADJ_DIRECTED, ADJ_UNDIRECTED, ADJ_MAX, etc.)
36
- weighted: bool/str, whether edges are weighted or weight attribute name
37
- loops: bool, whether self-loops are allowed
38
39
Returns:
40
Graph object
41
"""
42
43
@classmethod
44
def Incidence(cls, matrix, directed=False, mode=OUT, multiple=False, weighted=None):
45
"""
46
Create graph from incidence matrix.
47
48
Parameters:
49
- matrix: 2D array-like, incidence matrix (vertices x edges)
50
- directed: bool, whether graph is directed
51
- mode: edge direction mode for directed graphs (IN, OUT, ALL)
52
- multiple: bool, whether multiple edges are allowed
53
- weighted: bool/str, edge weights
54
55
Returns:
56
Graph object
57
"""
58
```
59
60
**Usage Examples:**
61
62
```python
63
import igraph as ig
64
import numpy as np
65
66
# From edge list
67
edges = [(0, 1), (1, 2), (2, 0), (2, 3)]
68
g1 = ig.Graph(edges=edges, directed=False)
69
70
# With attributes
71
g2 = ig.Graph(
72
n=4,
73
edges=edges,
74
vertex_attrs={"name": ["A", "B", "C", "D"]},
75
edge_attrs={"weight": [1, 2, 3, 1]}
76
)
77
78
# From adjacency matrix
79
adj_matrix = [[0, 1, 1, 0],
80
[1, 0, 1, 1],
81
[1, 1, 0, 1],
82
[0, 1, 1, 0]]
83
g3 = ig.Graph.Adjacency(adj_matrix, mode=ig.ADJ_UNDIRECTED)
84
85
# Weighted from numpy array
86
weights = np.random.rand(4, 4)
87
g4 = ig.Graph.Adjacency(weights, weighted=True)
88
```
89
90
### Classic Graph Families
91
92
Generate well-known graph structures including complete graphs, cycles, trees, and lattices.
93
94
```python { .api }
95
class Graph:
96
@classmethod
97
def Full(cls, n, directed=False, loops=False):
98
"""
99
Create complete graph.
100
101
Parameters:
102
- n: int, number of vertices
103
- directed: bool, whether graph is directed
104
- loops: bool, whether to include self-loops
105
106
Returns:
107
Complete graph with n vertices
108
"""
109
110
@classmethod
111
def Ring(cls, n, directed=False, mutual=False, circular=True):
112
"""
113
Create ring/cycle graph.
114
115
Parameters:
116
- n: int, number of vertices
117
- directed: bool, whether graph is directed
118
- mutual: bool, add reverse edges in directed case
119
- circular: bool, connect last vertex to first
120
121
Returns:
122
Ring graph
123
"""
124
125
@classmethod
126
def Tree(cls, n, children=2, mode=TREE_UNDIRECTED):
127
"""
128
Create regular tree.
129
130
Parameters:
131
- n: int, number of vertices
132
- children: int, children per internal node
133
- mode: tree direction (TREE_UNDIRECTED, TREE_IN, TREE_OUT)
134
135
Returns:
136
Tree graph
137
"""
138
139
@classmethod
140
def Lattice(cls, dim, nei=1, directed=False, mutual=False, circular=False):
141
"""
142
Create lattice graph.
143
144
Parameters:
145
- dim: list of ints, dimensions of lattice
146
- nei: int, neighborhood distance
147
- directed: bool, whether graph is directed
148
- mutual: bool, add reverse edges
149
- circular: bool, periodic boundary conditions
150
151
Returns:
152
Lattice graph
153
"""
154
155
@classmethod
156
def Star(cls, n, mode=STAR_UNDIRECTED, center=0):
157
"""
158
Create star graph.
159
160
Parameters:
161
- n: int, number of vertices
162
- mode: star type (STAR_UNDIRECTED, STAR_IN, STAR_OUT, STAR_MUTUAL)
163
- center: int, index of center vertex
164
165
Returns:
166
Star graph with center vertex
167
"""
168
```
169
170
**Usage Examples:**
171
172
```python
173
# Complete graphs
174
complete5 = ig.Graph.Full(5) # K5 complete graph
175
directed_complete = ig.Graph.Full(4, directed=True)
176
177
# Cycles and paths
178
cycle10 = ig.Graph.Ring(10) # 10-vertex cycle
179
path10 = ig.Graph.Ring(10, circular=False) # 10-vertex path
180
181
# Trees
182
binary_tree = ig.Graph.Tree(15, children=2) # Binary tree
183
ternary_tree = ig.Graph.Tree(40, children=3) # Ternary tree
184
185
# Lattices
186
grid_3x3 = ig.Graph.Lattice([3, 3], circular=False) # 3x3 grid
187
torus_4x4 = ig.Graph.Lattice([4, 4], circular=True) # 4x4 torus
188
189
# Star graphs
190
star6 = ig.Graph.Star(7) # 6 leaves + 1 center
191
```
192
193
### Random Graph Models
194
195
Generate random graphs using various probabilistic models for network simulation and testing.
196
197
```python { .api }
198
class Graph:
199
@classmethod
200
def Erdos_Renyi(cls, n, m=None, p=None, directed=False, loops=False):
201
"""
202
Create Erdős-Rényi random graph.
203
204
Parameters:
205
- n: int, number of vertices
206
- m: int, number of edges (use m OR p, not both)
207
- p: float, edge probability (use m OR p, not both)
208
- directed: bool, whether graph is directed
209
- loops: bool, allow self-loops
210
211
Returns:
212
Random graph
213
"""
214
215
@classmethod
216
def Barabasi(cls, n, m=1, power=1, zero_appeal=1, directed=False):
217
"""
218
Create Barabási-Albert preferential attachment graph.
219
220
Parameters:
221
- n: int, number of vertices
222
- m: int/list, edges per step or per-vertex out-degrees
223
- power: float, power of preferential attachment
224
- zero_appeal: float, attractiveness of vertices with degree 0
225
- directed: bool, whether graph is directed
226
227
Returns:
228
Scale-free graph
229
"""
230
231
@classmethod
232
def Watts_Strogatz(cls, dim, size, nei, p):
233
"""
234
Create Watts-Strogatz small-world graph.
235
236
Parameters:
237
- dim: int, dimension of lattice
238
- size: int, size in each dimension
239
- nei: int, neighborhood size
240
- p: float, rewiring probability
241
242
Returns:
243
Small-world graph
244
"""
245
246
@classmethod
247
def Random_Geometric(cls, n, radius, torus=False):
248
"""
249
Create random geometric graph.
250
251
Parameters:
252
- n: int, number of vertices
253
- radius: float, connection radius
254
- torus: bool, use torus topology
255
256
Returns:
257
Geometric graph with embedded coordinates
258
"""
259
```
260
261
**Usage Examples:**
262
263
```python
264
# Erdős-Rényi graphs
265
er_fixed_edges = ig.Graph.Erdos_Renyi(100, m=200) # 100 vertices, 200 edges
266
er_prob = ig.Graph.Erdos_Renyi(100, p=0.05) # 100 vertices, 5% edge probability
267
268
# Scale-free graphs
269
scale_free = ig.Graph.Barabasi(1000, m=3) # 1000 vertices, 3 edges per step
270
directed_sf = ig.Graph.Barabasi(500, m=2, directed=True)
271
272
# Small-world graphs
273
small_world = ig.Graph.Watts_Strogatz(1, 100, 4, 0.1) # Ring lattice rewiring
274
275
# Geometric graphs
276
geometric = ig.Graph.Random_Geometric(200, 0.1) # 200 points, radius 0.1
277
```
278
279
### Bipartite Graphs
280
281
Create and work with bipartite (two-mode) graphs where vertices are divided into two disjoint sets.
282
283
```python { .api }
284
class Graph:
285
@classmethod
286
def Bipartite(cls, types, edges, directed=False):
287
"""
288
Create bipartite graph from vertex types and edges.
289
290
Parameters:
291
- types: list of bool, vertex types (True/False for two sets)
292
- edges: list of tuples, edge list
293
- directed: bool, whether graph is directed
294
295
Returns:
296
Bipartite graph with type attribute
297
"""
298
299
@classmethod
300
def Random_Bipartite(cls, n1, n2, p=None, m=None, directed=False):
301
"""
302
Create random bipartite graph.
303
304
Parameters:
305
- n1, n2: int, sizes of vertex sets
306
- p: float, edge probability between sets
307
- m: int, number of edges (use p OR m)
308
- directed: bool, whether graph is directed
309
310
Returns:
311
Random bipartite graph
312
"""
313
314
def is_bipartite(self):
315
"""
316
Check if graph is bipartite.
317
318
Returns:
319
bool, True if graph is bipartite
320
"""
321
322
def bipartite_projection(self, types=None, multiplicity=True, probe1=-1):
323
"""
324
Create one-mode projections of bipartite graph.
325
326
Parameters:
327
- types: list, vertex types (if not stored as attribute)
328
- multiplicity: bool, whether to store edge multiplicities
329
- probe1: int, which projection to return (-1 for both)
330
331
Returns:
332
Projected graph(s)
333
"""
334
```
335
336
**Usage Examples:**
337
338
```python
339
# Create bipartite graph
340
types = [False, False, False, True, True, True] # 3+3 partition
341
edges = [(0, 3), (0, 4), (1, 3), (1, 5), (2, 4), (2, 5)]
342
bip = ig.Graph.Bipartite(types, edges)
343
344
# Random bipartite
345
random_bip = ig.Graph.Random_Bipartite(10, 15, p=0.3) # 10+15 vertices
346
347
# Check and project
348
is_bip = bip.is_bipartite()
349
proj1, proj2 = bip.bipartite_projection() # Both projections
350
```
351
352
### Named Graphs
353
354
Generate specific named graphs and graph families commonly used in graph theory and network analysis.
355
356
```python { .api }
357
class Graph:
358
@classmethod
359
def Famous(cls, name):
360
"""
361
Create famous named graphs.
362
363
Parameters:
364
- name: str, name of famous graph
365
("Petersen", "Karate", "Zachary", "Tutte", etc.)
366
367
Returns:
368
Named graph
369
"""
370
371
@classmethod
372
def Atlas(cls, n):
373
"""
374
Create graph from Graph Atlas.
375
376
Parameters:
377
- n: int, index in atlas (0-1252)
378
379
Returns:
380
Graph from atlas
381
"""
382
383
@classmethod
384
def Kautz(cls, m, n):
385
"""
386
Create Kautz graph.
387
388
Parameters:
389
- m: int, base (alphabet size minus 1)
390
- n: int, dimension
391
392
Returns:
393
Kautz graph
394
"""
395
396
@classmethod
397
def de_Bruijn(cls, m, n):
398
"""
399
Create de Bruijn graph.
400
401
Parameters:
402
- m: int, alphabet size
403
- n: int, string length
404
405
Returns:
406
de Bruijn graph
407
"""
408
```
409
410
**Usage Examples:**
411
412
```python
413
# Famous graphs
414
petersen = ig.Graph.Famous("Petersen")
415
karate = ig.Graph.Famous("Zachary") # Karate club network
416
tutte = ig.Graph.Famous("Tutte")
417
418
# Graph atlas
419
small_graph = ig.Graph.Atlas(123) # Graph #123 from atlas
420
421
# Kautz and de Bruijn
422
kautz = ig.Graph.Kautz(3, 4) # Kautz graph K(3,4)
423
debruijn = ig.Graph.de_Bruijn(2, 5) # Binary strings of length 5
424
```