import { BinaryDataType } from "../binary/type/BinaryDataType.js";
import { SquareMatrix } from "../math/matrix/SquareMatrix.js";
import { graph_compute_adjacency_matrix } from "./graph_compute_adjacency_matrix.js";
import { graph_compute_degree_matrix } from "./graph_compute_degree_matrix.js";

/**
 * Computes the Laplacian matrix of a graph.
 * Does NOT clear the matrix before writing.
 * @template T Type of graph nodes.
 * @param {Graph<T>} graph Input graph.
 * @param {SquareMatrix} result Output: Laplacian matrix (modified in-place).
 * @param {Map<T,number>} node_indices Map from graph nodes to matrix indices.
 */
export function graph_compute_laplacian_matrix(graph, result, node_indices) {
    // Uint32 prevents silent overflow for high-degree or multi-edge graphs
    const degree = new SquareMatrix(result.size, BinaryDataType.Uint32);
    const adjacency = new SquareMatrix(result.size, BinaryDataType.Uint8);

    graph_compute_degree_matrix(graph, degree, node_indices);
    graph_compute_adjacency_matrix(graph, adjacency, node_indices);

    result.subtractMatrices(degree, adjacency);
}
