• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

graphty-org / graphty-element / 20390753610

20 Dec 2025 06:53AM UTC coverage: 82.423% (-1.2%) from 83.666%
20390753610

push

github

apowers313
Merge branch 'master' of https://github.com/graphty-org/graphty-element

5162 of 6088 branches covered (84.79%)

Branch coverage included in aggregate %.

24775 of 30233 relevant lines covered (81.95%)

6480.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.92
/src/algorithms/LabelPropagationAlgorithm.ts
1
import {labelPropagation} from "@graphty/algorithms";
3✔
2
import {z} from "zod/v4";
3✔
3

4
import {defineOptions, type OptionsSchema as ZodOptionsSchema, type SuggestedStylesConfig} from "../config";
3✔
5
import {Algorithm} from "./Algorithm";
3✔
6
import type {OptionsSchema} from "./types/OptionSchema";
7
import {countUniqueCommunities} from "./utils/communityUtils";
3✔
8
import {toAlgorithmGraph} from "./utils/graphConverter";
3✔
9

10
/**
11
 * Zod-based options schema for Label Propagation algorithm
12
 */
13
export const labelPropagationOptionsSchema = defineOptions({
3✔
14
    maxIterations: {
3✔
15
        schema: z.number().int().min(1).max(500).default(100),
3✔
16
        meta: {
3✔
17
            label: "Max Iterations",
3✔
18
            description: "Maximum label propagation rounds",
3✔
19
        },
3✔
20
    },
3✔
21
    randomSeed: {
3✔
22
        schema: z.number().int().min(0).max(2147483647).default(42),
3✔
23
        meta: {
3✔
24
            label: "Random Seed",
3✔
25
            description: "Seed for reproducible tie-breaking",
3✔
26
            advanced: true,
3✔
27
        },
3✔
28
    },
3✔
29
});
3✔
30

31
/**
32
 * Options for the Label Propagation community detection algorithm
33
 */
34
export interface LabelPropagationOptions extends Record<string, unknown> {
35
    /** Maximum label propagation rounds */
36
    maxIterations: number;
37
    /** Seed for reproducible tie-breaking */
38
    randomSeed: number;
39
}
40

41
export class LabelPropagationAlgorithm extends Algorithm<LabelPropagationOptions> {
3✔
42
    static namespace = "graphty";
7✔
43
    static type = "label-propagation";
108✔
44

45
    static zodOptionsSchema: ZodOptionsSchema = labelPropagationOptionsSchema;
108✔
46

47
    static optionsSchema: OptionsSchema = {
108✔
48
        maxIterations: {
108✔
49
            type: "integer",
108✔
50
            default: 100,
108✔
51
            label: "Max Iterations",
108✔
52
            description: "Maximum label propagation rounds",
108✔
53
            min: 1,
108✔
54
            max: 500,
108✔
55
        },
108✔
56
        randomSeed: {
108✔
57
            type: "integer",
108✔
58
            default: 42,
108✔
59
            label: "Random Seed",
108✔
60
            description: "Seed for reproducible tie-breaking",
108✔
61
            min: 0,
108✔
62
            max: 2147483647,
108✔
63
            advanced: true,
108✔
64
        },
108✔
65
    };
108✔
66

67
    static suggestedStyles = (): SuggestedStylesConfig => ({
108✔
68
        layers: [
11✔
69
            {
11✔
70
                node: {
11✔
71
                    selector: "",
11✔
72
                    style: {
11✔
73
                        enabled: true,
11✔
74
                    },
11✔
75
                    calculatedStyle: {
11✔
76
                        inputs: ["algorithmResults.graphty.label-propagation.communityId"],
11✔
77
                        output: "style.texture.color",
11✔
78
                        expr: "{ return StyleHelpers.color.categorical.pastel(arguments[0] ?? 0) }",
11✔
79
                    },
11✔
80
                },
11✔
81
                metadata: {
11✔
82
                    name: "Label Propagation - Pastel Colors",
11✔
83
                    description: "8 soft pastel community colors",
11✔
84
                },
11✔
85
            },
11✔
86
        ],
11✔
87
        description: "Visualizes communities detected via fast label propagation",
11✔
88
        category: "grouping",
11✔
89
    });
11✔
90

91
    // eslint-disable-next-line @typescript-eslint/require-await
92
    async run(): Promise<void> {
7✔
93
        const g = this.graph;
3✔
94
        const nodes = Array.from(g.getDataManager().nodes.keys());
3✔
95

96
        if (nodes.length === 0) {
3!
97
            return;
×
98
        }
×
99

100
        // Get options from schema
101
        const {maxIterations, randomSeed} = this.schemaOptions;
3✔
102

103
        // Convert to @graphty/algorithms Graph format (undirected for community detection)
104
        const graphData = toAlgorithmGraph(g, {addReverseEdges: false});
3✔
105

106
        // Run Label Propagation algorithm - accepts Graph directly in new version
107
        const result = labelPropagation(graphData, {
3✔
108
            maxIterations,
3✔
109
            randomSeed,
3✔
110
        });
3✔
111

112
        // Store results on nodes
113
        for (const nodeId of nodes) {
3✔
114
            const communityId = result.communities.get(String(nodeId)) ?? 0;
43!
115
            this.addNodeResult(nodeId, "communityId", communityId);
43✔
116
        }
43✔
117

118
        // Store graph-level results
119
        this.addGraphResult("communityCount", countUniqueCommunities(result.communities));
3✔
120
        this.addGraphResult("converged", result.converged);
3✔
121
        this.addGraphResult("iterations", result.iterations);
3✔
122
    }
3✔
123
}
7✔
124

125
// Auto-register this algorithm when the module is imported
126
Algorithm.register(LabelPropagationAlgorithm);
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc