最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - VisJs - Get number of edges of a specific node - Stack Overflow

programmeradmin10浏览0评论

I want to find the number of edges which are connected to a specific node in visjs/javascript.

To illustrate my needs I have made up this example:

<html>
<head>
    <title>Get number of edges of a specific node</title>
    <script type="text/javascript" src="/home/altug/Downloads/BTAGViewer/libs/visjs/vis.min.js"></script>
</head>
<body>
    <div id="mynetwork"></div>
    <script type="text/javascript">
      // create an array with nodes
      var nodes = new vis.DataSet([
        {id: "A", label: 'label A'},
        {id: "B", label: 'label B'},
        {id: "C", label: 'label C'},
        {id: "D", label: 'label D'},
        {id: "E", label: 'label E'}
      ]);

      // create an array with edges
      var edges = new vis.DataSet([
        {from: "A", to: "C"},
        {from: "A", to: "B"},
        {from: "B", to: "D"},
        {from: "B", to: "E"}
      ]);

      // create a network
      var container = document.getElementById('mynetwork');
      var data = {
        nodes: nodes,
        edges: edges
      };
      var options = {};
      var network = new vis.Network(container, data, options);

      var some_id = "B";
      var some_node = nodes.get(some_id);
      console.dir(some_node);

      console.log(/*magically retrieve the number of edges for node "B" ---> 3 (for this graph)*/);
    </script>
</body>

I know I could iterate over the edges and count the occurences of the specific node id, like for example:

for(var i = 0; i < edges.get().length; i++){
    ...
}

But isn't there another possibility using vis.js built-in capabilities?

I want to find the number of edges which are connected to a specific node in visjs/javascript.

To illustrate my needs I have made up this example:

<html>
<head>
    <title>Get number of edges of a specific node</title>
    <script type="text/javascript" src="/home/altug/Downloads/BTAGViewer/libs/visjs/vis.min.js"></script>
</head>
<body>
    <div id="mynetwork"></div>
    <script type="text/javascript">
      // create an array with nodes
      var nodes = new vis.DataSet([
        {id: "A", label: 'label A'},
        {id: "B", label: 'label B'},
        {id: "C", label: 'label C'},
        {id: "D", label: 'label D'},
        {id: "E", label: 'label E'}
      ]);

      // create an array with edges
      var edges = new vis.DataSet([
        {from: "A", to: "C"},
        {from: "A", to: "B"},
        {from: "B", to: "D"},
        {from: "B", to: "E"}
      ]);

      // create a network
      var container = document.getElementById('mynetwork');
      var data = {
        nodes: nodes,
        edges: edges
      };
      var options = {};
      var network = new vis.Network(container, data, options);

      var some_id = "B";
      var some_node = nodes.get(some_id);
      console.dir(some_node);

      console.log(/*magically retrieve the number of edges for node "B" ---> 3 (for this graph)*/);
    </script>
</body>

I know I could iterate over the edges and count the occurences of the specific node id, like for example:

for(var i = 0; i < edges.get().length; i++){
    ...
}

But isn't there another possibility using vis.js built-in capabilities?

Share Improve this question asked Jan 14, 2016 at 13:47 kiltekkiltek 3,2116 gold badges50 silver badges73 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 9

This sort of logic is indeed easy to implement yourself, there is no built-in functionality for it in vis.js. There are a lot of different use cases which would need slightly different algorithms, so we think it's best to leave it up to you and just utilize the flexibility of JavaScript for it.

In your case you could indeed just filter the edges which have the nodeId you're looking for:

function getEdgesOfNode(nodeId) {
  return edges.get().filter(function (edge) {
    return edge.from === nodeId || edge.to === nodeId;
  });
}

Other suggested approaches iterate over all the edges in the network, looking only at the edges DataSet. But the network DataSet itself offers a getConnectedEdges() function, returning an array of edge ids when a nodeId is specified. So to get your count, it's just

network.getConnectedEdges(nodeId).length  

where nodeId is the id of the node you are interested in.

It looks like internally, nodes and edges are accessed directly by [nodeId], without having to do an iteration, so that access time in functions like getConnectedEdges(nodeId) should be roughly constant, rather than linear in the numbers of edges.

That should make a huge difference in large networks.

For those of you who are trying to get an edge that connects two nodes:

function getEdgeBetweenNodes(node1,node2) {
    return edges.get().filter(function (edge) {
        return (edge.from === node1 && edge.to === node2 )|| (edge.from === node2 && edge.to === node1);
    });
};
发布评论

评论列表(0)

  1. 暂无评论