anvaka / ngraph.graph

Graph data structure in JavaScript
BSD 3-Clause "New" or "Revised" License
520 stars 66 forks source link

getlink for multigraph #19

Closed lewis500 closed 4 years ago

lewis500 commented 5 years ago

From reading the code, it seems that getLlink(a,b) just returns the first link it finds between a and b. But if you have the multigraph option set to true, there may be multiple links. The reason I ask is I am using ngraph.path on a multigraph, and I want to get which particular links are being used.

anvaka commented 5 years ago

You can use forEachLinkedNode() for this. For example, here is a function that should return all links:

function getAllMultiLinks(graph, fromId, toId) {
  let multiLinks = [];

  graph.forEachLinkedNode(fromId, function(otherNode, link) {
    if (otherNode.id === toId) multiLinks.push(link);
  }, /* oriented = */ true);

  return multiLinks;
}

Would this work?