我想过滤
一个列表,以便我只得到有连接的节点,直接或间接与候选人
var candidate = 1;
var data = [
{ source: 1, target: 2 }, // is connected with 1
{ source: 2, target: 3 }, // is connected with 1
{ source: 6, target: 9 }, // no connection
{ source: 12, target: 15 }, // no connection
{ source: 3, target: 2 }, // is connected with 1
{ source: 5, target: 3 }, // is connected with 1
]
我在寻找什么样的算法?
感兴趣的语言是 JavaScript - 据我所知,有些语言会以不同于其他语言的方式实现算法
最佳答案
广度优先搜索:
维护“可能”边列表(使用给定列表初始化)、“连接”边列表(初始化为空)和节点列表(初始化为仅包含“候选”)。
从节点列表中删除一个节点。
遍历 Maybe 列表,寻找那个节点;如果一条边包含该节点,则将另一个节点复制到节点列表中,并将该边移动到已连接列表中。
继续,直到节点列表为空。
关于javascript - 过滤与候选人有联系的图节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39735807/