找到目标的目标,即朋友的力量有向图的朋友 [英] find target's target i.e friend of friend in 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 ?
编辑:
以下方式:
首先创建了相邻矩阵:
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;
}
}
然后将值分配给相邻矩阵: p>
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;
}
推荐答案
原理如下。给定特定节点,确定其直接邻居。要获得第二度邻居,取这个刚刚确定的邻居列表,并为每个邻居再次获得邻居列表。所有这些列表的联合是第一和第二级邻居的列表。
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屋!