@tomhundley/grafene
v1.0.0
Published
In-memory graph library with paths, cycles, components, MST, and PageRank
Downloads
121
Maintainers
Readme
@tomhundley/grafene
An in-memory graph library for Node.js. Build directed or undirected graphs, walk them, find shortest paths, detect cycles, compute components, spanning trees, and PageRank.
Zero runtime dependencies. Node 18+.
Install
npm install @tomhundley/grafeneQuick start
import { createGraph, shortestPath, topologicalSort } from "@tomhundley/grafene";
const g = createGraph({ directed: true });
g.addNode("build");
g.addNode("test");
g.addNode("deploy");
g.addEdge("build", "test", { weight: 1 });
g.addEdge("test", "deploy", { weight: 2 });
topologicalSort(g); // ["build", "test", "deploy"]
shortestPath(g, "build", "deploy");
// { path: ["build", "test", "deploy"], cost: 3 }Graph API
import { Graph } from "@tomhundley/grafene";
const g = new Graph({ directed: false });
g.addNode("a", { label: "Ada" });
g.addNode("b");
g.addEdge("a", "b", { weight: 4, data: { kind: "friend" } });
g.neighbors("a");
g.degree("a");
g.clone();
g.subgraph(["a", "b"]);
g.toJSON();
Graph.fromJSON(saved);Algorithms
| Function | Purpose |
| --- | --- |
| bfs / dfs | Walk from a start node |
| reachable | Nodes you can reach |
| shortestPath / dijkstra | Weighted shortest path |
| astar | Heuristic search |
| hasCycle | Cycle detection |
| topologicalSort | Order a DAG |
| connectedComponents | Undirected groups |
| stronglyConnectedComponents | Directed SCCs (Tarjan) |
| isBipartite | Two-color check |
| minimumSpanningTree | Kruskal MST |
| pageRank / density / degrees | Metrics |
import { connectedComponents, minimumSpanningTree, pageRank } from "@tomhundley/grafene";
const net = createGraph({ directed: false });
net.addNode("a");
net.addNode("b");
net.addNode("c");
net.addEdge("a", "b", { weight: 1 });
net.addEdge("b", "c", { weight: 2 });
net.addEdge("a", "c", { weight: 9 });
minimumSpanningTree(net).cost; // 3What it is for
Use Grafene when your data is a network: dependencies, maps, org charts, social links, routing. It is not a graph database server — it lives in one Node process.
License
MIT
