图工具上两个顶点的共同邻居

时间:2016-04-18 06:30:38

标签: python graph-tool

我想在图形工具上获得两个顶点的共同邻居。

根据document,,可以使用几种相似性度量,并且所有这些度量都使用共同邻居的数量。所以,我想这应该很容易得到共同的邻居。但是,我找不到。

2 个答案:

答案 0 :(得分:4)

从Sørensen-Dice的相似性可以很容易地得到:

>>> g = collection.data["karate"]
>>> u, v = g.vertex(0), g.vertex(1)
>>> n = vertex_similarity(g, "dice", [(u, v)], self_loops=False) * (u.out_degree() + v.out_degree()) / 2
>>> print(n)
7

答案 1 :(得分:1)

如果有人想找出实际的共同邻居,以下方法也可行:

In [2]: g = gt.collection.data["karate"]

In [11]: u, v = g.vertex(32), g.vertex(33)

In [16]: set(u.out_neighbours()) & set(v.out_neighbours())
Out[16]: 
{<Vertex object with index '8' at 0x7f2b62ae82a0>,
 <Vertex object with index '14' at 0x7f2b62ae8318>,
 <Vertex object with index '15' at 0x7f2b62ae8390>,
 <Vertex object with index '18' at 0x7f2b62ae8408>,
 <Vertex object with index '20' at 0x7f2b62ae8480>,
 <Vertex object with index '22' at 0x7f2b62ae84f8>,
 <Vertex object with index '23' at 0x7f2b62ae8570>,
 <Vertex object with index '29' at 0x7f2b62ae85e8>,
 <Vertex object with index '30' at 0x7f2b62ae8660>,
 <Vertex object with index '31' at 0x7f2b62ae86d8>}

此集的长度为您提供常见邻居的总数。