找到目标的目标,即有力有向图的朋友的朋友 [英] find target's target i.e friend of friend in force directed graph

查看:90
本文介绍了找到目标的目标,即有力有向图的朋友的朋友的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 D3.JS 库强制有向图,

I am using D3.JS libraries force directed graph,

我正在使用以下代码查找目标节点

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 ?

编辑:

解决了

首先创建相邻矩阵:

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;
    }

我的代码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;
        }
      });
  });

如果您想拥有任意n个邻居,则需要更改此设置以适应以下事实:您不知道要运行多少次迭代。

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.

完整演示此处

这篇关于找到目标的目标,即有力有向图的朋友的朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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