Overview of Graphology
masterGraph object. It provides a unified interface designed to support various types of graphs within a single ecosystem.repository·master·Indexed 22 days ago
https://github.com/graphology/graphologyA multipurpose Graph object for JavaScript and TypeScript supporting directed, undirected, and mixed graphs through a unified interface. It features a comprehensive standard library of graph theory algorithms, utilities, and an event system suitable for interactive visualizations. The ecosystem includes specialized packages such as graphology-assertions for graph comparison, graphology-bipartite for bipartite graph functions, graphology-canvas for rendering, and graphology-communities-leiden for community detection.
Graph object. It provides a unified interface designed to support various types of graphs within a single ecosystem.The graphology-indices library provides low-level indexation structures designed to optimize graph computations within the broader graphology ecosystem. Note that this library is intended for use by other graphology libraries and is not primarily designed for direct end-user consumption, which is why it lacks extensive documentation.
Currently exposed indices include:
graphology provides a robust and multipurpose Graph object for JavaScript and TypeScript.
Key features include:
sigma.js).While all edges in a graphology instance must have a key, you don't always have to provide one manually. The addEdge() method acts as syntactic sugar: it automatically generates a unique key for the edge and returns it to you.
This is particularly useful for simple use-cases where you don't want to manage edge IDs manually. The generated key is a permanent part of the edge and will persist if the graph is serialized and reloaded.
When a Graph is serialized, it is represented as an object containing attributes, options, nodes, and edges.
A node is an object with:
key (any): The node's unique identifier.attributes ([object]): The node's attributes (optional/nullable).An edge is an object with:
key ([any]): The edge's unique identifier (optional/nullable on import).source (any): The source node key.target (any): The target node key.attributes ([object]): The edge's attributes (optional/nullable).undirected ([boolean]): Whether the edge is undirected (optional/nullable).A full graph object contains:
attributes (object): Graph-level attributes.options (object): Graph configuration including allowSelfLoops, multi, and type.nodes (object): A list of serialized nodes.edges (object): A list of serialized edges.// Example of a serialized graph structure
{
attributes: { name: 'My Graph' },
options: { allowSelfLoops: true, multi: false, type: 'mixed' },
nodes: [{ key: 'Thomas' }, { key: 'Eric' }],
edges: [
{
key: 'T->E',
source: 'Thomas',
target: 'Eric',
attributes: { type: 'KNOWS' }
}
]
}In graphology, both nodes and edges are represented by keys. The graph coerces all provided keys into strings, similar to how native JavaScript objects behave.
Important implications:
"[object Object]".For undirected edges, the extremities (source and target) are recorded in the order they were first provided.
When iterating over undirected edges using forEachUndirectedEdge(node, callback), the source and target arguments in the callback are guaranteed to be consistent (the source method will always return the same node). However, the source node might not necessarily be the node you are currently iterating from.
Example behavior:
graph.forEachUndirectedEdge(node, (edge, attr, source, target) => {
console.log(node === source); // Might be true or false
});The graphology-indices library provides low-level indexation structures designed to optimize graph computations within the broader graphology ecosystem.
Note: This library is primarily intended for use by other graphology libraries to speed up specific algorithms. It is not designed as a primary consumer-facing API and lacks extensive documentation.
Currently exposed indices include:
A Graph instance acts as a Node.js-like event emitter. You can listen to specific events to react to changes in the graph structure or attributes. This is useful for synchronizing the graph with a UI (rendering) or maintaining external indexes.
Important Note: All emitted payloads are objects containing various keys related to the event.
By convention, if a method's return value is not documented, it returns the graph instance itself to allow for chaining.
Exception: To support the "get/has" pattern and avoid unnecessary graph reads during construction, addNode() and addEdge() return the node or edge key rather than the graph instance.
graphology throws errors instead of failing silently when an inconsistent operation is attempted. This is designed to help you debug quickly. For example, if you attempt to use addUndirectedEdge on a DirectedGraph instance, the error message will explicitly suggest using addEdge or addDirectedEdge instead.
import {DirectedGraph} from 'graphology';
const graph = new DirectedGraph();
graph.addNode('Lucy');
graph.addNode('Catherine');
// This throws an error:
graph.addUndirectedEdge('Lucy', 'Catherine');
// Error: `DirectedGraph.addUndirectedEdge: You cannot add an undirected edge.
to a directed graph Use the #.addEdge or #.addDirectedEdge method instead.`mutual edges are a specific type. Currently, graphology-gexf parses these as undirected edges rather than two separate directed edges to avoid potential key conflicts.