@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
27 lines (21 loc) • 941 B
JavaScript
import { assert } from "../assert.js";
/**
* Computes the adjacency matrix of a graph.
* Writes 1 to matrix elements where nodes are connected, leaves existing values where there are no connections.
* Does NOT zero-fill the matrix before writing.
* @template T Type of the graph's nodes.
* @param {Graph<T>} graph Input graph.
* @param {SquareMatrix} result Output: Adjacency matrix (modified in-place).
* @param {Map<T,number>} node_indices Map from graph nodes to matrix indices.
*/
export function graph_compute_adjacency_matrix(graph, result, node_indices) {
const edges = graph.getEdges();
for (const edge of edges) {
const a = node_indices.get(edge.first);
const b = node_indices.get(edge.second);
assert.isNonNegativeInteger(a, 'a');
assert.isNonNegativeInteger(b, 'b');
result.setCellValue(a, b, 1);
result.setCellValue(b, a, 1);
}
}