简体   繁体   English

找到目标的目标即朋友的朋友有力指导图

[英]find target's target i.e friend of friend in force directed graph

I am using D3.JS libraries force directed graph, 我正在使用D3.JS库强制有向图,

I am using the following code to find target nodes 我使用以下代码来查找目标节点

graph.links.forEach(function(d) {
          linkedByIndex[d.source.index + "," + d.target.index] = 1;
          linkedByIndex[d.target.index + "," + d.source.index] = 1;
                  });
    });


    function neighboring(a, b) {
      return a.index == b.index || linkedByIndex[a.index + "," + b.index];
    }

How can I find and highlight target's target ? 如何找到并突出目标的目标? Can anybody help me with this ? 任何人都可以帮我吗?

EDIT: 编辑:

Solved the problem in the following way: 通过以下方式解决了问题:

First created the adjacent matrix: 首先创建相邻矩阵:

var adjMat=null;
var adjMatSq=null;


adjMat=new Array(graph.nodes.length);
        for(var i = 0;i < graph.nodes.length; ++i)
        {
            adjMat[i]=new Array(graph.nodes.length);            
            for(var j = 0; j < graph.nodes.length; ++j)
            {
                adjMat[i][j] = 0;
            }
        }

Then assigned the values to the adjacent matrix: 然后将值分配给相邻矩阵:

graph.links.forEach(function(d) {
          adjMat[d.source.index][d.target.index] = adjMat[d.target.index][d.source.index] = 1;
          adjMat[d.source.index][d.source.index] = 1; 
        });

        adjMatSq=matrixMultiply(adjMat, adjMat);
    });

Then I found the square of matrix so that I'll be able to get the second degree nodes: 然后我找到了矩阵的平方,这样我就能得到第二度节点:

function matrixMultiply(m1,m2)
        {
            var result = [];
          for(var j = 0; j < m2.length; j++) {
            result[j] = [];
            for(var k = 0; k < m1[0].length; k++) {
                var sum = 0;
              for(var i = 0; i < m1.length; i++) {
                    sum += m1[i][k] * m2[j][i];
                }
                result[j].push(sum);
            }
        }
        return result;
        }

Defined a function to find the second degree nodes: 定义了一个查找二度节点的函数:

    function areAtSecondDegree(a,c)
    {
        return adjMatSq[a.index][c.index] == 1;
    }

My Code Plnkr 我的代码Plnkr

The principle is as follows. 原理如下。 Given a specific node, determine its immediate neighbours. 给定特定节点,确定其直接邻居。 To get second degree neighbours, take this list of neighbours you have just determined, and for each get the list of neighbours again. 要获得二级邻居,请获取您刚刚确定的邻居列表,并为每个邻居再次获取邻居列表。 The union of all those lists is the list of first and second degree neighbours. 所有这些列表的并集是一级和二级邻居的列表。

In code this could look like this: 在代码中,这可能如下所示:

var connected = {};
  var friends = graph.nodes.filter(function(o) { return areFriends(d, o); });
  friends.forEach(function(o) {
      connected[o.name] = 1;
      // second pass to get second-degree neighbours
      graph.nodes.forEach(function(p) {
        if(areFriends(o, p)) {
          connected[p.name] = 1;
        }
      });
  });

If you want to have arbitrary n degree neighbours, you would need to change this to accommodate the fact that you don't know how many iterations of this to run. 如果你想拥有任意的n度邻居,你需要改变它以适应你不知道要运行多少迭代的事实。

Complete demo here . 在这里完成演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM