cytoscape.js 查找具有数据字段值的连接节点

cytoscape.js find connected node with data field value

我在 cytoscape.js 中设置了一个图表。其中一些节点有一个数据字段"feature = true"。对于节点 I select,我想找到具有 "feature = true" 的最近连接节点。

我查看了不同的算法,但如果它们接受 select 验证多个节点的 ors,它们似乎不会以预期的方式返回到节点的最短路径匹配 select 或。例如,如果我正在寻找最接近 #start 且其数据字段中具有 feature = true 的节点:

var closestFeatureSearch = cytoGraph.elements().aStar({  
  root: '#start',
  goal: 'node[?feature]',
  directed : false
});

它似乎总是选择相同的结束节点...如果您选择 cytoGraph.filter('[?feature]') 无论我将哪个节点放入根目录,都会返回列表中的第一项,甚至如果其他具有 "feature = true" 的节点更近。

我是不是漏掉了什么明显的东西?我已经查阅了文档,但找不到针对此特定问题的任何内容,只是特定的节点到节点用例。谢谢!

好的,我能够添加一个案例来处理这个功能。在第 390 行附近关闭未缩小的当前版本 (2.7.8),您应该会看到如下条件:

if( cMin.id() == target.id() ){
  var rPath = reconstructPath( source.id(), target.id(), cameFrom, [] );
  rPath.reverse();
  return {
    found: true,
    distance: gScore[ cMin.id() ],
    path: eles.spawn( rPath ),
    steps: steps
  };
}

我添加了一个单独的案例来处理传入匹配紧跟其后的多个项目的过滤器:

//if there was a filter passed in, check the array of ids matching that to see if cMin.id() is in there
if (is.string( options.goal ) && this.filter( options.goal ).map(function(item){return item.id()}).indexOf(cMin.id()) > -1 ) {
  var rPath = reconstructPath( source.id(), cMin.id(), cameFrom, [] );
  rPath.reverse();
  return {
    found: true,
    distance: gScore[ cMin.id() ],
    path: eles.spawn( rPath ),
    steps: steps
  };
}

关键的两个变化是 if 条件(很明显)和 rPath 将当前 cMin.id() 作为目标,而不是 target.id()。

无论如何,它适用于我的用例!希望它对其他人有用。

我的理解是 A* 用于当您知道要作为目的地的确切节点时。因此,只有一个节点被接受为目标。如果指定的集合大小大于1,那么只能使用第一个。

我最近没有查看该算法,但我的直觉是您的更改可能不适用于所有情况——尤其是与启发式算法结合使用时。

您最好使用 Djikstra。在 运行 之后,您可以查看它为每个 [?feature] 节点找到的距离——使用最小的一个,并获取它的路径。

var closestFeatureSearch = cytoGraph.elements().aStar({     
  root: '#start',
  goal: 'node[?feature]',
  directed : false
});