@graphty/algorithms 1.7.3 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/dist/algorithms.d.ts +2 -37
  2. package/dist/algorithms.js +736 -223
  3. package/dist/algorithms.js.map +1 -1
  4. package/dist/algorithms.standalone.js +24524 -0
  5. package/dist/algorithms.standalone.js.map +1 -0
  6. package/dist/src/algorithms/centrality/betweenness.d.ts +16 -11
  7. package/dist/src/algorithms/centrality/betweenness.d.ts.map +1 -1
  8. package/dist/src/algorithms/centrality/betweenness.js +17 -0
  9. package/dist/src/algorithms/centrality/betweenness.js.map +1 -1
  10. package/dist/src/core/graph.d.ts +10 -0
  11. package/dist/src/core/graph.d.ts.map +1 -1
  12. package/dist/src/core/graph.js +31 -2
  13. package/dist/src/core/graph.js.map +1 -1
  14. package/dist/src/index.d.ts +10 -0
  15. package/dist/src/index.d.ts.map +1 -1
  16. package/dist/src/index.js +7 -0
  17. package/dist/src/index.js.map +1 -1
  18. package/dist/src/indexed/accelerator.d.ts +161 -0
  19. package/dist/src/indexed/accelerator.d.ts.map +1 -0
  20. package/dist/src/indexed/accelerator.js +60 -0
  21. package/dist/src/indexed/accelerator.js.map +1 -0
  22. package/dist/src/indexed/bfs.d.ts +28 -0
  23. package/dist/src/indexed/bfs.d.ts.map +1 -0
  24. package/dist/src/indexed/bfs.js +39 -0
  25. package/dist/src/indexed/bfs.js.map +1 -0
  26. package/dist/src/indexed/common-neighbors.d.ts +17 -0
  27. package/dist/src/indexed/common-neighbors.d.ts.map +1 -0
  28. package/dist/src/indexed/common-neighbors.js +41 -0
  29. package/dist/src/indexed/common-neighbors.js.map +1 -0
  30. package/dist/src/indexed/components.d.ts +28 -0
  31. package/dist/src/indexed/components.d.ts.map +1 -0
  32. package/dist/src/indexed/components.js +58 -0
  33. package/dist/src/indexed/components.js.map +1 -0
  34. package/dist/src/indexed/dijkstra.d.ts +63 -0
  35. package/dist/src/indexed/dijkstra.d.ts.map +1 -0
  36. package/dist/src/indexed/dijkstra.js +98 -0
  37. package/dist/src/indexed/dijkstra.js.map +1 -0
  38. package/dist/src/indexed/index.d.ts +22 -0
  39. package/dist/src/indexed/index.d.ts.map +1 -0
  40. package/dist/src/indexed/index.js +22 -0
  41. package/dist/src/indexed/index.js.map +1 -0
  42. package/dist/src/indexed/mst.d.ts +26 -0
  43. package/dist/src/indexed/mst.d.ts.map +1 -0
  44. package/dist/src/indexed/mst.js +48 -0
  45. package/dist/src/indexed/mst.js.map +1 -0
  46. package/dist/src/indexed/pagerank.d.ts +30 -0
  47. package/dist/src/indexed/pagerank.d.ts.map +1 -0
  48. package/dist/src/indexed/pagerank.js +50 -0
  49. package/dist/src/indexed/pagerank.js.map +1 -0
  50. package/dist/src/indexed/structures/arc-source.d.ts +13 -0
  51. package/dist/src/indexed/structures/arc-source.d.ts.map +1 -0
  52. package/dist/src/indexed/structures/arc-source.js +25 -0
  53. package/dist/src/indexed/structures/arc-source.js.map +1 -0
  54. package/dist/src/indexed/structures/index.d.ts +9 -0
  55. package/dist/src/indexed/structures/index.d.ts.map +1 -0
  56. package/dist/src/indexed/structures/index.js +9 -0
  57. package/dist/src/indexed/structures/index.js.map +1 -0
  58. package/dist/src/indexed/structures/min-heap.d.ts +44 -0
  59. package/dist/src/indexed/structures/min-heap.d.ts.map +1 -0
  60. package/dist/src/indexed/structures/min-heap.js +112 -0
  61. package/dist/src/indexed/structures/min-heap.js.map +1 -0
  62. package/dist/src/indexed/structures/union-find.d.ts +39 -0
  63. package/dist/src/indexed/structures/union-find.d.ts.map +1 -0
  64. package/dist/src/indexed/structures/union-find.js +70 -0
  65. package/dist/src/indexed/structures/union-find.js.map +1 -0
  66. package/dist/src/indexed/to-snapshot.d.ts +38 -0
  67. package/dist/src/indexed/to-snapshot.d.ts.map +1 -0
  68. package/dist/src/indexed/to-snapshot.js +58 -0
  69. package/dist/src/indexed/to-snapshot.js.map +1 -0
  70. package/dist/tsconfig.tsbuildinfo +1 -1
  71. package/package.json +7 -3
  72. package/src/algorithms/centrality/betweenness.ts +36 -11
  73. package/src/core/graph.ts +35 -2
  74. package/src/index.ts +46 -0
  75. package/src/indexed/accelerator.ts +225 -0
  76. package/src/indexed/bfs.ts +57 -0
  77. package/src/indexed/common-neighbors.ts +46 -0
  78. package/src/indexed/components.ts +78 -0
  79. package/src/indexed/dijkstra.ts +134 -0
  80. package/src/indexed/index.ts +28 -0
  81. package/src/indexed/mst.ts +62 -0
  82. package/src/indexed/pagerank.ts +73 -0
  83. package/src/indexed/structures/arc-source.ts +25 -0
  84. package/src/indexed/structures/index.ts +9 -0
  85. package/src/indexed/structures/min-heap.ts +122 -0
  86. package/src/indexed/structures/union-find.ts +74 -0
  87. package/src/indexed/to-snapshot.ts +78 -0
@@ -0,0 +1,39 @@
1
+ import { INVALID_INDEX } from "@graphty/graph-format";
2
+ /**
3
+ * Breadth-first search over out-neighbours. Takes any `AdjacencyView`, so `s.reverse()` gives an
4
+ * in-neighbour BFS with no extra code.
5
+ * @param g - The adjacency to traverse
6
+ * @param start - The node index to start from
7
+ * @param options - Traversal options
8
+ * @returns The visit order, the parent array, the depth array and the visited count
9
+ * @public
10
+ */
11
+ export function breadthFirstSearch(g, start, options = {}) {
12
+ const { nodeCount, rowPtr, colIdx } = g;
13
+ const parent = new Uint32Array(nodeCount).fill(INVALID_INDEX);
14
+ const depth = new Uint32Array(nodeCount).fill(INVALID_INDEX);
15
+ const order = new Uint32Array(nodeCount);
16
+ const maxDepth = options.maxDepth ?? INVALID_INDEX;
17
+ let head = 0;
18
+ let tail = 0;
19
+ order[tail++] = start;
20
+ depth[start] = 0;
21
+ while (head < tail) {
22
+ const u = order[head++];
23
+ const d = depth[u];
24
+ if (d >= maxDepth) {
25
+ continue;
26
+ }
27
+ const end = rowPtr[u + 1];
28
+ for (let a = rowPtr[u]; a < end; a++) {
29
+ const v = colIdx[a];
30
+ if (depth[v] === INVALID_INDEX) {
31
+ depth[v] = d + 1;
32
+ parent[v] = u;
33
+ order[tail++] = v;
34
+ }
35
+ }
36
+ }
37
+ return { order: order.subarray(0, tail), parent, depth, visitedCount: tail };
38
+ }
39
+ //# sourceMappingURL=bfs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bfs.js","sourceRoot":"","sources":["../../../src/indexed/bfs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,aAAa,EAAY,MAAM,uBAAuB,CAAC;AAoBpF;;;;;;;;GAQG;AACH,MAAM,UAAU,kBAAkB,CAAC,CAAgB,EAAE,KAAa,EAAE,UAAsB,EAAE;IACxF,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;IACxC,MAAM,MAAM,GAAG,IAAI,WAAW,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IAC9D,MAAM,KAAK,GAAG,IAAI,WAAW,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IAC7D,MAAM,KAAK,GAAG,IAAI,WAAW,CAAC,SAAS,CAAC,CAAC;IACzC,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,aAAa,CAAC;IACnD,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,CAAC,IAAI,EAAE,CAAC,GAAG,KAAK,CAAC;IACtB,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACjB,OAAO,IAAI,GAAG,IAAI,EAAE,CAAC;QACjB,MAAM,CAAC,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;QACxB,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACnB,IAAI,CAAC,IAAI,QAAQ,EAAE,CAAC;YAChB,SAAS;QACb,CAAC;QACD,MAAM,GAAG,GAAG,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QAC1B,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;YACnC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;YACpB,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,aAAa,EAAE,CAAC;gBAC7B,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;gBACjB,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;gBACd,KAAK,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC;YACtB,CAAC;QACL,CAAC;IACL,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC;AACjF,CAAC"}
@@ -0,0 +1,17 @@
1
+ import type { GraphSnapshot } from "@graphty/graph-format";
2
+ /** Options of the index-based common-neighbour score. @public */
3
+ export interface CommonNeighborsOptions {
4
+ /** Intersect out(u) with in(v) instead of the two undirected rows. */
5
+ readonly directed?: boolean | undefined;
6
+ }
7
+ /**
8
+ * The number of distinct common neighbours of two nodes, by a merge of their sorted rows.
9
+ * @param s - The snapshot
10
+ * @param u - A node index
11
+ * @param v - A node index
12
+ * @param o - Options
13
+ * @returns The count of distinct common neighbours
14
+ * @public
15
+ */
16
+ export declare function commonNeighborsScore(s: GraphSnapshot, u: number, v: number, o?: CommonNeighborsOptions): number;
17
+ //# sourceMappingURL=common-neighbors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common-neighbors.d.ts","sourceRoot":"","sources":["../../../src/indexed/common-neighbors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE1E,iEAAiE;AACjE,MAAM,WAAW,sBAAsB;IACnC,sEAAsE;IACtE,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CAC3C;AAED;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,aAAa,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,GAAE,sBAA2B,GAAG,MAAM,CA4BnH"}
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The number of distinct common neighbours of two nodes, by a merge of their sorted rows.
3
+ * @param s - The snapshot
4
+ * @param u - A node index
5
+ * @param v - A node index
6
+ * @param o - Options
7
+ * @returns The count of distinct common neighbours
8
+ * @public
9
+ */
10
+ export function commonNeighborsScore(s, u, v, o = {}) {
11
+ const fwd = s;
12
+ const bwd = o.directed === true ? s.reverse() : s;
13
+ let i = fwd.rowPtr[u];
14
+ const iEnd = fwd.rowPtr[u + 1];
15
+ let j = bwd.rowPtr[v];
16
+ const jEnd = bwd.rowPtr[v + 1];
17
+ let count = 0;
18
+ while (i < iEnd && j < jEnd) {
19
+ const a = fwd.colIdx[i];
20
+ const b = bwd.colIdx[j];
21
+ if (a === b) {
22
+ count++;
23
+ i++;
24
+ j++;
25
+ while (i < iEnd && fwd.colIdx[i] === a) {
26
+ i++;
27
+ }
28
+ while (j < jEnd && bwd.colIdx[j] === b) {
29
+ j++;
30
+ }
31
+ }
32
+ else if (a < b) {
33
+ i++;
34
+ }
35
+ else {
36
+ j++;
37
+ }
38
+ }
39
+ return count;
40
+ }
41
+ //# sourceMappingURL=common-neighbors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common-neighbors.js","sourceRoot":"","sources":["../../../src/indexed/common-neighbors.ts"],"names":[],"mappings":"AAQA;;;;;;;;GAQG;AACH,MAAM,UAAU,oBAAoB,CAAC,CAAgB,EAAE,CAAS,EAAE,CAAS,EAAE,IAA4B,EAAE;IACvG,MAAM,GAAG,GAAkB,CAAC,CAAC;IAC7B,MAAM,GAAG,GAAkB,CAAC,CAAC,QAAQ,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IACjE,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IACtB,MAAM,IAAI,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAC/B,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IACtB,MAAM,IAAI,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAC/B,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,OAAO,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,IAAI,EAAE,CAAC;QAC1B,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QACxB,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QACxB,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACV,KAAK,EAAE,CAAC;YACR,CAAC,EAAE,CAAC;YACJ,CAAC,EAAE,CAAC;YACJ,OAAO,CAAC,GAAG,IAAI,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;gBACrC,CAAC,EAAE,CAAC;YACR,CAAC;YACD,OAAO,CAAC,GAAG,IAAI,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;gBACrC,CAAC,EAAE,CAAC;YACR,CAAC;QACL,CAAC;aAAM,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;YACf,CAAC,EAAE,CAAC;QACR,CAAC;aAAM,CAAC;YACJ,CAAC,EAAE,CAAC;QACR,CAAC;IACL,CAAC;IACD,OAAO,KAAK,CAAC;AACjB,CAAC"}
@@ -0,0 +1,28 @@
1
+ import { type GraphSnapshot, type U32 } from "@graphty/graph-format";
2
+ /** A partition of the node set (graph-format design 14.2's result table, line 3738). @public */
3
+ export interface LabelResult {
4
+ /** Dense label per node index, in first-seen order. */
5
+ readonly labels: U32;
6
+ /** Number of distinct labels. */
7
+ readonly count: number;
8
+ /**
9
+ * Node indices grouped by label, computed once and cached.
10
+ * @returns One array per label
11
+ */
12
+ groups(): U32[];
13
+ }
14
+ /**
15
+ * Connected components of an UNDIRECTED snapshot.
16
+ * @param s - An undirected snapshot
17
+ * @returns The partition
18
+ * @public
19
+ */
20
+ export declare function connectedComponents(s: GraphSnapshot): LabelResult;
21
+ /**
22
+ * Weakly connected components: the same union-find pass with the direction check dropped.
23
+ * @param s - Any snapshot
24
+ * @returns The partition
25
+ * @public
26
+ */
27
+ export declare function weaklyConnectedComponents(s: GraphSnapshot): LabelResult;
28
+ //# sourceMappingURL=components.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../../../src/indexed/components.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,GAAG,EAAE,MAAM,uBAAuB,CAAC;AAIrE,gGAAgG;AAChG,MAAM,WAAW,WAAW;IACxB,uDAAuD;IACvD,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC;IACrB,iCAAiC;IACjC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,MAAM,IAAI,GAAG,EAAE,CAAC;CACnB;AAuCD;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,aAAa,GAAG,WAAW,CAOjE;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,EAAE,aAAa,GAAG,WAAW,CAEvE"}
@@ -0,0 +1,58 @@
1
+ import { IntUnionFind } from "./structures/union-find.js";
2
+ function withGroups(labels, count) {
3
+ let cached = null;
4
+ return {
5
+ labels,
6
+ count,
7
+ groups() {
8
+ if (cached === null) {
9
+ const sizes = new Uint32Array(count);
10
+ for (let i = 0; i < labels.length; i++) {
11
+ sizes[labels[i]]++;
12
+ }
13
+ const out = [];
14
+ for (let c = 0; c < count; c++) {
15
+ out.push(new Uint32Array(sizes[c]));
16
+ }
17
+ const fill = new Uint32Array(count);
18
+ for (let i = 0; i < labels.length; i++) {
19
+ const c = labels[i];
20
+ out[c][fill[c]++] = i;
21
+ }
22
+ cached = out;
23
+ }
24
+ return cached;
25
+ },
26
+ };
27
+ }
28
+ function unionEdges(s) {
29
+ const uf = new IntUnionFind(s.nodeCount);
30
+ const el = s.edgeList();
31
+ for (let e = 0; e < s.edgeCount; e++) {
32
+ uf.union(el.src[e], el.dst[e]);
33
+ }
34
+ const { labels, count } = uf.toLabels();
35
+ return withGroups(labels, count);
36
+ }
37
+ /**
38
+ * Connected components of an UNDIRECTED snapshot.
39
+ * @param s - An undirected snapshot
40
+ * @returns The partition
41
+ * @public
42
+ */
43
+ export function connectedComponents(s) {
44
+ if (s.directed) {
45
+ throw new Error("Connected components requires an undirected graph. Use weaklyConnectedComponents, or pass s.toUndirected().snapshot.");
46
+ }
47
+ return unionEdges(s);
48
+ }
49
+ /**
50
+ * Weakly connected components: the same union-find pass with the direction check dropped.
51
+ * @param s - Any snapshot
52
+ * @returns The partition
53
+ * @public
54
+ */
55
+ export function weaklyConnectedComponents(s) {
56
+ return unionEdges(s);
57
+ }
58
+ //# sourceMappingURL=components.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"components.js","sourceRoot":"","sources":["../../../src/indexed/components.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAe1D,SAAS,UAAU,CAAC,MAAW,EAAE,KAAa;IAC1C,IAAI,MAAM,GAAiB,IAAI,CAAC;IAChC,OAAO;QACH,MAAM;QACN,KAAK;QACL,MAAM;YACF,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;gBAClB,MAAM,KAAK,GAAG,IAAI,WAAW,CAAC,KAAK,CAAC,CAAC;gBACrC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAA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E,CAAC,QAAQ,EAAE,CAAC;IACxC,OAAO,UAAU,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AACrC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,CAAgB;IAChD,IAAI,CAAC,CAAC,QAAQ,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CACX,sHAAsH,CACzH,CAAC;IACN,CAAC;IACD,OAAO,UAAU,CAAC,CAAC,CAAC,CAAC;AACzB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,yBAAyB,CAAC,CAAgB;IACtD,OAAO,UAAU,CAAC,CAAC,CAAC,CAAC;AACzB,CAAC"}
@@ -0,0 +1,63 @@
1
+ import { type AdjacencyView, type NumericVector, type U32 } from "@graphty/graph-format";
2
+ /**
3
+ * Single-source shortest paths, index-based (graph-format design 14.2 Port 2, line 3860).
4
+ * `dist` is a `NumericVector` rather than the design's `F64` because ONE declaration serves both
5
+ * the CPU port (which produces a `Float64Array`) and the dispatcher's decoration of an
6
+ * accelerator's f32 result (plan decision PD-3, departure DEP-8A-C).
7
+ * @public
8
+ */
9
+ export interface SsspResult {
10
+ /** Distance per node; +Infinity for unreached nodes. */
11
+ readonly dist: NumericVector;
12
+ /** The ARC that relaxed each node; INVALID_INDEX for the source and for unreached nodes. */
13
+ readonly predArc: U32;
14
+ /**
15
+ * Node indices from the source to `target` inclusive; empty when `target` is unreached.
16
+ * @param target - The node index to walk back from
17
+ */
18
+ pathTo(target: number): U32;
19
+ /**
20
+ * LOGICAL EDGE indices along that path, one fewer than `pathTo`; this is what graphty-element's
21
+ * `isInPath` writes through. Empty when `target` is unreached.
22
+ * @param target - The node index to walk back from
23
+ */
24
+ pathEdges(target: number): U32;
25
+ }
26
+ /** Options of the index-based SSSP. @public */
27
+ export interface SsspOptions {
28
+ /** Stop relaxing beyond this distance. */
29
+ readonly cutoff?: number | undefined;
30
+ /** Per-arc weight override, arcCount long -- the facade passes `expandEdges(s, shadow.data)`. */
31
+ readonly weights?: NumericVector | undefined;
32
+ }
33
+ /**
34
+ * Walk the predecessor arcs back from `target` and return the node path, source first.
35
+ * @param g - The adjacency the search ran on
36
+ * @param predArc - The search's predecessor-arc array
37
+ * @param source - The search's source node index
38
+ * @param target - The node to walk back from
39
+ * @returns Node indices from source to target inclusive, or an empty array when unreached
40
+ * @public
41
+ */
42
+ export declare function walkPredArcs(g: AdjacencyView, predArc: U32, source: number, target: number): U32;
43
+ /**
44
+ * Walk the predecessor arcs back from `target` and return the logical edges on the path.
45
+ * @param g - The adjacency the search ran on
46
+ * @param predArc - The search's predecessor-arc array
47
+ * @param source - The search's source node index
48
+ * @param target - The node to walk back from
49
+ * @returns Logical edge indices from source to target, or an empty array when unreached
50
+ * @public
51
+ */
52
+ export declare function walkPredEdges(g: AdjacencyView, predArc: U32, source: number, target: number): U32;
53
+ /**
54
+ * Dijkstra over an adjacency view, with the predecessor recorded as the relaxing ARC so a parallel
55
+ * edge on the path is identified exactly.
56
+ * @param g - The adjacency to search
57
+ * @param source - The node index to start from
58
+ * @param options - Cutoff and per-arc weight override
59
+ * @returns The distances, the predecessor arcs, and the two path accessors
60
+ * @public
61
+ */
62
+ export declare function dijkstra(g: AdjacencyView, source: number, options?: SsspOptions): SsspResult;
63
+ //# sourceMappingURL=dijkstra.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dijkstra.d.ts","sourceRoot":"","sources":["../../../src/indexed/dijkstra.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAiB,KAAK,aAAa,EAAE,KAAK,GAAG,EAAE,MAAM,uBAAuB,CAAC;AAKxG;;;;;;GAMG;AACH,MAAM,WAAW,UAAU;IACvB,wDAAwD;IACxD,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAC7B,4FAA4F;IAC5F,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC;IACtB;;;OAGG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,CAAC;IAC5B;;;;OAIG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,CAAC;CAClC;AAED,+CAA+C;AAC/C,MAAM,WAAW,WAAW;IACxB,0CAA0C;IAC1C,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACrC,iGAAiG;IACjG,QAAQ,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC;CAChD;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,aAAa,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,GAAG,CAkBhG;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,aAAa,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,GAAG,CAgBjG;AAED;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,WAAgB,GAAG,UAAU,CA8BhG"}
@@ -0,0 +1,98 @@
1
+ import { INVALID_INDEX } from "@graphty/graph-format";
2
+ import { arcSourceIn } from "./structures/arc-source.js";
3
+ import { IndexedMinHeap } from "./structures/min-heap.js";
4
+ /**
5
+ * Walk the predecessor arcs back from `target` and return the node path, source first.
6
+ * @param g - The adjacency the search ran on
7
+ * @param predArc - The search's predecessor-arc array
8
+ * @param source - The search's source node index
9
+ * @param target - The node to walk back from
10
+ * @returns Node indices from source to target inclusive, or an empty array when unreached
11
+ * @public
12
+ */
13
+ export function walkPredArcs(g, predArc, source, target) {
14
+ if (target === source) {
15
+ return Uint32Array.of(source);
16
+ }
17
+ if (predArc[target] === INVALID_INDEX) {
18
+ return new Uint32Array(0);
19
+ }
20
+ const reversed = [target];
21
+ let node = target;
22
+ while (node !== source) {
23
+ node = arcSourceIn(g.rowPtr, predArc[node]);
24
+ reversed.push(node);
25
+ }
26
+ const out = new Uint32Array(reversed.length);
27
+ for (let i = 0; i < reversed.length; i++) {
28
+ out[i] = reversed[reversed.length - 1 - i];
29
+ }
30
+ return out;
31
+ }
32
+ /**
33
+ * Walk the predecessor arcs back from `target` and return the logical edges on the path.
34
+ * @param g - The adjacency the search ran on
35
+ * @param predArc - The search's predecessor-arc array
36
+ * @param source - The search's source node index
37
+ * @param target - The node to walk back from
38
+ * @returns Logical edge indices from source to target, or an empty array when unreached
39
+ * @public
40
+ */
41
+ export function walkPredEdges(g, predArc, source, target) {
42
+ if (target === source || predArc[target] === INVALID_INDEX) {
43
+ return new Uint32Array(0);
44
+ }
45
+ const reversed = [];
46
+ let node = target;
47
+ while (node !== source) {
48
+ const arc = predArc[node];
49
+ reversed.push(g.arcToEdge[arc]); // the EXACT parallel edge, not a (u, v) lookup
50
+ node = arcSourceIn(g.rowPtr, arc);
51
+ }
52
+ const out = new Uint32Array(reversed.length);
53
+ for (let i = 0; i < reversed.length; i++) {
54
+ out[i] = reversed[reversed.length - 1 - i];
55
+ }
56
+ return out;
57
+ }
58
+ /**
59
+ * Dijkstra over an adjacency view, with the predecessor recorded as the relaxing ARC so a parallel
60
+ * edge on the path is identified exactly.
61
+ * @param g - The adjacency to search
62
+ * @param source - The node index to start from
63
+ * @param options - Cutoff and per-arc weight override
64
+ * @returns The distances, the predecessor arcs, and the two path accessors
65
+ * @public
66
+ */
67
+ export function dijkstra(g, source, options = {}) {
68
+ const { nodeCount, rowPtr, colIdx } = g;
69
+ const weights = options.weights ?? g.weights;
70
+ const dist = new Float64Array(nodeCount).fill(Infinity);
71
+ const predArc = new Uint32Array(nodeCount).fill(INVALID_INDEX);
72
+ const heap = new IndexedMinHeap(nodeCount);
73
+ const cutoff = options.cutoff ?? Infinity;
74
+ dist[source] = 0;
75
+ heap.push(source, 0);
76
+ while (!heap.isEmpty()) {
77
+ const u = heap.pop();
78
+ const du = dist[u];
79
+ const end = rowPtr[u + 1];
80
+ for (let a = rowPtr[u]; a < end; a++) {
81
+ const w = weights === null ? 1 : weights[a];
82
+ const v = colIdx[a];
83
+ const dv = du + w;
84
+ if (dv < dist[v] && dv <= cutoff) {
85
+ dist[v] = dv;
86
+ predArc[v] = a;
87
+ heap.pushOrDecrease(v, dv);
88
+ }
89
+ }
90
+ }
91
+ return {
92
+ dist,
93
+ predArc,
94
+ pathTo: (target) => walkPredArcs(g, predArc, source, target),
95
+ pathEdges: (target) => walkPredEdges(g, predArc, source, target),
96
+ };
97
+ }
98
+ //# sourceMappingURL=dijkstra.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dijkstra.js","sourceRoot":"","sources":["../../../src/indexed/dijkstra.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,aAAa,EAAgC,MAAM,uBAAuB,CAAC;AAExG,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAmC1D;;;;;;;;GAQG;AACH,MAAM,UAAU,YAAY,CAAC,CAAgB,EAAE,OAAY,EAAE,MAAc,EAAE,MAAc;IACvF,IAAI,MAAM,KAAK,MAAM,EAAE,CAAC;QACpB,OAAO,WAAW,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC;IAClC,CAAC;IACD,IAAI,OAAO,CAAC,MAAM,CAAC,KAAK,aAAa,EAAE,CAAC;QACpC,OAAO,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;IAC9B,CAAC;IACD,MAAM,QAAQ,GAAa,CAAC,MAAM,CAAC,CAAC;IACpC,IAAI,IAAI,GAAG,MAAM,CAAC;IAClB,OAAO,IAAI,KAAK,MAAM,EAAE,CAAC;QACrB,IAAI,GAAG,WAAW,CAAC,CAAC,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;QAC5C,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;IACD,MAAM,GAAG,G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,CAAC,CAAC,IAAI,EAAE,IAAI,MAAM,EAAE,CAAC;gBAC/B,IAAI,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;gBACb,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;gBACf,IAAI,CAAC,cAAc,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YAC/B,CAAC;QACL,CAAC;IACL,CAAC;IACD,OAAO;QACH,IAAI;QACJ,OAAO;QACP,MAAM,EAAE,CAAC,MAAc,EAAO,EAAE,CAAC,YAAY,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC;QACzE,SAAS,EAAE,CAAC,MAAc,EAAO,EAAE,CAAC,aAAa,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC;KAChF,CAAC;AACN,CAAC"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Index-based implementations over `@graphty/graph-format` snapshots (graph-format design 14.1
3
+ * rule 2): every function takes a `GraphSnapshot` or an `AdjacencyView` first and an options object
4
+ * last, and returns typed arrays plus scalars. Reached as the `indexed` namespace of
5
+ * `@graphty/algorithms`.
6
+ *
7
+ * NOT re-exported here, deliberately (plan decision PD-9):
8
+ * - `./accelerator.js`, which imports this barrel -- re-exporting it would make a cycle, and its
9
+ * symbols are exported FLAT from the package barrel because the GPU package must write
10
+ * `import type { AlgorithmAccelerator } from "@graphty/algorithms"`.
11
+ * - `./to-snapshot.js`, whose parameter is a legacy `Graph`, which is not what this namespace
12
+ * promises. It is exported flat too.
13
+ * @module
14
+ */
15
+ export { type BfsOptions, type BfsResult, breadthFirstSearch } from "./bfs.js";
16
+ export { type CommonNeighborsOptions, commonNeighborsScore } from "./common-neighbors.js";
17
+ export { connectedComponents, type LabelResult, weaklyConnectedComponents } from "./components.js";
18
+ export { dijkstra, type SsspOptions, type SsspResult, walkPredArcs, walkPredEdges, } from "./dijkstra.js";
19
+ export { kruskalMST, type MstOptions, type MstResult } from "./mst.js";
20
+ export { pageRank, type PageRankOptions, type PageRankResult } from "./pagerank.js";
21
+ export { arcSourceIn, IndexedMinHeap, IntUnionFind } from "./structures/index.js";
22
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/indexed/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,SAAS,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAC/E,OAAO,EAAE,KAAK,sBAAsB,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAC1F,OAAO,EAAE,mBAAmB,EAAE,KAAK,WAAW,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AACnG,OAAO,EACH,QAAQ,EACR,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,YAAY,EACZ,aAAa,GAChB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,UAAU,EAAE,KAAK,UAAU,EAAE,KAAK,SAAS,EAAE,MAAM,UAAU,CAAC;AACvE,OAAO,EAAE,QAAQ,EAAE,KAAK,eAAe,EAAE,KAAK,cAAc,EAAE,MAAM,eAAe,CAAC;AACpF,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Index-based implementations over `@graphty/graph-format` snapshots (graph-format design 14.1
3
+ * rule 2): every function takes a `GraphSnapshot` or an `AdjacencyView` first and an options object
4
+ * last, and returns typed arrays plus scalars. Reached as the `indexed` namespace of
5
+ * `@graphty/algorithms`.
6
+ *
7
+ * NOT re-exported here, deliberately (plan decision PD-9):
8
+ * - `./accelerator.js`, which imports this barrel -- re-exporting it would make a cycle, and its
9
+ * symbols are exported FLAT from the package barrel because the GPU package must write
10
+ * `import type { AlgorithmAccelerator } from "@graphty/algorithms"`.
11
+ * - `./to-snapshot.js`, whose parameter is a legacy `Graph`, which is not what this namespace
12
+ * promises. It is exported flat too.
13
+ * @module
14
+ */
15
+ export { breadthFirstSearch } from "./bfs.js";
16
+ export { commonNeighborsScore } from "./common-neighbors.js";
17
+ export { connectedComponents, weaklyConnectedComponents } from "./components.js";
18
+ export { dijkstra, walkPredArcs, walkPredEdges, } from "./dijkstra.js";
19
+ export { kruskalMST } from "./mst.js";
20
+ export { pageRank } from "./pagerank.js";
21
+ export { arcSourceIn, IndexedMinHeap, IntUnionFind } from "./structures/index.js";
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/indexed/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAmC,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAC/E,OAAO,EAA+B,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAC1F,OAAO,EAAE,mBAAmB,EAAoB,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AACnG,OAAO,EACH,QAAQ,EAGR,YAAY,EACZ,aAAa,GAChB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,UAAU,EAAmC,MAAM,UAAU,CAAC;AACvE,OAAO,EAAE,QAAQ,EAA6C,MAAM,eAAe,CAAC;AACpF,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC"}
@@ -0,0 +1,26 @@
1
+ import type { GraphSnapshot, NumericVector, U32 } from "@graphty/graph-format";
2
+ /** Options of the index-based MST. @public */
3
+ export interface MstOptions {
4
+ /** Per-arc weight override, arcCount long; gathered back to per-edge through `edgeToArc`. */
5
+ readonly weights?: NumericVector | undefined;
6
+ }
7
+ /** Result of the index-based MST (graph-format design 14.2's result table, line 3745). @public */
8
+ export interface MstResult {
9
+ /** Logical edge indices of the spanning forest, in the order they were accepted. */
10
+ readonly edges: U32;
11
+ /** Sum of the accepted edges' weights. */
12
+ readonly totalWeight: number;
13
+ }
14
+ /**
15
+ * Kruskal's minimum spanning forest over logical edges.
16
+ *
17
+ * The sort is a comparator over an f64 key array, tie-broken by edge index, rather than the
18
+ * design's radix sort on the f32 bit pattern (plan departure DEP-8A-D): the tie-break makes it
19
+ * stable by construction and it has to serve both the f32 arc array and an f64 override.
20
+ * @param s - The snapshot
21
+ * @param o - The optional per-arc weight override
22
+ * @returns The accepted edge indices and their total weight
23
+ * @public
24
+ */
25
+ export declare function kruskalMST(s: GraphSnapshot, o?: MstOptions): MstResult;
26
+ //# sourceMappingURL=mst.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mst.d.ts","sourceRoot":"","sources":["../../../src/indexed/mst.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,uBAAuB,CAAC;AAI/E,8CAA8C;AAC9C,MAAM,WAAW,UAAU;IACvB,6FAA6F;IAC7F,QAAQ,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC;CAChD;AAED,kGAAkG;AAClG,MAAM,WAAW,SAAS;IACtB,oFAAoF;IACpF,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC;IACpB,0CAA0C;IAC1C,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;CAChC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,aAAa,EAAE,CAAC,GAAE,UAAe,GAAG,SAAS,CAgC1E"}
@@ -0,0 +1,48 @@
1
+ import { IntUnionFind } from "./structures/union-find.js";
2
+ /**
3
+ * Kruskal's minimum spanning forest over logical edges.
4
+ *
5
+ * The sort is a comparator over an f64 key array, tie-broken by edge index, rather than the
6
+ * design's radix sort on the f32 bit pattern (plan departure DEP-8A-D): the tie-break makes it
7
+ * stable by construction and it has to serve both the f32 arc array and an f64 override.
8
+ * @param s - The snapshot
9
+ * @param o - The optional per-arc weight override
10
+ * @returns The accepted edge indices and their total weight
11
+ * @public
12
+ */
13
+ export function kruskalMST(s, o = {}) {
14
+ const el = s.edgeList();
15
+ const m = s.edgeCount;
16
+ const keys = new Float64Array(m);
17
+ if (o.weights !== undefined) {
18
+ // The override is per ARC; edgeList().arc holds the arc of each edge's declared orientation.
19
+ for (let e = 0; e < m; e++) {
20
+ keys[e] = o.weights[el.arc[e]];
21
+ }
22
+ }
23
+ else if (el.weights !== null) {
24
+ for (let e = 0; e < m; e++) {
25
+ keys[e] = el.weights[e];
26
+ }
27
+ }
28
+ else {
29
+ keys.fill(1);
30
+ }
31
+ const order = new Array(m);
32
+ for (let e = 0; e < m; e++) {
33
+ order[e] = e;
34
+ }
35
+ order.sort((a, b) => keys[a] - keys[b] || a - b);
36
+ const uf = new IntUnionFind(s.nodeCount);
37
+ const accepted = new Uint32Array(Math.max(s.nodeCount - 1, 0));
38
+ let taken = 0;
39
+ let totalWeight = 0;
40
+ for (const e of order) {
41
+ if (uf.union(el.src[e], el.dst[e])) {
42
+ accepted[taken++] = e;
43
+ totalWeight += keys[e];
44
+ }
45
+ }
46
+ return { edges: accepted.subarray(0, taken), totalWeight };
47
+ }
48
+ //# sourceMappingURL=mst.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mst.js","sourceRoot":"","sources":["../../../src/indexed/mst.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAgB1D;;;;;;;;;;GAUG;AACH,MAAM,UAAU,UAAU,CAAC,CAAgB,EAAE,IAAgB,EAAE;IAC3D,MAAM,EAAE,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC;IACxB,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC;IACtB,MAAM,IAAI,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC;IACjC,IAAI,CAAC,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;QAC1B,6FAA6F;QAC7F,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAA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AC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YACjC,QAAQ,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;YACtB,WAAW,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC;QAC3B,CAAC;IACL,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,WAAW,EAAE,CAAC;AAC/D,CAAC"}
@@ -0,0 +1,30 @@
1
+ import type { F64, GraphSnapshot } from "@graphty/graph-format";
2
+ /** Options of the index-based PageRank (graph-format design 14.2 Port 3, line 3892). @public */
3
+ export interface PageRankOptions {
4
+ /** Probability of following a link; default 0.85. */
5
+ readonly dampingFactor?: number | undefined;
6
+ /** Iteration cap; default 100. */
7
+ readonly maxIterations?: number | undefined;
8
+ /** L1 convergence tolerance; default 1e-6. */
9
+ readonly tolerance?: number | undefined;
10
+ /** Use the snapshot's arc weights; default false. */
11
+ readonly weighted?: boolean | undefined;
12
+ }
13
+ /** Result of the index-based PageRank. @public */
14
+ export interface PageRankResult {
15
+ /** Score per node index. */
16
+ readonly scores: F64;
17
+ /** Iterations actually run. */
18
+ readonly iterations: number;
19
+ /** Whether the L1 delta fell below the tolerance. */
20
+ readonly converged: boolean;
21
+ }
22
+ /**
23
+ * PageRank by pull over `reverse()`, with the dangling mass redistributed uniformly.
24
+ * @param s - A DIRECTED snapshot
25
+ * @param o - Algorithm options
26
+ * @returns The scores, the iteration count and the convergence flag
27
+ * @public
28
+ */
29
+ export declare function pageRank(s: GraphSnapshot, o?: PageRankOptions): PageRankResult;
30
+ //# sourceMappingURL=pagerank.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pagerank.d.ts","sourceRoot":"","sources":["../../../src/indexed/pagerank.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,aAAa,EAAiB,MAAM,uBAAuB,CAAC;AAE/E,gGAAgG;AAChG,MAAM,WAAW,eAAe;IAC5B,qDAAqD;IACrD,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5C,kCAAkC;IAClC,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5C,8CAA8C;IAC9C,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC,qDAAqD;IACrD,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CAC3C;AAED,kDAAkD;AAClD,MAAM,WAAW,cAAc;IAC3B,4BAA4B;IAC5B,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC;IACrB,+BAA+B;IAC/B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,qDAAqD;IACrD,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;CAC/B;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,aAAa,EAAE,CAAC,GAAE,eAAoB,GAAG,cAAc,CAyClF"}
@@ -0,0 +1,50 @@
1
+ /**
2
+ * PageRank by pull over `reverse()`, with the dangling mass redistributed uniformly.
3
+ * @param s - A DIRECTED snapshot
4
+ * @param o - Algorithm options
5
+ * @returns The scores, the iteration count and the convergence flag
6
+ * @public
7
+ */
8
+ export function pageRank(s, o = {}) {
9
+ if (!s.directed) {
10
+ throw new Error("PageRank requires a directed graph");
11
+ }
12
+ const n = s.nodeCount;
13
+ const d = o.dampingFactor ?? 0.85;
14
+ const maxIter = o.maxIterations ?? 100;
15
+ const tol = o.tolerance ?? 1e-6;
16
+ const rev = s.reverse();
17
+ const weighted = o.weighted === true && rev.weights !== null;
18
+ const outW = weighted ? s.weightedOutDegree() : s.outDegree();
19
+ let rank = new Float64Array(n).fill(1 / n);
20
+ let next = new Float64Array(n);
21
+ let it = 0;
22
+ let converged = false;
23
+ for (; it < maxIter && !converged; it++) {
24
+ let dangling = 0;
25
+ for (let u = 0; u < n; u++) {
26
+ if (outW[u] === 0) {
27
+ dangling += rank[u];
28
+ }
29
+ }
30
+ const base = (1 - d) / n + (d * dangling) / n;
31
+ let delta = 0;
32
+ for (let v = 0; v < n; v++) {
33
+ let acc = 0;
34
+ const end = rev.rowPtr[v + 1];
35
+ for (let a = rev.rowPtr[v]; a < end; a++) {
36
+ const u = rev.colIdx[a];
37
+ const ow = outW[u];
38
+ if (ow > 0) {
39
+ acc += (rank[u] * (weighted && rev.weights !== null ? rev.weights[a] : 1)) / ow;
40
+ }
41
+ }
42
+ next[v] = base + d * acc;
43
+ delta += Math.abs(next[v] - rank[v]);
44
+ }
45
+ [rank, next] = [next, rank];
46
+ converged = delta < tol;
47
+ }
48
+ return { scores: rank, iterations: it, converged };
49
+ }
50
+ //# sourceMappingURL=pagerank.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pagerank.js","sourceRoot":"","sources":["../../../src/indexed/pagerank.ts"],"names":[],"mappings":"AAwBA;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CAAC,CAAgB,EAAE,IAAqB,EAAE;IAC9D,IAAI,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;IAC1D,CAAC;IACD,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC;IACtB,MAAM,CAAC,GAAG,CAAC,CAAC,aAAa,IAAI,IAAI,CAAC;IAClC,MAAM,OAAO,GAAG,CAAC,CAAC,aAAa,IAAI,GAAG,CAAC;IACvC,MAAM,GAAG,GAAG,CAAC,CAAC,SAAS,IAAI,IAAI,CAAC;IAChC,MAAM,GAAG,GAAG,CAAC,CAAC,OAAO,EAAE,CAAC;IACxB,MAAM,QAAQ,GAAG,CAAC,CAAC,QAAQ,KAAK,IAAI,IAAI,GAAG,CAAC,OAAO,KAAK,IAAI,CAAC;IAC7D,MAAM,IAAI,GAAkB,QAAQ,CAAC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,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;gBACnB,IAAI,EAAE,GAAG,CAAC,EAAE,CAAC;oBACT,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;gBACpF,CAAC;YACL,CAAC;YACD,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC,GAAG,GAAG,CAAC;YACzB,KAAK,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QACzC,CAAC;QACD,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC5B,SAAS,GAAG,KAAK,GAAG,GAAG,CAAC;IAC5B,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,SAAS,EAAE,CAAC;AACvD,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { U32 } from "@graphty/graph-format";
2
+ /**
3
+ * The source node of an arc, by binary search on `rowPtr`. `GraphSnapshot` exposes `arcSource(a)`
4
+ * (`graph-format/src/snapshot/graph-snapshot.ts:544`), but an `AdjacencyView` -- which is what the
5
+ * Port 1 and Port 2 signatures take, so that `reverse()` works as an input for free -- does not,
6
+ * so the predecessor walk carries its own.
7
+ * @param rowPtr - The view's nodeCount + 1 row offsets
8
+ * @param arc - An arc index in `[0, arcCount)`
9
+ * @returns The node index whose row contains the arc
10
+ * @public
11
+ */
12
+ export declare function arcSourceIn(rowPtr: U32, arc: number): number;
13
+ //# sourceMappingURL=arc-source.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arc-source.d.ts","sourceRoot":"","sources":["../../../../src/indexed/structures/arc-source.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,uBAAuB,CAAC;AAEjD;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAY5D"}