Skip to content
This repository was archived by the owner on Feb 27, 2025. It is now read-only.

Commit cebeb92

Browse files
committed
chore: updating types
Signed-off-by: Pawel Psztyc <[email protected]>
1 parent 47e1be5 commit cebeb92

26 files changed

+308
-114
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"Pettitt",
66
"Warshall",
77
"chainable",
8+
"eqls",
89
"graphlib",
910
"heapify",
1011
"postorder",

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@api-modeling/graphlib",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "A directed and undirected multi-graph library",
55
"author": "Chris Pettitt <[email protected]>",
66
"license": "MIT",

src/alg/components.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
import { Graph } from "../graph";
22
import { NodeIdentifier } from "../types";
33

4+
/**
5+
* Finds all connected components in a graph and returns an array of these components.
6+
* Each component is itself an array that contains the ids of nodes in the component.
7+
* Complexity: O(|V|).
8+
*
9+
* @argument graph - graph to find components in.
10+
* @returns array of nodes list representing components
11+
*/
412
export default function components(g: Graph): NodeIdentifier[][];

src/alg/components.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
/** @typedef {import('../types').NodeIdentifier} NodeIdentifier */
33

44
/**
5-
* @param {Graph} g
6-
* @returns {NodeIdentifier[][]}
5+
* Finds all connected components in a graph and returns an array of these components.
6+
* Each component is itself an array that contains the ids of nodes in the component.
7+
* Complexity: O(|V|).
8+
*
9+
* @param {Graph} g graph to find components in.
10+
* @returns {NodeIdentifier[][]} array of nodes list representing components
711
*/
812
export default function components(g) {
913

src/alg/dijkstra-all.d.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
import { Graph } from "../graph.js";
2-
import { Edge, FloydWarshallItem, NodeIdentifier } from "../types.js";
2+
import { Edge, NodePath, NodeIdentifier } from "../types.js";
33

4-
export default function dijkstraAll(g: Graph, weightFunc?: (edge: Edge) => number, edgeFunc?: (v: NodeIdentifier) => Edge[]): Record<NodeIdentifier, Record<NodeIdentifier, FloydWarshallItem>>;
4+
/**
5+
* This function finds the shortest path from each node to every other reachable node in
6+
* the graph. It is similar to alg.dijkstra, but instead of returning a single-source
7+
* array, it returns a mapping of source -> alg.dijksta(g, source, weightFn, edgeFn).
8+
* Complexity: O(|V| * (|E| + |V|) * log |V|).
9+
*
10+
* @argument graph graph where to search paths.
11+
* @argument weightFn function which takes edge e and returns the weight of it. If no weightFn
12+
* is supplied then each edge is assumed to have a weight of 1. This function throws an
13+
* Error if any of the traversed edges have a negative edge weight.
14+
* @argument edgeFn function which takes a node v and returns the ids of all edges incident to it
15+
* for the purposes of shortest path traversal. By default this function uses the graph.outEdges.
16+
* @returns shortest paths map.
17+
*/
18+
export default function dijkstraAll(g: Graph, weightFunc?: (edge: Edge) => number, edgeFunc?: (v: NodeIdentifier) => Edge[]): Record<NodeIdentifier, Record<NodeIdentifier, NodePath>>;

src/alg/dijkstra-all.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ import dijkstra from "./dijkstra.js";
33
/** @typedef {import('../graph').Graph} Graph */
44
/** @typedef {import('../types').NodeIdentifier} NodeIdentifier */
55
/** @typedef {import('../types').Edge} Edge */
6-
/** @typedef {import('../types').FloydWarshallItem} FloydWarshallItem */
6+
/** @typedef {import('../types').NodePath} NodePath */
77

88
/**
9+
* This function finds the shortest path from each node to every other reachable node in
10+
* the graph. It is similar to alg.dijkstra, but instead of returning a single-source
11+
* array, it returns a mapping of source -> alg.dijksta(g, source, weightFn, edgeFn).
12+
* Complexity: O(|V| * (|E| + |V|) * log |V|).
13+
*
914
* @param {Graph} g
1015
* @param {(edge: Edge) => number=} weightFunc
1116
* @param {(v: NodeIdentifier) => Edge[]=} edgeFunc
12-
* @returns {Record<NodeIdentifier, Record<NodeIdentifier, FloydWarshallItem>>}
17+
* @returns {Record<NodeIdentifier, Record<NodeIdentifier, NodePath>>}
1318
*/
1419
export default function dijkstraAll(g, weightFunc, edgeFunc) {
15-
/** @type Record<NodeIdentifier, Record<NodeIdentifier, FloydWarshallItem>> */
20+
/** @type Record<NodeIdentifier, Record<NodeIdentifier, NodePath>> */
1621
const result = {};
1722
const ids = g.nodes();
1823
ids.forEach((id) => {

src/alg/dijkstra.d.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
import { Graph } from "../graph";
2-
import { Edge, FloydWarshallItem, NodeIdentifier } from "../types";
2+
import { Edge, NodePath, NodeIdentifier } from "../types";
33

4-
export default function dijkstra(g: Graph, source: NodeIdentifier, weightFn?: ((edge: Edge) => number), edgeFn?: ((v: NodeIdentifier) => Edge[])): Record<NodeIdentifier, FloydWarshallItem>;
4+
/**
5+
* This function is an implementation of Dijkstra's algorithm which finds the shortest
6+
* path from source to all other nodes in graph. This function returns a map of
7+
* v -> { distance, predecessor }. The distance property holds the sum of the weights
8+
* from source to v along the shortest path or Number.POSITIVE_INFINITY if there is no path
9+
* from source. The predecessor property can be used to walk the individual elements of the
10+
* path from source to v in reverse order.
11+
* Complexity: O((|E| + |V|) * log |V|).
12+
*
13+
* @argument graph - graph where to search pathes.
14+
* @argument source - node to start pathes from.
15+
* @argument weightFn - function which takes edge e and returns the weight of it. If no weightFn
16+
* is supplied then each edge is assumed to have a weight of 1. This function throws an
17+
* Error if any of the traversed edges have a negative edge weight.
18+
* @argument edgeFn - function which takes a node v and returns the ids of all edges incident to it
19+
* for the purposes of shortest path traversal. By default this function uses the graph.outEdges.
20+
* @returns shortest pathes map that starts from node source
21+
*/
22+
export default function dijkstra(g: Graph, source: NodeIdentifier, weightFn?: ((edge: Edge) => number), edgeFn?: ((v: NodeIdentifier) => Edge[])): Record<NodeIdentifier, NodePath>;

src/alg/dijkstra.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { PriorityQueue } from '../data/PriorityQueue.js';
33
/** @typedef {import('../graph').Graph} Graph */
44
/** @typedef {import('../types').Edge} Edge */
55
/** @typedef {import('../types').NodeIdentifier} NodeIdentifier */
6-
/** @typedef {import('../types').FloydWarshallItem} FloydWarshallItem */
6+
/** @typedef {import('../types').NodePath} NodePath */
77

88
const DEFAULT_WEIGHT_FUNC = () => 1;
99

@@ -13,14 +13,14 @@ const DEFAULT_WEIGHT_FUNC = () => 1;
1313
* @param {string} source
1414
* @param {(edge: Edge) => number} weightFn
1515
* @param {(v: NodeIdentifier) => Edge[]} edgeFn
16-
* @returns {Record<NodeIdentifier, FloydWarshallItem>}
16+
* @returns {Record<NodeIdentifier, NodePath>}
1717
*/
1818
function runDijkstra(g, source, weightFn, edgeFn) {
19-
/** @type {Record<NodeIdentifier, FloydWarshallItem>} */
19+
/** @type {Record<NodeIdentifier, NodePath>} */
2020
const results = {};
2121
const pq = new PriorityQueue();
2222
let v;
23-
/** @type FloydWarshallItem */
23+
/** @type NodePath */
2424
let vEntry;
2525

2626
/**
@@ -64,11 +64,22 @@ function runDijkstra(g, source, weightFn, edgeFn) {
6464
}
6565

6666
/**
67-
* @param {Graph} g
68-
* @param {NodeIdentifier} source
69-
* @param {((edge: Edge) => number)=} weightFn
70-
* @param {((v: NodeIdentifier) => Edge[])=} edgeFn
71-
* @returns {Record<NodeIdentifier, FloydWarshallItem>}
67+
* This function is an implementation of Dijkstra's algorithm which finds the shortest
68+
* path from source to all other nodes in graph. This function returns a map of
69+
* v -> { distance, predecessor }. The distance property holds the sum of the weights
70+
* from source to v along the shortest path or Number.POSITIVE_INFINITY if there is no path
71+
* from source. The predecessor property can be used to walk the individual elements of the
72+
* path from source to v in reverse order.
73+
* Complexity: O((|E| + |V|) * log |V|).
74+
*
75+
* @param {Graph} g - graph where to search paths.
76+
* @param {NodeIdentifier} source node to start paths from.
77+
* @param {((edge: Edge) => number)=} weightFn function which takes edge e and returns the weight of it. If no weightFn
78+
* is supplied then each edge is assumed to have a weight of 1. This function throws an
79+
* Error if any of the traversed edges have a negative edge weight.
80+
* @param {((v: NodeIdentifier) => Edge[])=} edgeFn function which takes a node v and returns the ids of all edges incident to it
81+
* for the purposes of shortest path traversal. By default this function uses the graph.outEdges.
82+
* @returns {Record<NodeIdentifier, NodePath>} shortest paths map that starts from node source
7283
*/
7384
export default function dijkstra(g, source, weightFn, edgeFn) {
7485
return runDijkstra(g, String(source), weightFn || DEFAULT_WEIGHT_FUNC, edgeFn || (v => g.outEdges(v)));

src/alg/find-cycles.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
import { Graph } from "../graph";
22
import { NodeIdentifier } from "../types";
33

4+
/**
5+
* Given a Graph, graph, this function returns all nodes that are part of a cycle. As there
6+
* may be more than one cycle in a graph this function return an array of these cycles,
7+
* where each cycle is itself represented by an array of ids for each node involved in
8+
* that cycle. Method alg.isAcyclic is more efficient if you only need to determine whether a graph has a
9+
* cycle or not.
10+
* Complexity: O(|V| + |E|).
11+
*
12+
* @argument graph - graph where to search cycles.
13+
* @returns cycles list.
14+
*/
415
export default function findCycles(g: Graph): NodeIdentifier[][];

0 commit comments

Comments
 (0)