Search code examples
javascriptcytoscape.js

cytoscape.js find connected node with data field value


I have a graph set up in cytoscape.js. Some nodes in it have a data field "feature = true". For a node I select, I want to find the closest connected node that has "feature = true".

I've looked through the different algorithms, but if they accept selectors that validate to multiple nodes, they don't seem to behave in the expected manner of returning the shortest path to a node that matches the selector. For example, if I'm looking for the closest node to #start that has feature = true in its data field:

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

It appears to always pick the same ending node...the first item on the list returned if you did cytoGraph.filter('[?feature]') no matter which node I put into root, even if other nodes that have "feature = true" are closer.

Am I missing something obvious here? I've consulted the docs and can't find anything for this specific problem, just specific node-to-node use cases. Thanks!


Solution

  • My understanding is that A* is for when you know the exact node you want to be the destination. Thus, only one node is accepted as the goal. If you specify a collection of size more than 1, then only the first can be used.

    I haven't looked at the algorithm recently, but my intuition is that your change might not work for all cases -- especially in tandem with the heuristic.

    It's probably better for you to use Djikstra. After running, you can look at the distance it finds for every [?feature] node -- use the smallest one, and get its path.