【发布时间】:2021-11-29 15:22:18
【问题描述】:
我想在 javascript 的一个函数中比较决策树的单个路径。
我只知道如何做到这一点,因为节点正在累加,但那只是比较节点。 相反,我想查看一条路径,然后查看下一条路径。
例如:下一个决定应该在 -3 的range 中。给出的是数字[9, 8, 6, 5, 3, 2, 0]。列表的总和是value。需要 length 为 6 且 biggest value 的路径。
2 - 0
3 <
5< 0
/ 2 - 0
6
/ \ 2 - 0
3<
0
8 2 - 0
\ 3<
/ 5 < 0
2 - 0
9 2 - 0
3 <
5< 0
\ / 2 - 0
6
\ 2 - 0
3<
0
我想比较那些paths:
[9,6,3,0],
[9,6,3,2,0],
[9,6,5,2,0],
[9,6,5,3,0],
[9,6,5,3,2,0],
[9,8,6,3,0],
[9,8,6,3,2,0],
[9,8,6,5,2,0],
[9,8,6,5,3,0],
[9,8,6,5,3,2,0],
而不是计算每个path 然后同时比较它们,我只想计算第一个path 然后将它与第二个path 进行比较,如下所示:
[9,6,3,0], //value 18
[9,6,3,2,0]//value 21
|| which one has the nearest length of 6 with the most Value?
\/
[9,6,3,2,0]//value 21
[9,6,3,2,0],//value 21
[9,6,5,2,0] //value 23
|| which one has the nearest length of 6 with the most Value?
\/
[9,6,5,2,0] //value 23
... and so on
如何一次只比较两条路径而不是每条路径?
编辑 1:
到目前为止我的代码: https://replit.com/@RubyKanima/BestSumwithlength#index.js
const numbers = [9, 8, 6, 5, 3, 2, 0];
const starting_node = Math.max(...numbers) + 3;
const path = (current_node, memo ={}) => {
if (current_node in memo) return memo[current_node];
if (current_node == 0) return [[]];
let paths = [];
let tmp_nodes = [];
for (n of numbers) {
if (n >= current_node - 3 && n < current_node) {
tmp_nodes.push(n);
}
}
for (let tmp of tmp_nodes) {
const tmp_path = path(tmp);
const tmp_paths = tmp_path.map(way => [tmp, ...way]);
paths.push(...tmp_paths);
}
memo[current_node] = paths;
paths = paths.filter(x => x.length <= 6);
return paths;
};
const bestPath = all_paths => {
all_paths = all_paths.filter(x => (x.length = 6));
let bestValue = 0;
let bestPath = null;
for (let path of all_paths) {
let value = 0;
for (node of path) value += node;
if (value > bestValue) {
bestValue = value;
bestPath = path;
}
}
return bestPath;
};
const path_array = path(starting_node);
console.log(bestPath(path_array));
它完成了这项工作,但如果我得到超过一千个数字,它就会出现堆栈溢出。 (在示例中我减少了一些数字以便于理解,实际上范围是 -360 而不是 -3)
最大的问题:数据过多
如何解决?:一次只比较 2 个 Path,然后计算下一个 Path。
我想知道的:如何只计算 2 条路径。
【问题讨论】:
-
到目前为止你做了什么?添加一些代码你当前如何比较它们
-
@J_K 我编辑了我的问题并编辑了我的代码。它做了它应该做的,但它没有有效地做到这一点。我也有一个带有记忆功能的代码,但由于给出了 1500 个节点,它也会出现堆栈溢出,它的路径很大。问题之一是计算所有路线,而不仅仅是那些长度小于或等于 6 的路线。但最大的问题仍然是:一次计算所有路径而不是 2 条。
-
我使用归并排序算法添加了一个答案。看看这是否对你有帮助@rubykanima
标签: javascript arrays dynamic