09_leiden

command
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 7 Imported by: 0

README

Example 09 — Leiden community detection

What it demonstrates

Modularity-optimising community detection with community.Leiden: build an undirected graph, freeze it into an immutable CSR snapshot, run Leiden, and read the resulting Partition back — including how to handle the ghost-slot sentinel (-1) that sharded packing leaves in the NodeID-indexed Community slice on small graphs.

Domain / scenario

A textbook two-community graph: two K4 cliques (nodes 0–3 and 4–7), each a complete subgraph, joined by a single bridge edge 3–4.

0───1        4───5
│╲ ╱│        │╲ ╱│
│ ╳ │        │ ╳ │
│╱ ╲│        │╱ ╲│
2───3────────4   (bridge 3–4)   6───7

Each clique is densely connected internally and the two halves touch only through the one bridge, so a modularity-optimising method recovers exactly the two cliques as separate communities.

How to run

go run ./examples/09_leiden

Expected output

Found 2 communities across 8 live nodes
  node 0 -> community 0
  node 1 -> community 0
  node 2 -> community 0
  node 3 -> community 0
  node 4 -> community 1
  node 5 -> community 1
  node 6 -> community 1
  node 7 -> community 1

Key APIs

  • graph/adjlist.New / AdjList.AddEdge — build the mutable undirected graph (two cliques plus a bridge).
  • graph/adjlist.AdjList.Mapper — translate the user node values (int) into compact NodeIDs via Lookup.
  • graph/csr.BuildFromAdjList — freeze the builder into an immutable CSR snapshot for analytics.
  • search/community.Leiden / DefaultLeidenOptions — run Leiden community detection with the default parameters.
  • search/community.Partition — the result: NumCommunities and a NodeID-indexed Community slice whose ghost slots carry the sentinel -1.

Further reading

Documentation

Overview

Example 09_leiden — runs Leiden community detection on two K4 cliques joined by a single bridge edge and prints the discovered communities.

The graph has a textbook community structure: each K4 clique is densely connected internally and the two cliques touch only through one bridge edge, so a modularity-optimising method recovers exactly two communities. The example also shows how to read a Partition back: the Community slice is NodeID-indexed and ghost slots created by sharded packing carry the sentinel -1, so the report iterates the eight live nodes explicitly rather than walking the raw slice.

Sample output: run `go run ./examples/09_leiden` and capture the stdout — the output is deterministic for the inputs hard-coded above and serves as the regression baseline a future change should preserve.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL