Skip to content

@graphty/graphty-element / index / Algorithm

Abstract Class: Algorithm<TOptions> ​

Defined in: graphty-element/src/algorithms/Algorithm.ts:287

Base class for all graph algorithms

Example ​

typescript
// Algorithm with options
interface PageRankOptions {
    dampingFactor: number;
    maxIterations: number;
}

class PageRankAlgorithm extends Algorithm\<PageRankOptions\> {
    static optionsSchema: OptionsSchema = {
        dampingFactor: { type: 'number', default: 0.85, ... },
        maxIterations: { type: 'integer', default: 100, ... }
    };

    async run(): Promise<void> {
        const { dampingFactor, maxIterations } = this.options;
        // ... use options
    }
}

Extended by ​

Type Parameters ​

TOptions ​

TOptions extends Record<string, unknown> = Record<string, unknown>

The options type for this algorithm (defaults to empty object)

Constructors ​

Constructor ​

new Algorithm<TOptions>(g, options?): Algorithm<TOptions>

Defined in: graphty-element/src/algorithms/Algorithm.ts:362

Creates a new algorithm instance

Parameters ​

g ​

Graph

The graph to run the algorithm on

options? ​

Partial<TOptions>

Optional configuration options (uses schema defaults if not provided)

Returns ​

Algorithm<TOptions>

Properties ​

namespace ​

static namespace: string

Defined in: graphty-element/src/algorithms/Algorithm.ts:289


optionsSchema ​

static optionsSchema: OptionsSchema = {}

Defined in: graphty-element/src/algorithms/Algorithm.ts:298

Options schema for this algorithm

Subclasses should override this to define their configurable options. An empty schema means the algorithm has no configurable options.

Deprecated ​

Use zodOptionsSchema instead for new implementations


scopeInput? ​

static optional scopeInput?: ScopeInputDeclaration

Defined in: graphty-element/src/algorithms/Algorithm.ts:320

Whether this algorithm computes over its run's scope. "subgraph": the run context's input hands the algorithm its scope (the compact snapshot, and the scope's masks over the full graph), and the planner estimates the run over the scope. Absent or "none": the whole graph, of whose values the element keeps the scope's, with a caveat saying so; the planner estimates and refuses the run as a whole-graph one.

THE ONE DECLARATION: the input, the caveat and the published descriptor.scopeInput all read it. Declare it only once every node list, edge read and count the class takes comes from the input, because an algorithm that lists its nodes some other way would compute over a scoped topology while reporting every node.


type ​

static type: string

Defined in: graphty-element/src/algorithms/Algorithm.ts:288


zodOptionsSchema? ​

static optional zodOptionsSchema?: OptionsSchema

Defined in: graphty-element/src/algorithms/Algorithm.ts:306

NEW: Zod-based options schema with rich metadata for UI generation.

Override in subclasses to define algorithm-specific options. This is the new unified system that provides both validation and UI metadata.

Accessors ​

namespace ​

Get Signature ​

get namespace(): string

Defined in: graphty-element/src/algorithms/Algorithm.ts:612

Gets the algorithm namespace

Returns ​

string

The algorithm namespace identifier


type ​

Get Signature ​

get type(): string

Defined in: graphty-element/src/algorithms/Algorithm.ts:604

Gets the algorithm type

Returns ​

string

The algorithm type identifier

Methods ​

get() ​

static get(g, namespace, type, options?): Algorithm<Record<string, unknown>> | null

Defined in: graphty-element/src/algorithms/Algorithm.ts:705

Gets an algorithm instance from the registry

Parameters ​

g ​

Graph

The graph to run the algorithm on

namespace ​

string

The algorithm namespace

type ​

string

The algorithm type

options? ​

Record<string, unknown>

Optional algorithm-specific options to pass to constructor

Returns ​

Algorithm<Record<string, unknown>> | null

A new instance of the algorithm, or null if not found


getClass() ​

static getClass(namespace, type): AlgorithmClass & AlgorithmStatics | null

Defined in: graphty-element/src/algorithms/Algorithm.ts:720

Gets an algorithm class from the registry

Parameters ​

namespace ​

string

The algorithm namespace

type ​

string

The algorithm type

Returns ​

AlgorithmClass & AlgorithmStatics | null

The algorithm class, or null if not found


getOptionsSchema() ​

static getOptionsSchema(): OptionsSchema

Defined in: graphty-element/src/algorithms/Algorithm.ts:729

Get the options schema for this algorithm

Returns ​

OptionsSchema

The options schema, or an empty object if no options defined

Deprecated ​

Use getZodOptionsSchema() instead


getRegisteredAlgorithms() ​

static getRegisteredAlgorithms(namespace?): string[]

Defined in: graphty-element/src/algorithms/Algorithm.ts:765

Get all registered algorithm names.

Parameters ​

namespace? ​

string

Optional namespace to filter by

Returns ​

string[]

Array of algorithm names in "namespace:type" format


getRegisteredTypes() ​

static getRegisteredTypes(): string[]

Defined in: graphty-element/src/algorithms/Algorithm.ts:788

Get all registered algorithm types. This method is provided for API consistency with DataSource.

Returns ​

string[]

Array of algorithm keys in "namespace:type" format

Since ​

1.5.0

Example ​

typescript
const types = Algorithm.getRegisteredTypes();
console.log('Available algorithms:', types);
// ['graphty:betweenness', 'graphty:closeness', 'graphty:degree', ...]

getZodOptionsSchema() ​

static getZodOptionsSchema(): ZodOptionsSchema

Defined in: graphty-element/src/algorithms/Algorithm.ts:748

Get the Zod-based options schema for this algorithm.

Returns ​

ZodOptionsSchema

The Zod options schema, or an empty object if no schema defined


hasOptions() ​

static hasOptions(): boolean

Defined in: graphty-element/src/algorithms/Algorithm.ts:739

Check if this algorithm has configurable options

Returns ​

boolean

true if the algorithm has at least one option defined

Deprecated ​

Use hasZodOptions() instead


hasZodOptions() ​

static hasZodOptions(): boolean

Defined in: graphty-element/src/algorithms/Algorithm.ts:756

Check if this algorithm has a Zod-based options schema.

Returns ​

boolean

true if the algorithm has a Zod options schema defined


publishResult() ​

publishResult(_context, runId, _fields?): Promise<RunResult | undefined>

Defined in: graphty-element/src/algorithms/Algorithm.ts:638

Compute this algorithm and publish what it produced as a result object.

This is the entry point the run machinery calls, and it is the one that makes an algorithm startable as a Run: it takes a signal it must throw from, a progress channel, a yield, and the run id the result is published under -- and it RETURNS the result rather than writing it somewhere a caller has to go looking for. run() is the 1.10 entry point beside it, which returns nothing and can be neither watched nor stopped.

The default refuses, because an algorithm that has not been migrated genuinely cannot answer a run: it publishes through side effects under its own names and has no result object to hand back. Both shipped families -- a metric and a declared algorithm -- override it.

Parameters ​

_context ​

RunControls

A signal, a progress channel and a yield.

runId ​

string

The id the result is published under.

_fields? ​

readonly FieldDescriptor[]

The catalogue's descriptors for this algorithm's fields, when the caller holds them.

Returns ​

Promise<RunResult | undefined>

The result, or undefined when there was nothing to compute.

Throws ​

A GraphtyError with code E_UNSUPPORTED when this algorithm has no result to publish.


register() ​

static register<T>(cls, options?): T

Defined in: graphty-element/src/algorithms/Algorithm.ts:661

Registers an algorithm class in the global registry

Type Parameters ​

T ​

T extends AlgorithmClass

Parameters ​

cls ​

T

The algorithm class to register

options? ​

RegisterOptions

Whether a different class under a key already taken throws instead of replacing it.

Returns ​

T

The registered algorithm class


run() ​

abstract run(g): Promise<void>

Defined in: graphty-element/src/algorithms/Algorithm.ts:616

Parameters ​

g ​

Graph

Returns ​

Promise<void>