2

Cytoscape.jsを使用しています。エッジに接続されたノードがいくつかあります。

2 つのノード間の各接続をループしたい。問題は、場合によっては 2 つのノード間に複数のエッジがあることcy.edges().forEach()です。そのため、必要以上のエッジをループすることになるため、一概には言えません。

私ができることは、次のようなことを言うことです

const alreadyVisited = [];

cy.edges().forEach(edge => {
  const key1 = edge.source() + '-' + edge.target();
  const key2 = edge.target() + '-' + edge.source();

  if (alreadyVisited.indexOf(key1) === -1 && alreadyVisited.indexOf(key2)) {
    // ...
    alreadyVisited.push(key1);
    alreadyVisited.push(key2);
  }
})

しかし、それは少しばかげているようです。Cytoscape には 、 、 などの機能がありますedges.parallelEdges()nodes.connectedEdges()それらeles.neighborhood()の機能を利用して問題を解決できないでしょうか?

と の両方を持つ理由key1key2 、エッジの方向が重要でないことを確認する方法を知らなかったからです。

編集

あるいは、次のようなこともできます

cy.nodes().forEach(node1 => {
  cy.nodes().forEach(node2 => {
    if (node1 !== node2) {
      // now I have each pair
    }
  });
});

しかし、それらの多くはそれらの間にエッジがないため、それは愚かなアプローチでもありませんか? そして、2 つのノード A と B がある場合、このアプローチでは 2 つの関係 (A -> B と B -> A の両方) が得られます。

4

1 に答える 1

0

Take a look at the traversing functions: http://js.cytoscape.org/#collection/traversing

For example, node.edgesWith( node2 ) gives undirected edges between the nodes whereas node.edgesTo( node2 ) gives directed ones.node.connectedEdges()` may also be of interest if you want to focus on one node and all its connections rather than a specific pair of nodes.

The collections of elements can be used as sets, so you can do toTraverse = toTraverse.difference( justTraversed ) if you want to keep track of things.

于 2016-11-22T19:52:48.853 に答える