Skip to content

Contracts ​

Vertex identity ​

A vertex is the value stored as a Map key. NaN matches NaN, and +0 matches -0. getVertexKey is only an export label. Two vertices may share a label; export then assigns unique ids such as x, x#2, and x#1.

Adjacency snapshots ​

presenterAdjacencyLists returns ReadonlyMap<T, ReadonlySet<T>>. Building it copies every vertex and edge, which is O(V + E). Later edits of the graph do not appear in the snapshot.

Paths and weights ​

shortestPath with BFS counts edges. DFS in that call is the same unweighted search. The DFS iterator is a separate walk. Dijkstra adds finite weights that are >= 0 and scans every stored edge before the search. A negative, NaN, or infinite weight throws IllegalArgumentException and leaves a previous search in place.

Bellman–Ford allows a finite negative weight. The path exists only after the walk finishes without a reachable negative cycle. That cycle fails every goal, including the start. A cycle outside the reached set does not. A negative loop on a reached vertex is that cycle even when distance + weight === distance, as with 1e16 and -1. A* requires finite weights >= 0. Its heuristic is checked on every vertex and every directed arc before the search, which is O(V + E) even when the goal is close. The goal's distance is final once the goal leaves the queue. hasPath with either new mode uses BFS.

A distance, a priority g + h, and a consistency sum weight + h must be exact in IEEE-754: (left + right) - left === right and (left + right) - right === left. Overflow and an inexact sum throw IllegalArgumentException. A reachable negative cycle throws IllegalStateException when its own weight is negative, including an inexact distance along that cycle, either entry vertex, and an entry weight of 1e16 or -1e16. The proof adds those weights exactly. The returned path cost stays an exact IEEE-754 sum. A parent may be undefined. An error raised while walking sticks until the next initIterator. A check that fails before the new run starts leaves the previous path in place. The snapshot copies the array from vertices(), so a later edit of that array does not change the run. A zero A* heuristic matches Dijkstra when those sums are exact.

A graph from outside this package is usable when it implements IGraph, including isDirected(), forEachNeighbor, and forEachEdge. Direction is read from isDirected(), not from instanceof.

Memoized JSON keys ​

memoize accepts MemoJson: JSON objects, arrays, strings, finite numbers, true, false, and null. Key order does not matter. -0 and +0 share one entry. An own __proto__ key stays distinct from {}. The wrapper rejects undefined, holes, non-finite numbers, bigint, functions, symbols, cycles, Map, Set, Date, class instances, and any toJSON.

Numbers, matrices, and capacity ​

Factorial and Fibonacci accept integers >= 0 and return Infinity on overflow. roundNumber keeps a finite input finite when scaling would overflow. randomizeNumberInRange requires an integer interval [min, max) with min < max.

createGraphFromMatrix requires a dense label list whose length matches a square 0/1 matrix. An explicit undefined label is one vertex. A hole is rejected. An undirected matrix must be symmetric.

Omitted capacity is unlimited. Any other capacity is a positive integer. pushFromArray inserts the whole array or leaves the collection unchanged.

MIT Licensed