@graphty/algorithms 1.7.0 → 1.7.2
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.
- package/dist/algorithms.js +19 -18
- package/dist/algorithms.js.map +1 -1
- package/dist/src/algorithms/community/louvain-optimized.d.ts +1 -1
- package/dist/src/algorithms/community/louvain-optimized.d.ts.map +1 -1
- package/dist/src/clustering/spectral.d.ts +1 -0
- package/dist/src/clustering/spectral.d.ts.map +1 -1
- package/dist/src/clustering/spectral.js +18 -13
- package/dist/src/clustering/spectral.js.map +1 -1
- package/dist/src/optimized/direction-optimized-bfs.d.ts +3 -10
- package/dist/src/optimized/direction-optimized-bfs.d.ts.map +1 -1
- package/dist/src/optimized/direction-optimized-bfs.js +0 -11
- package/dist/src/optimized/direction-optimized-bfs.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -4
- package/src/algorithms/community/louvain-optimized.ts +1 -1
- package/src/clustering/spectral.ts +20 -10
- package/src/optimized/direction-optimized-bfs.ts +2 -17
- package/dist/src/utils/algorithm-utilities.d.ts +0 -2
- package/dist/src/utils/algorithm-utilities.d.ts.map +0 -1
- package/dist/src/utils/algorithm-utilities.js +0 -2
- package/dist/src/utils/algorithm-utilities.js.map +0 -1
- package/dist/src/utils/index.d.ts +0 -5
- package/dist/src/utils/index.d.ts.map +0 -1
- package/dist/src/utils/index.js +0 -5
- package/dist/src/utils/index.js.map +0 -1
- package/dist/src/utils/matrix-utilities.d.ts +0 -2
- package/dist/src/utils/matrix-utilities.d.ts.map +0 -1
- package/dist/src/utils/matrix-utilities.js +0 -2
- package/dist/src/utils/matrix-utilities.js.map +0 -1
- package/dist/src/utils/optimization-helpers.d.ts +0 -42
- package/dist/src/utils/optimization-helpers.d.ts.map +0 -1
- package/dist/src/utils/optimization-helpers.js +0 -59
- package/dist/src/utils/optimization-helpers.js.map +0 -1
- package/src/utils/algorithm-utilities.ts +0 -2
- package/src/utils/index.ts +0 -4
- package/src/utils/matrix-utilities.ts +0 -2
- package/src/utils/optimization-helpers.ts +0 -87
package/dist/algorithms.js
CHANGED
|
@@ -2093,7 +2093,7 @@ var hasRequiredLib;
|
|
|
2093
2093
|
function requireLib() {
|
|
2094
2094
|
if (hasRequiredLib) return lib;
|
|
2095
2095
|
hasRequiredLib = 1;
|
|
2096
|
-
(function(exports
|
|
2096
|
+
(function(exports) {
|
|
2097
2097
|
var __createBinding = lib && lib.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
2098
2098
|
if (k2 === void 0) k2 = k;
|
|
2099
2099
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -2107,12 +2107,12 @@ function requireLib() {
|
|
|
2107
2107
|
if (k2 === void 0) k2 = k;
|
|
2108
2108
|
o[k2] = m[k];
|
|
2109
2109
|
}));
|
|
2110
|
-
var __exportStar = lib && lib.__exportStar || function(m,
|
|
2111
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(
|
|
2110
|
+
var __exportStar = lib && lib.__exportStar || function(m, exports2) {
|
|
2111
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports2, p)) __createBinding(exports2, m, p);
|
|
2112
2112
|
};
|
|
2113
|
-
Object.defineProperty(exports
|
|
2114
|
-
__exportStar(requireSparseTypedFastBitSet(), exports
|
|
2115
|
-
__exportStar(requireTypedFastBitSet(), exports
|
|
2113
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2114
|
+
__exportStar(requireSparseTypedFastBitSet(), exports);
|
|
2115
|
+
__exportStar(requireTypedFastBitSet(), exports);
|
|
2116
2116
|
})(lib);
|
|
2117
2117
|
return lib;
|
|
2118
2118
|
}
|
|
@@ -9291,7 +9291,8 @@ function calculateMCLModularity(graph, communities) {
|
|
|
9291
9291
|
return modularity / (2 * m);
|
|
9292
9292
|
}
|
|
9293
9293
|
function spectralClustering(graph, options) {
|
|
9294
|
-
const { k, laplacianType = "normalized", maxIterations = 100, tolerance = 1e-4 } = options;
|
|
9294
|
+
const { k, laplacianType = "normalized", maxIterations = 100, tolerance = 1e-4, seed } = options;
|
|
9295
|
+
const random = seed !== void 0 ? SeededRandom.createGenerator(seed) : Math.random;
|
|
9295
9296
|
if (k < 1 || !Number.isInteger(k)) {
|
|
9296
9297
|
throw new Error("k must be a positive integer");
|
|
9297
9298
|
}
|
|
@@ -9306,7 +9307,7 @@ function spectralClustering(graph, options) {
|
|
|
9306
9307
|
}
|
|
9307
9308
|
const adjacencyMatrix = buildAdjacencyMatrix(graph, nodeIds);
|
|
9308
9309
|
const laplacianMatrix = buildLaplacianMatrix(adjacencyMatrix, laplacianType);
|
|
9309
|
-
const eigenResult = findSmallestEigenvectors(laplacianMatrix, k);
|
|
9310
|
+
const eigenResult = findSmallestEigenvectors(laplacianMatrix, k, random);
|
|
9310
9311
|
const dataPoints = [];
|
|
9311
9312
|
for (let i = 0; i < nodeIds.length; i++) {
|
|
9312
9313
|
const point = [];
|
|
@@ -9321,7 +9322,7 @@ function spectralClustering(graph, options) {
|
|
|
9321
9322
|
if (laplacianType === "normalized") {
|
|
9322
9323
|
normalizeRows(dataPoints);
|
|
9323
9324
|
}
|
|
9324
|
-
const kmeans = kMeansClustering(dataPoints, k, maxIterations, tolerance);
|
|
9325
|
+
const kmeans = kMeansClustering(dataPoints, k, maxIterations, tolerance, random);
|
|
9325
9326
|
const communities = Array.from({ length: k }, () => []);
|
|
9326
9327
|
const clusterAssignments = /* @__PURE__ */ new Map();
|
|
9327
9328
|
for (let i = 0; i < nodeIds.length; i++) {
|
|
@@ -9472,19 +9473,19 @@ function buildLaplacianMatrix(adjacency, type) {
|
|
|
9472
9473
|
}
|
|
9473
9474
|
return laplacian;
|
|
9474
9475
|
}
|
|
9475
|
-
function findSmallestEigenvectors(matrix, k) {
|
|
9476
|
+
function findSmallestEigenvectors(matrix, k, random) {
|
|
9476
9477
|
const n = matrix.length;
|
|
9477
9478
|
if (n === 0 || k === 0) {
|
|
9478
9479
|
return { eigenvalues: [], eigenvectors: [] };
|
|
9479
9480
|
}
|
|
9480
9481
|
if (k <= 3 && n > k) {
|
|
9481
|
-
return computeSmallestEigenvectorsSimple(matrix, k, n);
|
|
9482
|
+
return computeSmallestEigenvectorsSimple(matrix, k, n, random);
|
|
9482
9483
|
}
|
|
9483
9484
|
const eigenvectors = [];
|
|
9484
9485
|
const eigenvalues = [];
|
|
9485
9486
|
const maxIterations = 100;
|
|
9486
9487
|
for (let eigIdx = 0; eigIdx < k; eigIdx++) {
|
|
9487
|
-
let vector = Array(n).fill(0).map(() =>
|
|
9488
|
+
let vector = Array(n).fill(0).map(() => random() - 0.5);
|
|
9488
9489
|
const initNorm = Math.sqrt(vector.reduce((sum, val) => sum + val * val, 0));
|
|
9489
9490
|
if (initNorm > 0) {
|
|
9490
9491
|
vector = vector.map((val) => val / initNorm);
|
|
@@ -9572,7 +9573,7 @@ function normalizeRows(matrix) {
|
|
|
9572
9573
|
}
|
|
9573
9574
|
}
|
|
9574
9575
|
}
|
|
9575
|
-
function kMeansClustering(data, k, maxIterations, tolerance = 1e-4) {
|
|
9576
|
+
function kMeansClustering(data, k, maxIterations, tolerance = 1e-4, random = Math.random) {
|
|
9576
9577
|
const n = data.length;
|
|
9577
9578
|
const d = data[0]?.length ?? 0;
|
|
9578
9579
|
if (n === 0 || k === 0) {
|
|
@@ -9587,7 +9588,7 @@ function kMeansClustering(data, k, maxIterations, tolerance = 1e-4) {
|
|
|
9587
9588
|
const centroids = [];
|
|
9588
9589
|
const selectedIndices = /* @__PURE__ */ new Set();
|
|
9589
9590
|
while (centroids.length < k && selectedIndices.size < n) {
|
|
9590
|
-
const idx = Math.floor(
|
|
9591
|
+
const idx = Math.floor(random() * n);
|
|
9591
9592
|
if (!selectedIndices.has(idx) && data[idx]) {
|
|
9592
9593
|
selectedIndices.add(idx);
|
|
9593
9594
|
centroids.push([...data[idx]]);
|
|
@@ -9596,7 +9597,7 @@ function kMeansClustering(data, k, maxIterations, tolerance = 1e-4) {
|
|
|
9596
9597
|
while (centroids.length < k) {
|
|
9597
9598
|
const centroid = Array(d).fill(0);
|
|
9598
9599
|
for (let j = 0; j < d; j++) {
|
|
9599
|
-
centroid[j] =
|
|
9600
|
+
centroid[j] = random() - 0.5;
|
|
9600
9601
|
}
|
|
9601
9602
|
centroids.push(centroid);
|
|
9602
9603
|
}
|
|
@@ -9692,7 +9693,7 @@ function kMeansClustering(data, k, maxIterations, tolerance = 1e-4) {
|
|
|
9692
9693
|
}
|
|
9693
9694
|
return { assignments, centroids };
|
|
9694
9695
|
}
|
|
9695
|
-
function computeSmallestEigenvectorsSimple(matrix, k, n) {
|
|
9696
|
+
function computeSmallestEigenvectorsSimple(matrix, k, n, random) {
|
|
9696
9697
|
const eigenvectors = [];
|
|
9697
9698
|
const eigenvalues = [];
|
|
9698
9699
|
const firstVector = Array(n).fill(1 / Math.sqrt(n));
|
|
@@ -9700,7 +9701,7 @@ function computeSmallestEigenvectorsSimple(matrix, k, n) {
|
|
|
9700
9701
|
eigenvalues.push(0);
|
|
9701
9702
|
if (k >= 2) {
|
|
9702
9703
|
const maxEig = 2;
|
|
9703
|
-
let vector = Array(n).fill(0).map(() =>
|
|
9704
|
+
let vector = Array(n).fill(0).map(() => random() - 0.5);
|
|
9704
9705
|
const dot1 = vector.reduce((sum, val) => sum + val / Math.sqrt(n), 0);
|
|
9705
9706
|
vector = vector.map((val) => val - dot1 / Math.sqrt(n));
|
|
9706
9707
|
for (let iter = 0; iter < 100; iter++) {
|
|
@@ -9728,7 +9729,7 @@ function computeSmallestEigenvectorsSimple(matrix, k, n) {
|
|
|
9728
9729
|
eigenvalues.push(0.1);
|
|
9729
9730
|
}
|
|
9730
9731
|
if (k >= 3) {
|
|
9731
|
-
let vector = Array(n).fill(0).map(() =>
|
|
9732
|
+
let vector = Array(n).fill(0).map(() => random() - 0.5);
|
|
9732
9733
|
for (const prev of eigenvectors) {
|
|
9733
9734
|
const dot = vector.reduce((sum, val, idx) => sum + val * (prev[idx] ?? 0), 0);
|
|
9734
9735
|
vector = vector.map((val, idx) => val - dot * (prev[idx] ?? 0));
|