我可以使用predecessors函数查找节点的所有前身。我想要的是找到一个前辈。
(图被约束为有一个父)
示例:

在上面的图片中,如果我在predecessors节点8上调用[1, 2, 3, 4, 5, 6, 7],结果将给出[1, 2, 3, 4, 5, 6, 7](忽略边缘)。
我希望它将搜索限制在某个节点上。如果我希望它将搜索限制到4,它应该只返回[4, 5, 6, 7]。上面没有任何东西。
是否可以使用本机胞景函数?
predecessors函数接受选择器,根据医生们,我尝试过像node.predecessors("node#4 node")那样使用选择器。但什么也没回。
发布于 2022-07-21 05:43:55
我设法解决了这个问题,找到了我想要停止的节点的所有后继者,然后将它与输入节点的前辈交叉:
const node = cy.$('node#8');
const predecessors = node.predecessors(); // [1, 2, 3, 4, 5, 6, 7]
const requiredRootNode = cy.$('node#4');
const rootChildren = requiredRootNode.successors(); // [5, 6, 7, 8]
const intersection = rootChildren.intersection(predecessors); // [5, 6, 7]
const result = intersection.add(requiredRootNode); // [4, 5, 6, 7]发布于 2022-07-20 17:57:33
您可以使用breadthFirstSearch.或 depthFirstSearch 或。
那里的代码示例将允许您更改visit。根据您的喜好更改以下内容:
const limitingID = '4';
var bfs = cy.elements().bfs({
roots: '#e',
visit: function(v){
// Stopping at desired node
if( v.id == limitingID ){
return true;
}
},
directed: false
});
var path = bfs.path; // path to found node
var found = bfs.found; // found nodehttps://stackoverflow.com/questions/73038701
复制相似问题