есть код:
function permutationAlgorithm(n, s, matrix) {
let result = [];
let permutationArr = Array.from(Array(n).keys()).map(element => element + 1);
permutations(permutationArr);
result.map(element => element.push(s))
function permutations(arr, output = "") {
for (let i = 0; i < arr.length; i++) {
let secondArr = [...arr];
secondArr.splice(i, 1);
if (secondArr.length === 0 && output[0] == s) {
result.push((output + arr[i]).split("").map(element => parseInt(element)));
} else {
permutations(secondArr, output + arr[i]);
}
}
}
console.log(result);
console.log(matrix);
let weight = 0;
let minWeight = 1000;
let arr = [];
for (i = 0; i < result.length; i++) {
for (j = 0; j < result[i].length; j++) {
if (j !== result[i].length - 1) {
weight += matrix[(result[i][j] - 1)][(result[i][j + 1]) - 1];
}
}
if (weight < minWeight) {
minWeight = weight;
arr = result[i];
}
weight = 0;
}
console.log(" ");
console.log("Permutation algorithm:");
console.log("Weight: " + minWeight + ",");
console.log("Route: " + arr + ".");
}
где n = 10
s = 1
matrix = (грубо говоря)
___01 02 03 04
01 0 , 66, 39, 15
02 66, 0 , 23, 79
03 39, 23, 0 , 71
04 15, 79, 71, 0
chrome debugger ошибку выявляет в этой строке:
weight += matrix[(result[i][j] - 1)][(result[i][j + 1]) - 1];