Skip to content

Commit cd17bae

Browse files
authored
Add Leiden to Notebook (rapidsai#152)
Part of rapidsai#123 Building off of rapidsai#150 Authors: - Ralph Liu (https://github.com/nv-rliu) - Rick Ratzel (https://github.com/rlratzel) Approvers: - Rick Ratzel (https://github.com/rlratzel) URL: rapidsai#152
1 parent d0f2fe1 commit cd17bae

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

notebooks/demo/nx_cugraph_demo_2506.ipynb

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,63 @@
496496
"plot_hist(bc, \"betweenness centrality\")"
497497
]
498498
},
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+
},
499556
{
500557
"cell_type": "markdown",
501558
"metadata": {
@@ -506,9 +563,21 @@
506563
"\n",
507564
"The [Facebook Network Analysis](https://networkx.org/nx-guides/content/exploratory_notebooks/facebook_notebook.html)\n",
508565
"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",
510576
"\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"
512581
]
513582
}
514583
],

0 commit comments

Comments
 (0)