### Infomap .tree File Format Example Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/InfomapResultsReader.md Example of the expected structure for Infomap output files. ```text 1:1:1 0.00244731 "83698" 83698 1:2:1 0.00190281 "54321" 54321 2:1:1 0.00150000 "11111" 11111 ``` -------------------------------- ### Read Infomap results Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Examples for initializing the reader with default or custom character sets. ```java InfomapResultsReader reader = new InfomapResultsReader("infomap.tree"); ``` ```java InfomapResultsReader reader = new InfomapResultsReader("infomap.tree", StandardCharsets.ISO_8859_1); ``` -------------------------------- ### Execute PartitionWriter Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Example usage of the write method to save clustering results. ```java PartitionWriter writer = new PartitionWriter(); writer.write(detector.getCommunities(), "output.csv"); ``` -------------------------------- ### Run LouvainSelector Clustering Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Example for running the selector with a specific iteration count. ```java LouvainSelector selector = new LouvainSelector("input.csv", "best.csv"); LayeredCommunityStructure best = selector.cluster(50); ``` -------------------------------- ### Typical Usage Example Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/SparseIntMatrix.md Demonstrates building an adjacency matrix, maintaining symmetry, and iterating over specific segments of the matrix. ```java SparseIntMatrix adj = new SparseIntMatrix(1000); adj.set(0, 1, 5); adj.set(1, 0, 5); // Maintain symmetry for undirected graphs adj.add(0, 1, 2); // Aggregate multi-edges: now adj[0][1] = 7 if (adj.isSymmetric()) { System.out.println("Graph is undirected"); } for (SparseIntMatrix.Iterator it = adj.iterator(); it.hasNext(); ) { it.advance(); if (it.x() <= it.y()) { // Iterate only upper triangle System.out.println("Edge " + it.x() + "-" + it.y() + ": " + it.value()); } } ``` -------------------------------- ### Multi-Layer Partition Export Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/PartitionWriter.md Example of writing a 3-layer community structure to a file. ```java List comms = new ArrayList<>(); comms.add(new int[]{0, 0, 1, 1, 2}); // Layer 0: 5 nodes comms.add(new int[]{0, 1, 1}); // Layer 1: 3 communities comms.add(new int[]{0, 1}); // Layer 2: 2 communities PartitionWriter writer = new PartitionWriter(); writer.write(comms, "multi_layer.csv"); ``` ```text 0:0:0:0 1:0:1:1 2:1:1:1 3:1:1:1 4:2:1:1 ``` -------------------------------- ### Run LouvainDetector Clustering Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Examples for running clustering with different layer constraints and seeds. ```java LouvainDetector detector = new LouvainDetector(graph, 12345L); LayeredCommunityStructure result = detector.cluster(); ``` ```java LayeredCommunityStructure result = detector.cluster(5); ``` ```java LayeredCommunityStructure result = detector.cluster(); ``` -------------------------------- ### Initialize Graph and Get Node Count Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/Graph.md Constructs a graph from a file and retrieves the total number of nodes. ```java Graph g = new GraphBuilder().fromFile("data.csv").build(); System.out.println("Nodes: " + g.order()); ``` -------------------------------- ### Initialize CommunityStructure Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/CommunityStructure.md Create a new instance from a partitioning array. Community IDs are automatically re-indexed to start from 0. ```java int[] comms = new int[]{0, 0, 1, 1, 5, 5}; CommunityStructure cs = new CommunityStructure(comms); // Internal IDs are re-indexed: 0->0, 1->1, 5->2 ``` -------------------------------- ### Configure Log4J2 Logging Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Example XML configuration for Log4J2 to manage library logging levels and output. ```xml ``` -------------------------------- ### Output Format Example Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/PartitionWriter.md The resulting colon-delimited file structure where each line represents a node and its community IDs across layers. ```text node:layer0_comm:layer1_comm:layer2_comm:... 0:0:0:0 1:0:0:1 2:1:0:1 3:1:1:1 ``` -------------------------------- ### CSV file format example Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/GraphBuilder.md Represents the expected structure for input files with source, target, and weight fields. ```text source target weight 0 1 5 1 2 3 0 2 2 ``` -------------------------------- ### Iterate Through Clustering Layers Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LayeredCommunityStructure.md Example of iterating through all layers of a result produced by a LouvainDetector. ```java LouvainDetector detector = new LouvainDetector(graph); LayeredCommunityStructure result = detector.cluster(); for (int l = 0; l < result.layers(); l++) { CommunityStructure layer = result.layer(l); System.out.println("Layer " + l + ":"); System.out.println(" Nodes: " + layer.order()); System.out.println(" Communities: " + layer.numComms()); int[] communities = layer.communities(); for (int node = 0; node < layer.order(); node++) { System.out.println(" Node " + node + " -> Community " + communities[node]); } } ``` -------------------------------- ### Calculate NMI Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Example usage of the NMI calculation method. ```java double nmi = NMI.NMI(clustering1, clustering2); ``` -------------------------------- ### Graph File Format Example Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/INDEX.md The input file format uses a three-field structure representing source, target, and weight. ```text 0,1,5 1,2,3 0,2,7 ``` -------------------------------- ### CommunityStructure(int[] partitioning) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/CommunityStructure.md Constructor to create a new CommunityStructure instance from a partitioning array. Community IDs are automatically re-indexed to be consecutive starting from 0. ```APIDOC ## Constructor ### Description Creates a community structure from a partitioning array. Community IDs are internally re-indexed to be consecutive starting from 0. ### Parameters - **partitioning** (int[]) - Required - Array where partitioning[i] is the community ID of node i ``` -------------------------------- ### get(int x, int y) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/SparseIntMatrix.md Retrieves the value at the specified row and column index. ```APIDOC ## get(int x, int y) ### Description Get the value at position (x, y). ### Parameters - **x** (int) - Required - Row index - **y** (int) - Required - Column index ### Returns - **int** - Value at (x, y), or 0 if not set. ``` -------------------------------- ### Typical usage workflow Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainSelector.md Demonstrates the full process of initializing the selector, running multiple iterations, and accessing the results. ```java LouvainSelector selector = new LouvainSelector( "graphs/arxiv.txt", "best_clustering.csv" ); LayeredCommunityStructure best = selector.cluster(100); System.out.println("Best modularity: " + best.layer(0).communities().length); ``` -------------------------------- ### Initialize PartitionWriter Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/PartitionWriter.md Instantiate a new PartitionWriter object. ```java public PartitionWriter() ``` ```java PartitionWriter writer = new PartitionWriter(); ``` -------------------------------- ### Visualize Dependency Graph Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/types.md Overview of class dependencies and object creation patterns. ```text Graph └── Contains Graph.Partitioning GraphBuilder └── Creates Graph LayeredCommunityStructure └── Contains CommunityStructure[] CommunityStructure └── Created from int[] partitioning LouvainDetector ├── Takes Graph └── Returns LayeredCommunityStructure NMI ├── Takes CommunityStructure or int[] └── Uses Clustering implementations SparseIntMatrix └── Used internally by Graph ``` -------------------------------- ### Initialize PartitionReader Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/PartitionReader.md Instantiate a new reader by providing the path to the partition file. ```java PartitionReader reader = new PartitionReader("partition.csv"); ``` -------------------------------- ### Get Edge Weight Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/Graph.md Retrieves the weight of the edge between two specified nodes. ```java int edgeWeight = g.weight(0, 1); ``` -------------------------------- ### PartitionWriter() Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/PartitionWriter.md Constructor to initialize a new PartitionWriter instance. ```APIDOC ## PartitionWriter() ### Description Creates a new instance of the PartitionWriter class. ### Signature `public PartitionWriter()` ``` -------------------------------- ### Project File Structure Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/INDEX.md Overview of the source code and test resource directory layout. ```text src/main/java/com/github/neiljustice/louvain/ ├── clustering/ (Clusterer implementations) ├── exception/ (LouvainException) ├── file/ (FileLoader utility) ├── graph/ (Graph and GraphBuilder) ├── nmi/ (NMI and related classes) └── util/ (SparseIntMatrix, ArrayUtils) ``` ```text src/test/resources/graphs/ └── *.txt (Example graph files) ``` -------------------------------- ### Typical Usage Pattern Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/CommunityStructure.md Demonstrates iterating through all communities and their members. ```java CommunityStructure cs = structure.layer(0); System.out.println("Graph has " + cs.order() + " nodes in " + cs.numComms() + " communities"); for (int comm = 0; comm < cs.numComms(); comm++) { TIntArrayList members = cs.communityMembers(comm); System.out.println("Community " + comm + " has " + members.size() + " members:"); for (int i = 0; i < members.size(); i++) { System.out.println(" Node " + members.get(i)); } } ``` -------------------------------- ### Get Node Community in Java Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/Graph.md Retrieves the community ID assigned to a specific node. ```java int nodeComm = g.partitioning().community(3); ``` -------------------------------- ### Get Node Degree Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/Graph.md Retrieves the sum of weights for all edges incident to a specific node. ```java int nodeDegree = g.degree(5); ``` -------------------------------- ### Typical Louvain Workflow Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/INDEX.md Demonstrates the end-to-end process of loading a graph, detecting communities, analyzing results, and comparing against a baseline. ```java // 1. Load graph Graph g = new GraphBuilder().fromFile("data.csv", true).build(); // 2. Detect communities (with seed for reproducibility) LouvainDetector detector = new LouvainDetector(g, 42L); LayeredCommunityStructure result = detector.cluster(); // 3. Analyze results System.out.println("Modularity: " + detector.modularity()); System.out.println("Layers: " + result.layers()); CommunityStructure communities = result.layer(0); System.out.println("Communities: " + communities.numComms()); // 4. Save results PartitionWriter writer = new PartitionWriter(); writer.write(detector.getCommunities(), "output.csv"); // 5. Compare with baseline RandomCommunityAssigner rca = new RandomCommunityAssigner(result); LayeredCommunityStructure random = rca.cluster(); double nmi = NMI.NMI(communities, random.layer(0)); System.out.println("NMI vs random: " + nmi); ``` -------------------------------- ### Retrieve layer count in Java Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainDetector.md Get the total number of layers generated after the clustering process. ```java detector.cluster(); System.out.println("Layers created: " + detector.getLayerCount()); ``` -------------------------------- ### Compare Clustering Algorithms Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/INDEX.md Shows how to run different detection algorithms and compare their results using Normalized Mutual Information (NMI). ```java LouvainDetector louvain = new LouvainDetector(g); LayeredCommunityStructure lr = louvain.cluster(); InfomapResultsReader infomap = new InfomapResultsReader("infomap.tree"); LayeredCommunityStructure ir = infomap.cluster(); ``` ```java double nmi = NMI.NMI(lr.layer(0), ir.layer(0)); ``` -------------------------------- ### Get Matrix Value Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/SparseIntMatrix.md Retrieve the value at a specific coordinate, returning 0 if the value is not set. ```java int value = matrix.get(0, 5); ``` -------------------------------- ### Initialize and build a graph manually Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/GraphBuilder.md Construct a graph by defining its size and adding edges sequentially. Ensure setSize is called before adding edges to avoid errors. ```java Graph g = new GraphBuilder() .setSize(100) .addEdge(0, 1, 5) .addEdge(1, 2, 3) .build(); ``` ```java new GraphBuilder() .setSize(5) .addEdge(0, 1, 10) .addEdge(0, 2, 5) .addEdge(1, 2, 7) .build(); ``` -------------------------------- ### Load Partitioning in Java Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/Graph.md Initializes the graph's community assignments using a pre-computed integer array. ```java int[] communities = new int[]{0, 0, 1, 1, 2}; g.partitioning().loadPartitioning(communities); ``` -------------------------------- ### Retrieve Community Members Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/CommunityStructure.md Get the list of nodes belonging to a specific community using a Trove TIntArrayList. ```java TIntArrayList members = cs.communityMembers(2); for (int i = 0; i < members.size(); i++) { int nodeId = members.get(i); System.out.println("Community 2 contains node " + nodeId); } ``` -------------------------------- ### PartitionReader(String filename) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/PartitionReader.md Constructor to initialize a new PartitionReader instance with the specified file path. ```APIDOC ## Constructor: PartitionReader(String filename) ### Description Creates a reader for a partition file. ### Parameters - **filename** (String) - Required - Path to partition file written by PartitionWriter ### Example ```java PartitionReader reader = new PartitionReader("partition.csv"); ``` ``` -------------------------------- ### Retrieve All Community Assignments Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/CommunityStructure.md Get the complete array of community assignments where the index represents the node ID. ```java int[] allComms = cs.communities(); ``` -------------------------------- ### Initialize PartitionReader Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Defines the constructor signature for reading partition files. ```java public PartitionReader(String filename) ``` -------------------------------- ### Initialize LouvainSelector Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainSelector.md Create a new instance by specifying the input graph file path and the output file path. ```java LouvainSelector selector = new LouvainSelector( "src/test/resources/graphs/arxiv.txt", "output.csv" ); ``` -------------------------------- ### Prepare Output Directory Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Ensure parent directories exist before writing partition results, as the PartitionWriter does not create them automatically. ```bash mkdir -p output ``` -------------------------------- ### Construct a Graph object Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/GraphBuilder.md Initializes the build process for a Graph instance. ```java public Graph build() ``` -------------------------------- ### Build a graph from a file Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/GraphBuilder.md Demonstrates the fluent API usage to load data from a CSV file and construct the graph. ```java Graph g = new GraphBuilder() .fromFile("data.csv") .build(); ``` -------------------------------- ### Load and Cluster a Graph Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/INDEX.md Demonstrates the workflow for reading a graph from a file, performing community detection, and accessing the resulting structure. ```java Graph g = new GraphBuilder().fromFile("graph.csv", true).build(); ``` ```java LouvainDetector detector = new LouvainDetector(g, 12345L); LayeredCommunityStructure result = detector.cluster(); ``` ```java CommunityStructure layer0 = result.layer(0); int[] communities = layer0.communities(); ``` ```java new PartitionWriter().write(detector.getCommunities(), "output.csv"); ``` -------------------------------- ### Initialize SparseIntMatrix Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/SparseIntMatrix.md Create a new sparse matrix of a specified size. ```java SparseIntMatrix m = new SparseIntMatrix(100); ``` -------------------------------- ### Compare algorithm results Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/NMI.md Demonstrates comparing community structures generated by two different Louvain detector runs. ```java Graph g = new GraphBuilder().fromFile("data.csv").build(); LouvainDetector detector1 = new LouvainDetector(g, 123L); LayeredCommunityStructure result1 = detector1.cluster(); LouvainDetector detector2 = new LouvainDetector(g, 456L); LayeredCommunityStructure result2 = detector2.cluster(); CommunityStructure cs1 = result1.layer(0); CommunityStructure cs2 = result2.layer(0); double nmi = NMI.NMI(cs1, cs2); System.out.println("Agreement between runs: " + nmi); ``` -------------------------------- ### Initialize GraphBuilder Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/GraphBuilder.md Constructor for creating a new GraphBuilder instance. ```java public GraphBuilder() ``` -------------------------------- ### Save and Reload Clustering Workflow Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/PartitionReader.md Demonstrates the full cycle of detecting communities, writing them to a file, and reloading them using PartitionReader. ```java LouvainDetector detector = new LouvainDetector(graph); LayeredCommunityStructure result = detector.cluster(); PartitionWriter writer = new PartitionWriter(); writer.write(result.layer(0).communities(), "partition.csv"); PartitionReader reader = new PartitionReader("partition.csv"); LayeredCommunityStructure loaded = reader.cluster(); ``` -------------------------------- ### Initialize InfomapResultsReader Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Defines the constructor signature for reading Infomap .tree files. ```java public InfomapResultsReader(String filename, Charset charset) ``` -------------------------------- ### Initialize RandomCommunityAssigner from List of Arrays Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/RandomCommunityAssigner.md Create an assigner directly from a list of community partitioning arrays. ```java List communities = new ArrayList<>(); communities.add(new int[]{0, 0, 1, 1, 2}); RandomCommunityAssigner rca = new RandomCommunityAssigner(communities); ``` -------------------------------- ### Visualize Type Relationships Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/types.md Hierarchical overview of the library's interfaces and their implementations. ```text Clusterer (interface) ├── LouvainDetector ├── LouvainSelector ├── RandomCommunityAssigner ├── InfomapResultsReader └── PartitionReader Clustering (interface) ├── HardClustering └── SoftClustering Graph.Partitioning (inner class) └── Available via graph.partitioning() LayeredCommunityStructure └── Contains multiple CommunityStructure objects ``` -------------------------------- ### Execute Multi-run Best Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/README.md Runs the clustering algorithm multiple times to identify the best community structure. ```java LouvainSelector selector = new LouvainSelector(input, output); LayeredCommunityStructure best = selector.cluster(100); ``` -------------------------------- ### Compare Louvain and Infomap Results Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/InfomapResultsReader.md Demonstrates comparing community detection results between Louvain and Infomap using NMI. ```java Graph g = new GraphBuilder().fromFile("data.csv", true).build(); LouvainDetector louvain = new LouvainDetector(g); LayeredCommunityStructure louvainResult = louvain.cluster(); InfomapResultsReader infomap = new InfomapResultsReader("infomap_output.tree"); LayeredCommunityStructure infomapResult = infomap.cluster(); System.out.println("Louvain layers: " + louvainResult.layers()); System.out.println("Infomap layers: " + infomapResult.layers()); for (int i = 0; i < louvainResult.layers(); i++) { for (int j = 0; j < infomapResult.layers(); j++) { double nmi = NMI.NMI(louvainResult.layer(i), infomapResult.layer(j)); System.out.printf("Louvain L%d vs Infomap L%d: NMI=%.3f%n", i, j, nmi); } } ``` -------------------------------- ### Perform basic community detection in Java Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/README.md Initializes a graph from a CSV file and executes the Louvain algorithm with a fixed seed for reproducibility. ```java // Load graph from CSV file Graph g = new GraphBuilder().fromFile("graph.csv", true).build(); // Detect communities with reproducible seed LouvainDetector detector = new LouvainDetector(g, 12345L); LayeredCommunityStructure result = detector.cluster(); // Access results System.out.println("Modularity: " + detector.modularity()); CommunityStructure communities = result.layer(0); System.out.println("Communities: " + communities.numComms()); ``` -------------------------------- ### Run default clustering Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainSelector.md Execute the Louvain algorithm using the default number of iterations. ```java LayeredCommunityStructure best = selector.cluster(); ``` -------------------------------- ### Comparing Clustering Algorithms Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/INDEX.md Demonstrates how to compare the results of different clustering algorithms using Normalized Mutual Information (NMI). ```APIDOC ## Comparing Clustering Algorithms ### Description Run multiple clustering algorithms and compare their outputs using the NMI metric. ### Usage ```java // Run multiple algorithms LouvainDetector louvain = new LouvainDetector(g); LayeredCommunityStructure lr = louvain.cluster(); InfomapResultsReader infomap = new InfomapResultsReader("infomap.tree"); LayeredCommunityStructure ir = infomap.cluster(); // Compare with NMI double nmi = NMI.NMI(lr.layer(0), ir.layer(0)); ``` ``` -------------------------------- ### Run multiple iterations and save best result Source: https://github.com/neil-justice/louvain/blob/master/README.md Executes the Louvain algorithm multiple times and writes the clustering with the highest modularity to a file. ```java final Graph g = new GraphBuilder().erdosRenyi(1000, 0.1); final LouvainSelector ls = new LouvainSelector("src/test/resources/graphs/arxiv.txt", "out.csv"); final LayeredCommunityStructure cs = ls.cluster(10); ``` -------------------------------- ### LouvainSelector Constructor Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Initializes a new LouvainSelector instance for reading from and writing to files. ```APIDOC ## LouvainSelector(String fileToRead, String fileToWrite) ### Description Initializes the selector with input and output file paths. ### Parameters - **fileToRead** (String) - Required - Path to input graph CSV file. - **fileToWrite** (String) - Required - Path where the best result will be written. ``` -------------------------------- ### Run Clustering and Access Results Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainDetector.md Execute the clustering algorithm until convergence and retrieve the resulting community structure and modularity metrics. ```java LayeredCommunityStructure result = detector.cluster(); System.out.println("Number of layers: " + result.layers()); System.out.println("Modularity: " + detector.modularity()); ``` -------------------------------- ### Graph Clustering Workflow Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/INDEX.md Demonstrates the standard workflow for loading a graph, detecting communities, and saving the results. ```APIDOC ## Graph Clustering Workflow ### Description Load a graph from a file, detect communities using the Louvain algorithm, and export the resulting partition. ### Usage ```java // 1. Read from file Graph g = new GraphBuilder().fromFile("graph.csv", true).build(); // 2. Detect communities LouvainDetector detector = new LouvainDetector(g, 12345L); LayeredCommunityStructure result = detector.cluster(); // 3. Access results CommunityStructure layer0 = result.layer(0); int[] communities = layer0.communities(); // 4. Save for later new PartitionWriter().write(detector.getCommunities(), "output.csv"); ``` ``` -------------------------------- ### Save and Load Partition Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/README.md Persists community structures to disk and reloads them for further analysis. ```java new PartitionWriter().write(detector.getCommunities(), out); PartitionReader pr = new PartitionReader(out); LayeredCommunityStructure reloaded = pr.cluster(); ``` -------------------------------- ### Load and Cluster Graph Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/README.md Initializes a graph from a file and performs community detection using a specified seed. ```java Graph g = new GraphBuilder().fromFile(path, needsReindex).build(); LouvainDetector d = new LouvainDetector(g, seed); LayeredCommunityStructure r = d.cluster(); ``` -------------------------------- ### loadPartitioning(int[] partitioning) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/Graph.md Loads a pre-computed array of community assignments into the graph. ```APIDOC ## loadPartitioning(int[] partitioning) ### Description Load a pre-computed partitioning (array of community assignments) into the graph's partitioning. ### Parameters - **partitioning** (int[]) - Required - Array where partitioning[i] is the community ID of node i ### Returns - void ### Throws - LouvainException if array length doesn't match graph order. ``` -------------------------------- ### Configure LouvainSelector Clustering Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Method definition for running the selector clustering process. ```java public LayeredCommunityStructure cluster(int times) ``` -------------------------------- ### Initialize LouvainDetector Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainDetector.md Create a detector instance using a graph object. The random seed is generated automatically. ```java Graph g = new GraphBuilder().fromFile("data.csv").build(); LouvainDetector detector = new LouvainDetector(g); ``` -------------------------------- ### Initialize LouvainDetector with Seed Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainDetector.md Create a detector instance with a specific random seed to ensure reproducible clustering results. ```java Graph g = new GraphBuilder().fromFile("data.csv").build(); LouvainDetector detector = new LouvainDetector(g, 12345L); LayeredCommunityStructure result = detector.cluster(); ``` -------------------------------- ### partitioning() Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/Graph.md Returns the current partitioning object for the graph. ```APIDOC ## public Partitioning partitioning() ### Description Get the current partitioning object, which tracks community assignments and provides modularity calculations. ### Returns - **Partitioning** - The graph's partitioning object. ``` -------------------------------- ### Partition Output Format Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/README.md Format for representing node community assignments across multiple layers. ```text node:layer0_comm:layer1_comm:... 0:0:0 1:0:1 2:1:1 ``` -------------------------------- ### Calculate Entropy in Java Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Methods for calculating entropy using different logarithm bases. ```java double e = Entropy.entropy(distribution, (int)Math.E); ``` ```java double e = Entropy.entropy(distribution, distribution.length); ``` -------------------------------- ### Initialize RandomCommunityAssigner from LayeredCommunityStructure Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/RandomCommunityAssigner.md Create an assigner using a reference clustering from a LouvainDetector. ```java LouvainDetector detector = new LouvainDetector(graph); LayeredCommunityStructure result = detector.cluster(); RandomCommunityAssigner rca = new RandomCommunityAssigner(result); ``` -------------------------------- ### fromFile(String filename, boolean needsReIndexing, String delimiter, Charset charset) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/GraphBuilder.md Loads a graph with explicit delimiter and character encoding specifications. ```APIDOC ## fromFile(String filename, boolean needsReIndexing, String delimiter, Charset charset) ### Description Load a graph with explicit delimiter and character encoding specifications. ### Parameters - **filename** (String) - Required - Path to graph file - **needsReIndexing** (boolean) - Required - Whether to re-index string node names - **delimiter** (String) - Required - Field delimiter in file - **charset** (Charset) - Required - Character encoding ### Returns - **Graph** - The constructed graph object. ### Example ```java Graph g = new GraphBuilder().fromFile("graphs/data.tsv", true, "\t", StandardCharsets.UTF_8); ``` ``` -------------------------------- ### PartitionWriter.write(List communities, String filename) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Method to write clustering results to a file. ```APIDOC ## PartitionWriter.write(List communities, String filename) ### Description Writes clustering results to the specified output file. ### Parameters - **communities** (List) - Required - One array per layer; communities.get(layer)[node] is community ID - **filename** (String) - Required - Output file path ### Example ```java PartitionWriter writer = new PartitionWriter(); writer.write(detector.getCommunities(), "output.csv"); ``` ``` -------------------------------- ### Load Graph from File Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/GraphBuilder.md Methods for loading graph data from files with varying levels of configuration for indexing, delimiters, and encoding. ```java public Graph fromFile(String filename) ``` ```java Graph g = new GraphBuilder().fromFile("graphs/arxiv.txt"); ``` ```java public Graph fromFile(String filename, boolean needsReIndexing) ``` ```java Graph g = new GraphBuilder().fromFile("graphs/named_nodes.txt", true); ``` ```java public Graph fromFile(String filename, boolean needsReIndexing, String delimiter, Charset charset) ``` ```java Graph g = new GraphBuilder().fromFile( "graphs/data.tsv", true, "\t", StandardCharsets.UTF_8 ); ``` ```java public Graph fromFile(File file) ``` -------------------------------- ### Initialize LouvainSelector Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Constructor definition for the LouvainSelector class. ```java public LouvainSelector(String fileToRead, String fileToWrite) ``` -------------------------------- ### Initialize LayeredCommunityStructure Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LayeredCommunityStructure.md Construct a new instance using a list of community assignment arrays. ```java List comms = new ArrayList<>(); comms.add(new int[]{0, 0, 1, 1}); comms.add(new int[]{0, 1}); LayeredCommunityStructure structure = new LayeredCommunityStructure(comms); ``` -------------------------------- ### Perform Community Detection with LouvainDetector Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainDetector.md Initializes a graph from a file and executes the Louvain clustering algorithm to retrieve community structures. ```java Graph g = new GraphBuilder().fromFile("graph.csv", true).build(); LouvainDetector detector = new LouvainDetector(g, 42L); LayeredCommunityStructure structure = detector.cluster(); System.out.println("Modularity: " + detector.modularity()); System.out.println("Layers: " + detector.getLayerCount()); int[] baseComms = structure.layer(0).communities(); for (int i = 0; i < baseComms.length; i++) { System.out.println("Node " + i + " -> Community " + baseComms[i]); } ``` -------------------------------- ### cluster() Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/PartitionReader.md Reads the partition file and returns the parsed community structure. ```APIDOC ## Method: cluster() ### Description Read the partition file and return a layered community structure. ### Returns - **LayeredCommunityStructure** - Community assignments parsed from file. ### Throws - **IllegalStateException** - If file I/O or parsing fails. - **NumberFormatException** - If file format is invalid. ### Example ```java LayeredCommunityStructure result = reader.cluster(); ``` ``` -------------------------------- ### SparseIntMatrix(SparseIntMatrix m) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/SparseIntMatrix.md Constructor to create a copy of an existing sparse matrix. ```APIDOC ## SparseIntMatrix(SparseIntMatrix m) ### Description Create a copy of an existing sparse matrix. ### Parameters - **m** (SparseIntMatrix) - Required - Matrix to copy ``` -------------------------------- ### LouvainSelector Constructor Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainSelector.md Initializes a new LouvainSelector instance to read a graph from a file and write the best clustering result to a specified output path. ```APIDOC ## Constructor ### Signature `public LouvainSelector(String fileToRead, String fileToWrite)` ### Parameters - **fileToRead** (String) - Required - Path to input graph CSV file - **fileToWrite** (String) - Required - Path to write the best clustering result ### Example ```java LouvainSelector selector = new LouvainSelector("src/test/resources/graphs/arxiv.txt", "output.csv"); ``` ``` -------------------------------- ### Load Graphs from File Paths Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Specify relative or absolute paths when building graphs from files. ```java // Relative to current directory Graph g = new GraphBuilder().fromFile("data/graph.csv").build(); // Absolute path Graph g = new GraphBuilder().fromFile("/home/user/graphs/large.csv").build(); ``` -------------------------------- ### setSize(int order) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/GraphBuilder.md Initializes the builder with a specific number of nodes. This must be called before adding edges. ```APIDOC ## setSize(int order) ### Description Initialize the builder with a specific number of nodes. Must be called before manual edge addition. ### Parameters - **order** (int) - Required - Number of nodes in the graph (0 to order-1) ### Returns - **GraphBuilder** - This builder instance for chaining. ### Throws - **LouvainException** - if order is invalid. ``` -------------------------------- ### fromFile(File file) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/GraphBuilder.md Loads a graph from a File object using comma delimiter and UTF-8 encoding. ```APIDOC ## fromFile(File file) ### Description Load a graph from a File object using comma delimiter and UTF-8 encoding. ### Parameters - **file** (File) - Required - File object pointing to graph CSV ### Returns - **Graph** - The constructed graph object. ``` -------------------------------- ### InfomapResultsReader(String filename) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/InfomapResultsReader.md Constructor to create a reader for an Infomap output file using UTF-8 encoding. ```APIDOC ## Constructor: InfomapResultsReader(String filename) ### Description Creates a reader for an Infomap output file using UTF-8 encoding. ### Parameters - **filename** (String) - Required - Path to Infomap .tree output file ### Example ```java InfomapResultsReader reader = new InfomapResultsReader("results.tree"); ``` ``` -------------------------------- ### Write partition results Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Defines the method signature for writing community data to a file. ```java public void write(List communities, String filename) ``` -------------------------------- ### cluster() Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainDetector.md Executes the Louvain algorithm until convergence. ```APIDOC ## cluster() ### Description Runs the Louvain community detection algorithm until convergence, where no further modularity improvement is possible. ### Returns - **LayeredCommunityStructure** - Multi-layer community assignments where layer 0 corresponds to original nodes. ``` -------------------------------- ### Copy SparseIntMatrix Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/SparseIntMatrix.md Create a new instance by copying an existing SparseIntMatrix. ```java SparseIntMatrix copy = new SparseIntMatrix(original); ``` -------------------------------- ### InfomapResultsReader(String filename, Charset charset) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/InfomapResultsReader.md Constructor to create a reader for an Infomap output file with explicit character encoding. ```APIDOC ## Constructor: InfomapResultsReader(String filename, Charset charset) ### Description Creates a reader for an Infomap output file with explicit character encoding. ### Parameters - **filename** (String) - Required - Path to Infomap output file - **charset** (Charset) - Required - Character encoding (e.g., StandardCharsets.UTF_8) ### Example ```java InfomapResultsReader reader = new InfomapResultsReader("results.tree", StandardCharsets.ISO_8859_1); ``` ``` -------------------------------- ### fromFile(String filename) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/GraphBuilder.md Loads a graph from a CSV file where nodes have integer indices. The file format expects three comma-separated integers per line: source, target, weight. ```APIDOC ## fromFile(String filename) ### Description Loads a graph from a CSV file where nodes have integer indices. ### Parameters - **filename** (String) - Required - Path to CSV file ### Returns - **Graph** - The constructed graph object. ### Throws - **IllegalStateException** - If file I/O fails - **LouvainException** - If constructed matrix is asymmetric ### Example ```java Graph g = new GraphBuilder().fromFile("graphs/arxiv.txt"); ``` ``` -------------------------------- ### Initialize InfomapResultsReader Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/InfomapResultsReader.md Constructs a reader instance using either default UTF-8 or an explicit character set. ```java InfomapResultsReader reader = new InfomapResultsReader("results.tree"); ``` ```java InfomapResultsReader reader = new InfomapResultsReader( "results.tree", StandardCharsets.ISO_8859_1 ); ``` -------------------------------- ### Run custom number of iterations Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainSelector.md Execute the Louvain algorithm a specified number of times and return the best result. ```java LayeredCommunityStructure best = selector.cluster(50); System.out.println("Best result found after 50 runs"); ``` -------------------------------- ### write(List communities, String filename) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/PartitionWriter.md Writes community assignments to a file in a colon-delimited format. ```APIDOC ## write(List communities, String filename) ### Description Writes community assignments to a file. Each element in the provided list represents one layer of the partition hierarchy. ### Parameters - **communities** (List) - Required - List of partitioning arrays where `communities.get(layer)[node]` is the community ID. - **filename** (String) - Required - The output file path. ### Returns - **void** ### Throws - **IllegalStateException** - Thrown if the file write operation fails. ``` -------------------------------- ### Compare Louvain results with Infomap output Source: https://github.com/neil-justice/louvain/blob/master/README.md Compares layers of a Louvain-detected community structure against an Infomap results file using NMI. ```java final Graph g = new GraphBuilder().fromFile("src/test/resources/graphs/arxiv.txt", true); final LouvainDetector ld = new LouvainDetector(g); final LayeredCommunityStructure cs = ld.cluster(); final InfomapResultsReader irr = new InfomapResultsReader("graphs/infomap.tree"); final LayeredCommunityStructure cs2 = irr.cluster(); for (int i = 0; i < cs.layers(); i++) { for (int j = 0; j < cs2.layers(); j++) { System.out.printf("%.03f ", NMI.NMI(cs.layer(i), cs2.layer(j))); } System.out.println(); } ``` -------------------------------- ### GraphBuilder.fromFile Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Constructs a Graph object by loading data from a specified file path with configurable indexing, delimiters, and character encoding. ```APIDOC ## GraphBuilder.fromFile ### Description Loads graph data from a CSV or TSV file into a Graph object. The file must contain three fields per line: source, target, and weight. ### Method public Graph fromFile(String filename, boolean needsReIndexing, String delimiter, Charset charset) ### Parameters - **filename** (String) - Required - Path to the graph CSV file. - **needsReIndexing** (boolean) - Optional (Default: false) - If true, treats the first two columns as string node names and re-indexes them to 0-based integers. - **delimiter** (String) - Optional (Default: ",") - The field separator used in the file. - **charset** (Charset) - Optional (Default: StandardCharsets.UTF_8) - The character encoding of the file. ### Usage Example ```java Graph g = new GraphBuilder().fromFile("graph.csv", true, ",", StandardCharsets.UTF_8); ``` ``` -------------------------------- ### Execute Clustering and Access Results Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/InfomapResultsReader.md Parses the Infomap file and iterates through the resulting hierarchical layers. ```java LayeredCommunityStructure result = reader.cluster(); for (int layer = 0; layer < result.layers(); layer++) { System.out.println("Layer " + layer + ": " + result.layer(layer).numComms() + " communities"); } ``` -------------------------------- ### read() Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/InfomapResultsReader.md Manually triggers file reading and processing. ```APIDOC ## Method: read() ### Description Manually trigger file reading and processing. This is called automatically by the cluster() method. ``` -------------------------------- ### Iterate Community Sizes Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/CommunityStructure.md Retrieve and iterate through the sizes of all communities. ```java int[] sizes = cs.communitySizes(); for (int i = 0; i < sizes.length; i++) { System.out.println("Community " + i + " has " + sizes[i] + " nodes"); } ``` -------------------------------- ### Update Partition File Path Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/PartitionReader.md Change the target file path dynamically to read multiple partition files sequentially. ```java PartitionReader reader = new PartitionReader("file1.csv"); LayeredCommunityStructure result1 = reader.cluster(); reader.setFilename("file2.csv"); LayeredCommunityStructure result2 = reader.cluster(); ``` -------------------------------- ### cluster() Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainSelector.md Executes the Louvain algorithm 10 times (default) and returns the community structure with the highest modularity. ```APIDOC ## cluster() ### Signature `public LayeredCommunityStructure cluster()` ### Returns - **LayeredCommunityStructure** - Community structure from the best run. ### Example ```java LayeredCommunityStructure best = selector.cluster(); ``` ``` -------------------------------- ### Retry with Different Seed in Java Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/errors.md Implements a loop to retry the Louvain detection process up to three times using a new seed on each failure. ```java Graph g = new GraphBuilder().fromFile("data.csv").build(); LouvainDetector detector = null; for (int attempt = 0; attempt < 3; attempt++) { try { detector = new LouvainDetector(g, System.nanoTime()); detector.cluster(); break; } catch (Exception e) { System.err.println("Attempt " + (attempt+1) + " failed: " + e); } } ``` -------------------------------- ### Initialize LouvainDetector Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Constructor definition for the LouvainDetector class. ```java public LouvainDetector(Graph g, long seed) ``` -------------------------------- ### SparseIntMatrix(int size) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/SparseIntMatrix.md Constructor to create an empty sparse matrix of the given dimensions. ```APIDOC ## SparseIntMatrix(int size) ### Description Create an empty sparse matrix of the given dimensions. ### Parameters - **size** (int) - Required - Matrix dimensions (creates size × size matrix) ``` -------------------------------- ### LouvainSelector.cluster() Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Runs the Louvain clustering algorithm a specified number of times to find the best result. ```APIDOC ## LayeredCommunityStructure cluster(int times) ### Description Runs the Louvain algorithm multiple times to select the best result. ### Parameters - **times** (int) - Optional - Number of times to run the algorithm. Defaults to 10. ``` -------------------------------- ### Detect communities and calculate modularity Source: https://github.com/neil-justice/louvain/blob/master/README.md Loads a graph from a file and computes the modularity of the detected communities. ```java final Graph g = new GraphBuilder().fromFile("src/test/resources/graphs/arxiv.txt", true); final LouvainDetector ld = new LouvainDetector(g); final LayeredCommunityStructure cs = ld.cluster(); System.out.println(ld.modularity()); ``` -------------------------------- ### size() Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/SparseIntMatrix.md Returns the dimensions of the matrix. ```APIDOC ## size() ### Description Get the matrix dimensions. ### Returns - **int** - Size (number of rows/columns). ``` -------------------------------- ### cluster(int times) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainSelector.md Executes the Louvain algorithm for a specified number of iterations and returns the best result, while also writing the output to the configured file. ```APIDOC ## cluster(int times) ### Signature `public LayeredCommunityStructure cluster(int times)` ### Parameters - **times** (int) - Required - Number of times to run Louvain ### Returns - **LayeredCommunityStructure** - Community structure with highest modularity. ### Example ```java LayeredCommunityStructure best = selector.cluster(50); ``` ``` -------------------------------- ### LouvainDetector(Graph g) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainDetector.md Constructor to initialize the detector with a graph and a randomly generated seed. ```APIDOC ## LouvainDetector(Graph g) ### Description Initializes a new LouvainDetector instance with the provided graph. A random seed is automatically generated to determine the node processing order. ### Parameters - **g** (Graph) - Required - The input graph to cluster. ``` -------------------------------- ### Access Partitioning Information Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/Graph.md Retrieves the current partitioning object to determine community assignments. ```java Graph.Partitioning p = g.partitioning(); int comm = p.community(5); System.out.println("Node 5 is in community " + comm); ``` -------------------------------- ### Run Clustering with Layer Limit Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainDetector.md Execute the clustering algorithm with a specified maximum number of layers to control depth in large graphs. ```java LayeredCommunityStructure result = detector.cluster(5); ``` -------------------------------- ### fromFile(String filename, boolean needsReIndexing) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/GraphBuilder.md Loads a graph from a CSV file, optionally handling non-integer node names by automatically re-indexing them. ```APIDOC ## fromFile(String filename, boolean needsReIndexing) ### Description Load a graph from a CSV file, optionally handling non-integer node names by automatically re-indexing them. ### Parameters - **filename** (String) - Required - Path to CSV file - **needsReIndexing** (boolean) - Required - If true, treat first two columns as string node names and re-index them to integers starting from 0 ### Returns - **Graph** - The constructed graph object. ### Throws - **IllegalStateException** - If file I/O fails - **LouvainException** - If matrix is asymmetric or duplicate edge found ### Example ```java Graph g = new GraphBuilder().fromFile("graphs/named_nodes.txt", true); ``` ``` -------------------------------- ### LouvainDetector(Graph g, long seed) Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/LouvainDetector.md Constructor to initialize the detector with a graph and a specific random seed for reproducibility. ```APIDOC ## LouvainDetector(Graph g, long seed) ### Description Initializes a new LouvainDetector instance with the provided graph and a specific random seed to ensure reproducible clustering results. ### Parameters - **g** (Graph) - Required - The input graph to cluster. - **seed** (long) - Required - Random seed for node processing order. ``` -------------------------------- ### GraphBuilder File Loading Method Signature Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Defines the parameters for loading a graph from a file, including indexing options and character encoding. ```java public Graph fromFile( String filename, boolean needsReIndexing, String delimiter, Charset charset ) ``` -------------------------------- ### Define LouvainSelector class Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/types.md Runs Louvain multiple times and returns the best result. ```java package com.github.neiljustice.louvain.clustering; public class LouvainSelector implements Clusterer { public LouvainSelector(String fileToRead, String fileToWrite) public LayeredCommunityStructure cluster() public LayeredCommunityStructure cluster(int times) } ``` -------------------------------- ### Configure LouvainDetector Clustering Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Method definition for clustering with a specified layer limit. ```java public LayeredCommunityStructure cluster(int maxLayers) ``` -------------------------------- ### GraphBuilder.build() Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/GraphBuilder.md Constructs and returns the Graph object after data has been loaded or added. ```APIDOC ## Method: build() ### Description Construct and return the `Graph` object. This method must be called after data has been loaded or manually added to the builder. ### Signature `public Graph build()` ### Returns - **Graph** - The constructed graph object. ### Example ```java Graph g = new GraphBuilder() .fromFile("data.csv") .build(); ``` ``` -------------------------------- ### Batch Process Graphs Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/configuration.md Create separate LouvainDetector instances for each graph when processing multiple graphs in a loop. ```java List graphs = loadGraphs(...); for (Graph g : graphs) { LouvainDetector detector = new LouvainDetector(g); LayeredCommunityStructure result = detector.cluster(); processResult(result); } ``` -------------------------------- ### Iterate Over Non-Zero Entries Source: https://github.com/neil-justice/louvain/blob/master/_autodocs/api-reference/SparseIntMatrix.md Traverse all non-zero entries in the matrix. Note that this triggers internal compression. ```java for (SparseIntMatrix.Iterator it = matrix.iterator(); it.hasNext(); ) { it.advance(); int x = it.x(); int y = it.y(); int value = it.value(); System.out.println("m[" + x + "][" + y + "] = " + value); } ```