d3js - 强制定向图:仅链接到自身的删除节点 [英] d3js - Force-Directed Graph: Delete node that only link to itself

查看:164
本文介绍了d3js - 强制定向图:仅链接到自身的删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从这个例子执行Force-Directed Graph,但我的数据是真的很大(5000个节点),所以我想删除一些不链接到任何其他节点的节点。

然而,在我的JSON中,每个节点至少有一个链接(链接到自己)。这是我的数据的一个例子:

  {directed:false,graph:{},
节点:{id:0},{id:1},{id:2},{id:3},{id:4},{id 5},
{id:6},{id:7},{id:8},{id:9},{id:10} :11},{id:12}],

links:[{source:0,target:0},{source:1, :1},
{source:2,target:2},{source:3,target:3},
{source:4, :4,{source:5,target:5},
{source:6,target:6},{source:7,target:8} ,
{source:7,target:9,{source:7,target:10},
{source:7,target:11} ,{source:7,target:7},
{source:8,target:8},{source:9,target:9},
{source:10,target:10},{source:11,target:11},
{source:12,target:12}], :false}

即节点 id:0 只有链接 {source:0,target:0} 应删除,但节点 id:7 包含链接 {source:7,target:8},{source:7, target:7} 。它是链接到另一个节点,所以它不应该被删除。



我的问题是如何删除只链接到自身的节点?


解决方案一种方法是首先将链接指向那些不指向同一节点的链接,即 source! ==目标。我选择使用 Array.prototype.reduce() ,但其他迭代方式的工作原理也是一样的。为了加速节点的消除,我还使用了设置以存储唯一的 id值,但这在技术上不是必需的。

  let uniqueNonSelfReferringLinks = data.links.reduce((set,{source:s,target:t})=> 
s!== t?set.add(s).add(t ):set,
new Set()
);

对节点进行过滤然后迭代到节点数组查找节点'ID的值:

  let filteredNodes = data.nodes.filter(n => uniqueNonSelfReferringLinks.has(n.id)); 

将两个步骤合并为一个可以更简洁地编写:

  let filteredNodes = data.nodes.filter(
function(n){return this.has(n.id);},
data.links.reduce((set,{source:s,target:t})=>
s!== t?set.add(s).add(t):set,
new Set()

);

看看下面的工作演示:



var data = {directed:false,graph:{},nodes {id:0},{id:1},{id:2},{id:3},{id:4},{id:5} {id:7},{id:8},{id:9},{id:10},{id:11},{id :},links:[{source:0,target:0},{source:1,target:1},{source:2,target },{source:3,target:3},{source:4,target:4},{source:5,target:5},{source ,target:6},{source:7,target:8},{source:7,target:9},{source:7,target:10}, {source:7,target:11},{source:7,target:7},{source:8,target:8},{source:9,目标:9},{源:10,目标:10},{源:11,焦油get:11},{source:12,target:12}],multigraph:false}; let filteredNodes = data.nodes.filter(function(n){return this.has(n.id ); },data.links.reduce((set,{source:s,target:t})=> s!== t?set.add(s).add(t):set,new Set())) ; console.log(filteredNodes);


I am trying to do the Force-Directed Graph from this example but my data is really big (5000 nodes) so I want to remove some nodes that do not link to any other nodes.

However, in my JSON, every node has at least 1 link (that link to itself). This is an example of my data:

{"directed": false, "graph": {}, 
"nodes": [{"id": 0}, {"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}, {"id": 5}, 
         {"id": 6}, {"id": 7}, {"id": 8}, {"id": 9}, {"id": 10}, {"id": 11}, {"id": 12}], 

"links": [{"source": 0, "target": 0}, {"source": 1, "target": 1}, 
    {"source": 2, "target": 2}, {"source": 3, "target": 3}, 
    {"source": 4, "target": 4}, {"source": 5, "target": 5}, 
    {"source": 6, "target": 6}, {"source": 7, "target": 8}, 
    {"source": 7, "target": 9}, {"source": 7, "target": 10}, 
    {"source": 7, "target": 11}, {"source": 7, "target": 7}, 
    {"source": 8, "target": 8}, {"source": 9, "target": 9}, 
    {"source": 10, "target": 10}, {"source": 11, "target": 11}, 
    {"source": 12, "target": 12}], "multigraph": false}

I.e., node id:0 only has link {"source": 0, "target": 0} so it should get removed but node id:7 has links {"source": 7, "target": 8}, {"source": 7, "target": 7}. It is linked to another node so it should not get removed.

My question is how do I delete nodes that only link to itself?

解决方案

One way to do this is to first boil down the links to those which are not referring to the same node, i.e. where source !== target. I chose to use Array.prototype.reduce() although other ways of iteration will work just the same. To speed up the elimintation of nodes later on I am also employing a Set to store unique id values, which technically is not necessary for this to work, though.

let uniqueNonSelfReferringLinks = data.links.reduce((set, {source:s, target:t}) =>
  s !== t ? set.add(s).add(t) : set,
  new Set()
);

Filtering the nodes then comes down to iterating the nodes array looking up the set for the value of the nodes' ids:

let filteredNodes = data.nodes.filter(n => uniqueNonSelfReferringLinks.has(n.id));

Consolidating both steps into one this can be written even more concisely:

let filteredNodes = data.nodes.filter(
  function(n) { return this.has(n.id); },  
  data.links.reduce((set, {source:s, target:t}) =>
    s !== t ? set.add(s).add(t) : set,
    new Set()
  )
);

Have a look at the following working demo:

var data = {
  "directed": false,
  "graph": {}, 
  "nodes": [{"id": 0}, {"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}, {"id": 5}, 
         {"id": 6}, {"id": 7}, {"id": 8}, {"id": 9}, {"id": 10}, {"id": 11}, {"id": 12}], 

  "links": [{"source": 0, "target": 0}, {"source": 1, "target": 1}, 
    {"source": 2, "target": 2}, {"source": 3, "target": 3}, 
    {"source": 4, "target": 4}, {"source": 5, "target": 5}, 
    {"source": 6, "target": 6}, {"source": 7, "target": 8}, 
    {"source": 7, "target": 9}, {"source": 7, "target": 10}, 
    {"source": 7, "target": 11}, {"source": 7, "target": 7}, 
    {"source": 8, "target": 8}, {"source": 9, "target": 9}, 
    {"source": 10, "target": 10}, {"source": 11, "target": 11}, 
    {"source": 12, "target": 12}],
  "multigraph": false
};

let filteredNodes = data.nodes.filter(
  function(n) { return this.has(n.id); },  
  data.links.reduce((set, {source:s, target:t}) =>
    s !== t ? set.add(s).add(t) : set,
    new Set()
  )
);

console.log(filteredNodes);

这篇关于d3js - 强制定向图:仅链接到自身的删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆