|
496 | 496 | "plot_hist(bc, \"betweenness centrality\")" |
497 | 497 | ] |
498 | 498 | }, |
| 499 | + { |
| 500 | + "cell_type": "markdown", |
| 501 | + "metadata": {}, |
| 502 | + "source": [ |
| 503 | + "## Backend-only Functions\n", |
| 504 | + "Besides improved performance, another benefit backends provide is the ability to add functionality to NetworkX that is not present in the default implementation.\n", |
| 505 | + "\n", |
| 506 | + "NetworkX 3.5 adds the `leiden_communities` function, but still does not provide an implementation. This allows backends to implement Leiden community detection using a common function signature, so when other backends or even NetworkX provide an implemtation, users can use those without requiring code changes.\n", |
| 507 | + "\n", |
| 508 | + "### Leiden community detection\n", |
| 509 | + "Let's take a look at the communities in the Pokec social network dataset using `leiden_communities`" |
| 510 | + ] |
| 511 | + }, |
| 512 | + { |
| 513 | + "cell_type": "code", |
| 514 | + "execution_count": null, |
| 515 | + "metadata": {}, |
| 516 | + "outputs": [], |
| 517 | + "source": [ |
| 518 | + "from networkx.algorithms.community import leiden_communities" |
| 519 | + ] |
| 520 | + }, |
| 521 | + { |
| 522 | + "cell_type": "code", |
| 523 | + "execution_count": null, |
| 524 | + "metadata": {}, |
| 525 | + "outputs": [], |
| 526 | + "source": [ |
| 527 | + "%%time\n", |
| 528 | + "leiden_res = leiden_communities(G, seed=42, backend=\"cugraph\")" |
| 529 | + ] |
| 530 | + }, |
| 531 | + { |
| 532 | + "cell_type": "markdown", |
| 533 | + "metadata": {}, |
| 534 | + "source": [ |
| 535 | + "`leiden_communities` returns a list of sets, where each set contains the node IDs making up a community in the graph." |
| 536 | + ] |
| 537 | + }, |
| 538 | + { |
| 539 | + "cell_type": "code", |
| 540 | + "execution_count": null, |
| 541 | + "metadata": {}, |
| 542 | + "outputs": [], |
| 543 | + "source": [ |
| 544 | + "print(f\"Total number of extracted communities: {len(leiden_res)}\")\n", |
| 545 | + "\n", |
| 546 | + "sizes = [len(s) for s in leiden_res]\n", |
| 547 | + "\n", |
| 548 | + "print(f\"Largest community: {max(sizes)}\")\n", |
| 549 | + "print(f\"Smallest community: {min(sizes)}\")\n", |
| 550 | + "\n", |
| 551 | + "import statistics\n", |
| 552 | + "\n", |
| 553 | + "print(f\"Median community size: {statistics.median(sizes)}\")" |
| 554 | + ] |
| 555 | + }, |
499 | 556 | { |
500 | 557 | "cell_type": "markdown", |
501 | 558 | "metadata": { |
|
506 | 563 | "\n", |
507 | 564 | "The [Facebook Network Analysis](https://networkx.org/nx-guides/content/exploratory_notebooks/facebook_notebook.html)\n", |
508 | 565 | "example in [nx-guides](https://networkx.org/nx-guides) goes much more in depth\n", |
509 | | - "and is a good tour of networkx analysis.\n", |
| 566 | + "and is a good tour of networkx analysis." |
| 567 | + ] |
| 568 | + }, |
| 569 | + { |
| 570 | + "cell_type": "markdown", |
| 571 | + "metadata": {}, |
| 572 | + "source": [ |
| 573 | + "---\n", |
| 574 | + "\n", |
| 575 | + "### Information on the Pocek Social Network dataset used in this notebook\n", |
510 | 576 | "\n", |
511 | | - "TODO: mention leiden, FA2, give suggestions and warnings" |
| 577 | + "**Authors:** Lubos Takac and Michal Zabovsky \n", |
| 578 | + "**Title:** SNAP Datasets, Stanford Large Network Dataset Collection \n", |
| 579 | + "**URL:** [http://snap.stanford.edu/data](http://snap.stanford.edu/data) \n", |
| 580 | + "**Date:** May 2012" |
512 | 581 | ] |
513 | 582 | } |
514 | 583 | ], |
|
0 commit comments