- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 D3.JS
库强制定向图,
我正在使用以下代码来查找目标节点
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];
}
如何找到并突出显示目标的目标?有人可以帮我解决这个问题吗?
编辑:
通过以下方式解决了问题:
先创建邻接矩阵:
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;
}
}
然后将值赋值给相邻的矩阵:
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);
});
然后我找到了矩阵的平方,这样我就可以得到二阶节点:
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;
}
定义了一个函数来查找二阶节点:
function areAtSecondDegree(a,c)
{
return adjMatSq[a.index][c.index] == 1;
}
最佳答案
原理如下。给定一个特定的节点,确定它的直接邻居。要获得二级邻居,请使用您刚刚确定的邻居列表,并为每个邻居再次获取邻居列表。所有这些列表的联合是一级和二级邻居的列表。
在代码中这可能是这样的:
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 度邻居,则需要更改此设置以适应您不知道要运行多少次迭代的事实。
完整演示 here .
关于javascript - 找到目标的目标,即强制定向图中 friend 的 friend ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23386277/
我想知道将来对我来说最简单的方法是什么,可以使查询既有效又不那么复杂。 我应该像这样保存双向关系吗 from_id=1, to_id=2from_id=2, to_id=1 或者只创建一个唯一的行 f
假设您有一个拥有十亿用户的社交网络。在每个用户的页面上,您想要显示该用户的 friend 数、 friend 的 friend 等等,最多五度。友谊是相互的。计数不需要立即更新,但它们应该是精确的。
public void notMyFriend(Student student1) { System.out.println("Friends who are not my frien
我有如下3个表格 用户 - id integer primary_key - user_name friend - id autoincrement primary_key - user1 integ
如何从 Friends 表中获取 friend 列表以及我 friend 的 friend 数(不包括我的 friend 数) friend 表"tbl_users_friends 字段 1:id字段
我的 MySQL 表结构是这样的。 USER int id varchar username FRIEND_LIST int user_id int friend_id 对于每个 friend 关系,
假设 a,b,c 和 b,d,e 和 c,f,g 和 a,e,g,h 是 friend 。所以 获取共同好友 b/w a&b: MATCH (me:User {username:'a'}) -[r:F
我正在开发“可能的 friend ”功能。我需要显示所有不是我 friend 的 friend 的 friend ,也不要发送我或没有我的待处理请求 FRIENDSHIPS user_id frien
如果两个用户是 friend ,我在“ friend ”表中有一个条目,如下所示: table: friends ------------------------ uid1
我有一个 friend 表,用于跟踪数据库中的关系 - 我将一个查询与子查询放在一起,该子查询从 friend 的 friend 那里获取个人资料信息 我的 friend 表如下所示 - id 双向出
我想结交 friend 的 friend 的 friend 。我得到了结果,但我不确定我的查询是否正确。我正在使用嵌套选择。 我的问题是: 查询是否正确? 我如何使用 join 执行此查询? 这是我的
假设我有一个包含 friend 属性的用户对象。这个 friends 属性是其他用户对象的数组,因此是你的 friend 。找到你 friend 的 friend 但不是你 friend 的最快算法是
我想结交我的 friend 以及 friend 的 friend ,但仅限于二级 friend 我的表结构是这样的 user_id friend_user_id 1 2 1
为了了解使用 Neo4J 建立 friend 关系的优势,我在 MySQL 数据库上创建了一张用于 Persons 的表(“Persons”,20900 个数据集): id | name --
假设我有一个名为“ friend ”的表,对于每个友谊,我添加两个条目。例如,如果用户 1 和 2 是 friend ,我们将有: uid1 uid2 ---------- 1 2 2 1
在我的图形数据库中,我有 Twig 和树叶。分支可以“包含”叶子,分支可以“包含”分支。 如何使用 Gremlin 找到与给定分支直接或间接相关的所有叶子? 我让这个在 Cypher 中工作: STA
我正在尝试使用 iPhone Facebook SDK 获取我 friend 的 friend 列表。我尝试了一种使用 FQL 和 Graph API 的方法,但在这两种情况下我都遇到了错误: "Ca
我有这个 MySQL 表: 假设我以用户 1 的身份登录,正在浏览用户 2 的个人资料。由于我们是共同的 friend (1 是 2 的 friend ,2 是 1 的 friend ),我需要回应“
我想得到我 friend 的 friend 不是我 friend 的 friend 。我有一张这样的 table :用户 friend (idUser,idUserFriend) 我在想这样的事情:
我有这个 Cypher 查询... match (p:Person{userid:8432})-[r:friends_with]->(p1:Person)-[r2:friends_with]->(p2
我是一名优秀的程序员,十分优秀!