### Local Development Setup Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Commands to set up the repository for local development, including installing dependencies, testing, and building the project. ```sh npm install npm test npm run build ``` -------------------------------- ### Quick Start: Undirected Graph Community Detection (CommonJS) Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Provides the CommonJS equivalent for the quick start example, showing how to import the necessary modules using `require`. ```js const createGraph = require('ngraph.graph') const { detectClusters } = require('ngraph.leiden') ``` -------------------------------- ### Install ngraph.leiden Package Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Install the published package using npm. This command is used to add the library to your project dependencies. ```sh npm i ngraph.leiden ``` -------------------------------- ### Quick Start: Undirected Graph Community Detection (ES Modules) Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Demonstrates how to create an undirected graph, detect communities using the Leiden algorithm, and access the results like community membership and partition quality. Uses ES module imports. ```js import createGraph from 'ngraph.graph' import { detectClusters } from 'ngraph.leiden' const g = createGraph() // Undirected graphs: just add a single link; adapter will symmetrize for you g.addNode('a'); g.addNode('b'); g.addNode('c'); g.addNode('d') g.addLink('a','b') g.addLink('c','d') const result = detectClusters(g, { randomSeed: 42 }) // Get a community id for a node console.log('a ->', result.getClass('a')) // Group membership as Map console.log(result.getCommunities()) // Partition quality for the chosen objective console.log('Q =', result.quality()) ``` -------------------------------- ### Community Detection with Custom Weights and Modularity Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Perform community detection using the modularity quality metric with custom link weights. This example demonstrates how to set up a graph with specific edge weights and apply the detection algorithm. ```javascript const g = createGraph() g.addNode('x'); g.addNode('y'); g.addNode('z') g.addLink('x','y', { weight: 2 }); g.addLink('y','x', { weight: 2 }) g.addLink('y','z', { weight: 0.3 }); g.addLink('z','y', { weight: 0.3 }) const res = detectClusters(g, { quality: 'modularity', randomSeed: 1, linkWeight: l => l.data?.weight ?? 1 }) ``` -------------------------------- ### Run Benchmarks Source: https://github.com/anvaka/ngraph.leiden/blob/main/benchmarks/README.md Execute the benchmark suite to compare ngraph.leiden and ngraph.louvain. This command generates a table showing operations per second and time per run. ```sh npm run bench ``` -------------------------------- ### Run Community Detection from DOT File Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Use this command to detect communities in a graph stored in a DOT file and save the results to a JSON file. ```sh npx ngraph.leiden --in graph.dot --out membership.json ``` -------------------------------- ### Run Community Detection from Stdin with Membership Only Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Pipe graph data from stdin to detect communities and output only the node-to-community mapping. ```sh cat graph.dot | npx ngraph.leiden --membership-only ``` -------------------------------- ### Evaluate Community Quality from DOT File with Membership Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Evaluate the modularity or CPM quality of an existing membership mapping for a graph stored in a DOT file. Specify the input format and the quality metric to use. ```sh npx ngraph.leiden \ --in graph.dot --format dot \ --evaluate --membership membership.json \ --quality modularity ``` -------------------------------- ### Run Community Detection from JSON Edgelist Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Process a JSON file containing an edgelist (array of {source,target,weight?} or {nodes,links}) and output the community detection results to stdout. ```sh cat edges.json | npx ngraph.leiden > out.json ``` -------------------------------- ### CPM Community Detection with Resolution and Node Sizes Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Apply the Constant Potts Model (CPM) for community detection, allowing for custom resolution and node sizes. This enables fine-tuning the granularity of communities and making the detection size-aware. ```javascript const res = detectClusters(g, { quality: 'cpm', resolution: 0.5, nodeSize: n => n.data?.size ?? 1, randomSeed: 3, }) ``` -------------------------------- ### Custom Node Size for Community Detection Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Define a custom function to read a node's size, which is used by algorithms like CPM. This allows for size-aware community detection, where node importance can influence community formation. ```javascript const res = detectClusters(g, { nodeSize: n => n.data?.pop ?? 1, quality: 'cpm' }) ``` -------------------------------- ### Custom Link Weight for Community Detection Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Use a custom function to determine the weight of links, allowing for non-standard attributes like frequency or strength. This is useful for algorithms that consider edge weights. ```javascript const res = detectClusters(g, { linkWeight: l => Math.max(0, l.data?.w ?? 0) }) ``` -------------------------------- ### detectClusters Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Detects communities in a graph using the Leiden algorithm. It can process both single-layer and multilayer graphs and offers various configuration options for tuning the community detection process. ```APIDOC ## detectClusters(input, options?) ### Description Detects communities in a graph using the Leiden algorithm. It can process both single-layer and multilayer graphs and offers various configuration options for tuning the community detection process. ### Method `detectClusters` ### Parameters #### Input - **input**: `ngraph.graph` instance or a multilayer array `[{ graph, weight?, linkWeight?, nodeSize? }]`. #### Options - **quality**: ('modularity' | 'cpm') - Objective to optimize. Defaults to 'modularity'. - **resolution** (CPM only): `number` - The `gamma` parameter for CPM. Larger values yield more/smaller communities. Defaults to `1.0`. - **directed**: `boolean` - Enable Leicht–Newman directed modularity. Defaults to `false`. - **randomSeed**: `number` - Seed for the internal RNG used for node order shuffles. Defaults to `42`. - **candidateStrategy**: ('neighbors' | 'all' | 'random' | 'random-neighbor') - Controls which target communities are considered when moving a node. Defaults to 'neighbors'. - **allowNewCommunity**: `boolean` - Allow moves that create a fresh singleton community. Defaults to `false`. - **maxCommunitySize**: `number` - Upper bound on a community’s total size. Defaults to `Infinity`. - **refine**: `boolean` - Enable Leiden-style refinement between coarsening levels. Defaults to `true`. - **fixedNodes**: `Set` or `Array` of node ids - Nodes that must remain in their initial communities at the finest level. ### Return value `Clusters` object with the following methods: - `getClass(nodeId): number` — community id for `nodeId`. - `getCommunities(): Map` — nodes grouped by community id. - `quality(): number` — objective value for the final partition. - `toJSON(): { membership: Record, meta: { levels: number, quality: number, options: object } }` ``` -------------------------------- ### Community Detection with Multilayer Graphs Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Detect communities across multiple layers of a graph. Each layer can have its own weight and custom link/node size functions. This is useful for multiplex networks, temporal smoothing, or fusing heterogeneous signals. ```javascript const result = detectClusters([ { graph: layer1, weight: 1.0 }, { graph: layer2, weight: 0.2 }, ], { quality: 'modularity', randomSeed: 7 }) ``` -------------------------------- ### Fixing Nodes During Community Detection Source: https://github.com/anvaka/ngraph.leiden/blob/main/README.md Constrain a specific subset of nodes to remain in their initial communities throughout the detection and refinement process. This is useful for anchoring certain nodes or communities. ```javascript const res = detectClusters(g, { fixedNodes: new Set(['x','z']), randomSeed: 11, refine: true }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.