or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

algorithms.mdgraph.mdindex.mdjson.md
tile.json

tessl/npm-graphlib

A directed and undirected multi-graph library with comprehensive graph algorithms

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/graphlib@2.1.x

To install, run

npx @tessl/cli install tessl/npm-graphlib@2.1.0

index.mddocs/

GraphLib

GraphLib is a JavaScript library for directed and undirected multi-graphs. It provides a comprehensive Graph data structure with support for compound graphs (hierarchical node relationships), multiple edges between nodes, and a complete suite of graph algorithms including shortest path, topological sorting, and strongly connected components.

Package Information

  • Package Name: graphlib
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install graphlib

Core Imports

import { Graph, json, alg, version } from "graphlib";

For CommonJS:

const { Graph, json, alg, version } = require("graphlib");

Basic Usage

import { Graph, alg } from "graphlib";

// Create a new directed graph
const g = new Graph();

// Add nodes with optional labels
g.setNode("a", "Node A");
g.setNode("b", "Node B");
g.setNode("c", "Node C");

// Add edges with optional labels
g.setEdge("a", "b", "Edge A->B");
g.setEdge("b", "c", "Edge B->C");
g.setEdge("a", "c", "Edge A->C");

// Query the graph
console.log(g.nodes()); // ["a", "b", "c"]
console.log(g.edges()); // [{v:"a", w:"b"}, {v:"b", w:"c"}, {v:"a", w:"c"}]

// Use algorithms
const shortestPaths = alg.dijkstra(g, "a");
console.log(shortestPaths.c.distance); // 2 (via b)

const topOrder = alg.topsort(g);
console.log(topOrder); // ["a", "b", "c"]

Architecture

GraphLib is built around several key components:

  • Graph Class: Core data structure supporting directed/undirected, simple/multi, and compound/flat graphs
  • Algorithm Suite: Comprehensive collection of graph algorithms with configurable weight and edge functions
  • JSON Serialization: Complete serialization/deserialization for graph persistence and network transfer
  • Lodash Integration: Selected utility functions for cross-platform compatibility

Capabilities

Graph Data Structure

Core graph data structure with comprehensive node and edge management, support for hierarchical relationships, and configurable graph properties.

class Graph {
  constructor(opts?: {
    directed?: boolean;
    multigraph?: boolean; 
    compound?: boolean;
  });
}

Graph Management

Graph Algorithms

Complete suite of graph algorithms including shortest paths, traversal methods, cycle detection, and topological sorting.

const alg = {
  components(g: Graph): string[][],
  dijkstra(g: Graph, source: string, weightFn?: Function, edgeFn?: Function): Object,
  dijkstraAll(g: Graph, weightFn?: Function, edgeFn?: Function): Object,
  dfs(g: Graph, vs: string | string[], order: "pre" | "post"): string[],
  findCycles(g: Graph): string[][],
  floydWarshall(g: Graph, weightFn?: Function, edgeFn?: Function): Object,
  isAcyclic(g: Graph): boolean,
  postorder(g: Graph, vs: string | string[]): string[],
  preorder(g: Graph, vs: string | string[]): string[],
  prim(g: Graph, weightFn: Function): Graph,
  tarjan(g: Graph): string[][],
  topsort(g: Graph): string[]
};

Graph Algorithms

JSON Serialization

Complete graph serialization and deserialization for persistence, network transfer, and interoperability.

const json = {
  write(g: Graph): Object,
  read(json: Object): Graph
};

JSON Serialization

Types

Core Types

// Edge object format used throughout the API
interface EdgeObj {
  v: string;        // Source node ID
  w: string;        // Target node ID
  name?: string;    // Optional edge name (for multigraphs)
}

// Graph options for constructor
interface GraphOptions {
  directed?: boolean;     // Whether graph is directed (default: true)
  multigraph?: boolean;   // Whether multiple edges allowed (default: false)
  compound?: boolean;     // Whether hierarchical nodes supported (default: false)
}

Version Information