@graphty/graphty-element / algorithms/Algorithm / Algorithm
Abstract Class: Algorithm<TOptions>
Defined in: src/algorithms/Algorithm.ts:93
Base class for all graph algorithms
Example
// 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
}
}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: src/algorithms/Algorithm.ts:147
Creates a new algorithm instance
Parameters
g
The graph to run the algorithm on
options?
Partial<TOptions>
Optional configuration options (uses schema defaults if not provided)
Returns
Algorithm<TOptions>
Properties
namespace
staticnamespace:string
Defined in: src/algorithms/Algorithm.ts:95
optionsSchema
staticoptionsSchema:OptionsSchema={}
Defined in: src/algorithms/Algorithm.ts:105
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
suggestedStyles?
staticoptionalsuggestedStyles:SuggestedStylesProvider
Defined in: src/algorithms/Algorithm.ts:96
type
statictype:string
Defined in: src/algorithms/Algorithm.ts:94
zodOptionsSchema?
staticoptionalzodOptionsSchema:OptionsSchema
Defined in: src/algorithms/Algorithm.ts:113
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: src/algorithms/Algorithm.ts:181
Gets the algorithm namespace
Returns
string
The algorithm namespace identifier
results
Get Signature
get results():
AdHocData
Defined in: src/algorithms/Algorithm.ts:189
Gets all algorithm results for nodes, edges, and graph
Returns
An object containing node, edge, and graph results
type
Get Signature
get type():
string
Defined in: src/algorithms/Algorithm.ts:173
Gets the algorithm type
Returns
string
The algorithm type identifier
Methods
addEdgeResult()
addEdgeResult(
edge,resultName,result):void
Defined in: src/algorithms/Algorithm.ts:249
Adds a result value for a specific edge
Parameters
edge
The edge to add the result to
resultName
string
The name of the result field
result
unknown
The result value to store
Returns
void
addGraphResult()
addGraphResult(
resultName,result):void
Defined in: src/algorithms/Algorithm.ts:259
Adds a result value for the graph
Parameters
resultName
string
The name of the result field
result
unknown
The result value to store
Returns
void
addNodeResult()
addNodeResult(
nodeId,resultName,result):void
Defined in: src/algorithms/Algorithm.ts:231
Adds a result value for a specific node
Parameters
nodeId
The ID of the node to add the result to
string | number
resultName
string
The name of the result field
result
unknown
The result value to store
Returns
void
get()
staticget(g,namespace,type):Algorithm<Record<string,unknown>> |null
Defined in: src/algorithms/Algorithm.ts:288
Gets an algorithm instance from the registry
Parameters
g
The graph to run the algorithm on
namespace
string
The algorithm namespace
type
string
The algorithm type
Returns
Algorithm<Record<string, unknown>> | null
A new instance of the algorithm, or null if not found
getClass()
staticgetClass(namespace,type):AlgorithmClass&AlgorithmStatics|null
Defined in: src/algorithms/Algorithm.ts:303
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()
staticgetOptionsSchema():OptionsSchema
Defined in: src/algorithms/Algorithm.ts:328
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()
staticgetRegisteredAlgorithms(namespace?):string[]
Defined in: src/algorithms/Algorithm.ts:365
Get all registered algorithm names.
Parameters
namespace?
string
Optional namespace to filter by
Returns
string[]
Array of algorithm names in "namespace:type" format
getRegisteredTypes()
staticgetRegisteredTypes():string[]
Defined in: src/algorithms/Algorithm.ts:388
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
const types = Algorithm.getRegisteredTypes();
console.log('Available algorithms:', types);
// ['graphty:betweenness', 'graphty:closeness', 'graphty:degree', ...]getSuggestedStyles()
staticgetSuggestedStyles():SuggestedStylesConfig|null
Defined in: src/algorithms/Algorithm.ts:319
Get suggested styles for this algorithm
Returns
SuggestedStylesConfig | null
The suggested styles configuration, or null if none defined
getZodOptionsSchema()
staticgetZodOptionsSchema():OptionsSchema
Defined in: src/algorithms/Algorithm.ts:347
Get the Zod-based options schema for this algorithm.
Returns
The Zod options schema, or an empty object if no schema defined
hasOptions()
statichasOptions():boolean
Defined in: src/algorithms/Algorithm.ts:338
Check if this algorithm has configurable options
Returns
boolean
true if the algorithm has at least one option defined
Deprecated
Use hasZodOptions() instead
hasSuggestedStyles()
statichasSuggestedStyles():boolean
Defined in: src/algorithms/Algorithm.ts:311
Check if this algorithm has suggested styles
Returns
boolean
true if suggested styles are defined
hasZodOptions()
statichasZodOptions():boolean
Defined in: src/algorithms/Algorithm.ts:355
Check if this algorithm has a Zod-based options schema.
Returns
boolean
true if the algorithm has a Zod options schema defined
register()
staticregister<T>(cls):T
Defined in: src/algorithms/Algorithm.ts:272
Registers an algorithm class in the global registry
Type Parameters
T
T extends AlgorithmClass
Parameters
cls
T
The algorithm class to register
Returns
T
The registered algorithm class
run()
abstractrun(g):Promise<void>
Defined in: src/algorithms/Algorithm.ts:212
Parameters
g
Returns
Promise<void>