entropyx
Version:
A simple data mining library, written in TypeScript
59 lines • 2.05 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdjacencyMatrix = void 0;
const promises_1 = require("fs/promises");
class AdjacencyMatrix {
constructor(matrix, nodeMapping) {
this.matrix = matrix;
this.nodeMapping = nodeMapping;
}
getMatrix() {
return this.matrix;
}
getLegend() {
const legend = {};
this.nodeMapping.forEach((node, index) => {
legend[index] = node;
});
return legend;
}
getNodeName(index) {
return this.nodeMapping[index];
}
static fromEdgeList(edgeList) {
const nodeSet = new Set();
for (const [source, target] of edgeList) {
nodeSet.add(source);
nodeSet.add(target);
}
const nodes = Array.from(nodeSet);
const mapping = new Map();
nodes.forEach((node, index) => mapping.set(node, index));
const n = nodes.length;
const matrix = Array.from({ length: n }, () => Array(n).fill(0));
for (const [source, target] of edgeList) {
const i = mapping.get(source);
const j = mapping.get(target);
if (i !== undefined && j !== undefined) {
matrix[i][j] = 1;
}
}
return new AdjacencyMatrix(matrix, nodes);
}
static async fromEdgeListFile(path) {
const content = await (0, promises_1.readFile)(path, 'utf8');
const lines = content
.split('\n')
.map((line) => line.trim())
.filter((line) => line.length > 0);
const edges = lines.map((line) => {
const parts = line.split(/\s+/);
const source = isNaN(Number(parts[0])) ? parts[0] : Number(parts[0]);
const target = isNaN(Number(parts[1])) ? parts[1] : Number(parts[1]);
return [source, target];
});
return AdjacencyMatrix.fromEdgeList(edges);
}
}
exports.AdjacencyMatrix = AdjacencyMatrix;
//# sourceMappingURL=adjacency-matrix.js.map
;