javascript - find target's target i.e friend of friend in force directed graph -
javascript - find target's target i.e friend of friend in force directed graph -
i using d3.js
libraries forcefulness directed graph,
i using next code 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) { homecoming a.index == b.index || linkedbyindex[a.index + "," + b.index]; }
how can find , highlight target's target ? can help me ?
edit:
solved problem in next way:
first created adjacent matrix:
var adjmat=null; var adjmatsq=null; adjmat=new array(graph.nodes.length); for(var = 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 values 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 found square of matrix i'll able sec grade 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 = 0; < m1.length; i++) { sum += m1[i][k] * m2[j][i]; } result[j].push(sum); } } homecoming result; }
defined function find sec grade nodes:
function areatseconddegree(a,c) { homecoming adjmatsq[a.index][c.index] == 1; }
my code plnkr
the principle follows. given specific node, determine immediate neighbours. sec grade neighbours, take list of neighbours have determined, , each list of neighbours again. union of lists list of first , sec grade neighbours.
in code this:
var connected = {}; var friends = graph.nodes.filter(function(o) { homecoming arefriends(d, o); }); friends.foreach(function(o) { connected[o.name] = 1; // sec pass second-degree neighbours graph.nodes.foreach(function(p) { if(arefriends(o, p)) { connected[p.name] = 1; } }); });
if want have arbitrary n grade neighbours, need alter accommodate fact don't know how many iterations of run.
complete demo here.
javascript d3.js
Comments
Post a Comment