npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@thi.ng/adjacency

v3.0.65

Published

Sparse & bitwise adjacency matrices, lists and selected traversal algorithms for directed & undirected graphs

Readme

@thi.ng/adjacency

npm version npm downloads Mastodon Follow

[!NOTE] This is one of 211 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.

🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️

About

Sparse & bitwise adjacency matrices, lists and selected traversal algorithms for directed & undirected graphs.

[!IMPORTANT] In July 2024 this package was restructured and split-up to extract some features into smaller more focused packages:

Graph implementations

The following types all implement the IGraph interface and support both directed & undirected graphs:

Traversals

Status

STABLE - used in production

Search or submit any issues for this package

Related packages

  • @thi.ng/dgraph - Type-agnostic directed acyclic graph (DAG) & graph operations

Installation

yarn add @thi.ng/adjacency

ESM import:

import * as adj from "@thi.ng/adjacency";

Browser ESM import:

<script type="module" src="https://esm.run/@thi.ng/adjacency"></script>

JSDelivr documentation

For Node.js REPL:

const adj = await import("@thi.ng/adjacency");

Package sizes (brotli'd, pre-treeshake): ESM: 2.53 KB

Dependencies

Note: @thi.ng/api is in most cases a type-only import (not used at runtime)

Usage examples

One project in this repo's /examples directory is using this package:

| Screenshot | Description | Live demo | Source | |:------------------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------|:-------------------------------------------------------|:------------------------------------------------------------------------------------| | | Poisson-disk shape-aware sampling, Voronoi & Minimum Spanning Tree visualization | Demo | Source |

API

Generated API docs

TODO

Basic usage

import { defAdjBitMatrix, type Edge } from "@thi.ng/adjacency";

// relationships
const rels = {
    a: ["b", "c"],
    b: ["d"],
    c: ["d", "e"],
    e: ["a", "d", "b"],
};

// form set of unique node IDs
const nodeIDs = [
    ...new Set(Object.entries(rels).flatMap(([id, rels]) => [id, ...rels])),
];

// the current adjacency matrix impls only support numeric node IDs
// therefore, we first map node names to numeric IDs
const index = new Map(nodeIDs.map((id, i) => [id, i]));

// transform relationships into sequence of edges (aka `[from,to]` tuples)
const edges = Object.entries(rels).flatMap(([id, rels]) =>
    rels.map((x) => <Edge>[index.get(id), index.get(x)])
);

// build adjacency matrix, treat as undirected graph
// edges can also be added/removed later
const graph = defAdjBitMatrix(nodeIDs.length, edges, true);

// graph queries
console.log("edges:", graph.numEdges(), "verts:", graph.numVertices());
// edges: 8 verts: 5

// check if vertex/node is present in graph
// (this is implementation specific and for the bitmatrix backed version here
// only true if the vertex has at least 1 edge...)
console.log(graph.hasVertex(index.get("d")!));
// true

// are `a` and `d` connected?
console.log(graph.hasEdge(index.get("a")!, index.get("d")!));
// false

// number of connected nodes for `a`
// (in directed graphs, there's also possibility to distinguish between in/out/inout)
console.log(graph.degree(index.get("a")!));
// 3

// neighbors of `a` (with reverse lookup of node names)
console.log(graph.neighbors(index.get("a")!).map((x) => nodeIDs[x]));
// [ 'b', 'c', 'e' ]

// serialize to GraphViz DOT format (see result visualization below)
console.log(graph.toDot(nodeIDs));
// graph g {
// "d"--"e";
// "c"--"d";
// "c"--"e";
// "b"--"d";
// "b"--"e";
// "a"--"b";
// "a"--"c";
// "a"--"e";
// }

// resize to new capacity & add add/remove vertices/edges
graph.resize(10);

graph.addEdge(4, 5);
graph.removeEdge(0, 1);

GraphViz visualization of the above example graph:

example graph

Authors

If this project contributes to an academic publication, please cite it as:

@misc{thing-adjacency,
  title = "@thi.ng/adjacency",
  author = "Karsten Schmidt and others",
  note = "https://thi.ng/adjacency",
  year = 2018
}

License

© 2018 - 2025 Karsten Schmidt // Apache License 2.0