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

graphty-org / graphty-element / 20514590651

26 Dec 2025 02:37AM UTC coverage: 70.559% (-0.3%) from 70.836%
20514590651

push

github

apowers313
ci: fix npm ci

9591 of 13363 branches covered (71.77%)

Branch coverage included in aggregate %.

25136 of 35854 relevant lines covered (70.11%)

6233.71 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";
15✔
2
import {z} from "zod/v4";
15✔
3

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

10
/**
11
 * Zod-based options schema for Label Propagation algorithm
12
 */
13
export const labelPropagationOptionsSchema = defineOptions({
15✔
14
    maxIterations: {
15✔
15
        schema: z.number().int().min(1).max(500).default(100),
15✔
16
        meta: {
15✔
17
            label: "Max Iterations",
15✔
18
            description: "Maximum label propagation rounds",
15✔
19
        },
15✔
20
    },
15✔
21
    randomSeed: {
15✔
22
        schema: z.number().int().min(0).max(2147483647).default(42),
15✔
23
        meta: {
15✔
24
            label: "Random Seed",
15✔
25
            description: "Seed for reproducible tie-breaking",
15✔
26
            advanced: true,
15✔
27
        },
15✔
28
    },
15✔
29
});
15✔
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
/**
42
 *
43
 */
44
export class LabelPropagationAlgorithm extends Algorithm<LabelPropagationOptions> {
15✔
45
    static namespace = "graphty";
19✔
46
    static type = "label-propagation";
120✔
47

48
    static zodOptionsSchema: ZodOptionsSchema = labelPropagationOptionsSchema;
120✔
49

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

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

94
    /**
95
     * Executes the label propagation algorithm on the graph
96
     *
97
     * Detects communities by propagating labels through the network.
98
     */
99
    async run(): Promise<void> {
19✔
100
        const g = this.graph;
3✔
101
        const nodes = Array.from(g.getDataManager().nodes.keys());
3✔
102

103
        if (nodes.length === 0) {
3!
104
            return;
×
105
        }
×
106

107
        // Get options from schema
108
        const {maxIterations, randomSeed} = this.schemaOptions;
3✔
109

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

113
        // Run Label Propagation algorithm - accepts Graph directly in new version
114
        const result = labelPropagation(graphData, {
3✔
115
            maxIterations,
3✔
116
            randomSeed,
3✔
117
        });
3✔
118

119
        // Store results on nodes
120
        for (const nodeId of nodes) {
3✔
121
            const communityId = result.communities.get(String(nodeId)) ?? 0;
43!
122
            this.addNodeResult(nodeId, "communityId", communityId);
43✔
123
        }
43✔
124

125
        // Store graph-level results
126
        this.addGraphResult("communityCount", countUniqueCommunities(result.communities));
3✔
127
        this.addGraphResult("converged", result.converged);
3✔
128
        this.addGraphResult("iterations", result.iterations);
3✔
129
    }
3✔
130
}
19✔
131

132
// Auto-register this algorithm when the module is imported
133
Algorithm.register(LabelPropagationAlgorithm);
15✔
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

© 2025 Coveralls, Inc