networkit-rs 0.1.0

Rust bindings for Networkit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# NetworKit Graph Generators"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook we will cover some algorithms to generate random graphs implemented in the `generators` module of NetworKit. Graph generators generate graphs that match certain user-defined parameters. It is particularly useful in cases when one does not have real graphs at hand (or none that matches specific properties). All algorithms in the `generators` module implement a `generate` function that must be called after the specific algorithm has been initialized which returns a [networkit.Graph](https://networkit.github.io/dev-docs/python_api/networkit.html?highlight=graph#networkit.Graph).\n",
    "\n",
    "As a first step we import NetworKit:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import networkit as nk"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Erdős-Rényi Generator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Erdős-Rényi generator creates a random graph in the G(n, p) model, i.e., a graph with `n` nodes connected randomly. Each edge is included in the graph with probability `p` independently from every other edge.\n",
    "\n",
    "Its constructor [ErdosRenyiGenerator(count nNodes, double prob, directed = False, selfLoops = False)](https://networkit.github.io/dev-docs/python_api/generators.html?highlight=erdos#networkit.generators.ErdosRenyiGenerator) expects the number of nodes there will be in the graph via the parameter `nNodes`, and the probability of existence for each edge `prob` as mandatory parameters. If `directed` is set to true, a directed graph will be generated. If `selfLoops` is true and the graph is directed, the graph may have self loops."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initalize algorithm\n",
    "erg = nk.generators.ErdosRenyiGenerator(200, 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run algorithm\n",
    "ergG = erg.generate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Verify\n",
    "print(ergG.numberOfNodes(), ergG.numberOfEdges())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rmat Generator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Rmat generator generates static R-MAT (Recursive MATrix) graphs by operating on the graph's adjacency matrix in a recursive manner. R-MAT graphs are random graphs with $n$ = $2^{scale}$ nodes and $n * edgeFactor$ edges. More details can be found in the original paper: `Deepayan Chakrabarti, Yiping Zhan, Christos Faloutsos: R-MAT: A Recursive Model for Graph Mining. SDM 2004: 442-446.`. \n",
    "\n",
    "The constructor, [RmatGenerator(scale, edgeFactor, a, b, c, d, weighted=False, reduceNodes=0)](https://networkit.github.io/dev-docs/python_api/generators.html?highlight=rmat#networkit.generators.RmatGenerator),  expects the number of nodes `n` via the `scale` parameter; $n$ = $2^{scale}$. `edgeFactor` specifies the number of edges `m` there should be in the graph and is computed using the following equation: $m$ = $n * edgeFactor$. The parameters `a, b, c, d` are probabilities that an edge should be in the upper left, upper right, lower left or lower right quadrant of the matrix respectively. The total sum of the four probabilities should be 1. Set `weighted` to true if the resulting graph should be weighted. The `reduceNodes` parameter dictates the number of random nodes to delete to achieve a given node count. By default it is set to 0.\n",
    "\n",
    "We can create a graph with 64 nodes and 192 edges as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initalize algorithm\n",
    "rmat = nk.generators.RmatGenerator(6, 3, 0.1, 0.2, 0.5, 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run algorithm\n",
    "rmatG = rmat.generate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Verify\n",
    "print(rmatG.numberOfNodes(), rmatG.numberOfEdges())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Barabási - Albert Generator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Barabási–Albert model is an algorithm for generating random scale-free networks using a preferential attachment mechanism. The network begins with an initial connected network of $n_0$ nodes, and new nodes are added to the network one at a time. This generator implements the preferential attachment model as introduced by [Barabási and Albert ](http://arxiv.org/pdf/cond-mat/9910332.pdf). The original algorithm is very slow and thus, the much faster method from [Batagelj and Brandes ](https://kops.uni-konstanz.de/bitstream/handle/123456789) is implemented and the current default in NetworKit.\n",
    "\n",
    "The constructor [BarabasiAlbertGenerator(k, nMax, n0=0, batagelj=True)](https://networkit.github.io/dev-docs/python_api/generators.html?highlight=barabasi#networkit.generators.BarabasiAlbertGenerator) expects the parameter `k`, the number of attachments per node, and `nMax`, the maximum number of nodes in the graph as mandatory paramaters. `n0` is the number of connected nodes to begin with. Set `batagelj`to false if you want to use the original preferential attachment model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initalize algorithm\n",
    "bag = nk.generators.BarabasiAlbertGenerator(3, 1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run algorithm\n",
    "bagG = bag.generate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Verify\n",
    "print(bagG.numberOfNodes(), bagG.numberOfEdges())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Hyperbolic Generator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Hyperbolic generator distributes points in hyperbolic space and adds edges between points with a probability depending on their distance. The resulting graphs have a power-law degree distribution, small diameter and high clustering coefficient. For a temperature of 0, the model resembles a unit-disk model in hyperbolic space.\n",
    "\n",
    "The constructor [HyperbolicGenerator(n=10000, k=6, gamma=3, T=0)](https://networkit.github.io/dev-docs/python_api/generators.html?highlight=hyperbolic#networkit.generators.HyperbolicGenerator) expects the number of nodes via the parameter `n`, the target average degree of each node which is specified by `k`, the target exponent of power-law distribution which is passed via the `gamma` parameter , and `T` which is the temperature."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initalize algorithm\n",
    "hg = nk.generators.HyperbolicGenerator(5000, 16, 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run algorithm\n",
    "hgG = hg.generate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Verify\n",
    "print(hgG.numberOfNodes(), hgG.numberOfEdges())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## LFR Generator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LFR benchmark is an algorithm that generates benchmark networks. The node degrees are distributed according to a power law with different exponents. \n",
    "\n",
    "The [LFR(n)](https://networkit.github.io/dev-docs/python_api/generators.html?highlight=lfr#networkit.generators.LFRGenerator) constructor only expects the number of nodes the generated graph should have. However, before generating the graph one needs to set a [degree sequence](https://networkit.github.io/dev-docs/python_api/generators.html?highlight=degreese#networkit.generators.LFRGenerator.setDegreeSequence), [community size sequence](https://networkit.github.io/dev-docs/python_api/generators.html?highlight=communitysi#networkit.generators.LFRGenerator.setCommunitySizeSequence) and [ mu](https://networkit.github.io/dev-docs/python_api/generators.html?highlight=setm#networkit.generators.LFRGenerator.setMu) or generate the sequences using the provided [generate-methods](https://networkit.github.io/dev-docs/python_api/generators.html?highlight=generatepower#networkit.generators.LFRGenerator.generatePowerlawCommunitySizeSequence). \n",
    "\n",
    "For this example, we generate the sequences a power-law degree sequence with average degree 20, maximum degree 50, and exponent of the node degree distribution -2. We also generate a power-law community size sequence  with minimum size 10, maximum size 50, and exponent of the community size distribution -2. Finally, we set the mixing parameter `mu` to 0.5."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initalize algorithm\n",
    "lfr = nk.generators.LFRGenerator(500)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate sequences\n",
    "lfr.generatePowerlawDegreeSequence(20, 50, -2)\n",
    "lfr.generatePowerlawCommunitySizeSequence(10, 50, -1)\n",
    "lfr.setMu(0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run algorithm\n",
    "lfrG = lfr.generate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Verify\n",
    "print(lfrG.numberOfNodes(), lfrG.numberOfEdges())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Clustered Random Graph Generator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The clustered random graph generates a clustered random graph. The number of nodes and the number of edges are adjustable as well as the probabilities for intra-cluster and inter-cluster edges.\n",
    "\n",
    "The constructor [ClusteredRandomGraphGenerator(n, k, pin, pout)](https://networkit.github.io/dev-docs/python_api/generators.html?highlight=clustered#networkit.generators.ClusteredRandomGraphGenerator) expects the number of nodes `n` and the number of clusters `k` followed by the intra-cluster edge probability and the inter-cluster edge probability as `pin` and `pout` respectively.\n",
    "\n",
    "\n",
    "A graph with 100 nodes grouped in 10 clusters with intra-cluster edge probability 0.5 and inter-cluster edge probability 0.01 can be generated as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize algorithm\n",
    "crg = nk.generators.ClusteredRandomGraphGenerator(100, 10, 0.5, 0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run algorithm\n",
    "crgG = crg.generate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Verify\n",
    "print(crgG.numberOfNodes(), crgG.numberOfEdges())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dorogovtsev-Mendes Generator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This generator creates graphs using the Dorogovtsev-Mendes algorithm. It starts by creating three nodes and tree edges, and then adding one node at a time. Each time a node is added, an edge is chosen randomly and the node is connected via two new edges to the two ends of the chosen edge.\n",
    "\n",
    "The number of nodes the generated graph should have is passed to the constructor, [DorogovtsevMendesGenerator(nNodes)](https://networkit.github.io/dev-docs/python_api/generators.html?highlight=dorog#networkit.generators.DorogovtsevMendesGenerator), via the `nNodes` parameter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initalize algorithm\n",
    "dmg = nk.generators.DorogovtsevMendesGenerator(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run algorithm\n",
    "dmgG = dmg.generate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Verify\n",
    "print(dmgG.numberOfNodes(), dmgG.numberOfEdges())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chung-Lu Generator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Given an arbitrary degree sequence, the Chung-Lu generative model will produce a random graph with the same expected degree sequence if possible.\n",
    "\n",
    "The constructor [ChungLuGenerator(degreeSequence)](https://networkit.github.io/dev-docs/python_api/generators.html?highlight=chung#networkit.generators.ChungLuGenerator) expects a degree sequence as a parameter.\n",
    "\n",
    "In order to create a graph with 5 nodes, we first need to generate a degree sequence that will be passed to the constructor. In order to create a graph with 5 nodes, we first need to generate a degree sequence that will be passed to the constructor. Note that the degree sequence is not required to be sorted."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate degree sequence\n",
    "degSeq = [4, 3, 2, 1, 1, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initalize algorithm\n",
    "clg = nk.generators.ChungLuGenerator(degSeq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run algorithm\n",
    "clgG = clg.generate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Verify\n",
    "chungLuSeq = []\n",
    "for u in range (clgG.upperNodeIdBound()):\n",
    "    chungLuSeq.append(clgG.degree(u))\n",
    "print(chungLuSeq)\n",
    "\n",
    "print(clgG.numberOfNodes(), clgG.numberOfEdges())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Havel-Hakimi Generator "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Havel-Hakimi algorithm for generating a graph according to a given degree sequence $(d_1, d_2,...,d_n)$. The degree sequence must be non-increasing, i.e., $d_1$ must be the highest degree. \n",
    "\n",
    "\"The contructor [HavelHakimiGenerator(sequence, ignoreIfRealizable=True)](https://networkit.github.io/dev-docs/python_api/generators.html?highlight=havel#networkit.generators.HavelHakimiGenerator) expects the degree sequence as a mandatory parameter. If `ignoreIfRealizable` is true, the graph is generated even if the degree sequence is not realizable. Some nodes may then get lower degrees than requested in the sequence. If `ignoreIfRealizable` is false and the sequence is not realizable, an exception is thrown and the graph cannot be generated.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate degree sequence\n",
    "sequence = []\n",
    "for i in range (20):\n",
    "    sequence.append(20-i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initalize algorithm\n",
    "hhg = nk.generators.HavelHakimiGenerator(sequence, ignoreIfRealizable=False)\n",
    "\n",
    "# Check if sequence is realiziable\n",
    "print(\"Sequence is realiziable: \", hhg.isRealizable())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As the generated sequence is not realizable, a graph cannot be generated. We can either set `ignoreIfRealizable` to true, or try with another sequence.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate degree sequence\n",
    "sequence = [1, 2, 1, 2, 2]\n",
    "\n",
    "# Initalize algorithm\n",
    "hhg = nk.generators.HavelHakimiGenerator(sequence, ignoreIfRealizable=False)\n",
    "\n",
    "# Check if sequence is realiziable\n",
    "print(\"Sequence is realiziable \", hhg.isRealizable())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run algorithm\n",
    "hhgG = hhg.generate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Verify\n",
    "print(hhgG.numberOfNodes(), hhgG.numberOfEdges())\n",
    "for u in range (hhgG.upperNodeIdBound()):\n",
    "    assert(sequence[u] == hhgG.degree(u))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mocnik Generator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Mocnik graph generator creates random spatial graphs according to the Mocnik model. \n",
    "\n",
    "The constructor [MocnikGenerator(dim, n, k, weighted)](https://networkit.github.io/dev-docs/python_api/generators.html?highlight=mocnik#networkit.generators.MocnikGenerator) expects the parameters `dim` which dictates the dimension of the space, the number of nodes`n` and the density parameter `k`. The density parameter determines the ratio of edges to nodes. Set `weighted` to true if the generated graph should be weighted."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initalize algorithm\n",
    "mg = nk.generators.MocnikGenerator(3, 10000, 2.6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run algorithm\n",
    "mgG = mg.generate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Verify \n",
    "print(mgG.numberOfNodes(), mgG.numberOfEdges())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}